Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
ForwardService.php
1<?php
2
4
6use Bitrix\Im\V2\Common\ContextCustomer;
11
13{
14 use ContextCustomer;
15
16 private const PARAMS_TO_COPY_WHITELIST = [
17 Message\Params::ATTACH => Message\Params::ATTACH,
18 Message\Params::URL_ID => Message\Params::URL_ID,
19 Message\Params::IS_DELETED => Message\Params::IS_DELETED,
20 Message\Params::URL_ONLY => Message\Params::URL_ONLY,
21 Message\Params::LARGE_FONT => Message\Params::LARGE_FONT,
22 Message\Params::FORWARD_CONTEXT_ID => Message\Params::FORWARD_CONTEXT_ID,
23 Message\Params::FORWARD_ID => Message\Params::FORWARD_ID,
24 Message\Params::FORWARD_USER_ID => Message\Params::FORWARD_USER_ID,
25 Message\Params::REPLY_ID => Message\Params::REPLY_ID,
26 ];
27
28 private Chat $toChat;
29
30 public function __construct(Chat $toChat)
31 {
32 $this->toChat = $toChat;
33 }
34
39 public function createMessages(MessageCollection $forwardingMessages): Result
40 {
41 $result = new Result();
42
43 $uuidMap = [];
44 foreach ($forwardingMessages as $forwardingMessage)
45 {
46 if ($forwardingMessage->getChat()->hasAccess($this->getContext()->getUserId()))
47 {
48 $forwardMessageResult = $this->createForwardMessage($forwardingMessage);
49 if ($forwardMessageResult->hasResult())
50 {
51 $messageMap = $forwardMessageResult->getResult();
52 $uuidMap[$messageMap['uuid']] = $messageMap['id'];
53 }
54
55 $result->addErrors($forwardMessageResult->getErrors());
56 }
57 }
58
59 return $result->setResult($uuidMap);
60 }
61
66 private function createForwardMessage(Message $forwardingMessage): Result
67 {
68 $paramsResult = $this->createParamsForForwardMessage($forwardingMessage);
69 if (!$paramsResult->hasResult())
70 {
71 return $paramsResult;
72 }
73
74 $messageConfig = [
75 'MESSAGE_TYPE' => $this->toChat->getType(),
76 'MESSAGE' => $forwardingMessage->getMessage() !== '' ? $forwardingMessage->getMessage() : null,
77 'PARAMS' => $paramsResult->getResult(),
78 'TO_CHAT_ID' => $this->toChat->getChatId(),
79 'FROM_USER_ID' => $this->getContext()->getUserId(),
80 'URL_PREVIEW' => 'N',
81 ];
82
83 $result = new Result();
84
85 $messageId = \CIMMessenger::Add($messageConfig); //TODO replace with $chat->sendMessage
86 if (!$messageId)
87 {
89 }
90
91 return $result->setResult([
92 'uuid' => $forwardingMessage->getForwardUuid(),
93 'id' => $messageId
94 ]);
95 }
96
101 private function createParamsForForwardMessage(Message $forwardingMessage): Result
102 {
103 $result = new Result();
104
105 $newParams = $this->getForwardingMessageParams($forwardingMessage);
106
107 if ($this->isOriginalMessage($newParams))
108 {
109 $userId = $forwardingMessage->isSystem() ? 0 : $forwardingMessage->getAuthorId();
110 $newParams[Message\Params::FORWARD_ID] = $forwardingMessage->getId();
111 $newParams[Message\Params::FORWARD_CONTEXT_ID] = $forwardingMessage->getContextId();
112 $newParams[Message\Params::FORWARD_USER_ID] = $userId;
113 }
114
115 if ($forwardingMessage->getParams()->isSet(Message\Params::FILE_ID))
116 {
117 $newFileIds = [];
118 foreach ($forwardingMessage->getFiles() as $file)
119 {
120 $copy = $file->getCopyToChat($this->toChat);
121 if ($copy instanceof FileItem)
122 {
123 $newFileIds[] = $copy->getId();
124 }
125 }
126
127 $newParams[Message\Params::FILE_ID] = $newFileIds;
128 }
129
130 return $result->setResult($newParams);
131 }
132
138 private function isOriginalMessage(array $messageParams): bool
139 {
140 return !isset(
141 $messageParams[Message\Params::FORWARD_CONTEXT_ID],
142 $messageParams[Message\Params::FORWARD_USER_ID]
143 );
144 }
145
150 private function getForwardingMessageParams(Message $message): array
151 {
152 $result = [];
153 foreach ($message->getParams() as $param)
154 {
155 if (isset(self::PARAMS_TO_COPY_WHITELIST[$param->getName()]))
156 {
157 $value = $param->getValue();
158 if (is_bool($value))
159 {
160 $value = $value ? 'Y' : 'N';
161 }
162 $result[$param->getName()] = $value;
163 }
164 }
165
166 return $result;
167 }
168}
createMessages(MessageCollection $forwardingMessages)