Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
UpdateService.php
1<?php
2
4
8use Bitrix\Im\V2\Common\ContextCustomer;
17
19{
20 use ContextCustomer;
21
22 public const EVENT_AFTER_MESSAGE_UPDATE = 'OnAfterMessagesUpdate';
23
24 private Message $message;
25 private ?array $chatLastMessage = null;
26 private bool $urlPreview = true;
27 private bool $byEvent = false;
28 private bool $withCheckAccess = true;
29
30
31 public function __construct(Message $message)
32 {
33 $this->message = $message;
34 }
35
36 public function setMessage(Message $message): self
37 {
38 $this->message = $message;
39
40 return $this;
41 }
42
43 public function setUrlPreview(bool $urlPreview): self
44 {
45 $this->urlPreview = $urlPreview;
46
47 return $this;
48 }
49
50 public function setByEvent(bool $byEvent): self
51 {
52 $this->byEvent = $byEvent;
53
54 return $this;
55 }
56
57 public function withoutCheckAccess(): self
58 {
59 $this->withCheckAccess = false;
60
61 return $this;
62 }
63
64 public function update(array $fieldsToUpdate): Result
65 {
66 if ($this->withCheckAccess && !$this->canUpdate())
67 {
69 }
70
71 $this->message->fill($fieldsToUpdate);
72
73 if ($this->message->isCompletelyEmpty())
74 {
75 return (new Message\Delete\DeleteService($this->message))->delete();
76 }
77
78 if ($this->message->isViewedByOthers())
79 {
80 $this->message->getParams()->get(Params::IS_EDITED)->setValue(true);
81 }
82
83 $filesFromText = $this->message->autocompleteParams($this->urlPreview)->uploadFileFromText();
84 $result = $this->message->save();
85 if (!$result->isSuccess())
86 {
87 return $result;
88 }
89
90 Application::getConnection()->queryExecute("
91 UPDATE b_im_recent
92 SET DATE_UPDATE = NOW()
93 WHERE ITEM_MID = " . $this->message->getId()
94 );
95
96 $this->message->getChat()->sendPushUpdateMessage($this->message);
97 (new Message\Param\PushService())->sendPull($this->message, ['KEYBOARD', 'ATTACH', 'MENU']);
98
99 MessageTable::indexRecord($this->message->getId());
100
101 (new UrlService())->updateUrlsFromMessage($this->message);
102 (new FileService())->saveFilesFromMessage($filesFromText, $this->message);
103
104 $this->fireEventAfterMessageUpdate();
105
106 return $result;
107 }
108
109 public function canUpdate(): bool
110 {
111 $isMessageDelete = $this->message->getParams()->get(Params::IS_DELETED)->getValue() === true;
112 $isForward = $this->message->getParams()->isSet(Params::FORWARD_ID);
113
114 if ($isMessageDelete || $isForward)
115 {
116 return false;
117 }
118
119 $user = $this->getContext()->getUser();
120 $chat = $this->message->getChat();
121
122 if ($chat instanceof Chat\OpenLineChat && Loader::includeModule('imopenlines'))
123 {
124 if ($user->isBot())
125 {
126 return true;
127 }
128
129 if ($user->getId() === $this->message->getAuthorId())
130 {
131 return $chat->canUpdateOwnMessage();
132 }
133
134 return false;
135 }
136
137 if ($this->message->getAuthorId() === $user->getId())
138 {
139 return true;
140 }
141
142 return false;
143 }
144
145 private function getBotInChat(): array
146 {
147 $result = [];
148 $users = $this->message->getChat()->getRelations()->getUsers();
149
150 foreach ($users as $user)
151 {
152 if ($user->isBot())
153 {
154 $result[$user->getId()] = $user->getId();
155 }
156 }
157
158 return $result;
159 }
160
161 private function fireEventAfterMessageUpdate(): void
162 {
163 $chat = $this->message->getChat();
164 $messageFields = [
165 'ID' => $this->message->getId(),
166 'CHAT_ID' => $this->message->getChatId(),
167 'AUTHOR_ID' => $this->message->getAuthorId(),
168 'MESSAGE' => $this->message->getMessage(),
169 'MESSAGE_OUT' => $this->message->getMessageOut(),
170 'DATE_CREATE' => $this->message->getDateCreate()->getTimestamp(),
171 'EMAIL_TEMPLATE' => $this->message->getEmailTemplate(),
172 'NOTIFY_TYPE' => $this->message->getNotifyType(),
173 'NOTIFY_MODULE' => $this->message->getNotifyModule(),
174 'NOTIFY_EVENT' => $this->message->getNotifyEvent(),
175 'NOTIFY_TAG' => $this->message->getNotifyTag(),
176 'NOTIFY_SUB_TAG' => $this->message->getNotifySubTag(),
177 'NOTIFY_TITLE' => $this->message->getNotifyTitle(),
178 'NOTIFY_BUTTONS' => $this->message->getNotifyButtons(),
179 'NOTIFY_READ' => $this->message->isNotifyRead(),
180 'IMPORT_ID' => $this->message->getImportId(),
181 'MESSAGE_TYPE' => $chat->getType(),
182 'CHAT_AUTHOR_ID' => $chat->getAuthorId(),
183 'CHAT_ENTITY_TYPE' => $chat->getEntityType(),
184 'CHAT_ENTITY_ID' => $chat->getEntityId(),
185 'CHAT_PARENT_ID' => $chat->getParentChatId(),
186 'CHAT_PARENT_MID' => $chat->getParentMessageId(),
187 'CHAT_ENTITY_DATA_1' => $chat->getEntityData1(),
188 'CHAT_ENTITY_DATA_2' => $chat->getEntityData2(),
189 'CHAT_ENTITY_DATA_3' => $chat->getEntityData3(),
190 'PARAMS' => $this->message->getParams()->toRestFormat(),
191 'DATE_MODIFY' => new DateTime()
192 ];
193
194 if ($chat instanceof Chat\PrivateChat)
195 {
196 $messageFields['FROM_USER_ID'] = $this->message->getAuthorId();
197 $messageFields['TO_USER_ID'] = $chat->getCompanion($this->message->getAuthorId())->getId();
198 }
199 else
200 {
201 $messageFields['BOT_IN_CHAT'] = $this->getBotInChat();
202 }
203
204 $updateFlags = [
205 'ID' => $this->message->getId(),
206 'TEXT' => $this->message->getMessage(),
207 'URL_PREVIEW' => $this->urlPreview,
208 'EDIT_FLAG' => $this->message->getParams()->get(Params::IS_EDITED)->getValue(),
209 'USER_ID' => $this->message->getAuthorId(),
210 'BY_EVENT' => $this->byEvent,
211 ];
212
213 foreach(GetModuleEvents('im', self::EVENT_AFTER_MESSAGE_UPDATE, true) as $event)
214 {
215 ExecuteModuleEventEx($event, [$this->message->getId(), $messageFields, $updateFlags]);
216 }
217
218 Bot::onMessageUpdate($this->message->getId(), $messageFields);
219 }
220}
static getConnection($name="")
addError(Error $error)
Definition result.php:50