Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
ReminderService.php
1
<?php
2
3
namespace
Bitrix\Im\V2\Link\Reminder
;
4
5
use
Bitrix\Im\Model\LinkReminderTable
;
6
use
Bitrix\Im\V2\Chat
;
7
use Bitrix\Im\V2\Common\ContextCustomer;
8
use
Bitrix\Im\V2\Link\Push
;
9
use
Bitrix\Im\V2\Message
;
10
use
Bitrix\Im\V2\Result
;
11
use
Bitrix\Im\V2\Service\Context
;
12
use
Bitrix\Main\Localization\Loc
;
13
use
Bitrix\Main\ORM\Query\Query
;
14
use
Bitrix\Main\Type\DateTime
;
15
use CIMNotify;
16
17
class
ReminderService
18
{
19
use ContextCustomer;
20
21
public
const
ADD_REMINDERS_EVENT
=
'reminderAdd'
;
22
public
const
DELETE_REMINDERS_EVENT
=
'reminderDelete'
;
23
24
public
static
function
remindAgent
(): string
25
{
26
(
new
static
())->remind();
27
28
return
__METHOD__.
'();'
;
29
}
30
31
public
function
getCount
(
int
$chatId): int
32
{
33
$filter = Query::filter()
34
->where(
'CHAT_ID'
, $chatId)
35
->where(
'AUTHOR_ID'
, $this->getContext()->getUserId())
36
;
37
38
return
LinkReminderTable::getCount
($filter);
39
}
40
41
public
function
addMessageToReminders
(
Message
$message,
DateTime
$dateRemind):
Result
42
{
43
$result =
new
Result
();
44
45
if
($dateRemind->
getTimestamp
() < (
new
DateTime
())->getTimestamp())
46
{
47
return
$result->addError(
new
ReminderError
(
ReminderError::DATE_REMIND_PASSED
));
48
}
49
50
$reminder =
ReminderItem::createFromMessage
($message, $this->getContext())->setDateRemind($dateRemind);
51
$saveResult = $this->
saveReminder
($reminder);
52
53
if
(!$saveResult->isSuccess())
54
{
55
return
$result->addErrors($saveResult->getErrors());
56
}
57
58
$pushRecipient = [
'RECIPIENT'
=> [$this->getContext()->getUserId()]];
59
60
Push::getInstance
()
61
->setContext($this->context)
62
->sendFull($reminder, static::ADD_REMINDERS_EVENT, $pushRecipient)
63
;
64
65
return
$result;
66
}
67
68
public
function
deleteRemindersByMessage
(
Message
$message):
Result
69
{
70
$result =
new
Result
();
71
72
$reminders =
ReminderCollection::getByMessage
($message);
73
74
if
($reminders->count() === 0)
75
{
76
return
$result;
77
}
78
79
$deleteResult = $reminders->delete();
80
81
if
(!$deleteResult->isSuccess())
82
{
83
return
$result->addErrors($deleteResult->getErrors());
84
}
85
86
foreach
($reminders as $reminder)
87
{
88
$pushRecipient = [
'RECIPIENT'
=> [$reminder->getAuthorId()]];
89
Push::getInstance
()
90
->setContext((
new
Context
())->setUserId($reminder->getAuthorId()))
91
->sendIdOnly($reminder, static::DELETE_REMINDERS_EVENT, $pushRecipient)
92
;
93
}
94
95
return
$result;
96
}
97
98
public
function
deleteReminder
(
ReminderItem
$reminder):
Result
99
{
100
$result =
new
Result
();
101
102
$deleteResult = $reminder->
delete
();
103
104
if
(!$deleteResult->isSuccess())
105
{
106
return
$result->addErrors($deleteResult->getErrors());
107
}
108
109
$deleteNotifyResult = $this->
deleteNotify
($reminder);
110
111
if
(!$deleteNotifyResult->isSuccess())
112
{
113
$result->addErrors($deleteNotifyResult->getErrors());
114
}
115
116
$pushRecipient = [
'RECIPIENT'
=> [$reminder->
getAuthorId
()]];
117
118
Push::getInstance
()
119
->setContext((
new
Context
())->setUserId($reminder->
getAuthorId
()))
120
->sendIdOnly($reminder, static::DELETE_REMINDERS_EVENT, $pushRecipient)
121
;
122
123
return
$result;
124
}
125
126
public
function
remind
():
Result
127
{
128
$result =
new
Result
();
129
$reminders =
ReminderCollection::getNeedReminded
();
130
131
$reminders->getMessageCollection()->fillFiles();
132
133
foreach
($reminders as $reminder)
134
{
135
$sendResult = $this->
sendNotifyAboutReminder
($reminder);
136
if
(!$sendResult->isSuccess())
137
{
138
continue
;
139
}
140
$reminder->setIsReminded(
true
);
141
}
142
143
$saveResult = $reminders->save(
true
);
144
145
if
(!$saveResult->isSuccess())
146
{
147
return
$result->addErrors($saveResult->getErrors());
148
}
149
150
return
$result;
151
}
152
153
protected
function
sendNotifyAboutReminder
(
ReminderItem
$reminder):
Result
154
{
155
$result =
new
Result
();
156
157
$attach = new \CIMMessageParamAttach();
158
159
$user = $reminder->
getEntity
()->getAuthor();
160
161
if
($user !==
null
)
162
{
163
$attach->AddUser([
164
'NAME'
=> $user->getFullName(),
165
'AVATAR'
=> $user->getAvatar(),
166
]);
167
}
168
169
$attach->AddMessage($reminder->
getEntity
()->getPreviewMessage());
170
171
$notifyParams = [
172
'TO_USER_ID'
=> $reminder->
getAuthorId
(),
173
'FROM_USER_ID'
=> 0,
174
'NOTIFY_TYPE'
=> IM_NOTIFY_SYSTEM,
175
'NOTIFY_MODULE'
=>
'im'
,
176
'NOTIFY_SUB_TAG'
=> $this->getSubTag($reminder),
177
'NOTIFY_MESSAGE'
=> $this->
getNotifyMessageText
($reminder,
false
),
178
'NOTIFY_MESSAGE_OUT'
=> $this->
getNotifyMessageText
($reminder,
true
),
179
'ATTACH'
=> $attach
180
];
181
182
$notifyId = CIMNotify::Add($notifyParams);
183
184
if
($notifyId ===
false
)
185
{
186
return
$result->addError(
new
ReminderError
(
ReminderError::REMINDER_NOTIFY_ADD_ERROR
));
187
}
188
189
return
$result;
190
}
191
192
protected
function
deleteNotify
(
ReminderItem
$reminder):
Result
193
{
194
$isDeleteSuccess = CIMNotify::DeleteBySubTag($this->getSubTag($reminder));
195
196
if
($isDeleteSuccess)
197
{
198
return
new
Result
();
199
}
200
201
return
(
new
Result
())->addError(
new
ReminderError
(
ReminderError::REMINDER_NOTIFY_DELETE_ERROR
));
202
}
203
204
protected
function
saveReminder
(
ReminderItem
$reminder):
Result
205
{
206
try
207
{
208
return
$reminder->
save
();
209
}
210
catch
(\
Bitrix
\Main\
SystemException
$exception)
211
{
212
return
(
new
Result
())->addError(
new
Message
\
MessageError
(
Message
\MessageError::MESSAGE_IS_ALREADY_IN_REMINDERS));
213
}
214
}
215
216
protected
function
getNotifyMessageText
(
ReminderItem
$reminder,
bool
$isOut): callable
217
{
218
$chat =
Chat::getInstance
($reminder->
getChatId
())->setContext((
new
Context
())->setUserId($reminder->
getAuthorId
()));
219
220
$chatTitle = $isOut ? $chat->getDisplayedTitle() :
"[CHAT={$chat->getChatId()}]{$chat->getDisplayedTitle()}[/CHAT]"
;
221
222
return
fn (?
string
$languageId =
null
) =>
Loc::getMessage
(
223
'IM_CHAT_REMINDER_REMIND_NOTIFICATION'
,
224
[
'#CHAT_TITLE#'
=> $chatTitle],
225
$languageId
226
);
227
}
228
229
private
function
getSubTag(
ReminderItem
$reminder): string
230
{
231
return
"MESSAGE_REMINDER_{$reminder->getId()}"
;
232
}
233
}
Bitrix\Im\Model\LinkReminderTable
Definition
linkreminder.php:43
Bitrix\Im\V2\Link\BaseLinkItem\getChatId
getChatId()
Definition
BaseLinkItem.php:111
Bitrix\Im\V2\Link\BaseLinkItem\getEntity
getEntity()
Definition
BaseLinkItem.php:147
Bitrix\Im\V2\Link\BaseLinkItem\getAuthorId
getAuthorId()
Definition
BaseLinkItem.php:89
Bitrix\Im\V2\Link\Push
Definition
Push.php:10
Bitrix\Im\V2\Link\Push\getInstance
static getInstance()
Definition
Push.php:24
Bitrix\Im\V2\Link\Reminder\ReminderCollection\getNeedReminded
static getNeedReminded()
Definition
ReminderCollection.php:133
Bitrix\Im\V2\Link\Reminder\ReminderCollection\getByMessage
static getByMessage(Message $message)
Definition
ReminderCollection.php:76
Bitrix\Im\V2\Link\Reminder\ReminderError
Definition
ReminderError.php:9
Bitrix\Im\V2\Link\Reminder\ReminderError\DATE_REMIND_PASSED
const DATE_REMIND_PASSED
Definition
ReminderError.php:10
Bitrix\Im\V2\Link\Reminder\ReminderError\REMINDER_NOTIFY_ADD_ERROR
const REMINDER_NOTIFY_ADD_ERROR
Definition
ReminderError.php:11
Bitrix\Im\V2\Link\Reminder\ReminderError\REMINDER_NOTIFY_DELETE_ERROR
const REMINDER_NOTIFY_DELETE_ERROR
Definition
ReminderError.php:12
Bitrix\Im\V2\Link\Reminder\ReminderItem
Definition
ReminderItem.php:20
Bitrix\Im\V2\Link\Reminder\ReminderItem\createFromMessage
static createFromMessage(Message $message, ?Context $context=null)
Definition
ReminderItem.php:68
Bitrix\Im\V2\Link\Reminder\ReminderService
Definition
ReminderService.php:18
Bitrix\Im\V2\Link\Reminder\ReminderService\deleteNotify
deleteNotify(ReminderItem $reminder)
Definition
ReminderService.php:192
Bitrix\Im\V2\Link\Reminder\ReminderService\ADD_REMINDERS_EVENT
const ADD_REMINDERS_EVENT
Definition
ReminderService.php:21
Bitrix\Im\V2\Link\Reminder\ReminderService\getCount
getCount(int $chatId)
Definition
ReminderService.php:31
Bitrix\Im\V2\Link\Reminder\ReminderService\DELETE_REMINDERS_EVENT
const DELETE_REMINDERS_EVENT
Definition
ReminderService.php:22
Bitrix\Im\V2\Link\Reminder\ReminderService\sendNotifyAboutReminder
sendNotifyAboutReminder(ReminderItem $reminder)
Definition
ReminderService.php:153
Bitrix\Im\V2\Link\Reminder\ReminderService\addMessageToReminders
addMessageToReminders(Message $message, DateTime $dateRemind)
Definition
ReminderService.php:41
Bitrix\Im\V2\Link\Reminder\ReminderService\saveReminder
saveReminder(ReminderItem $reminder)
Definition
ReminderService.php:204
Bitrix\Im\V2\Link\Reminder\ReminderService\deleteReminder
deleteReminder(ReminderItem $reminder)
Definition
ReminderService.php:98
Bitrix\Im\V2\Link\Reminder\ReminderService\getNotifyMessageText
getNotifyMessageText(ReminderItem $reminder, bool $isOut)
Definition
ReminderService.php:216
Bitrix\Im\V2\Link\Reminder\ReminderService\remind
remind()
Definition
ReminderService.php:126
Bitrix\Im\V2\Link\Reminder\ReminderService\deleteRemindersByMessage
deleteRemindersByMessage(Message $message)
Definition
ReminderService.php:68
Bitrix\Im\V2\Link\Reminder\ReminderService\remindAgent
static remindAgent()
Definition
ReminderService.php:24
Bitrix\Im\V2\Message\MessageError
Definition
MessageError.php:9
Bitrix\Im\V2\Message
Definition
Message.php:44
Bitrix\Im\V2\Result
Definition
Result.php:9
Bitrix\Im\V2\Service\Context
Definition
Context.php:8
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\Data\DataManager\getCount
static getCount($filter=array(), array $cache=array())
Definition
datamanager.php:526
Bitrix\Main\ORM\Query\Query
Definition
query.php:118
Bitrix\Main\SystemException
Definition
exception.php:8
Bitrix\Main\Type\Date\getTimestamp
getTimestamp()
Definition
date.php:215
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Im\V2\ActiveRecord\delete
delete()
Bitrix\Im\V2\ActiveRecord\save
save()
Bitrix\Im\V2\Chat
Definition
ChannelChat.php:3
Bitrix\Im\V2\Link\Reminder
Definition
ReminderCollection.php:3
Bitrix\Im\V2\Message
Definition
AdditionalMessagePopupItem.php:3
Bitrix
modules
im
lib
V2
Link
Reminder
ReminderService.php
Создано системой
1.10.0