Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
PushFormat.php
1
<?php
2
3
namespace
Bitrix\Im\V2\Message
;
4
5
use
Bitrix\Im\Text
;
6
use
Bitrix\Im\V2\Chat
;
7
use
Bitrix\Im\V2\Chat\PrivateChat
;
8
use
Bitrix\Im\V2\Message
;
9
use
Bitrix\Im\V2\Result
;
10
use
Bitrix\Main\Type\DateTime
;
11
use Bitrix\Im\V2\Common\ContextCustomer;
12
13
class
PushFormat
14
{
15
use ContextCustomer;
16
17
private
const
MAX_CHAT_MESSAGE_TIME = 600;
18
private
const
DND_USER_STATUS =
'dnd'
;
19
20
public
function
formatPrivateMessage
(
Message
$message,
PrivateChat
$chat): array
21
{
22
$fromUserId = $message->
getAuthorId
();
23
$toUserId = $chat->
getCompanion
()->getId();
24
25
$users = \CIMContactList::GetUserData([
26
'ID'
=> [$toUserId, $fromUserId],
27
'PHONES'
=>
'Y'
,
28
]);
29
30
return
[
31
'chatId'
=> $chat->getChatId(),
32
'dialogId'
=> 0,
33
'chat'
=> [],
34
'lines'
=>
null
,
35
'userInChat'
=> [],
36
'userBlockChat'
=> [],
37
'users'
=> !empty($users[
'users'
]) ? $users[
'users'
] :
null
,
38
'message'
=> [
39
'id'
=> $message->
getMessageId
(),
40
'templateId'
=> $message->
getUuid
(),
41
'templateFileId'
=> $message->
getFileUuid
(),
42
'prevId'
=> $chat->getPrevMessageId(),
43
'chatId'
=> $chat->getChatId(),
44
'senderId'
=> $fromUserId,
45
'recipientId'
=> $toUserId,
46
'system'
=> ($message->
isSystem
() ?
'Y'
:
'N'
),
47
'date'
=> $message->
getDateCreate
() ??
DateTime::createFromTimestamp
(time()),
// DATE_CREATE
48
'text'
=>
Text::parse
($message->
getMessage
()),
49
'textLegacy'
=>
Text::parseLegacyFormat
($message->
getMessage
()),
50
'params'
=> $message->
getParams
()->toPullFormat(),
51
'counter'
=> 0,
52
'isImportant'
=> $message->
isImportant
(),
53
'importantFor'
=> $message->
getImportantFor
(),
54
],
55
'files'
=> $message->
getFilesDiskData
(),
56
'notify'
=>
true
,
57
];
58
}
59
60
public
function
formatStartRecordVoice
(
Chat
$chat): array
61
{
62
$userId = $this->getContext()->getUserId();
63
return
[
64
'module_id'
=>
'im'
,
65
'command'
=>
'startRecordVoice'
,
66
'expiry'
=> 60,
67
'params'
=> [
68
'dialogId'
=> $chat instanceof
PrivateChat
? (string)$userId : $chat->getDialogId(),
69
'userId'
=> $userId,
70
'userName'
=> $this->getContext()->getUser()->getName()
71
],
72
'extra'
=> \Bitrix\Im\Common::getPullExtra()
73
];
74
}
75
76
public
function
formatMessageUpdate
(
Message
$message): array
77
{
78
return
[
79
'module_id'
=>
'im'
,
80
'command'
=>
'messageUpdate'
,
81
'params'
=> [
82
'id'
=> $message->
getId
(),
83
'type'
=> $message->
getChat
()->getType() ===
Chat::IM_TYPE_PRIVATE
?
'private'
:
'chat'
,
84
'text'
=> $message->
getParsedMessage
(),
85
'textLegacy'
=>
Text::parseLegacyFormat
($message->
getMessage
()),
86
'chatId'
=> $message->
getChatId
(),
87
'senderId'
=> $message->
getAuthorId
(),
88
'params'
=> $message->
getEnrichedParams
()->toPullFormat([
'IS_EDITED'
,
'URL_ID'
,
'ATTACH'
,
'DATE_TEXT'
,
'DATE_TS'
]),
89
],
90
'extra'
=> \Bitrix\Im\Common::getPullExtra()
91
];
92
}
93
94
public
function
validateDataForInform
(
Message
$message,
PrivateChat
$chat):
Result
95
{
96
$result =
new
Result
();
97
98
$toUser = $chat->
getCompanion
();
99
$toUserStatus = $toUser->getStatus(
true
);
100
101
if
(!($message->
getAuthorId
() === $this->getContext()->getUserId()))
102
{
103
$result->addError(
new
Message
\
MessageError
(
Message
\
MessageError::INFORM_USER_CONTEXT_ERROR
));
104
}
105
106
if
($message->
isViewedByOthers
())
107
{
108
$result->addError(
new
Message
\
MessageError
(
Message
\
MessageError::INFORM_VIEWED_ERROR
));
109
}
110
111
$timestampTimeNow =
DateTime::createFromTimestamp
(time())->getTimestamp();
112
if
(!($timestampTimeNow - $message->
getDateCreate
()->getTimestamp() <= self::MAX_CHAT_MESSAGE_TIME))
113
{
114
$result->addError(
new
Message
\
MessageError
(
Message
\
MessageError::INFORM_TIMEOUT_ERROR
));
115
}
116
117
if
(!($toUserStatus === self::DND_USER_STATUS))
118
{
119
$result->addError(
new
Message
\
MessageError
(
Message
\
MessageError::INFORM_USER_STATUS_ERROR
));
120
}
121
122
return
$result;
123
}
124
}
Bitrix\Im\Chat
Definition
chat.php:19
Bitrix\Im\Text
Definition
text.php:9
Bitrix\Im\Text\parse
static parse($text, $params=Array())
Definition
text.php:24
Bitrix\Im\Text\parseLegacyFormat
static parseLegacyFormat($text, $params=Array())
Definition
text.php:90
Bitrix\Im\V2\Chat\PrivateChat
Definition
PrivateChat.php:33
Bitrix\Im\V2\Chat\PrivateChat\getCompanion
getCompanion(?int $userId=null)
Definition
PrivateChat.php:162
Bitrix\Im\V2\Chat\IM_TYPE_PRIVATE
const IM_TYPE_PRIVATE
Definition
Chat.php:55
Bitrix\Im\V2\Message\MessageError
Definition
MessageError.php:9
Bitrix\Im\V2\Message\MessageError\INFORM_TIMEOUT_ERROR
const INFORM_TIMEOUT_ERROR
Definition
MessageError.php:31
Bitrix\Im\V2\Message\MessageError\INFORM_USER_STATUS_ERROR
const INFORM_USER_STATUS_ERROR
Definition
MessageError.php:32
Bitrix\Im\V2\Message\MessageError\INFORM_USER_CONTEXT_ERROR
const INFORM_USER_CONTEXT_ERROR
Definition
MessageError.php:29
Bitrix\Im\V2\Message\MessageError\INFORM_VIEWED_ERROR
const INFORM_VIEWED_ERROR
Definition
MessageError.php:30
Bitrix\Im\V2\Message\PushFormat
Definition
PushFormat.php:14
Bitrix\Im\V2\Message\PushFormat\formatPrivateMessage
formatPrivateMessage(Message $message, PrivateChat $chat)
Definition
PushFormat.php:20
Bitrix\Im\V2\Message\PushFormat\formatStartRecordVoice
formatStartRecordVoice(Chat $chat)
Definition
PushFormat.php:60
Bitrix\Im\V2\Message\PushFormat\validateDataForInform
validateDataForInform(Message $message, PrivateChat $chat)
Definition
PushFormat.php:94
Bitrix\Im\V2\Message\PushFormat\formatMessageUpdate
formatMessageUpdate(Message $message)
Definition
PushFormat.php:76
Bitrix\Im\V2\Message
Definition
Message.php:44
Bitrix\Im\V2\Message\getChatId
getChatId()
Definition
Message.php:867
Bitrix\Im\V2\Message\getMessage
getMessage()
Definition
Message.php:911
Bitrix\Im\V2\Message\getId
getId()
Definition
Message.php:214
Bitrix\Im\V2\Message\getImportantFor
getImportantFor()
Definition
Message.php:260
Bitrix\Im\V2\Message\getUuid
getUuid()
Definition
Message.php:532
Bitrix\Im\V2\Message\getDateCreate
getDateCreate()
Definition
Message.php:998
Bitrix\Im\V2\Message\isImportant
isImportant()
Definition
Message.php:248
Bitrix\Im\V2\Message\getAuthorId
getAuthorId()
Definition
Message.php:840
Bitrix\Im\V2\Message\getFilesDiskData
getFilesDiskData()
Definition
Message.php:730
Bitrix\Im\V2\Message\getMessageId
getMessageId()
Definition
Message.php:816
Bitrix\Im\V2\Message\getParams
getParams(bool $disallowLazyLoad=false)
Definition
Message.php:313
Bitrix\Im\V2\Message\getChat
getChat()
Definition
Message.php:872
Bitrix\Im\V2\Message\getParsedMessage
getParsedMessage()
Definition
Message.php:916
Bitrix\Im\V2\Message\isSystem
isSystem()
Definition
Message.php:226
Bitrix\Im\V2\Message\getEnrichedParams
getEnrichedParams()
Definition
Message.php:1670
Bitrix\Im\V2\Message\isViewedByOthers
isViewedByOthers()
Definition
Message.php:424
Bitrix\Im\V2\Message\getFileUuid
getFileUuid()
Definition
Message.php:561
Bitrix\Im\V2\Result
Definition
Result.php:9
Bitrix\Main\Type\DateTime
Definition
datetime.php:9
Bitrix\Main\Type\DateTime\createFromTimestamp
static createFromTimestamp($timestamp)
Definition
datetime.php:246
Bitrix\Im\V2\Chat
Definition
ChannelChat.php:3
Bitrix\Im\V2\Message
Definition
AdditionalMessagePopupItem.php:3
modules
im
lib
V2
Message
PushFormat.php
Создано системой
1.10.0