1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
PinService.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Link\Pin;
4
5use Bitrix\Im\Dialog;
6use Bitrix\Im\Model\LinkPinTable;
7use Bitrix\Im\Services\MessageParam;
8use Bitrix\Im\V2\Chat;
9use Bitrix\Im\V2\Common\ContextCustomer;
10use Bitrix\Im\V2\Error;
11use Bitrix\Im\V2\Link\Push;
12use Bitrix\Im\V2\Message;
13use Bitrix\Im\V2\MessageCollection;
14use Bitrix\Im\V2\Result;
15use Bitrix\Main\Localization\Loc;
16use Bitrix\Main\ORM\Query\Query;
17use Bitrix\Im\V2\Sync;
18
20{
21 use ContextCustomer;
22
23 public const ADD_PIN_EVENT = 'pinAdd';
24 public const DELETE_PIN_EVENT = 'pinDelete';
25
27 {
28 $result = new Result();
29
30 $pin = PinItem::createFromMessage($message, $this->getContext());
31 $saveResult = $this->savePin($pin);
32
33 if (!$saveResult->isSuccess())
34 {
35 return $result->addErrors($saveResult->getErrors());
36 }
37
38 $saveParamResult = $this->saveInParam($message);
39
40 if (!$saveParamResult->isSuccess())
41 {
42 return $result->addErrors($saveParamResult->getErrors());
43 }
44
45 $chat = Chat::getInstance($pin->getChatId());
47 new Sync\Event(Sync\Event::ADD_EVENT, Sync\Event::PIN_MESSAGE_ENTITY, $pin->getId()),
48 fn () => $chat->getRelations()->getUserIds(),
49 $chat
50 );
51
52 $this->sendMessageAboutPin($pin);
53
55 ->setContext($this->context)
56 ->sendFull($pin, static::ADD_PIN_EVENT, ['CHAT_ID' => $pin->getChatId()])
57 ;
58
59 return $result;
60 }
61
63 {
64 $result = new Result();
65
67
68 if ($pin === null)
69 {
70 return $result;
71 }
72
73 $deleteResult = $pin->delete();
74
75 if (!$deleteResult->isSuccess())
76 {
77 return $result->addErrors($deleteResult->getErrors());
78 }
79
80 $deleteParamResult = $this->deleteFromParam($message);
81
82 if (!$deleteParamResult->isSuccess())
83 {
84 return $result->addErrors($deleteParamResult->getErrors());
85 }
86
87 $chat = Chat::getInstance($pin->getChatId());
89 new Sync\Event(Sync\Event::DELETE_EVENT, Sync\Event::PIN_MESSAGE_ENTITY, $pin->getId()),
90 fn () => $chat->getRelations()->getUserIds(),
91 $chat
92 );
93
95 ->setContext($this->context)
96 ->sendIdOnly($pin, static::DELETE_PIN_EVENT, ['CHAT_ID' => $pin->getChatId()])
97 ;
98
99 return $result;
100 }
101
102 public function unpinMessages(MessageCollection $messages, bool $clearParams = true): Result
103 {
104 $result = new Result();
105
106 $pinCollection = PinCollection::getByMessages($messages);
107
108 if ($pinCollection->count() === 0)
109 {
110 return $result;
111 }
112
113 $deleteResult = $pinCollection->delete();
114
115 if (!$deleteResult->isSuccess())
116 {
117 return $result->addErrors($deleteResult->getErrors());
118 }
119
120 if ($clearParams)
121 {
122 $deleteParamResult = $this->deleteFromParams($messages);
123
124 if (!$deleteParamResult->isSuccess())
125 {
126 return $result->addErrors($deleteParamResult->getErrors());
127 }
128 }
129
130
131 $chat = Chat::getInstance($pinCollection->getRelatedChatId());
132
134 foreach ($pinCollection as $pin)
135 {
136 Sync\Logger::getInstance()->add(
137 new Sync\Event(Sync\Event::DELETE_EVENT, Sync\Event::PIN_MESSAGE_ENTITY, $pin->getId()),
138 fn () => $chat->getRelations()->getUserIds(),
139 $chat
140 );
141
143 ->setContext($this->context)
144 ->sendIdOnly($pin, static::DELETE_PIN_EVENT, ['CHAT_ID' => $pin->getChatId()])
145 ;
146 }
147
148 return $result;
149 }
150
151 public function getCount(int $chatId, ?int $startId = null): int
152 {
153 $filter = Query::filter()->where('CHAT_ID', $chatId);
154
155 if (isset($startId) && $startId > 0)
156 {
157 $filter->where('MESSAGE_ID', '>=', $startId);
158 }
159
160 return LinkPinTable::getCount($filter);
161 }
162
163 protected function savePin(PinItem $pin): Result
164 {
165 try
166 {
167 return $pin->save();
168 }
169 catch (\Bitrix\Main\SystemException $exception)
170 {
171 return (new Result())->addError(new Message\MessageError(Message\MessageError::MESSAGE_IS_ALREADY_PIN));
172 }
173 }
174
175 protected function saveInParam(Message $message): Result
176 {
177 //todo replace this with new api
178 \CIMMessageParam::Set($message->getMessageId(), ['IS_PINNED' => 'Y']);
179 \CIMMessageParam::SendPull($message->getMessageId(), ['IS_PINNED']);
180
181 return new Result();
182 }
183
185 {
186 //todo replace this with new api
187 \CIMMessageParam::Set($message->getMessageId(), ['IS_PINNED' => 'N']);
188 \CIMMessageParam::SendPull($message->getMessageId(), ['IS_PINNED']);
189
190 return new Result();
191 }
192
194 {
195 $pushService = new Message\Param\PushService();
196 foreach ($messages as $message)
197 {
198 $message->getParams(true)->load([Message\Params::IS_PINNED => 'N']);
199 $pushService->sendPull($message, ['IS_PINNED']);
200 }
201
202 return new Result();
203 }
204
205 protected function sendMessageAboutPin(PinItem $pin): Result
206 {
207 //todo: Replace with new API
208 $dialogId = Dialog::getDialogId($pin->getChatId());
209 $authorId = $this->getContext()->getUserId();
210
211 $messageId = \CIMChat::AddMessage([
212 'DIALOG_ID' => $dialogId,
213 'SYSTEM' => 'Y',
214 'MESSAGE' => $this->getMessageText($pin),
215 'FROM_USER_ID' => $authorId,
216 'PARAMS' => [
217 'CLASS' => 'bx-messenger-content-item-system',
218 'BETA' => 'Y'
219 ],
220 'URL_PREVIEW' => 'N',
221 'SKIP_CONNECTOR' => 'Y',
222 'SKIP_COMMAND' => 'Y',
223 'SILENT_CONNECTOR' => 'Y',
224 ]);
225
226 $result = new Result();
227
228 if ($messageId === false)
229 {
230 return $result->addError(new Error(''));
231 }
232
233 return $result;
234 }
235
236 protected function getMessageText(PinItem $pin): string
237 {
238 if (Chat::getInstance($pin->getChatId()) instanceof Chat\ChannelChat)
239 {
240 $type = 'CHANNEL';
241 $versionPostfix = '';
242 }
243 else
244 {
245 $type = 'CHAT';
246 $versionPostfix = '_MSGVER_1';
247 }
248
249 $genderModifier = ($this->getContext()->getUser()->getGender() === 'F') ? '_F' : '';
250 $text = (new Message($pin->getMessageId()))->getQuotedMessage() . "\n";
251 $text .= Loc::getMessage(
252 "IM_{$type}_PIN_ADD_NOTIFICATION" . $genderModifier . $versionPostfix,
253 [
254 '#MESSAGE_ID#' => $pin->getMessageId(),
255 '#USER_ID#' => $this->getContext()->getUserId(),
256 '#DIALOG_ID#' => Chat::getInstance($pin->getChatId())->getDialogContextId(),
257 ]
258 );
259
260 return $text;
261 }
262}
$type
Определения options.php:106
if(! $messageFields||!isset($messageFields['message_id'])||!isset($messageFields['status'])||!CModule::IncludeModule("messageservice")) $messageId
Определения callback_ismscenter.php:26
static getDialogId(int $chatId, $userId=null)
Определения dialog.php:50
static getInstance()
Определения Logger.php:37
static getInstance()
Определения application.php:98
Определения result.php:20
Определения error.php:15
$result
Определения get_property_values.php:14
$filter
Определения iblock_catalog_list.php:54
Определения Uuid.php:3
Определения ChatsSync.php:3
$message
Определения payment.php:8
$text
Определения template_pdf.php:79
$messages
Определения template.php:8