Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PinService.php
1<?php
2
4
8use Bitrix\Im\V2\Common\ContextCustomer;
16
18{
19 use ContextCustomer;
20
21 public const ADD_PIN_EVENT = 'pinAdd';
22 public const DELETE_PIN_EVENT = 'pinDelete';
23
24 public function pinMessage(Message $message): Result
25 {
26 $result = new Result();
27
28 $pin = PinItem::createFromMessage($message, $this->getContext());
29 $saveResult = $this->savePin($pin);
30
31 if (!$saveResult->isSuccess())
32 {
33 return $result->addErrors($saveResult->getErrors());
34 }
35
36 $saveParamResult = $this->saveInParam($message);
37
38 if (!$saveParamResult->isSuccess())
39 {
40 return $result->addErrors($saveParamResult->getErrors());
41 }
42
43 Sync\Logger::getInstance()->add(
44 new Sync\Event(Sync\Event::ADD_EVENT, Sync\Event::PIN_MESSAGE_ENTITY, $pin->getId()),
45 fn () => Chat::getInstance($pin->getChatId())->getRelations()->getUserIds()
46 );
47
48 $this->sendMessageAboutPin($pin);
49
51 ->setContext($this->context)
52 ->sendFull($pin, static::ADD_PIN_EVENT, ['CHAT_ID' => $pin->getChatId()])
53 ;
54
55 return $result;
56 }
57
58 public function unpinMessage(Message $message): Result
59 {
60 $result = new Result();
61
62 $pin = PinItem::getByMessage($message);
63
64 if ($pin === null)
65 {
66 return $result;
67 }
68
69 $deleteResult = $pin->delete();
70
71 if (!$deleteResult->isSuccess())
72 {
73 return $result->addErrors($deleteResult->getErrors());
74 }
75
76 $deleteParamResult = $this->deleteFromParam($message);
77
78 if (!$deleteParamResult->isSuccess())
79 {
80 return $result->addErrors($deleteParamResult->getErrors());
81 }
82
83 Sync\Logger::getInstance()->add(
84 new Sync\Event(Sync\Event::DELETE_EVENT, Sync\Event::PIN_MESSAGE_ENTITY, $pin->getId()),
85 fn () => Chat::getInstance($pin->getChatId())->getRelations()->getUserIds()
86 );
87
89 ->setContext($this->context)
90 ->sendIdOnly($pin, static::DELETE_PIN_EVENT, ['CHAT_ID' => $pin->getChatId()])
91 ;
92
93 return $result;
94 }
95
96 public function getCount(int $chatId, ?int $startId = null): int
97 {
98 $filter = Query::filter()->where('CHAT_ID', $chatId);
99
100 if (isset($startId) && $startId > 0)
101 {
102 $filter->where('MESSAGE_ID', '>=', $startId);
103 }
104
105 return LinkPinTable::getCount($filter);
106 }
107
108 protected function savePin(PinItem $pin): Result
109 {
110 try
111 {
112 return $pin->save();
113 }
114 catch (\Bitrix\Main\SystemException $exception)
115 {
116 return (new Result())->addError(new Message\MessageError(Message\MessageError::MESSAGE_IS_ALREADY_PIN));
117 }
118 }
119
120 protected function saveInParam(Message $message): Result
121 {
122 //todo replace this with new api
123 \CIMMessageParam::Set($message->getMessageId(), ['IS_PINNED' => 'Y']);
124 \CIMMessageParam::SendPull($message->getMessageId(), ['IS_PINNED']);
125
126 return new Result();
127 }
128
129 protected function deleteFromParam(Message $message): Result
130 {
131 //todo replace this with new api
132 \CIMMessageParam::Set($message->getMessageId(), ['IS_PINNED' => 'N']);
133 \CIMMessageParam::SendPull($message->getMessageId(), ['IS_PINNED']);
134
135 return new Result();
136 }
137
138 protected function sendMessageAboutPin(PinItem $pin): Result
139 {
140 //todo: Replace with new API
141 $dialogId = Dialog::getDialogId($pin->getChatId());
142 $authorId = $this->getContext()->getUserId();
143
144 $messageId = \CIMChat::AddMessage([
145 'DIALOG_ID' => $dialogId,
146 'SYSTEM' => 'Y',
147 'MESSAGE' => $this->getMessageText($pin),
148 'FROM_USER_ID' => $authorId,
149 'PARAMS' => [
150 'CLASS' => 'bx-messenger-content-item-system',
151 'BETA' => 'Y'
152 ],
153 'URL_PREVIEW' => 'N',
154 'SKIP_CONNECTOR' => 'Y',
155 'SKIP_COMMAND' => 'Y',
156 'SILENT_CONNECTOR' => 'Y',
157 ]);
158
159 $result = new Result();
160
161 if ($messageId === false)
162 {
163 return $result->addError(new Error(''));
164 }
165
166 return $result;
167 }
168
169 protected function getMessageText(PinItem $pin): string
170 {
171 $genderModifier = ($this->getContext()->getUser()->getGender() === 'F') ? '_F' : '';
172 $text = (new Message($pin->getMessageId()))->getQuotedMessage() . "\n";
173 $text .= Loc::getMessage(
174 'IM_CHAT_PIN_ADD_NOTIFICATION' . $genderModifier,
175 [
176 '#MESSAGE_ID#' => $pin->getMessageId(),
177 '#USER_ID#' => $this->getContext()->getUserId(),
178 '#DIALOG_ID#' => Chat::getInstance($pin->getChatId())->getDialogContextId(),
179 ]
180 );
181
182 return $text;
183 }
184}
static getDialogId(int $chatId, $userId=null)
Definition dialog.php:48
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29