1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
OwnerService.php
См. документацию.
1<?php
3
10
12{
13 protected const DELAY_AFTER_USER_FIRED = 10;
14 protected const CHAT_LIMIT = 1000;
15
16 public static function onAfterUserUpdate(array $fields): void
17 {
18 if (!isset($fields['ACTIVE']) || $fields['ACTIVE'] !== 'N')
19 {
20 return;
21 }
22
23 \CAgent::AddAgent(
24 self::getAgentName((int)$fields['ID'], 0),
25 'im',
26 'N',
27 self::DELAY_AFTER_USER_FIRED,
28 '',
29 'Y',
30 ConvertTimeStamp(time() + \CTimeZone::GetOffset() + self::DELAY_AFTER_USER_FIRED, "FULL"),
31 existError: false
32 );
33 }
34
35 public static function changeChatsOwnerAfterUserFiredAgent(int $ownerId, int $targetId = 0): string
36 {
37 $chatTypes = [Chat::IM_TYPE_OPEN, Chat::IM_TYPE_CHAT, Chat::IM_TYPE_OPEN_CHANNEL, Chat::IM_TYPE_CHANNEL];
38
39 $query = ChatTable::query()
40 ->setSelect(['ID'])
41 ->where('AUTHOR_ID', $ownerId)
42 ->whereIn('TYPE', $chatTypes)
43 ->setLimit(self::CHAT_LIMIT)
44 ->setOrder(['ID'])
45 ;
46
47 if ($targetId > 0)
48 {
49 $query->where('ID', '>', $targetId);
50 }
51
52 $chatCount = 0;
53 foreach ($query->fetchAll() as $row)
54 {
55 $chatCount++;
56 $chatId = (int)$row['ID'];
57
58 $chat = Chat::getInstance($chatId);
59 if (!isset($chat))
60 {
61 continue;
62 }
63
64 RelationTable::updateByFilter(
65 ['=CHAT_ID' => $chatId, '=USER_ID' => $chat->getAuthorId()],
66 ['MANAGER' => 'N']
67 );
68
69 $relationFilter = [
70 'CHAT_ID' => $chatId,
71 '!USER_ID' => $chat->getAuthorId(),
72 'ACTIVE' => true,
73 'ONLY_INTERNAL_TYPE' => true,
74 'ONLY_INTRANET' => true,
75 ];
76
77 $relations = RelationCollection::find($relationFilter, ['USER_ID' => 'ASC'], 1);
78 foreach ($relations as $relation)
79 {
80 $user = $relation->getUser();
81 if ($user->isExist())
82 {
83 $chat->setAuthorId($user->getId());
84 $relation->setManager(true);
85
86 break;
87 }
88 }
89
90 $chat->save();
91 $relations->save();
92 $targetId = $chatId;
93 }
94
95 if ($chatCount === self::CHAT_LIMIT)
96 {
97 return self::getAgentName($ownerId, $targetId);
98 }
99
100 return '';
101 }
102
103 private static function getAgentName(int $userId, int $chatId): string
104 {
105 return "\Bitrix\Im\V2\Chat\User\OwnerService::changeChatsOwnerAfterUserFiredAgent({$userId}, {$chatId});";
106 }
107
108 public static function changeChatsOwnerForAllFiredUsersAgent(): string
109 {
110 $firedUsers = UserTable::getList([
111 'select' => [
112 'ID'
113 ],
114 'filter' => [
115 'ACTIVE' => 'N'
116 ]
117 ])->fetchAll();
118
119 foreach ($firedUsers as $key => $user)
120 {
121 \CAgent::AddAgent(
122 self::getAgentName((int)$user['ID'], 0),
123 'im',
124 'N',
125 self::DELAY_AFTER_USER_FIRED,
126 '',
127 'Y',
128 ConvertTimeStamp(time() + \CTimeZone::GetOffset() + self::DELAY_AFTER_USER_FIRED * $key, "FULL")
129 );
130 }
131
132 return '';
133 }
134
135 public static function migrateOwnershipOfGeneralChatAgent(): string
136 {
137 $generalChatId = \COption::GetOptionInt('im', 'general_chat_id');
138 if (!$generalChatId)
139 {
140 return '';
141 }
142
143 $oldChat = Chat\ChatFactory::getInstance()->getChatById($generalChatId);
144 if ($oldChat instanceof Chat\NullChat)
145 {
146 return '';
147 }
148
149 $oldChat
150 ->setType(Chat::IM_TYPE_OPEN)
151 ->setEntityType(Chat::ENTITY_TYPE_GENERAL)
152 ->save();
153
154 $generalChat = Chat\ChatFactory::getInstance()->getGeneralChat();
155 if (!$generalChat || $generalChat instanceof Chat\NullChat)
156 {
157 return '';
158 }
159
160 $canPostAll = (\COption::GetOptionString('im', 'allow_send_to_general_chat_all', 'Y') === 'Y');
161 if ($canPostAll)
162 {
163 $generalChat
164 ->setManageMessages(Chat::MANAGE_RIGHTS_MEMBER)
165 ->save();
166
167 return '';
168 }
169
170 $chatRights = \COption::GetOptionString('im', 'allow_send_to_general_chat_rights');
171 if (!$chatRights)
172 {
173 return '';
174 }
175
176 $users = UserAccessTable::getList([
177 'select' => [
178 'USER_ID'
179 ],
180 'filter' => [
181 '=ACCESS_CODE' => explode(',', $chatRights)
182 ],
183 'group' => [
184 'USER_ID'
185 ]
186 ])->fetchAll();
187
188 if (!$users)
189 {
190 return '';
191 }
192
193 $userIds = array_column($users, 'USER_ID');
194
195 $relations = $generalChat->getRelations();
196 foreach ($relations as $relation)
197 {
198 if (in_array($relation->getUserId(), $userIds))
199 {
200 $relation->setManager(true);
201 $relation->save();
202 }
203 }
204
205 $generalChat
206 ->setManageMessages(Chat::MANAGE_RIGHTS_MANAGERS)
207 ->save();
208
209 return '';
210 }
211}
if(!is_object($USER)||! $USER->IsAuthorized()) $userId
Определения check_mail.php:18
static getInstance()
Определения ChatFactory.php:37
static onAfterUserUpdate(array $fields)
Определения OwnerService.php:16
static migrateOwnershipOfGeneralChatAgent()
Определения OwnerService.php:135
static changeChatsOwnerAfterUserFiredAgent(int $ownerId, int $targetId=0)
Определения OwnerService.php:35
static changeChatsOwnerForAllFiredUsersAgent()
Определения OwnerService.php:108
static find(array $filter, array $order=[], ?int $limit=null, ?Context $context=null, array $select=self::COMMON_FIELDS)
Определения RelationCollection.php:50
static getInstance()
Определения application.php:98
Определения user.php:48
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$query
Определения get_search.php:11
$user
Определения mysql_to_pgsql.php:33
if(empty($signedUserToken)) $key
Определения quickway.php:257
$fields
Определения yandex_run.php:501