1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ChatAnalytics.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Im\V2\Analytics;
6
7use Bitrix\Disk\Analytics\DiskAnalytics;
8use Bitrix\Disk\Analytics\Enum\ImSection;
9use Bitrix\Disk\File;
10use Bitrix\Im\V2\Analytics\Event\ChatEvent;
11use Bitrix\Im\V2\Chat;
12use Bitrix\Im\V2\Chat\CollabChat;
13use Bitrix\Im\V2\Relation\Reason;
14use Bitrix\Main\Loader;
15
17{
18 public const SUBMIT_CREATE_NEW = 'submit_create_new';
19
20 protected const JOIN = 'join';
21 protected const ADD_DEPARTMENT = 'add_department';
22 protected const ADD_USER = 'add_user';
23 protected const DELETE_USER = 'delete_user';
24 protected const FOLLOW_COMMENTS = 'follow_comments';
25 protected const UNFOLLOW_COMMENTS = 'unfollow_comments';
26 protected const EDIT_DESCRIPTION = 'edit_description';
27 protected const DELETE_DEPARTMENT = 'delete_department';
28 protected const EDIT_PERMISSIONS = 'edit_permissions';
29 protected const SET_TYPE = 'set_type';
30 protected const AUTODELETE_ON = 'autodelete_on';
31 protected const AUTODELETE_OFF = 'autodelete_off';
32
33 protected static array $oneTimeEvents = [];
34 protected static array|bool $blockSingleUserEvents = [];
35
36 public function addSubmitCreateNew(): void
37 {
38 $this->async(function () {
39 $this
40 ->createChatEvent(self::SUBMIT_CREATE_NEW)
41 ?->setChatType()
42 ?->send()
43 ;
44 });
45 }
46
47 public function addAddUser(Reason $reason = Reason::DEFAULT, bool $isJoin = false): void
48 {
49 if ($this->isSingleUserEventsBlocked())
50 {
51 return;
52 }
53
54 $this->async(function () use ($reason, $isJoin) {
55 if ($isJoin)
56 {
57 $eventName = self::JOIN;
58 }
59 else
60 {
61 $eventName = match ($reason) {
62 Reason::STRUCTURE => self::ADD_DEPARTMENT,
63 default => self::ADD_USER,
64 };
65 }
66
67 $this
68 ->createChatEvent($eventName)
69 ?->send()
70 ;
71 (new CopilotAnalytics($this->chat))->addAddUser();
72 });
73 }
74
75 public function addDeleteUser(): void
76 {
77 if ($this->isSingleUserEventsBlocked())
78 {
79 return;
80 }
81
82 $this->async(function () {
83 $this
84 ->createChatEvent(self::DELETE_USER)
85 ?->send()
86 ;
87 (new CopilotAnalytics($this->chat))->addDeleteUser();
88 });
89 }
90
91 public function addFollowComments(bool $flag): void
92 {
93 $this->async(function () use ($flag) {
94 $this
95 ->createChatEvent($flag ? self::FOLLOW_COMMENTS : self::UNFOLLOW_COMMENTS)
96 ?->send()
97 ;
98 });
99 }
100
101 public function addAutoDeleteOn(int $messagesAutoDeleteDelay): void
102 {
103 $this->async(function () use ($messagesAutoDeleteDelay) {
104 $this
105 ->createChatEvent(self::AUTODELETE_ON)
106 ?->setP2(null)
107 ->setP3('timer_' . $messagesAutoDeleteDelay)
108 ->setP4(null)
109 ->setP5(null)
110 ->send()
111 ;
112 });
113 }
114
115 public function addAutoDeleteOff(): void
116 {
117 $this->async(function () {
118 $this
119 ->createChatEvent(self::AUTODELETE_OFF)
120 ?->setP2(null)
121 ->setP3(null)
122 ->setP4(null)
123 ->setP5(null)
124 ->send()
125 ;
126 });
127 }
128
129 protected function addChatEditEvent(string $eventName): void
130 {
131 if ($this->chat instanceof CollabChat)
132 {
133 return;
134 }
135
136 $this->async(function () use ($eventName) {
137 $this
138 ->createChatEvent($eventName)
139 ?->send()
140 ;
141 });
142 }
143
144 public function addEditDescription(): void
145 {
146 $this->async(function () {
147 $this
148 ->createChatEvent(self::EDIT_DESCRIPTION)
149 ?->send()
150 ;
151 });
152 }
153
154 public function addAddDepartment(): void
155 {
156 $this->addChatEditEvent(self::ADD_DEPARTMENT);
157 }
158
159 public function addDeleteDepartment(): void
160 {
161 $this->addChatEditEvent(self::DELETE_DEPARTMENT);
162 }
163
164 public function addEditPermissions(): void
165 {
166 if ($this->isFirstTimeEvent(self::EDIT_PERMISSIONS))
167 {
168 $this->addChatEditEvent(self::EDIT_PERMISSIONS);
169 }
170 }
171
172 public function addSetType(): void
173 {
174 $this->addChatEditEvent(self::SET_TYPE);
175 }
176
177 public function addUploadFile(File $file): void
178 {
179 if (Loader::includeModule('disk'))
180 {
181 $this->async(fn () => DiskAnalytics::sendUploadFileToImEvent($file, $this->getImSectionForDiskEvent()));
182 }
183 }
184
185 protected function getImSectionForDiskEvent(): ImSection
186 {
187 return match ($this->chat->getType())
188 {
189 Chat::IM_TYPE_CHANNEL, Chat::IM_TYPE_OPEN_CHANNEL => ImSection::Channel,
190 Chat::IM_TYPE_OPEN_LINE => ImSection::Openline,
191 default => ImSection::Chat
192 };
193 }
194
195 protected function createChatEvent(
196 string $eventName,
197 ): ?ChatEvent
198 {
199 if (!$this->isChatTypeAllowed($this->chat))
200 {
201 return null;
202 }
203
204 return (new ChatEvent($eventName, $this->chat, $this->userId));
205 }
206
207 protected function isFirstTimeEvent(string $eventName): bool
208 {
209 $result = self::$oneTimeEvents[$eventName][$this->chat->getId()] ?? true;
210 self::$oneTimeEvents[$eventName][$this->chat->getId()] = false;
211
212 return $result;
213 }
214
215 public static function blockSingleUserEvents(?Chat $chat = null): void
216 {
217 if (self::$blockSingleUserEvents === true)
218 {
219 return;
220 }
221
222 if ($chat === null)
223 {
224 self::$blockSingleUserEvents = true;
225
226 return;
227 }
228
229 self::$blockSingleUserEvents[$chat->getId()] = true;
230 }
231
232 public static function unblockSingleUserEventsByChat(Chat $chat): void
233 {
234 if (is_bool(self::$blockSingleUserEvents))
235 {
236 return;
237 }
238
239 unset(self::$blockSingleUserEvents[$chat->getId()]);
240 }
241
242 protected function isSingleUserEventsBlocked(): bool
243 {
244 if (!is_array(self::$blockSingleUserEvents))
245 {
246 return true;
247 }
248
249 return self::$blockSingleUserEvents[$this->chat->getId()] ?? false;
250 }
251}
addAddUser(Reason $reason=Reason::DEFAULT, bool $isJoin=false)
Определения ChatAnalytics.php:47
createChatEvent(string $eventName,)
Определения ChatAnalytics.php:195
static array bool $blockSingleUserEvents
Определения ChatAnalytics.php:34
addChatEditEvent(string $eventName)
Определения ChatAnalytics.php:129
addAutoDeleteOn(int $messagesAutoDeleteDelay)
Определения ChatAnalytics.php:101
addFollowComments(bool $flag)
Определения ChatAnalytics.php:91
static array $oneTimeEvents
Определения ChatAnalytics.php:33
static unblockSingleUserEventsByChat(Chat $chat)
Определения ChatAnalytics.php:232
addUploadFile(File $file)
Определения ChatAnalytics.php:177
static blockSingleUserEvents(?Chat $chat=null)
Определения ChatAnalytics.php:215
isFirstTimeEvent(string $eventName)
Определения ChatAnalytics.php:207
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
Определения Image.php:9