Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
countercontroller.php
1<?php
2
4
9use Bitrix\Socialnetwork\Internals\LiveFeed\Counter\Processor\CommandTrait;
12
14{
15 use CommandTrait;
16
17 public const STEP_LIMIT = 1000;
18 private int $userId;
19
24 public function __construct(int $userId = 0)
25 {
26 $this->userId = $userId;
27 }
28
33 public static function isEnabled(int $userId = 0): bool
34 {
35 $isLegacyEnabled = Option::get('socialnetwork', CounterDictionary::LEGACY_COUNTER_ENABLED, 'null', '-');
36 if ($isLegacyEnabled === 'null')
37 {
38 // new counters enabled for all users
39 return true;
40 }
41
42 if (
43 $userId
44 && \CUserOptions::GetOption(
45 'socialnetwork',
47 false,
48 $userId
49 )
50 )
51 {
52 return true;
53 }
54
55 return false;
56 }
57
63 public function process(Event $event): void
64 {
65 if ($event->getType() === EventDictionary::EVENT_SPACE_LIVEFEED_COUNTER_UPD)
66 {
67 return;
68 }
69
70 if (!in_array($event->getType(), CounterDictionary::SUPPORTED_EVENTS, true))
71 {
72 return;
73 }
74
75 switch ($event->getType())
76 {
77 case EventDictionary::EVENT_SPACE_LIVEFEED_READ_ALL:
78 $this->readAll($event);
79 break;
80 case EventDictionary::EVENT_SPACE_LIVEFEED_POST_VIEW:
81 $this->viewed($event);
82 break;
83 default:
84 $this->recount($event);
85 }
86 }
87
94 public function processQueueItem(array $queueItem): void
95 {
96 $type = $queueItem['TYPE'] ?? null;
97 $items = $queueItem['SONET_LOGS'] ?? null;
98 if (!$type || !is_array($items))
99 {
100 return;
101 }
102
103 switch ($type)
104 {
107 $userProcessor = UserProcessor::getInstance($this->userId);
108 $userProcessor->recount($type, $items);
109 break;
110 }
111 }
112
117 public function updateInOptionCounter()
118 {
119 $value = Counter::getInstance($this->userId)->get(CounterDictionary::COUNTER_TOTAL);
120 if (!$this->isSameValueCached($value))
121 {
122 \CUserCounter::Set(
123 $this->userId,
125 $value,
126 '**',
127 '',
128 true
129 );
130 }
131 }
132
133 private function recount(Event $event): void
134 {
135 $sonetLogId = $event->getData()['SONET_LOG_ID'] ?? null;
136 if (!$sonetLogId)
137 {
138 return;
139 }
140
141 $userProcessor = UserProcessor::getInstance($this->userId);
142 $userProcessor->recount(CounterDictionary::COUNTER_NEW_POSTS, [$sonetLogId]);
143 $userProcessor->recount(CounterDictionary::COUNTER_NEW_COMMENTS, [$sonetLogId]);
144 }
145
146 private function viewed(Event $event): void
147 {
148 $data = $event->getData();
149 if (!isset($data['ENTITY_ID'], $data['TYPE_ID'], $data['USER_ID']))
150 {
151 return;
152 }
153
154 $contentViewed = UserContentViewTable::query()
155 ->where('USER_ID', $data['USER_ID'])
156 ->where('RATING_TYPE_ID', $data['TYPE_ID'])
157 ->where('RATING_ENTITY_ID', $data['ENTITY_ID'])
158 ->setLimit(1)
159 ->exec()->fetch();
160
161 if ($contentViewed)
162 {
163 $this->recount($event);
164 }
165 }
166
167 public function recountAll(): void
168 {
169 if (!$this->userId)
170 {
171 return;
172 }
173
174 self::reset($this->userId);
175
176 $userProcessor = UserProcessor::getInstance($this->userId);
177 $userProcessor->recountAll(CounterDictionary::COUNTER_NEW_POSTS);
178
179 $this->saveFlag($this->userId);
180 }
181
182 private function readAll(Event $event): void
183 {
184 if (!$this->userId)
185 {
186 return;
187 }
188
189 $groupId = (int)($event->getData()['GROUP_ID'] ?? 0);
190 UserProcessor::getInstance($this->userId)->readAll($groupId);
191 }
192
193 private function isSameValueCached(int $value): bool
194 {
195 global $CACHE_MANAGER;
196
197 $cache = $CACHE_MANAGER->Get('user_counter' . $this->userId);
198 if (!$cache)
199 {
200 return false;
201 }
202
203 foreach ($cache as $item)
204 {
205 if (
206 $item['CODE'] === CounterDictionary::LEFT_MENU_SONET
207 && $item['SITE_ID'] === '**'
208 && (int)$item['CNT'] === $value
209 )
210 {
211 return true;
212 }
213 }
214
215 return false;
216 }
217}