1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
sendingemailnotification.php
См. документацию.
1<?php
2
3namespace Bitrix\Calendar\Core\Queue\Processor;
4
5use Bitrix\Calendar\Core\Base\BaseException;
6use Bitrix\Calendar\Core\Queue\Interfaces;
7use Bitrix\Calendar\Core\Queue\Message\Message;
8use Bitrix\Calendar\Core\Queue\Producer\Producer;
9use Bitrix\Calendar\ICal\MailInvitation\InvitationInfo;
10use Bitrix\Calendar\ICal\MailInvitation\SenderInvitation;
11use Bitrix\Calendar\Internals\Log\Logger;
12use Bitrix\Main\ArgumentException;
13use Bitrix\Main\LoaderException;
14use Bitrix\Main\Localization\Loc;
15use Bitrix\Main\NotImplementedException;
16use Bitrix\Main\ObjectPropertyException;
17use Bitrix\Main\SiteTable;
18use Bitrix\Main\SystemException;
19use Bitrix\Main\Text\Emoji;
20use TypeError;
21
23{
24 private const MAX_ATTEMPTS_INVITATION = 3;
25 private const LOG_MARKER = 'DEBUG_CALENDAR_EMAIL_NOTIFICATION';
26
27 private Logger $logger;
28
29 public function __construct()
30 {
31 $this->logger = new Logger(self::LOG_MARKER);
32 }
33
41 public function process(Interfaces\Message $message): string
42 {
43 $serializedNotificationData = $this->getSerializedNotificationData($message);
44 if (is_null($serializedNotificationData))
45 {
46 return self::REJECT;
47 }
48
49 try
50 {
51 $notificationData = $this->unserializeNotificationData($serializedNotificationData);
52 }
53 catch (TypeError $exception)
54 {
55 $this->logger->log($exception);
56
57 return self::REJECT;
58 }
59
60 if (!$this->isNotificationFieldsCorrect($notificationData))
61 {
62 $this->logger->log($serializedNotificationData);
63
64 return self::REJECT;
65 }
66
67 $invitation = new InvitationInfo(
68 $notificationData['eventId'],
69 $notificationData['addresserId'],
70 $notificationData['receiverId'],
71 $notificationData['type'],
72 $notificationData['changeFields'] ?? [],
73 $notificationData['counterInvitation'] + 1,
74 );
75
76 $notification = $invitation->getSenderInvitation();
77 if (is_null($notification))
78 {
79 $this->logger->log($serializedNotificationData);
80
81 return self::REJECT;
82 }
83
84 $this->setLanguageId();
85 if ($notification->send())
86 {
87 $notification->executeAfterSuccessfulInvitation();
88
89 return self::ACK;
90 }
91
92 if ($notification->getCountAttempsSend() < self::MAX_ATTEMPTS_INVITATION)
93 {
94 self::sendMessageToQueue($invitation->toArray());
95
96 return self::ACK;
97 }
98
99 $failSent = [];
100 $failSent[$notification->getEventParentId()] = $this->getDataForNotify($notification);
101 $this->sendFailSendNotify($failSent);
102
103 return self::REJECT;
104 }
105
106 private function getSerializedNotificationData(Interfaces\Message $message): ?string
107 {
108 $messageBody = $message->getBody();
109 if (!is_array($messageBody) || !isset($messageBody['requestInvitation']))
110 {
111 return null;
112 }
113
114 return $messageBody['requestInvitation'];
115 }
116
117 private function isNotificationFieldsCorrect (mixed $notification): bool
118 {
119 if (!is_array($notification))
120 {
121 return false;
122 }
123
124 return isset(
125 $notification['eventId'],
126 $notification['addresserId'],
127 $notification['receiverId'],
128 $notification['type'],
129 $notification['counterInvitation']
130 );
131 }
132
133 private function unserializeNotificationData(string $serializeNotificationData): mixed
134 {
135 $notification = str_replace("\'", "'", $serializeNotificationData);
136 $notification = Emoji::decode($notification);
137
138 return unserialize($notification, ['allowed_classes' => false]);
139 }
140
141 private function sendFailSendNotify(array $failSent): void
142 {
143 foreach ($failSent as $parentId => $item)
144 {
145 if (isset($item[0]))
146 {
147 $item = $item[0];
148 }
149
150 \CCalendarNotify::Send([
151 'mode' => 'fail_ical_invite',
152 'eventId' => $parentId,
153 'userId' => $item['userId'],
154 'guestId' => $item['userId'],
155 'items' => [$item],
156 'name' => $item['name'],
157 'icalMethod' => $item['method'],
158 ]);
159 }
160 }
161
162 private function getDataForNotify(SenderInvitation $sender): array
163 {
164 $event = $sender->getEvent();
165
166 return [
167 'email' => $sender->getReceiver()->getEmail(),
168 'eventId' => $event['PARENT_ID'],
169 'name' => $event['NAME'],
170 'userId' => $event['MEETING_HOST'],
171 'method' => $sender->getMethod(),
172 ];
173 }
174
180 private function setLanguageId(): void
181 {
182 $siteDb = SiteTable::getById(SITE_ID);
183 if ($site = $siteDb->fetchObject())
184 {
185 Loc::setCurrentLang($site->getLanguageId());
186 }
187 }
188
194 public static function sendMessageToQueue(array $invitation): void
195 {
196 $serializedData = str_replace("'", "\'", serialize($invitation));
197 $serializedData = Emoji::encode($serializedData);
198
199 $message = (new Message())
200 ->setBody(['requestInvitation' => $serializedData])
201 ->setRoutingKey('calendar:sending_email_notification')
202 ;
203
204 (new Producer())->send($message);
205 }
206
213 public static function sendBatchOfMessagesToQueue(array $invitations): void
214 {
215 $messages = [];
216
217 if (!is_iterable($invitations))
218 {
219 return;
220 }
221
222 foreach ($invitations as $invitation)
223 {
224 $serializedData = str_replace("'", "\'", serialize($invitation));
225 $serializedData = Emoji::encode($serializedData);
226
227 $messages[] = (new Message())
228 ->setBody(['requestInvitation' => $serializedData])
229 ->setRoutingKey('calendar:sending_email_notification')
230 ;
231 }
232
233 (new Producer())->sendBatch($messages);
234 }
235}
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$message
Определения payment.php:8
$event
Определения prolog_after.php:141
$messages
Определения template.php:8
const SITE_ID
Определения sonet_set_content_view.php:12
$site
Определения yandex_run.php:614