Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
OpenLineChat.php
1<?php
2
3namespace Bitrix\Im\V2\Chat;
4
13use Bitrix\ImOpenLines\Config;
14use Bitrix\ImOpenLines\Model\ChatIndexTable;
17
19{
20 protected const EXTRANET_CAN_SEE_HISTORY = true;
21
22 protected $entityMap = [
23 'entityId' => [
24 'connectorId',
25 'lineId',
26 'connectorChatId',
27 'connectorUserId',
28 ],
29 'entityData1' => [
30 'crmEnabled',
31 'crmEntityType',
32 'crmEntityId',
33 'pause',
34 'waitAction',
35 'sessionId',
36 'dateCreate',
37 'lineId',
38 'blockDate',
39 'blockReason',
40 ],
41 'entityData2' => [
42 'u0',
43 'leadId',
44 'u2',
45 'companyId',
46 'u4',
47 'contactId',
48 'u6',
49 'dealId',
50 ],
51 'entityData3' => [
52 'silentMode',
53 ],
54 ];
55
56 public function setEntityMap(array $entityMap): EntityChat
57 {
58 return $this;
59 }
60
61 public function read(bool $onlyRecent = false, bool $byEvent = false, bool $forceRead = false): Result
62 {
63 Recent::unread($this->getDialogId(), false, $this->getContext()->getUserId());
64
65 if ($onlyRecent)
66 {
67 $lastId = $this->getReadService()->getLastMessageIdInChat($this->chatId);
68
69 return (new Result())->setResult([
70 'CHAT_ID' => $this->chatId,
71 'LAST_ID' => $lastId,
72 'COUNTER' => $this->getReadService()->getCounterService()->getByChat($this->chatId),
73 'VIEWED_MESSAGES' => [],
74 ]);
75 }
76
77 return $this->readAllMessages($byEvent, $forceRead);
78 }
79
80 public function readAllMessages(bool $byEvent = false, bool $forceRead = false): Result
81 {
82 $result = $this->readMessages(null, $byEvent, $forceRead);
83
84 $userId = $this->getContext()->getUserId();
85 Application::getInstance()->addBackgroundJob(function () use ($byEvent, $forceRead, $userId) {
86 $chat = $this->withContextUser($userId);
87
88 if ($chat->getSelfRelation() === null)
89 {
90 $chat->readMessages(null, $byEvent, $forceRead);
91 }
92 });
93
94 return $result;
95 }
96
97 public function readMessages(?MessageCollection $messages, bool $byEvent = false, bool $forceRead = false): Result
98 {
99 if (!$forceRead && $this->getAuthorId() === 0)
100 {
101 return new Result();
102 }
103
104 return parent::readMessages($messages, $byEvent);
105 }
106
107 protected function getDefaultType(): string
108 {
109 return self::IM_TYPE_OPEN_LINE;
110 }
111
112 protected function getDefaultEntityType(): string
113 {
114 return self::ENTITY_TYPE_LINE;
115 }
116
117 protected function checkAccessWithoutCaching(int $userId): bool
118 {
119 $inChat = parent::checkAccessWithoutCaching($userId);
120
121 if ($inChat)
122 {
123 return true;
124 }
125
126 if (!Loader::includeModule('imopenlines'))
127 {
128 return false;
129 }
130
131 $entityData = $this->getEntityData(true);
132 return Config::canJoin(
133 $entityData['lineId'] ?? 0,
134 $entityData['crmEntityType'] ?? null,
135 $entityData['crmEntityId'] ?? null
136 );
137 }
138
139 public function canUpdateOwnMessage(): bool
140 {
141 if (!Loader::includeModule('imopenlines'))
142 {
143 return false;
144 }
145
146 [$connectorType] = explode("|", $this->getEntityId() ?? '');
147
148 return in_array($connectorType, \Bitrix\ImOpenlines\Connector::getListCanUpdateOwnMessage(), true);
149 }
150
151 protected function prepareParams(array $params = []): Result
152 {
153 $params['AUTHOR_ID'] = 0;
154 return parent::prepareParams($params);
155 }
156
157 public function extendPullWatch(): void
158 {
159 if (Loader::includeModule('pull'))
160 {
161 \CPullWatch::Add($this->getContext()->getUserId(), "IM_PUBLIC_{$this->getId()}", true);
162 }
163 }
164
169 public function riseInRecent(Message $message): void
170 {
172 foreach ($this->getRelations() as $relation)
173 {
174 if (!User::getInstance($relation->getUserId())->isActive())
175 {
176 continue;
177 }
178
179 $sessionId = 0;
180 if ($this->getEntityType() == self::ENTITY_TYPE_LINE)
181 {
182 if (User::getInstance($relation->getUserId())->getExternalAuthId() == 'imconnector')
183 {
184 continue;
185 }
186
187 if ($this->getEntityData1())
188 {
189 //todo: replace it with method
190 $fieldData = explode("|", $this->getEntityData1());
191 $sessionId = (int)$fieldData[5];
192 }
193 }
194
195 \CIMContactList::SetRecent([
196 'ENTITY_ID' => $this->getChatId(),
197 'MESSAGE_ID' => $message->getMessageId(),
198 'CHAT_TYPE' => $this->getType(),
199 'USER_ID' => $relation->getUserId(),
200 'CHAT_ID' => $relation->getChatId(),
201 'RELATION_ID' => $relation->getId(),
202 'SESSION_ID' => $sessionId,
203 ]);
204
205 if ($relation->getUserId() == $message->getAuthorId())
206 {
207 $relation
208 ->setLastId($message->getMessageId())
209 ->save();
210 }
211 }
212 }
213
214 protected function filterUsersToAdd(array $userIds): array
215 {
216 $filteredUsers = parent::filterUsersToAdd($userIds);
217
218 foreach ($filteredUsers as $key => $userId)
219 {
220 $user = \Bitrix\Im\V2\Entity\User\User::getInstance($userId);
221 if (!$user->isConnector() && ($user->isExtranet() || $user->isNetwork()))
222 {
223 unset($filteredUsers[$key]);
224 }
225 }
226
227 return $filteredUsers;
228 }
229
230 public function setExtranet(?bool $extranet): \Bitrix\Im\V2\Chat
231 {
232 return $this;
233 }
234
235 public function getExtranet(): ?bool
236 {
237 return false;
238 }
239
240 protected function updateStateAfterUsersAdd(array $usersToAdd): self
241 {
242 parent::updateStateAfterUsersAdd($usersToAdd);
243
244 if (Loader::includeModule('pull'))
245 {
246 foreach ($usersToAdd as $userId)
247 {
248 \CPullWatch::Delete($userId, 'IM_PUBLIC_' . $this->getId());
249 }
250 }
251
252 return $this;
253 }
254
255 protected function sendPushUsersAdd(array $usersToAdd, RelationCollection $oldRelations): array
256 {
257 $pushMessage = parent::sendPushUsersAdd($usersToAdd, $oldRelations);
258
259 if (Loader::includeModule('pull'))
260 {
261 \CPullWatch::AddToStack('IM_PUBLIC_' . $this->getId(), $pushMessage);
262 }
263
264 return $pushMessage;
265 }
266
267 protected function addUsersToRelation(array $usersToAdd, array $managerIds = [], ?bool $hideHistory = null)
268 {
269 parent::addUsersToRelation($usersToAdd, $managerIds, false);
270 }
271
272 public function startRecordVoice(): void
273 {
274 if (!Loader::includeModule('pull'))
275 {
276 return;
277 }
278
279 parent::startRecordVoice();
280 $pushFormatter = new PushFormat();
281 \CPullWatch::AddToStack('IM_PUBLIC_'.$this->getId(), $pushFormatter->formatStartRecordVoice($this));
282 }
283
284 protected function addIndex(): \Bitrix\Im\V2\Chat
285 {
286 if (!Loader::includeModule('imopenlines'))
287 {
288 return $this;
289 }
290
291 ChatIndexTable::addIndex($this->getId(), $this->getTitle());
292
293 return $this;
294 }
295
296 protected function updateIndex(): \Bitrix\Im\V2\Chat
297 {
298 if (!Loader::includeModule('imopenlines'))
299 {
300 return $this;
301 }
302
303 ChatIndexTable::updateIndex($this->getId(), $this->getTitle());
304
305 return $this;
306 }
307
308 public function sendPushUpdateMessage(Message $message): void
309 {
310 parent::sendPushUpdateMessage($message);
311 $pushFormat = new Message\PushFormat();
312 $push = $pushFormat->formatMessageUpdate($message);
313 $push['params']['dialogId'] = $this->getDialogId();
314 \CPullWatch::AddToStack('IM_PUBLIC_' . $message->getChatId(), $push);
315 }
316}
static unread($dialogId, $unread, $userId=null, ?int $markedId=null)
Definition recent.php:1308
getEntityData(bool $force=false)
sendPushUpdateMessage(Message $message)
read(bool $onlyRecent=false, bool $byEvent=false, bool $forceRead=false)
addUsersToRelation(array $usersToAdd, array $managerIds=[], ?bool $hideHistory=null)
readAllMessages(bool $byEvent=false, bool $forceRead=false)
readMessages(?MessageCollection $messages, bool $byEvent=false, bool $forceRead=false)
sendPushUsersAdd(array $usersToAdd, RelationCollection $oldRelations)
updateStateAfterUsersAdd(array $usersToAdd)