1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
countercontroller.php
См. документацию.
1<?php
2
3namespace Bitrix\Socialnetwork\Internals\LiveFeed\Counter;
4
5use Bitrix\Socialnetwork\Internals\EventService\Event;
6use Bitrix\Socialnetwork\Internals\EventService\EventDictionary;
7use Bitrix\Socialnetwork\Internals\LiveFeed\Counter;
8use Bitrix\Socialnetwork\Internals\LiveFeed\Counter\Processor\CommandTrait;
9use Bitrix\Socialnetwork\Internals\LiveFeed\Counter\Processor\UserProcessor;
10use Bitrix\Socialnetwork\Livefeed\Provider;
11use Bitrix\Socialnetwork\Space\Service;
12use Bitrix\Socialnetwork\UserContentViewTable;
13
15{
16 use CommandTrait;
17
18 public const STEP_LIMIT = 1000;
19 private int $userId;
20
25 public function __construct(int $userId = 0)
26 {
27 $this->userId = $userId;
28 }
29
34 public static function isEnabled(int $userId = 0): bool
35 {
36 return Service::isAvailable(true);
37 }
38
44 public function process(Event $event): void
45 {
46 if ($event->getType() === EventDictionary::EVENT_SPACE_LIVEFEED_COUNTER_UPD)
47 {
48 return;
49 }
50
51 if (!in_array($event->getType(), CounterDictionary::SUPPORTED_EVENTS, true))
52 {
53 return;
54 }
55
56 switch ($event->getType())
57 {
58 case EventDictionary::EVENT_SPACE_LIVEFEED_READ_ALL:
59 $this->readAll($event);
60 break;
61 case EventDictionary::EVENT_SPACE_LIVEFEED_POST_VIEW:
62 $this->seen($event);
63 break;
64 case EventDictionary::EVENT_SPACE_LIVEFEED_POST_ADD:
66 break;
67 case EventDictionary::EVENT_SPACE_LIVEFEED_COMMENT_ADD:
69 break;
70 case EventDictionary::EVENT_SPACE_LIVEFEED_POST_DEL:
71 $this->delete($event, [CounterDictionary::COUNTER_NEW_POSTS]);
72 break;
73 case EventDictionary::EVENT_SPACE_LIVEFEED_COMMENT_DEL:
74 $this->delete($event, [CounterDictionary::COUNTER_NEW_COMMENTS]);
75 break;
76 default:
78 }
79
80 $this->updateLeftMenuCounter();
81 }
82
89 public function processQueueItem(array $queueItem): void
90 {
91 $type = $queueItem['TYPE'] ?? null;
92 $items = $queueItem['SONET_LOGS'] ?? null;
93 if (!$type || !is_array($items))
94 {
95 return;
96 }
97
98 switch ($type)
99 {
102 $userProcessor = UserProcessor::getInstance($this->userId);
103 $userProcessor->recount($type, $items);
104 break;
105 }
106 }
107
112 public function updateLeftMenuCounter(): void
113 {
114 $value = Counter::getInstance($this->userId)->get(CounterDictionary::COUNTER_TOTAL);
115 if (!$this->isSameValueCached($value))
116 {
117 \CUserCounter::Set(
118 $this->userId,
120 $value,
121 '**',
122 '',
123 false
124 );
125 }
126 }
127
128 private function recount(Event $event, array $counters = []): void
129 {
130 $sonetLogId = $event->getData()['SONET_LOG_ID'] ?? null;
131 if (!$sonetLogId)
132 {
133 return;
134 }
135
136 $userProcessor = UserProcessor::getInstance($this->userId);
137 foreach ($counters as $counter)
138 {
139 $userProcessor->recount($counter, [$sonetLogId]);
140 }
141 }
142
143 private function add(Event $event, array $counters = []): void
144 {
145 $sonetLogId = $event->getData()['SONET_LOG_ID'] ?? null;
146 if (!$sonetLogId)
147 {
148 return;
149 }
150
151 $userProcessor = UserProcessor::getInstance($this->userId);
152 foreach ($counters as $counter)
153 {
154 $userProcessor->add($counter, [$sonetLogId]);
155 }
156 }
157
158 private function delete(Event $event, array $counters = []): void
159 {
160 $sonetLogId = $event->getData()['SONET_LOG_ID'] ?? null;
161 if (!$sonetLogId)
162 {
163 return;
164 }
165
166 $userProcessor = UserProcessor::getInstance($this->userId);
167 foreach ($counters as $counter)
168 {
169 $userProcessor->seen($counter, [$sonetLogId]);
170 }
171 }
172
173 private function seen(Event $event): void
174 {
175 $data = $event->getData();
176 if (!isset($data['ENTITY_ID'], $data['ENTITY_TYPE_ID'], $data['USER_ID'], $data['SONET_LOG_ID']))
177 {
178 return;
179 }
180
181 $userProcessor = UserProcessor::getInstance($this->userId);
182 $userProcessor->seen(CounterDictionary::COUNTER_NEW_POSTS, [$data['SONET_LOG_ID']]);
183 $userProcessor->seen(CounterDictionary::COUNTER_NEW_COMMENTS, [$data['SONET_LOG_ID']]);
184 }
185
186 public function recountAll(): void
187 {
188 if (!$this->userId)
189 {
190 return;
191 }
192
193 self::reset($this->userId);
194
195 $userProcessor = UserProcessor::getInstance($this->userId);
196 $userProcessor->recountAll(CounterDictionary::COUNTER_NEW_POSTS);
197
198 $this->saveFlag($this->userId);
199 }
200
201 private function readAll(Event $event): void
202 {
203 if (!$this->userId)
204 {
205 return;
206 }
207
208 $groupId = (int)($event->getData()['GROUP_ID'] ?? 0);
209
210 // save the timestamp
211 $viewParams = [
212 'userId' => $this->userId,
213 'typeId' => Provider::DATA_ENTITY_TYPE_LIVE_FEED_VIEW,
214 'entityId' => $groupId,
215 'logId' => $groupId,
216 'save' => true,
217 ];
218 UserContentViewTable::set($viewParams);
219 UserProcessor::getInstance($this->userId)->readAll($groupId);
220 }
221
222 private function isSameValueCached(int $value): bool
223 {
224 global $CACHE_MANAGER;
225
226 $cache = $CACHE_MANAGER->Get('user_counter' . $this->userId);
227 if (!$cache)
228 {
229 return false;
230 }
231
232 foreach ($cache as $item)
233 {
234 if (
235 $item['CODE'] === CounterDictionary::LEFT_MENU_SONET
236 && $item['SITE_ID'] === '**'
237 && (int)$item['CNT'] === $value
238 )
239 {
240 return true;
241 }
242 }
243
244 return false;
245 }
246}
$type
Определения options.php:106
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getInstance()
Определения application.php:98
Определения event.php:5
static set($params=[])
Определения usercontentview.php:92
global $CACHE_MANAGER
Определения clear_component_cache.php:7
$data['IS_AVAILABLE']
Определения .description.php:13
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$counter
Определения options.php:5
$event
Определения prolog_after.php:141
$items
Определения template.php:224
$counters
Определения options.php:100