1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
BaseStage.php
См. документацию.
1<?php
2
3namespace Bitrix\Im\V2\Recent\Initializer;
4
5use Bitrix\Im\V2\Recent\Initializer\Queue\QueueItem;
6use Bitrix\Im\V2\Recent\Initializer\Stage\OtherUsersStage;
7use Bitrix\Im\V2\Recent\Initializer\Stage\TargetUserStage;
8use Bitrix\Main\Loader;
9use Bitrix\Main\Type\DateTime;
10
11abstract class BaseStage implements Stage
12{
13 protected int $targetId;
15 protected int $gapTime = self::WITHOUT_GAP_TIME;
16
17 public function __construct(int $targetId)
18 {
19 $this->targetId = $targetId;
20 }
21
22 public static function getInstance(StageType $type, int $targetId): Stage
23 {
24 return match ($type)
25 {
26 StageType::Target => new TargetUserStage($targetId),
27 StageType::Other => new OtherUsersStage($targetId),
28 };
29 }
30
31 public static function createFromQueueItem(QueueItem $queueItem): Stage
32 {
33 return static::getInstance($queueItem->stageType, $queueItem->userId);
34 }
35
36 public function setGapTime(int $gapTime = self::GAP_TIME): static
37 {
38 $this->gapTime = $gapTime;
39
40 return $this;
41 }
42
44 {
45 $users = $result->getItems();
46 $filteredUsers = $this->filterUsers($users);
47 $items = $this->getItemsByUsers($filteredUsers);
48
49 return $result->setItems($items)->setHasNextStep($this->hasNextStep($result));
50 }
51
52 public function sendPullAfterInsert(array $items): void
53 {
54 if (empty($items) || !Loader::includeModule('pull'))
55 {
56 return;
57 }
58
59 $recipients = $this->getPullRecipients($items);
60 $pullParams = $this->getPullParams($items);
61 \Bitrix\Pull\Event::add($recipients, [
62 'module_id' => 'im',
63 'command' => 'userShowInRecent',
64 'expiry' => 3600,
65 'params' => $pullParams,
66 'extra' => \Bitrix\Im\Common::getPullExtra()
67 ]);
68 }
69
70 abstract protected function getPullRecipients(array $items): array;
71
72 abstract protected function getPullParams(array $items): array;
73
74 final protected function filterUsers(array $users): array
75 {
76 $usersWithExistingItems = $this->getUsersWithExistingItems($users);
77
78 return array_diff_key($users, $usersWithExistingItems);
79 }
80
81 abstract protected function getUsersWithExistingItems(array $users): array;
82
83 protected function getItemsByUsers(array $users): array
84 {
85 $result = [];
86
87 foreach ($users as $user)
88 {
89 if ($user === $this->targetId)
90 {
91 continue;
92 }
93 $result[] = $this->getItemByTargetAndUser($this->targetId, $user);
94 }
95
96 return $result;
97 }
98
99 protected function getCurrentDate(): DateTime
100 {
101 // We need to subtract n seconds from the current time to create a time gap.
102 // This ensures that the order of elements in the "recent" is correct.
103 // Collabers should always appear below the main collab chat.
104 if ($this->gapTime)
105 {
106 $this->currentDate ??= (new DateTime())->add("-{$this->gapTime} seconds");
107 }
108 else
109 {
110 $this->currentDate ??= new DateTime();
111 }
112
113 return $this->currentDate;
114 }
115
116 abstract protected function getItemByTargetAndUser(int $targetUserId, int $otherUserId): array;
117
118 protected function getItem(int $userId, int $itemId): array
119 {
120 return [
121 'USER_ID' => $userId,
122 'ITEM_TYPE' => 'P',
123 'ITEM_ID' => $itemId,
124 'DATE_MESSAGE' => $this->getCurrentDate(),
125 'DATE_UPDATE' => $this->getCurrentDate(),
126 'DATE_LAST_ACTIVITY' => $this->getCurrentDate(),
127 ];
128 }
129
130 protected function hasNextStep(InitialiazerResult $result): bool
131 {
132 return $result->hasNextStep();
133 }
134}
$type
Определения options.php:106
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getPullExtra()
Определения common.php:127
hasNextStep(InitialiazerResult $result)
Определения BaseStage.php:130
static getInstance(StageType $type, int $targetId)
Определения BaseStage.php:22
__construct(int $targetId)
Определения BaseStage.php:17
filterUsers(array $users)
Определения BaseStage.php:74
getItems(InitialiazerResult $result)
Определения BaseStage.php:43
getItem(int $userId, int $itemId)
Определения BaseStage.php:118
static createFromQueueItem(QueueItem $queueItem)
Определения BaseStage.php:31
getItemByTargetAndUser(int $targetUserId, int $otherUserId)
setGapTime(int $gapTime=self::GAP_TIME)
Определения BaseStage.php:36
getItemsByUsers(array $users)
Определения BaseStage.php:83
sendPullAfterInsert(array $items)
Определения BaseStage.php:52
static add($recipient, array $parameters, $channelType=\CPullChannel::TYPE_PRIVATE)
Определения event.php:22
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$user
Определения mysql_to_pgsql.php:33
$items
Определения template.php:224