Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
counter.php
1
<?php
2
namespace
Bitrix\Im
;
3
4
use
Bitrix\Im\V2\Message\CounterServiceLegacy
;
5
use
Bitrix\Main\EventResult
;
6
use
Bitrix\Main\Localization\Loc
;
7
8
class
Counter
9
{
10
const
CACHE_TTL
= 86400;
// 1 month
11
const
CACHE_NAME
=
'counter_v3'
;
// 1 month
12
const
CACHE_PATH
=
'/bx/im/counter/'
;
13
14
const
TYPE_MESSENGER
=
'messenger'
;
15
const
MODULE_ID
=
'im'
;
16
17
public
static
function
get
($userId =
null
, $options = [])
18
{
19
if
(isset($options[
'JSON'
]))
20
{
21
return \Bitrix\Im\Common::toJson((
new
CounterServiceLegacy
($userId))->
get
());
22
}
23
return
(
new
CounterServiceLegacy
($userId))->get();
24
/*
25
$result = [
26
'TYPE' => [
27
'ALL' => 0,
28
'NOTIFY' => 0,
29
'DIALOG' => 0,
30
'CHAT' => 0,
31
'LINES' => 0,
32
],
33
'DIALOG' => [],
34
'DIALOG_UNREAD' => [],
35
'CHAT' => [],
36
'CHAT_MUTED' => [],
37
'CHAT_UNREAD' => [],
38
'LINES' => [],
39
];
40
41
$userId = Common::getUserId($userId);
42
if ($userId <= 0)
43
{
44
return $result;
45
}
46
47
$cache = \Bitrix\Main\Data\Cache::createInstance();
48
if ($cache->initCache(self::CACHE_TTL, self::CACHE_NAME.'_'.$userId, self::CACHE_PATH))
49
{
50
$result = $cache->getVars();
51
if (isset($options['JSON']))
52
{
53
$result = \Bitrix\Im\Common::toJson($result);
54
}
55
return $result;
56
}
57
58
$query = "
59
SELECT
60
R1.CHAT_ID,
61
R1.MESSAGE_TYPE,
62
CASE WHEN RC.ITEM_TYPE = '".IM_MESSAGE_PRIVATE."' THEN RC.ITEM_ID ELSE 0 END AS PRIVATE_USER_ID,
63
U.ACTIVE AS PRIVATE_USER_ACTIVE,
64
R1.COUNTER,
65
R1.NOTIFY_BLOCK MUTED,
66
CASE WHEN RC.USER_ID > 0 THEN 'Y' ELSE 'N' END AS IN_RECENT,
67
RC.UNREAD
68
FROM b_im_relation R1
69
LEFT JOIN b_im_recent RC ON RC.ITEM_RID = R1.ID
70
LEFT JOIN b_user U ON RC.ITEM_TYPE = '".IM_MESSAGE_PRIVATE."' AND U.ID = RC.ITEM_ID
71
WHERE R1.USER_ID = ".intval($userId)." AND (R1.STATUS <> ".IM_STATUS_READ." OR RC.UNREAD = 'Y')
72
";
73
$counters = \Bitrix\Main\Application::getInstance()->getConnection()->query($query)->fetchAll();
74
75
foreach ($counters as $entity)
76
{
77
if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_SYSTEM)
78
{
79
$result['TYPE']['ALL'] += (int)$entity['COUNTER'];
80
$result['TYPE']['NOTIFY'] += (int)$entity['COUNTER'];
81
}
82
else
83
{
84
if ($entity['IN_RECENT'] == 'N')
85
{
86
continue;
87
}
88
if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_PRIVATE)
89
{
90
if ($entity['PRIVATE_USER_ACTIVE'] === 'N')
91
{
92
continue;
93
}
94
95
if ($entity['COUNTER'] > 0)
96
{
97
$result['TYPE']['ALL'] += (int)$entity['COUNTER'];
98
$result['TYPE']['DIALOG'] += (int)$entity['COUNTER'];
99
$result['DIALOG'][$entity['PRIVATE_USER_ID']] = (int)$entity['COUNTER'];
100
}
101
else if ($entity['UNREAD'] === 'Y')
102
{
103
$result['TYPE']['ALL']++;
104
$result['TYPE']['DIALOG']++;
105
$result['DIALOG_UNREAD'][] = (int)$entity['PRIVATE_USER_ID'];
106
}
107
}
108
else if ($entity['MESSAGE_TYPE'] == IM_MESSAGE_OPEN_LINE)
109
{
110
$result['TYPE']['ALL'] += (int)$entity['COUNTER'];
111
$result['TYPE']['LINES'] += (int)$entity['COUNTER'];
112
$result['LINES'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
113
}
114
else
115
{
116
if ($entity['COUNTER'] > 0)
117
{
118
if ($entity['MUTED'] === 'N')
119
{
120
$result['TYPE']['ALL'] += (int)$entity['COUNTER'];
121
$result['TYPE']['CHAT'] += (int)$entity['COUNTER'];
122
$result['CHAT'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
123
}
124
else
125
{
126
$result['CHAT_MUTED'][$entity['CHAT_ID']] = (int)$entity['COUNTER'];
127
}
128
}
129
else if ($entity['UNREAD'] === 'Y')
130
{
131
if ($entity['MUTED'] === 'N')
132
{
133
$result['TYPE']['ALL']++;
134
$result['TYPE']['CHAT']++;
135
}
136
$result['CHAT_UNREAD'][] = (int)$entity['CHAT_ID'];
137
}
138
139
}
140
}
141
}
142
143
$cache->startDataCache();
144
$cache->endDataCache($result);
145
146
if (isset($options['JSON']))
147
{
148
$result = \Bitrix\Im\Common::toJson($result);
149
}
150
151
return $result;
152
*/
153
}
154
155
public
static
function
clearCache
($userId =
null
)
156
{
157
$cache = \Bitrix\Main\Data\Cache::createInstance();
158
if
($userId)
159
{
160
$cache->clean(self::CACHE_NAME.
'_'
.$userId, self::CACHE_PATH);
161
}
162
else
163
{
164
$cache->cleanDir(self::CACHE_PATH);
165
}
166
167
return
true
;
168
}
169
170
public
static
function
getChatCounter
($chatId, $userId =
null
)
171
{
172
$chatId = intval($chatId);
173
$userId =
Common::getUserId
($userId);
174
175
if
($chatId <= 0 || $userId <= 0)
176
{
177
return
false
;
178
}
179
180
$counters =
self::get
($userId);
181
182
return
intval($counters[
'CHAT'
][$chatId]);
183
}
184
185
public
static
function
getDialogCounter
($opponentUserId, $userId =
null
)
186
{
187
$userId =
Common::getUserId
($userId);
188
$opponentUserId = intval($opponentUserId);
189
if
($userId <= 0 || $opponentUserId <= 0)
190
{
191
return
false
;
192
}
193
194
$counters =
self::get
($userId);
195
196
return
intval($counters[
'DIALOG'
][$opponentUserId]);
197
}
198
199
public
static
function
getNotifyCounter
($userId =
null
)
200
{
201
$userId =
Common::getUserId
($userId);
202
if
($userId <= 0)
203
{
204
return
false
;
205
}
206
207
$counters =
self::get
($userId);
208
209
return
intval($counters[
'TYPE'
][
'NOTIFY'
]);
210
}
211
212
public
static
function
countingLostCountersAgent
($notifyRelationId = 0, $chatRelationId = 0)
213
{
214
return
''
;
215
216
/*
217
$foundNotify = false;
218
$foundChat = false;
219
220
$notifyStartId = intval($notifyRelationId);
221
222
if ($notifyStartId >= 0)
223
{
224
$query = "
225
SELECT COUNT(1) CNT, R.ID, R.USER_ID
226
FROM b_im_message M
227
INNER JOIN b_im_relation R ON R.CHAT_ID = M.CHAT_ID AND R.MESSAGE_TYPE = '".IM_MESSAGE_SYSTEM."' AND R.COUNTER = 0
228
WHERE M.NOTIFY_READ <> 'Y' AND R.ID > ".$notifyStartId."
229
GROUP BY R.ID, R.USER_ID
230
HAVING CNT > 0
231
";
232
$cursor = \Bitrix\Main\Application::getInstance()->getConnection()->query($query);
233
234
$count = 0;
235
while ($row = $cursor->fetch())
236
{
237
$notifyRelationId = $row['ID'];
238
$foundNotify = true;
239
240
\Bitrix\Im\Model\RelationTable::update($row['ID'], Array(
241
'STATUS' => IM_STATUS_UNREAD,
242
"MESSAGE_STATUS" => IM_MESSAGE_STATUS_RECEIVED,
243
'COUNTER' => $row['CNT'],
244
));
245
246
$count++;
247
if ($count > 100)
248
{
249
break;
250
}
251
}
252
}
253
254
$chatRelationId = intval($chatRelationId);
255
256
if ($chatRelationId >= 0)
257
{
258
$query = "
259
SELECT R.ID, R.COUNTER PREVIOUS_COUNTER, (
260
SELECT COUNT(1) FROM b_im_message M WHERE M.CHAT_ID = R.CHAT_ID AND M.ID > R.LAST_ID
261
) COUNTER
262
FROM b_im_relation R
263
WHERE R.STATUS <> ".IM_STATUS_READ." AND R.COUNTER = 0 AND R.ID > ".$chatRelationId."
264
ORDER BY R.ID ASC
265
LIMIT 0, 100;
266
";
267
$cursor = \Bitrix\Main\Application::getInstance()->getConnection()->query($query);
268
269
while ($row = $cursor->fetch())
270
{
271
$chatRelationId = $row['ID'];
272
$foundChat = true;
273
274
if ($row['COUNTER'] == 0)
275
{
276
$update = Array(
277
'STATUS' => IM_STATUS_READ,
278
"MESSAGE_STATUS" => IM_MESSAGE_STATUS_RECEIVED,
279
);
280
}
281
else if ($row['PREVIOUS_COUNTER'] == $row['COUNTER'])
282
{
283
continue;
284
}
285
else
286
{
287
$update = Array(
288
'COUNTER' => $row['COUNTER']
289
);
290
}
291
\Bitrix\Im\Model\RelationTable::update($row['ID'], $update);
292
}
293
}
294
295
if ($foundNotify || $foundChat)
296
{
297
return '\Bitrix\Im\Counter::countingLostCountersAgent('.($foundNotify? $notifyRelationId: -1).', '.($foundChat? $chatRelationId: -1).');';
298
}
299
else
300
{
301
return '';
302
}
303
*/
304
}
305
306
public
static
function
onGetMobileCounterTypes
(\
Bitrix
\Main\
Event
$event)
307
{
308
return
new
EventResult
(EventResult::SUCCESS, Array(
309
self::TYPE_MESSENGER => Array(
310
'NAME'
=>
Loc::getMessage
(
'IM_COUNTER_TYPE_MESSENGER_2'
),
311
'DEFAULT'
=>
true
312
),
313
), self::MODULE_ID);
314
}
315
316
public
static
function
onGetMobileCounter
(\
Bitrix
\Main\
Event
$event)
317
{
318
$params = $event->getParameters();
319
320
$counters =
self::get
($params[
'USER_ID'
]);
321
322
return
new
EventResult
(EventResult::SUCCESS, Array(
323
'TYPE'
=> self::TYPE_MESSENGER,
324
'COUNTER'
=> $counters[
'TYPE'
][
'ALL'
]
325
), self::MODULE_ID);
326
}
327
}
Bitrix\Im\Common\getUserId
static getUserId($userId=null)
Definition
common.php:74
Bitrix\Im\Counter
Definition
counter.php:9
Bitrix\Im\Counter\countingLostCountersAgent
static countingLostCountersAgent($notifyRelationId=0, $chatRelationId=0)
Definition
counter.php:212
Bitrix\Im\Counter\CACHE_PATH
const CACHE_PATH
Definition
counter.php:12
Bitrix\Im\Counter\CACHE_NAME
const CACHE_NAME
Definition
counter.php:11
Bitrix\Im\Counter\clearCache
static clearCache($userId=null)
Definition
counter.php:155
Bitrix\Im\Counter\MODULE_ID
const MODULE_ID
Definition
counter.php:15
Bitrix\Im\Counter\onGetMobileCounterTypes
static onGetMobileCounterTypes(\Bitrix\Main\Event $event)
Definition
counter.php:306
Bitrix\Im\Counter\getDialogCounter
static getDialogCounter($opponentUserId, $userId=null)
Definition
counter.php:185
Bitrix\Im\Counter\getNotifyCounter
static getNotifyCounter($userId=null)
Definition
counter.php:199
Bitrix\Im\Counter\TYPE_MESSENGER
const TYPE_MESSENGER
Definition
counter.php:14
Bitrix\Im\Counter\get
static get($userId=null, $options=[])
Definition
counter.php:17
Bitrix\Im\Counter\getChatCounter
static getChatCounter($chatId, $userId=null)
Definition
counter.php:170
Bitrix\Im\Counter\onGetMobileCounter
static onGetMobileCounter(\Bitrix\Main\Event $event)
Definition
counter.php:316
Bitrix\Im\Counter\CACHE_TTL
const CACHE_TTL
Definition
counter.php:10
Bitrix\Im\V2\Message\CounterServiceLegacy
Definition
CounterServiceLegacy.php:11
Bitrix\Im\V2\Sync\Event
Definition
Event.php:9
Bitrix\Main\EventResult
Definition
eventresult.php:5
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Im
Bitrix
modules
im
lib
counter.php
Создано системой
1.10.0