Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
PinService.php
1
<?php
2
3
namespace
Bitrix\Im\V2\Link\Pin
;
4
5
use
Bitrix\Im\Dialog
;
6
use
Bitrix\Im\Model\LinkPinTable
;
7
use
Bitrix\Im\V2\Chat
;
8
use Bitrix\Im\V2\Common\ContextCustomer;
9
use
Bitrix\Im\V2\Error
;
10
use
Bitrix\Im\V2\Link\Push
;
11
use
Bitrix\Im\V2\Message
;
12
use
Bitrix\Im\V2\Result
;
13
use
Bitrix\Main\Localization\Loc
;
14
use
Bitrix\Main\ORM\Query\Query
;
15
use
Bitrix\Im\V2\Sync
;
16
17
class
PinService
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
50
Push::getInstance
()
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
88
Push::getInstance
()
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
}
Bitrix\Im\Dialog
Definition
dialog.php:9
Bitrix\Im\Dialog\getDialogId
static getDialogId(int $chatId, $userId=null)
Definition
dialog.php:48
Bitrix\Im\Model\LinkPinTable
Definition
linkpin.php:42
Bitrix\Im\V2\Error
Definition
Error.php:8
Bitrix\Im\V2\Link\BaseLinkItem\getChatId
getChatId()
Definition
BaseLinkItem.php:111
Bitrix\Im\V2\Link\Pin\PinItem
Definition
PinItem.php:20
Bitrix\Im\V2\Link\Pin\PinItem\getMessageId
getMessageId()
Definition
PinItem.php:112
Bitrix\Im\V2\Link\Pin\PinItem\createFromMessage
static createFromMessage(Message $message, ?Context $context=null)
Definition
PinItem.php:65
Bitrix\Im\V2\Link\Pin\PinItem\getByMessage
static getByMessage(Message $message)
Definition
PinItem.php:79
Bitrix\Im\V2\Link\Pin\PinService
Definition
PinService.php:18
Bitrix\Im\V2\Link\Pin\PinService\unpinMessage
unpinMessage(Message $message)
Definition
PinService.php:58
Bitrix\Im\V2\Link\Pin\PinService\savePin
savePin(PinItem $pin)
Definition
PinService.php:108
Bitrix\Im\V2\Link\Pin\PinService\DELETE_PIN_EVENT
const DELETE_PIN_EVENT
Definition
PinService.php:22
Bitrix\Im\V2\Link\Pin\PinService\ADD_PIN_EVENT
const ADD_PIN_EVENT
Definition
PinService.php:21
Bitrix\Im\V2\Link\Pin\PinService\getCount
getCount(int $chatId, ?int $startId=null)
Definition
PinService.php:96
Bitrix\Im\V2\Link\Pin\PinService\deleteFromParam
deleteFromParam(Message $message)
Definition
PinService.php:129
Bitrix\Im\V2\Link\Pin\PinService\saveInParam
saveInParam(Message $message)
Definition
PinService.php:120
Bitrix\Im\V2\Link\Pin\PinService\getMessageText
getMessageText(PinItem $pin)
Definition
PinService.php:169
Bitrix\Im\V2\Link\Pin\PinService\sendMessageAboutPin
sendMessageAboutPin(PinItem $pin)
Definition
PinService.php:138
Bitrix\Im\V2\Link\Pin\PinService\pinMessage
pinMessage(Message $message)
Definition
PinService.php:24
Bitrix\Im\V2\Link\Push
Definition
Push.php:10
Bitrix\Im\V2\Link\Push\getInstance
static getInstance()
Definition
Push.php:24
Bitrix\Im\V2\Message\MessageError
Definition
MessageError.php:9
Bitrix\Im\V2\Message
Definition
Message.php:44
Bitrix\Im\V2\Message\getMessageId
getMessageId()
Definition
Message.php:816
Bitrix\Im\V2\Result
Definition
Result.php:9
Bitrix\Im\V2\Sync\Event
Definition
Event.php:9
Bitrix\Main\Application\getInstance
static getInstance()
Definition
application.php:95
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Query\Query
Definition
query.php:118
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Im\V2\ActiveRecord\save
save()
Bitrix\Im\V2\Chat
Definition
ChannelChat.php:3
Bitrix\Im\V2\Link\Pin
Definition
PinCollection.php:3
Bitrix\Im\V2\Message
Definition
AdditionalMessagePopupItem.php:3
Bitrix\Im\V2\Sync
Definition
Entity.php:3
Bitrix
modules
im
lib
V2
Link
Pin
PinService.php
Создано системой
1.10.0