Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
DisappearService.php
1
<?php
2
3
namespace
Bitrix\Im\V2\Message\Delete
;
4
5
use
Bitrix\Im\Model\MessageDisappearingTable
;
6
use
Bitrix\Im\V2\Chat
;
7
use
Bitrix\Im\V2\Message
;
8
use
Bitrix\Main\Localization\Loc
;
9
use \Bitrix\Main\Result;
10
use
Bitrix\Main\Type\DateTime
;
11
12
class
DisappearService
13
{
14
public
const
TIME_WHITELIST
= [
15
0,
//disable
16
1,
//hour
17
24,
//day
18
168,
//week
19
720,
//month
20
];
21
private
const
DISAPPEARING_TIME_UNIT =
'hours'
;
22
private
const
TIME_UNIT_HOUR =
'HOUR'
;
23
private
const
TIME_UNIT_DAY =
'DAY'
;
24
private
const
TIME_UNIT_WEEK =
'WEEK'
;
25
private
const
TIME_UNIT_MONTH =
'MONTH'
;
26
34
public
static
function
checkDisappearing
($messageId, $messageFields): bool
35
{
36
$message =
new
Message
($messageId);
37
if
(
38
!$message->getChat()->getDisappearingTime()
39
|| $message->isSystem()
40
)
41
{
42
return
false
;
43
}
44
45
$result =
MessageDisappearingTable::add
([
46
'MESSAGE_ID'
=> $message->getId(),
47
'DATE_CREATE'
=>
new
DateTime
(),
48
'DATE_REMOVE'
=> (
new
DateTime
())->add($message->getChat()->getDisappearingTime() .
' '
. self::DISAPPEARING_TIME_UNIT)
49
]);
50
51
return
$result->isSuccess();
52
}
53
54
public
static
function
disappearMessage
(
Message
$message,
int
$hours):
Result
55
{
56
if
(
57
$message->
isDisappearing
()
58
)
59
{
60
return
(
new
Result
())->
addError
(
new
Chat
\
ChatError
(
Chat
\
ChatError::ALREADY_DISAPPEARING
));
61
}
62
63
return
MessageDisappearingTable::add
([
64
'MESSAGE_ID'
=> $message->
getId
(),
65
'DATE_CREATE'
=>
new
DateTime
(),
66
'DATE_REMOVE'
=> (
new
DateTime
())->add($hours .
' '
. self::DISAPPEARING_TIME_UNIT)
67
]);
68
}
69
70
public
static
function
disappearChat
(
Chat
$chat,
int
$hours):
Result
71
{
72
$prevDisappearingTime = $chat->getDisappearingTime();
73
if
((
int
)$prevDisappearingTime === $hours)
74
{
75
return
new
Result
();
76
}
77
78
$chat->setDisappearingTime($hours);
79
$result = $chat->save();
80
81
if
(!$result->isSuccess())
82
{
83
return
$result;
84
}
85
86
if
((
int
)$prevDisappearingTime === 0 && $hours > 0)
87
{
88
\CIMMessage::Add([
89
'MESSAGE_TYPE'
=> $chat->
getType
(),
90
'TO_CHAT_ID'
=> $chat->getChatId(),
91
'MESSAGE'
=> self::getDisappearingMessage($hours),
92
'SYSTEM'
=>
'Y'
,
93
'PUSH'
=>
'N'
94
]);
95
}
96
elseif ($prevDisappearingTime > 0 && $hours === 0)
97
{
98
\CIMMessage::Add([
99
'MESSAGE_TYPE'
=> $chat->
getType
(),
100
'TO_CHAT_ID'
=> $chat->getChatId(),
101
'MESSAGE'
=>
Loc::getMessage
(
'DISAPPEAR_MESSAGES_OFF'
),
102
'SYSTEM'
=>
'Y'
,
103
'PUSH'
=>
'N'
104
]);
105
}
106
elseif ($prevDisappearingTime > 0 && $hours > 0)
107
{
108
\CIMMessage::Add([
109
'MESSAGE_TYPE'
=> $chat->
getType
(),
110
'TO_CHAT_ID'
=> $chat->getChatId(),
111
'MESSAGE'
=> self::getDisappearingMessage($hours,
true
),
112
'SYSTEM'
=>
'Y'
,
113
'PUSH'
=>
'N'
114
]);
115
}
116
117
return
$result;
118
}
119
120
public
static
function
getMessagesDisappearingTime
(array $messageIds): array
121
{
122
$rows =
MessageDisappearingTable::getList
([
123
'filter'
=> [
124
'MESSAGE_ID'
=> $messageIds
125
]
126
]);
127
$result = [];
128
foreach
($rows as $row)
129
{
130
$result[$row[
'MESSAGE_ID'
]] = $row;
131
}
132
133
return
$result;
134
}
135
136
private
static
function
getDisappearingMessage(
int
$hours,
bool
$change =
false
): string
137
{
138
switch
($hours)
139
{
140
case
720:
141
$timeUnit = self::TIME_UNIT_MONTH;
142
break
;
143
case
168:
144
$timeUnit = self::TIME_UNIT_WEEK;
145
break
;
146
case
24:
147
$timeUnit = self::TIME_UNIT_DAY;
148
break
;
149
default
:
150
$timeUnit = self::TIME_UNIT_HOUR;
151
}
152
153
$messageParts = [
154
'DISAPPEAR_MESSAGES'
,
155
$change ?
'CHANGE'
:
'ON'
,
156
$timeUnit,
157
];
158
159
return
Loc::getMessage
(implode(
'_'
, $messageParts));
160
}
161
}
Bitrix\Im\Chat
Definition
chat.php:19
Bitrix\Im\Chat\getType
static getType($chatData)
Definition
chat.php:41
Bitrix\Im\Model\MessageDisappearingTable
Definition
messagedisappearing.php:10
Bitrix\Im\V2\Chat\ChatError
Definition
ChatError.php:9
Bitrix\Im\V2\Chat\ChatError\ALREADY_DISAPPEARING
const ALREADY_DISAPPEARING
Definition
ChatError.php:20
Bitrix\Im\V2\Message\Delete\DisappearService
Definition
DisappearService.php:13
Bitrix\Im\V2\Message\Delete\DisappearService\disappearChat
static disappearChat(Chat $chat, int $hours)
Definition
DisappearService.php:70
Bitrix\Im\V2\Message\Delete\DisappearService\disappearMessage
static disappearMessage(Message $message, int $hours)
Definition
DisappearService.php:54
Bitrix\Im\V2\Message\Delete\DisappearService\getMessagesDisappearingTime
static getMessagesDisappearingTime(array $messageIds)
Definition
DisappearService.php:120
Bitrix\Im\V2\Message\Delete\DisappearService\checkDisappearing
static checkDisappearing($messageId, $messageFields)
Definition
DisappearService.php:34
Bitrix\Im\V2\Message\Delete\DisappearService\TIME_WHITELIST
const TIME_WHITELIST
Definition
DisappearService.php:14
Bitrix\Im\V2\Message
Definition
Message.php:44
Bitrix\Im\V2\Message\getId
getId()
Definition
Message.php:214
Bitrix\Im\V2\Message\isDisappearing
isDisappearing()
Definition
Message.php:231
Bitrix\Im\V2\Result
Definition
Result.php:9
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\getList
static getList(array $parameters=array())
Definition
datamanager.php:441
Bitrix\Main\ORM\Data\DataManager\add
static add(array $data)
Definition
datamanager.php:874
Bitrix\Main\Result\addError
addError(Error $error)
Definition
result.php:50
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Im\V2\Chat
Definition
ChannelChat.php:3
Bitrix\Im\V2\Message\Delete
Definition
DeleteService.php:3
Bitrix\Im\V2\Message
Definition
AdditionalMessagePopupItem.php:3
modules
im
lib
V2
Message
Delete
DisappearService.php
Создано системой
1.10.0