Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
chat.php
1<?php
2namespace Bitrix\Im\Model;
3
12
13
54class ChatTable extends Entity\DataManager
55{
56 public static function getFilePath()
57 {
58 return __FILE__;
59 }
60
61 public static function getTableName()
62 {
63 return 'b_im_chat';
64 }
65
66 public static function getMap()
67 {
68 return array(
69 'ID' => array(
70 'data_type' => 'integer',
71 'primary' => true,
72 'autocomplete' => true,
73 //'title' => Loc::getMessage('CHAT_ENTITY_ID_FIELD'),
74 ),
75 'PARENT_ID' => array(
76 'data_type' => 'integer',
77 'default_value' => 0,
78 ),
79 'PARENT_MID' => array(
80 'data_type' => 'integer',
81 'default_value' => 0,
82 ),
83 'TITLE' => array(
84 'data_type' => 'string',
85 'validation' => array(__CLASS__, 'validateTitle'),
86 //'title' => Loc::getMessage('CHAT_ENTITY_TITLE_FIELD'),
87 'save_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getSaveModificator'),
88 'fetch_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getFetchModificator'),
89 ),
90 'DESCRIPTION' => array(
91 'data_type' => 'text',
92 //'title' => Loc::getMessage('CHAT_ENTITY_DESCRIPTION_FIELD'),
93 'save_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getSaveModificator'),
94 'fetch_data_modification' => array('\Bitrix\Main\Text\Emoji', 'getFetchModificator'),
95 ),
96 'COLOR' => array(
97 'data_type' => 'string',
98 'validation' => array(__CLASS__, 'validateColor'),
99 //'title' => Loc::getMessage('CHAT_ENTITY_COLOR_FIELD'),
100 ),
101 'TYPE' => array(
102 'data_type' => 'string',
103 'validation' => array(__CLASS__, 'validateType'),
104 //'title' => Loc::getMessage('CHAT_ENTITY_TYPE_FIELD'),
105 'default_value' => 'C',
106 ),
107 'EXTRANET' => array(
108 'data_type' => 'boolean',
109 'values' => array('N', 'Y'),
110 //'title' => Loc::getMessage('CHAT_ENTITY_EXTRANET_FIELD'),
111 'default_value' => 'N',
112 ),
113 'AUTHOR_ID' => array(
114 'data_type' => 'integer',
115 'required' => true,
116 //'title' => Loc::getMessage('CHAT_ENTITY_AUTHOR_ID_FIELD'),
117 ),
118 'AVATAR' => array(
119 'data_type' => 'integer'
120 ),
121 'CALL_TYPE' => array(
122 'data_type' => 'integer',
123 //'title' => Loc::getMessage('CHAT_ENTITY_CALL_TYPE_FIELD'),
124 ),
125 'CALL_NUMBER' => array(
126 'data_type' => 'string',
127 'validation' => array(__CLASS__, 'validateCallNumber'),
128 //'title' => Loc::getMessage('CHAT_ENTITY_CALL_NUMBER_FIELD'),
129 ),
130 'ENTITY_TYPE' => array(
131 'data_type' => 'string',
132 'validation' => array(__CLASS__, 'validateEntityType'),
133 //'title' => Loc::getMessage('CHAT_ENTITY_ENTITY_TYPE_FIELD'),
134 ),
135 'ENTITY_ID' => array(
136 'data_type' => 'string',
137 'validation' => array(__CLASS__, 'validateEntityId'),
138 //'title' => Loc::getMessage('CHAT_ENTITY_ENTITY_ID_FIELD'),
139 ),
140 'ENTITY_DATA_1' => array(
141 'data_type' => 'string',
142 'validation' => array(__CLASS__, 'validateEntityData'),
143 //'title' => Loc::getMessage('CHAT_ENTITY_ENTITY_DATA_1_FIELD'),
144 ),
145 'ENTITY_DATA_2' => array(
146 'data_type' => 'string',
147 'validation' => array(__CLASS__, 'validateEntityData'),
148 //'title' => Loc::getMessage('CHAT_ENTITY_ENTITY_DATA_2_FIELD'),
149 ),
150 'ENTITY_DATA_3' => array(
151 'data_type' => 'string',
152 'validation' => array(__CLASS__, 'validateEntityData'),
153 //'title' => Loc::getMessage('CHAT_ENTITY_ENTITY_DATA_3_FIELD'),
154 ),
155 'AUTHOR' => array(
156 'data_type' => 'Bitrix\Main\User',
157 'reference' => array('=this.AUTHOR_ID' => 'ref.ID'),
158 ),
159 'DISK_FOLDER_ID' => array(
160 'data_type' => 'integer'
161 ),
162 'PIN_MESSAGE_ID' => array(
163 'data_type' => 'integer',
164 'default_value' => 0
165 ),
166 'MESSAGE_COUNT' => array(
167 'data_type' => 'integer',
168 'default_value' => 0,
169 ),
170 'USER_COUNT' => array(
171 'data_type' => 'integer',
172 'default_value' => 0,
173 ),
174 'PREV_MESSAGE_ID' => array(
175 'data_type' => 'integer',
176 'default_value' => 0
177 ),
178 'LAST_MESSAGE_ID' => array(
179 'data_type' => 'integer',
180 'default_value' => 0
181 ),
182 'LAST_MESSAGE_STATUS' => array(
183 'data_type' => 'string',
184 'default_value' => IM_MESSAGE_STATUS_RECEIVED,
185 'validation' => array(__CLASS__, 'validateMessageStatus'),
186 ),
187 'DATE_CREATE' => array(
188 'data_type' => 'datetime',
189 'required' => false,
190 'default_value' => array(__CLASS__, 'getCurrentDate'),
191 ),
192 'MANAGE_USERS_ADD' => array(
193 'data_type' => 'string',
194 'default_value' => Chat::MANAGE_RIGHTS_MEMBER,
195 ),
196 'MANAGE_USERS_DELETE' => array(
197 'data_type' => 'string',
198 'default_value' => Chat::MANAGE_RIGHTS_MANAGERS,
199 ),
200 'MANAGE_UI' => array(
201 'data_type' => 'string',
202 'default_value' => Chat::MANAGE_RIGHTS_MEMBER,
203 ),
204 'MANAGE_SETTINGS' => array(
205 'data_type' => 'string',
206 'default_value' => Chat::MANAGE_RIGHTS_OWNER,
207 ),
208 'CAN_POST' => array(
209 'data_type' => 'string',
210 'default_value' => Chat::MANAGE_RIGHTS_MEMBER,
211 ),
212 'INDEX' => array(
213 'data_type' => 'Bitrix\Im\Model\ChatIndex',
214 'reference' => array('=this.ID' => 'ref.CHAT_ID'),
215 'join_type' => 'INNER',
216 ),
217 'OL_INDEX' => array(
218 'data_type' => 'Bitrix\ImOpenLines\Model\ChatIndexTable',
219 'reference' => array('=this.ID' => 'ref.CHAT_ID'),
220 'join_type' => 'INNER',
221 ),
222 'ALIAS' => array(
223 'data_type' => 'Bitrix\Im\Model\AliasTable',
224 'reference' => array(
225 '=this.ID' => 'ref.ENTITY_ID',
226 '=this.ENTITY_TYPE' => 'ref.ENTITY_TYPE'
227 ),
228 'join_type' => 'LEFT',
229 ),
230 'DISAPPEARING_TIME' => array(
231 'data_type' => 'integer'
232 ),
233 );
234 }
235
236 public static function onAfterUpdate(\Bitrix\Main\ORM\Event $event)
237 {
238 $fields = $event->getParameter("fields");
239 if (isset($fields['TITLE']))
240 {
241 $primary = $event->getParameter("id");
242
243 $chatIndex =
244 ChatIndex::create()
245 ->setChatId((int)$primary["ID"])
246 ;
247 static::updateIndexRecord($chatIndex);
248 }
249
250 $chatId = (int)$event->getParameter("id")['ID'];
251 if (static::needCacheInvalidate($fields))
252 {
253 Chat::cleanCache($chatId);
254 }
255
256 if (!Chat::getInstance($chatId) instanceof Chat\OpenLineChat)
257 {
258 Sync\Logger::getInstance()->add(
259 new Sync\Event(Sync\Event::ADD_EVENT, Sync\Event::CHAT_ENTITY, $chatId),
260 static fn () => Chat::getInstance($chatId)->getRelations()->getUserIds()
261 );
262 }
263
264 return new Entity\EventResult();
265 }
266
267 public static function onAfterDelete(Event $event)
268 {
269 $id = (int)$event->getParameter('primary')['ID'];
270 Chat::cleanCache($id);
271
272 return new Entity\EventResult();
273 }
274
275 public static function indexRecord(ChatIndex $chatIndex)
276 {
277 if ($chatIndex->getChatId() === 0)
278 {
279 return;
280 }
281
282 $record = static::getRecordChatData($chatIndex->getChatId());
283 if(!is_array($record))
284 {
285 return;
286 }
287
288 $chatIndex->setTitle($record['TITLE']);
289
291 'CHAT_ID' => $chatIndex->getChatId(),
292 'SEARCH_CONTENT' => MapBuilder::create()->addText(self::generateSearchContent($chatIndex))->build(),
293 'SEARCH_TITLE' => MapBuilder::create()->addText(self::generateSearchTitle($chatIndex))->build(),
294 ]);
295 }
296
297 public static function addIndexRecord(ChatIndex $index)
298 {
299 if ($index->getChatId() === 0)
300 {
301 return;
302 }
303 $insertData = self::prepareParamsForIndex($index);
304
305 ChatIndexTable::add($insertData);
306 }
307
308 public static function updateIndexRecord(ChatIndex $index)
309 {
310 $record = static::getRecordChatData($index->getChatId());
311 if(!is_array($record))
312 {
313 return;
314 }
315
316 if ($record['TYPE'] === Chat::IM_TYPE_OPEN_LINE)
317 {
318 if (Loader::includeModule('imopenlines'))
319 {
320 \Bitrix\ImOpenLines\Model\ChatIndexTable::updateIndex($index->getChatId(), $record['TITLE'] ?? null);
321 }
322
323 return;
324 }
325
326 $index->setTitle($record['TITLE']);
327 $updateData = self::prepareParamsForIndex($index);
328
330 $index->getChatId(),
331 'CHAT_ID',
332 $updateData
333 );
334 }
335
336 protected static function needCacheInvalidate(array $updatedFields): bool
337 {
338 $cacheInvalidatingFields = [
339 'TITLE',
340 'DESCRIPTION',
341 'COLOR',
342 'TYPE',
343 'EXTRANET',
344 'AUTHOR_ID',
345 'AVATAR',
346 'ENTITY_TYPE',
347 'ENTITY_ID',
348 'ENTITY_DATA_1',
349 'ENTITY_DATA_2',
350 'ENTITY_DATA_3',
351 'DISK_FOLDER_ID',
352 'MANAGE_USERS_ADD',
353 'MANAGE_USERS_DELETE',
354 'MANAGE_UI',
355 'MANAGE_SETTINGS',
356 'CAN_POST',
357 ];
358
359 return !empty(array_intersect($cacheInvalidatingFields, array_keys($updatedFields)));
360 }
361
362
367 public static function generateSearchContent(ChatIndex $chatIndex)
368 {
369 $indexTitle = $chatIndex->getClearedTitle();
370 $userNameList = static::getChatUserNameList($chatIndex);
371
372 return $indexTitle . ' ' . implode(' ', $userNameList);
373 }
374
375 public static function generateSearchTitle(ChatIndex $chatIndex): string
376 {
377 return $chatIndex->getClearedTitle();
378 }
379
380 public static function validateTitle()
381 {
382 return array(
383 new Entity\Validator\Length(null, 255),
384 );
385 }
386 public static function validateType()
387 {
388 return array(
389 new Entity\Validator\Length(null, 1),
390 );
391 }
392 public static function validateColor()
393 {
394 return array(
395 new Entity\Validator\Length(null, 255),
396 );
397 }
398 public static function validateEntityType()
399 {
400 return array(
401 new Entity\Validator\Length(null, 50),
402 );
403 }
404 public static function validateEntityId()
405 {
406 return array(
407 new Entity\Validator\Length(null, 255),
408 );
409 }
410 public static function validateCallNumber()
411 {
412 return array(
413 new Entity\Validator\Length(null, 20),
414 );
415 }
416 public static function validateEntityData()
417 {
418 return array(
419 new Entity\Validator\Length(null, 255),
420 );
421 }
422 public static function getCurrentDate()
423 {
424 return new \Bitrix\Main\Type\DateTime();
425 }
426 public static function validateMessageStatus()
427 {
428 return array(
429 new Entity\Validator\Length(null, 50),
430 );
431 }
432
437 private static function getRecordChatData(int $id)
438 {
439 return
440 self::query()
441 ->setSelect(['*'])
442 ->where('ID', $id)
443 ->where(
444 Query::filter()
445 ->logic('or')
446 ->whereNot('ENTITY_TYPE', 'LIVECHAT')
447 ->whereNull('ENTITY_TYPE')
448 )
450 ->fetch()
451 ;
452 }
453
454 private static function getChatUserNameList(ChatIndex $chatIndex): array
455 {
456 if (!$chatIndex->isEmptyUsers())
457 {
458 return $chatIndex->getClearedUserList();
459 }
460
461 $query =
462 \Bitrix\Im\Model\RelationTable::query()
463 ->addSelect('USER_ID')
464 ->where('CHAT_ID', $chatIndex->getChatId())
465 ->setLimit(100)
466 ;
467
468 $clearedUsers = [];
469 foreach ($query->exec() as $relation)
470 {
471 $rowUserName = \Bitrix\Im\User::getInstance($relation['USER_ID'])->getFullName(false);
472 $clearedUsers[] = ChatIndex::clearText($rowUserName);
473 }
474
475 return $clearedUsers;
476 }
477
482 private static function prepareParamsForIndex(ChatIndex $index): array
483 {
484 return [
485 'CHAT_ID' => $index->getChatId(),
486 'SEARCH_CONTENT' => MapBuilder::create()->addText(self::generateSearchContent($index))->build(),
487 'SEARCH_TITLE' => MapBuilder::create()->addText(self::generateSearchTitle($index))->build(),
488 ];
489 }
490}
const TYPE_PRIVATE
Definition chat.php:21
const TYPE_SYSTEM
Definition chat.php:20
static updateIndex($id, $primaryField, array $updateData)
static merge(array $data)
Definition chatindex.php:85
static validateMessageStatus()
Definition chat.php:426
static validateCallNumber()
Definition chat.php:410
static validateEntityData()
Definition chat.php:416
static validateEntityId()
Definition chat.php:404
static validateEntityType()
Definition chat.php:398
static generateSearchTitle(ChatIndex $chatIndex)
Definition chat.php:375
static generateSearchContent(ChatIndex $chatIndex)
Definition chat.php:367
static needCacheInvalidate(array $updatedFields)
Definition chat.php:336
static indexRecord(ChatIndex $chatIndex)
Definition chat.php:275
static onAfterDelete(Event $event)
Definition chat.php:267
static updateIndexRecord(ChatIndex $index)
Definition chat.php:308
static onAfterUpdate(\Bitrix\Main\ORM\Event $event)
Definition chat.php:236
static addIndexRecord(ChatIndex $index)
Definition chat.php:297
const MANAGE_RIGHTS_MEMBER
Definition Chat.php:133
const IM_TYPE_OPEN_LINE
Definition Chat.php:58
const MANAGE_RIGHTS_MANAGERS
Definition Chat.php:135
const MANAGE_RIGHTS_OWNER
Definition Chat.php:134
static cleanCache(int $id)
Definition Chat.php:306
static getInstance(?int $chatId)
Definition Chat.php:282
const IM_TYPE_COPILOT
Definition Chat.php:62