Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CounterServiceLegacy.php
1<?php
2
4
9
11{
12 protected const CACHE_PATH = '/bx/im/counter/';
13
14 protected const DEFAULT_COUNTERS = [
15 'TYPE' => [
16 'ALL' => 0,
17 'NOTIFY' => 0,
18 'CHAT' => 0,
19 'LINES' => 0,
20 'DIALOG' => 0,
21 ],
22 'CHAT' => [],
23 'CHAT_MUTED' => [],
24 'CHAT_UNREAD' => [],
25 'LINES' => [],
26 'DIALOG' => [],
27 'DIALOG_UNREAD' => [],
28 ];
29
30 protected function countUnreadMessages(?array $chatIds = null): void
31 {
32 $counters = $this->getCountersForEachChat($chatIds);
33
34 $privateChatIds = [];
35 foreach ($counters as $counter)
36 {
37 if ($counter['CHAT_TYPE'] === \IM_MESSAGE_PRIVATE)
38 {
39 $privateChatIds[] = $counter['CHAT_ID'];
40 }
41 }
42
43 $chatIdToDialogId = $this->getMapChatToDialog($privateChatIds);
44
45 foreach ($counters as $counter)
46 {
47 $chatId = (int)$counter['CHAT_ID'];
48 $count = (int)$counter['COUNT'];
49 if ($counter['IS_MUTED'] === 'Y')
50 {
51 $this->setFromMutedChat($chatId, $count);
52 }
53 else if ($counter['CHAT_TYPE'] === \IM_MESSAGE_SYSTEM)
54 {
55 $this->setFromNotify($count);
56 }
57 else if ($counter['CHAT_TYPE'] === \IM_MESSAGE_OPEN_LINE)
58 {
59 $this->setFromLine($chatId, $count);
60 }
61 else if ($counter['CHAT_TYPE'] === \IM_MESSAGE_PRIVATE && isset($chatIdToDialogId[$chatId]))
62 {
63 $this->setFromDialog($chatIdToDialogId[$chatId], $count);
64 }
65 else if ($counter['CHAT_TYPE'] === Chat::IM_TYPE_COPILOT)
66 {
67 // nothing
68 }
69 else
70 {
71 $this->setFromChat($chatId, $count);
72 }
73 $this->countersByChatIds[$chatId] = $count;
74 }
75 }
76 protected function getUnreadChats(?bool $isMuted = null): array
77 {
78 $query = RecentTable::query()
79 ->setSelect(['CHAT_ID' => 'ITEM_CID', 'IS_MUTED' => 'RELATION.NOTIFY_BLOCK', 'DIALOG_ID' => 'ITEM_ID', 'ITEM_TYPE'])
80 ->where('USER_ID', $this->getContext()->getUserId())
81 ->where('UNREAD', true)
82 ;
83 if (isset($isMuted))
84 {
85 $query->where('IS_MUTED', $isMuted);
86 }
87
88 return $query->fetchAll();
89 }
90
91 protected function countUnreadChats(): void
92 {
93 $unreadChats = $this->getUnreadChats();
94
95 foreach ($unreadChats as $unreadChat)
96 {
97 if ($unreadChat['ITEM_TYPE'] === \IM_MESSAGE_PRIVATE)
98 {
99 $this->setUnreadDialog((int)$unreadChat['DIALOG_ID']);
100 }
101 else
102 {
103 $this->setUnreadChat((int)$unreadChat['CHAT_ID'], $unreadChat['IS_MUTED'] === 'Y');
104 }
105 }
106 }
107
108 protected function setUnreadDialog(int $id): void
109 {
110 $this->counters['TYPE']['ALL']++;
111 $this->counters['TYPE']['DIALOG']++;
112 $this->counters['DIALOG_UNREAD'][] = $id;
113 }
114
115 protected function setFromDialog(int $id, int $count): void
116 {
117 $this->counters['TYPE']['ALL'] += $count;
118 $this->counters['TYPE']['DIALOG'] += $count;
119 $this->counters['DIALOG'][$id] = $count;
120 }
121
122 protected function getMapChatToDialog(array $privateChatIds)
123 {
124 if (empty($privateChatIds))
125 {
126 return [];
127 }
128
129 $result = RelationTable::query()
130 ->setSelect(['USER_ID', 'CHAT_ID'])
131 ->whereNot('USER_ID', $this->getContext()->getUserId())
132 ->whereIn('CHAT_ID', $privateChatIds)
133 ->fetchAll()
134 ;
135
136 $map = [];
137
138 foreach ($result as $row)
139 {
140 $map[$row['CHAT_ID']] = $row['USER_ID'];
141 }
142
143 return $map;
144 }
145}
const IM_TYPE_COPILOT
Definition Chat.php:62
setUnreadChat(int $id, bool $isMuted)
getCountersForEachChat(?array $chatIds=null, bool $forCurrentUser=true)