- Модуль: im
- Путь к файлу: ~/bitrix/modules/im/lib/counter.php
- Класс: BitrixImCounter
- Вызов: Counter::get
static function get($userId = null, $options = [])
{
if (isset($options['JSON']))
{
return BitrixImCommon::toJson((new CounterServiceLegacy($userId))->get());
}
return (new CounterServiceLegacy($userId))->get();
$result = [
'TYPE' => [
'ALL' => 0,
'NOTIFY' => 0,
'DIALOG' => 0,
'CHAT' => 0,
'LINES' => 0,
],
'DIALOG' => [],
'DIALOG_UNREAD' => [],
'CHAT' => [],
'CHAT_MUTED' => [],
'CHAT_UNREAD' => [],
'LINES' => [],
];
$userId = Common::getUserId($userId);
if ($userId <= 0)
{
return $result;
}
$cache = BitrixMainDataCache::createInstance();
if ($cache->initCache(self::CACHE_TTL, self::CACHE_NAME.'_'.$userId, self::CACHE_PATH))
{
$result = $cache->getVars();
if (isset($options['JSON']))
{
$result = BitrixImCommon::toJson($result);
}
return $result;
}
$query = "
SELECT
R1.CHAT_ID,
R1.MESSAGE_TYPE,
IF(RC.ITEM_TYPE = '".IM_MESSAGE_PRIVATE."', RC.ITEM_ID, 0) PRIVATE_USER_ID,
U.ACTIVE PRIVATE_USER_ACTIVE,
R1.COUNTER,
R1.NOTIFY_BLOCK MUTED,
IF(RC.USER_ID > 0, 'Y', 'N') IN_RECENT,
RC.UNREAD
FROM b_im_relation R1
LEFT JOIN b_im_recent RC ON RC.ITEM_RID = R1.ID
LEFT JOIN b_user U ON RC.ITEM_TYPE = '".IM_MESSAGE_PRIVATE."' AND U.ID = RC.ITEM_ID
WHERE R1.USER_ID = ".intval($userId)." AND (R1.STATUS <> ".IM_STATUS_READ." OR RC.UNREAD = 'Y')
";
$counters = BitrixMainApplication::getInstance()->getConnection()->query($query)->fetchAll();
foreach ($counters as $entity)
{
if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM)
{
$result['TYPE']['ALL'] += (int)$entity['COUNTER'];
$result['TYPE']['NOTIFY'] += (int)$entity['COUNTER'];
}
else
{
if ($entity['IN_RECENT'] == 'N')
{
continue;
}
if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
{
if ($entity['PRIVATE_USER_ACTIVE'] === 'N')
{
continue;
}
if ($entity['COUNTER'] > 0)
{
$result['TYPE']['ALL'] += (int)$entity['COUNTER'];
$result['TYPE']['DIALOG'] += (int)$entity['COUNTER'];
$result['DIALOG'][$entity['PRIVATE_USER_ID']] = (int)$entity['COUNTER'];
}
else if ($entity['UNREAD'] === 'Y')
{
$result['TYPE']['ALL']++;
$result['TYPE']['DIALOG']++;
$result['DIALOG_UNREAD'][] = (int)$entity['PRIVATE_USER_ID'];
}
}
else if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
{
$result['TYPE']['ALL'] += (int)$entity['COUNTER'];
$result['TYPE']['LINES'] += (int)$entity['COUNTER'];
$result['LINES'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
}
else
{
if ($entity['COUNTER'] > 0)
{
if ($entity['MUTED'] === 'N')
{
$result['TYPE']['ALL'] += (int)$entity['COUNTER'];
$result['TYPE']['CHAT'] += (int)$entity['COUNTER'];
$result['CHAT'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
}
else
{
$result['CHAT_MUTED'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
}
}
else if ($entity['UNREAD'] === 'Y')
{
if ($entity['MUTED'] === 'N')
{
$result['TYPE']['ALL']++;
$result['TYPE']['CHAT']++;
}
$result['CHAT_UNREAD'][] = (int)$entity['CHAT_ID'];
}
}
}
}
$cache->startDataCache();
$cache->endDataCache($result);
if (isset($options['JSON']))
{
$result = BitrixImCommon::toJson($result);
}
return $result;
}