1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
|
/*
Copyright (c) 2013-24 Miranda NG team (https://miranda-ng.org)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
//////////////////////////////////////////////////////////////////////////////
int CVkProto::ForwardMsg(MCONTACT hContact, std::vector<MEVENT>& vForvardEvents, const char* szMsg)
{
debugLogA("CVkProto::ForwardMsg");
if (!IsOnline() || !vForvardEvents.size())
return -1;
bool bIsChat = isChatRoom(hContact);
VKUserID_t iUserId = ReadVKUserID(hContact);
if (iUserId == VK_INVALID_USER || iUserId == VK_FEED_USER) {
ProtoBroadcastAsync(hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, 0);
return 0;
}
CMStringA szBody;
int StickerId = 0;
ptrA pszRetMsg(GetStickerId(szMsg, StickerId));
if (StickerId) {
SendMsg(hContact, 0, CMStringA(FORMAT, "[sticker:%d]", StickerId));
szBody = pszRetMsg;
}
else
szBody = szMsg;
CMStringA szIds;
CMStringW wszForwardMessagesTxt;
int iForwardVKMessageCount = 0;
for (auto &mEvnt : vForvardEvents) {
if (iForwardVKMessageCount == VK_MAX_FORWARD_MESSAGES)
break;
iForwardVKMessageCount++;
DB::EventInfo dbei(mEvnt);
if (!dbei || dbei.eventType != EVENTTYPE_MESSAGE)
continue;
if (!Proto_IsProtoOnContact(dbei.hContact, m_szModuleName)) {
CMStringW wszContactName = (dbei.flags & DBEF_SENT) ? getWStringA(0, "Nick", TranslateT("Me")) : Clist_GetContactDisplayName(dbei.hContact);
wchar_t ttime[64];
time_t tTimestamp(dbei.timestamp);
_locale_t locale = _create_locale(LC_ALL, "");
_wcsftime_l(ttime, _countof(ttime), TranslateT("%x at %X"), localtime(&tTimestamp), locale);
_free_locale(locale);
wchar_t tcSplit = m_vkOptions.bSplitFormatFwdMsg ? '\n' : ' ';
wszForwardMessagesTxt.AppendFormat(L"%s %s%c%s %s:\n\n%s\n\n",
TranslateT("Message from"),
wszContactName.c_str(),
tcSplit,
TranslateT("at"),
ttime,
dbei.pBlob ? ptrW(mir_utf8decodeW((char*)dbei.pBlob)) : L""
);
} else if (mir_strlen(dbei.szId) > 0) {
if (!szIds.IsEmpty())
szIds.AppendChar(',');
szIds += dbei.szId;
}
}
ULONG uMsgId = ::InterlockedIncrement(&m_iMsgId);
AsyncHttpRequest* pReq = new AsyncHttpRequest(
this,
REQUEST_POST,
"/method/messages.send.json",
true,
&CVkProto::OnSendMessage,
AsyncHttpRequest::rpHigh
);
pReq
<< INT_PARAM(bIsChat ? "chat_id" : "peer_id", iUserId)
<< INT_PARAM("random_id", ((long)time(0)) * 100 + uMsgId % 100)
<< CHAR_PARAM("forward_messages", szIds);
pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded");
szBody += T2Utf(wszForwardMessagesTxt);
if (!IsEmpty(szBody)) {
pReq << CHAR_PARAM("message", szBody);
if (m_vkOptions.bSendVKLinksAsAttachments) {
CMStringA szAttachments = GetAttachmentsFromMessage(szBody);
if (!szAttachments.IsEmpty()) {
debugLogA("CVkProto::ForwardMsg Attachments = %s", szAttachments.c_str());
pReq << CHAR_PARAM("attachment", szAttachments);
}
}
}
pReq->pUserInfo = new CVkSendMsgParam(hContact, uMsgId);
Push(pReq);
if (!m_vkOptions.bServerDelivery)
ProtoBroadcastAsync(hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)uMsgId);
if (vForvardEvents.size() > VK_MAX_FORWARD_MESSAGES) {
std::vector vNextForvardEvents(vForvardEvents.begin() + VK_MAX_FORWARD_MESSAGES, vForvardEvents.end());
ForwardMsg(hContact, vNextForvardEvents, "");
}
if (m_iStatus == ID_STATUS_INVISIBLE)
Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/account.setOffline.json", true, &CVkProto::OnReceiveSmth));
return uMsgId;
}
int CVkProto::SendMsg(MCONTACT hContact, MEVENT hReplyEvent, const char *szMsg)
{
debugLogA("CVkProto::SendMsg hReplyEvent = %d", hReplyEvent);
if (!IsOnline())
return -1;
bool bIsChat = isChatRoom(hContact);
VKUserID_t iUserId = ReadVKUserID(hContact);
if (iUserId == VK_INVALID_USER || iUserId == VK_FEED_USER) {
ProtoBroadcastAsync(hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, 0);
return 0;
}
int StickerId = 0;
ptrA pszRetMsg(GetStickerId(szMsg, StickerId));
ULONG uMsgId = ::InterlockedIncrement(&m_iMsgId);
AsyncHttpRequest *pReq = new AsyncHttpRequest(
this,
REQUEST_POST,
"/method/messages.send.json",
true,
&CVkProto::OnSendMessage,
AsyncHttpRequest::rpHigh
);
pReq
<< INT_PARAM(bIsChat ? "chat_id" : "peer_id", iUserId)
<< INT_PARAM("random_id", ((long)time(0)) * 100 + uMsgId % 100);
pReq->AddHeader("Content-Type", "application/x-www-form-urlencoded");
if (hReplyEvent) {
DB::EventInfo dbei(hReplyEvent, false);
if (dbei && mir_strlen(dbei.szId) > 0)
pReq << CHAR_PARAM("reply_to", dbei.szId);
}
if (StickerId)
pReq << INT_PARAM("sticker_id", StickerId);
else {
pReq << CHAR_PARAM("message", szMsg);
if (m_vkOptions.bSendVKLinksAsAttachments) {
CMStringA szAttachments = GetAttachmentsFromMessage(szMsg);
if (!szAttachments.IsEmpty()) {
debugLogA("CVkProto::SendMsg Attachments = %s", szAttachments.c_str());
pReq << CHAR_PARAM("attachment", szAttachments);
}
}
}
pReq->pUserInfo = new CVkSendMsgParam(hContact, uMsgId);
Push(pReq);
if (!m_vkOptions.bServerDelivery)
ProtoBroadcastAsync(hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)uMsgId);
if (!IsEmpty(pszRetMsg))
SendMsg(hContact, 0, pszRetMsg);
else if (m_iStatus == ID_STATUS_INVISIBLE)
Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/account.setOffline.json", true, &CVkProto::OnReceiveSmth));
return uMsgId;
}
void CVkProto::OnSendMessage(MHttpResponse *reply, AsyncHttpRequest *pReq)
{
int iResult = ACKRESULT_FAILED;
if (pReq->pUserInfo == nullptr) {
debugLogA("CVkProto::OnSendMessage failed! (pUserInfo == nullptr)");
return;
}
CVkSendMsgParam *param = (CVkSendMsgParam *)pReq->pUserInfo;
debugLogA("CVkProto::OnSendMessage %d", reply->resultCode);
VKMessageID_t iMessageId = 0;
if (reply->resultCode == 200) {
JSONNode jnRoot;
const JSONNode &jnResponse = CheckJsonResponse(pReq, reply, jnRoot);
if (jnResponse) {
iMessageId = jnResponse["message_id"].as_int();
debugLogA("CVkProto::OnSendMessage jnResponse %d", iMessageId ? iMessageId : jnResponse.as_int());
if (!iMessageId)
switch (jnResponse.type()) {
case JSON_NUMBER:
iMessageId = jnResponse.as_int();
break;
case JSON_STRING:
if (swscanf(jnResponse.as_mstring(), L"%u", &iMessageId) != 1)
iMessageId = 0;
break;
case JSON_ARRAY:
iMessageId = jnResponse.as_array()[json_index_t(0)].as_int();
break;
default:
iMessageId = 0;
}
if (iMessageId > ReadQSWord(param->hContact, "lastmsgid"))
WriteQSWord(param->hContact, "lastmsgid", iMessageId);
if (m_vkOptions.iMarkMessageReadOn >= MarkMsgReadOn::markOnReply)
MarkMessagesRead(param->hContact);
iResult = ACKRESULT_SUCCESS;
}
}
char szMid[40];
_ltoa(iMessageId, szMid, 10);
if (param->pFUP) {
ProtoBroadcastAck(param->hContact, ACKTYPE_FILE, iResult, (HANDLE)(param->pFUP));
if (!pReq->bNeedsRestart || m_bTerminated)
delete param->pFUP;
}
else if (m_vkOptions.bServerDelivery)
ProtoBroadcastAck(param->hContact, ACKTYPE_MESSAGE, iResult, (HANDLE)(param->iMsgID), (LPARAM)szMid);
MEVENT hDbEvent;
if ((iResult == ACKRESULT_SUCCESS) && (hDbEvent = db_event_getById(m_szModuleName, szMid)))
db_event_delivered(param->hContact, hDbEvent);
if (!pReq->bNeedsRestart || m_bTerminated) {
delete param;
pReq->pUserInfo = nullptr;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
void CVkProto::OnMarkRead(MCONTACT hContact, MEVENT)
{
debugLogA("CVkProto::OnDbEventRead");
if (m_vkOptions.iMarkMessageReadOn == MarkMsgReadOn::markOnRead)
MarkMessagesRead(hContact);
}
INT_PTR CVkProto::SvcMarkMessagesAsRead(WPARAM hContact, LPARAM)
{
MarkDialogAsRead(hContact);
MarkMessagesRead(hContact);
return 0;
}
void CVkProto::MarkMessagesRead(const MCONTACT hContact)
{
debugLogA("CVkProto::MarkMessagesRead (hContact)");
if (!IsOnline() || !hContact)
return;
if (!IsEmpty(ptrW(db_get_wsa(hContact, m_szModuleName, "Deactivated"))))
return;
VKUserID_t iUserId = ReadVKUserID(hContact);
if (iUserId == VK_INVALID_USER || iUserId == VK_FEED_USER)
return;
Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/messages.markAsRead.json", true, &CVkProto::OnReceiveSmth, AsyncHttpRequest::rpLow)
<< INT_PARAM("mark_conversation_as_read", 1)
<< INT_PARAM("peer_id", isChatRoom(hContact) ? VK_CHAT_MIN + iUserId : iUserId));
}
void CVkProto::RetrieveMessagesByIds(const CMStringA &szMids)
{
debugLogA("CVkProto::RetrieveMessagesByIds");
if (!IsOnline() || szMids.IsEmpty())
return;
Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/execute.RetrieveMessagesConversationByIds", true, &CVkProto::OnReceiveMessages, AsyncHttpRequest::rpHigh)
<< CHAR_PARAM("mids", szMids)
);
}
void CVkProto::RetrieveUnreadMessages()
{
debugLogA("CVkProto::RetrieveUnreadMessages");
if (!IsOnline())
return;
Push(new AsyncHttpRequest(this, REQUEST_GET, "/method/execute.RetrieveUnreadConversations", true, &CVkProto::OnReceiveDlgs, AsyncHttpRequest::rpHigh));
}
void CVkProto::OnReceiveMessages(MHttpResponse *reply, AsyncHttpRequest *pReq)
{
debugLogA("CVkProto::OnReceiveMessages %d", reply->resultCode);
if (reply->resultCode != 200)
return;
JSONNode jnRoot;
const JSONNode &jnResponse = CheckJsonResponse(pReq, reply, jnRoot);
if (!jnResponse)
return;
if (!jnResponse["Msgs"])
return;
CMStringA szMids;
int iNumMessages = jnResponse["Msgs"]["count"].as_int();
const JSONNode &jnMsgs = jnResponse["Msgs"]["items"];
const JSONNode &jnFUsers = jnResponse["fwd_users"];
debugLogA("CVkProto::OnReceiveMessages numMessages = %d", iNumMessages);
if (jnResponse["conv"]) {
const JSONNode& jnConversation = jnResponse["conv"]["items"];
for (auto& jnItem : jnConversation) {
const JSONNode& jnPeer = jnItem["peer"];
if (!jnPeer)
break;
CMStringW wszPeerType(jnPeer["type"].as_mstring());
VKUserID_t iUserId = jnPeer["id"].as_int();
MCONTACT hContact = (wszPeerType == L"chat") ? FindChat(iUserId % VK_CHAT_MIN) : FindUser(iUserId, true);
WriteQSWord(hContact, "in_read", jnItem["in_read"].as_int());
WriteQSWord(hContact, "out_read", jnItem["out_read"].as_int());
if (m_vkOptions.iMarkMessageReadOn == MarkMsgReadOn::markOnReceive)
MarkMessagesRead(hContact);
const JSONNode& jnCanWrite = jnItem["can_write"];
if (jnCanWrite)
Contact::Readonly(hContact, !jnCanWrite["allowed"].as_bool());
}
}
for (auto& jnMsg : jnMsgs) {
if (!jnMsg) {
debugLogA("CVkProto::OnReceiveMessages pMsg == nullptr");
break;
}
VKMessageID_t iMessageId = jnMsg["id"].as_int();
CMStringW wszBody(jnMsg["text"].as_mstring());
time_t tDateTime = jnMsg["date"].as_int();
int isOut = jnMsg["out"].as_int();
VKUserID_t iUserId = jnMsg["peer_id"].as_int();
MCONTACT hContact = 0;
VKUserID_t iChatId = (GetVKPeerType(iUserId) == VKPeerType::vkPeerMUC) ? iUserId % VK_CHAT_MIN : 0;
if (iChatId == 0)
hContact = FindUser(iUserId, true);
char szMid[40], szReplyId[40] = "";
_ltoa(iMessageId, szMid, 10);
bool bUseServerReadFlag = m_vkOptions.bSyncReadMessageStatusFromServer ? true : !m_vkOptions.bMesAsUnread;
if (iChatId != 0) {
debugLogA("CVkProto::OnReceiveMessages chat_id != 0");
CMStringW wszActionChat = jnMsg["action"]["type"].as_mstring();
VKMessageID_t iActionMessageId = _wtol(jnMsg["action"]["member_id"].as_mstring());
if ((wszActionChat == L"chat_kick_user") && (iActionMessageId == m_iMyUserId))
KickFromChat(iChatId, iUserId, jnMsg, jnFUsers);
else {
MCONTACT chatContact = FindChat(iChatId);
if (chatContact && getBool(chatContact, "kicked", true))
db_unset(chatContact, m_szModuleName, "kicked");
AppendChatConversationMessage(iChatId, jnMsg, jnFUsers, false);
}
continue;
}
const JSONNode& jnFwdMessages = jnMsg["fwd_messages"];
if (jnFwdMessages && !jnFwdMessages.empty()) {
CMStringW wszFwdMessages = GetFwdMessages(jnFwdMessages, jnFUsers, m_vkOptions.BBCForAttachments());
if (!wszBody.IsEmpty())
wszFwdMessages = L"\n" + wszFwdMessages;
wszBody += wszFwdMessages;
}
const JSONNode& jnReplyMessages = jnMsg["reply_message"];
if (jnReplyMessages && !jnReplyMessages.empty())
if (m_vkOptions.bShowReplyInMessage) {
CMStringW wszReplyMessages = GetFwdMessages(jnReplyMessages, jnFUsers, m_vkOptions.BBCForAttachments());
if (!wszBody.IsEmpty())
wszReplyMessages = L"\n" + wszReplyMessages;
wszBody += wszReplyMessages;
}
else if (jnReplyMessages["id"])
_ltoa(jnReplyMessages["id"].as_int(), szReplyId, 10);
CMStringW wszBodyNoAttachments = wszBody;
CMStringW wszAttachmentDescr;
const JSONNode& jnAttachments = jnMsg["attachments"];
if (jnAttachments && !jnAttachments.empty()) {
wszAttachmentDescr = GetAttachmentDescr(jnAttachments, m_vkOptions.BBCForAttachments(), hContact, iMessageId);
if (wszAttachmentDescr == L"== FilterAudioMessages ==") {
if (hContact && (iMessageId > ReadQSWord(hContact, "lastmsgid", -1)))
WriteQSWord(hContact, "lastmsgid", iMessageId);
continue;
}
if (!wszBody.IsEmpty())
wszBody += L"\n";
wszBody += wszAttachmentDescr;
}
if (m_vkOptions.bAddMessageLinkToMesWAtt && ((jnAttachments && !jnAttachments.empty()) || (jnFwdMessages && !jnFwdMessages.empty()) || (jnReplyMessages && !jnReplyMessages.empty() && m_vkOptions.bShowReplyInMessage)))
wszBody += SetBBCString(TranslateT("Message link"), m_vkOptions.BBCForAttachments(), vkbbcUrl,
CMStringW(FORMAT, L"https://vk.com/im?sel=%d&msgid=%d", iUserId, iMessageId));
VKMessageID_t iReadMsg = ReadQSWord(hContact, "in_read", 0);
bool bIsRead = (iMessageId <= iReadMsg);
time_t tUpdateTime = (time_t)jnMsg["update_time"].as_int();
bool bEdited = (tUpdateTime != 0);
if (bEdited) {
wchar_t ttime[64];
_locale_t locale = _create_locale(LC_ALL, "");
_wcsftime_l(ttime, _countof(ttime), TranslateT("%x at %X"), localtime(&tUpdateTime), locale);
_free_locale(locale);
wszBody = SetBBCString(
CMStringW(FORMAT, TranslateT("Edited message (updated %s):\n"), ttime),
m_vkOptions.BBCForAttachments(), vkbbcB) +
wszBody;
if (m_vkOptions.bShowBeforeEditedPostVersion) {
CMStringW wszOldMsg;
if (GetMessageFromDb(iMessageId, tDateTime, wszOldMsg))
wszBody += SetBBCString(TranslateT("\nOriginal message:\n"), m_vkOptions.BBCForAttachments(), vkbbcB) +
wszOldMsg;
}
}
DB::EventInfo dbei;
if (bIsRead && bUseServerReadFlag)
dbei.flags |= DBEF_READ;
if (isOut)
dbei.flags |= DBEF_SENT;
else if (m_vkOptions.bUserForceInvisibleOnActivity && ((time(0) - tDateTime) < (60 * m_vkOptions.iInvisibleInterval)))
SetInvisible(hContact);
T2Utf pszBody(wszBody);
dbei.timestamp = bEdited ? tDateTime : (m_vkOptions.bUseLocalTime ? time(0) : tDateTime);
dbei.pBlob = pszBody;
if (!m_vkOptions.bShowReplyInMessage && szReplyId) {
dbei.szReplyId = szReplyId;
debugLogA("CVkProto::OnReceiveMessages szReplyId = %s", szReplyId);
}
debugLogA("CVkProto::OnReceiveMessages mid = %d, datetime = %d, isOut = %d, isRead = %d, iUserId = %d, Edited = %d", iMessageId, tDateTime, isOut, (int)bIsRead, iUserId, (int)bEdited);
if (!IsMessageExist(iMessageId, vkALL) || bEdited || szReplyId) {
debugLogA("CVkProto::OnReceiveMessages new or edited message");
dbei.szId = szMid;
ProtoChainRecvMsg(hContact, dbei);
if (iMessageId > ReadQSWord(hContact, "lastmsgid", -1))
WriteQSWord(hContact, "lastmsgid", iMessageId);
}
else if (m_vkOptions.bLoadSentAttachments && !wszAttachmentDescr.IsEmpty()) {
CMStringW wszOldMsg;
if (GetMessageFromDb(iMessageId, tDateTime, wszOldMsg) && (wszOldMsg == wszBody))
continue;
if (wszBodyNoAttachments != wszOldMsg)
continue;
debugLogA("CVkProto::OnReceiveMessages add attachments");
T2Utf pszAttach(wszAttachmentDescr);
dbei.timestamp = isOut ? time(0) : tDateTime;
dbei.pBlob = pszAttach;
dbei.szId = strcat(szMid, "_");
ProtoChainRecvMsg(hContact, dbei);
}
}
}
void CVkProto::OnReceiveDlgs(MHttpResponse *reply, AsyncHttpRequest *pReq)
{
debugLogA("CVkProto::OnReceiveDlgs %d", reply->resultCode);
if (reply->resultCode != 200)
return;
JSONNode jnRoot;
const JSONNode &jnResponse = CheckJsonResponse(pReq, reply, jnRoot);
if (!jnResponse)
return;
const JSONNode &jnDialogs = jnResponse["dialogs"];
if (!jnDialogs)
return;
const JSONNode &jnDlgs = jnDialogs["items"];
if (!jnDlgs)
return;
OBJLIST<VKUserID_t> lufUsers(20, NumericKeySortT);
const JSONNode &jnUsers = jnResponse["users"];
if (jnUsers)
for (auto &it : jnUsers) {
VKUserID_t iUserId = it["user_id"].as_int();
int iStatus = it["friend_status"].as_int();
// iStatus == 3 - user is friend
// iUserId < 0 - user is group
if (GetVKPeerType(iUserId) != VKPeerType::vkPeerUser || iStatus != 3 || lufUsers.find((VKUserID_t *) &iUserId))
continue;
lufUsers.insert(new VKUserID_t(iUserId));
}
const JSONNode &jnGroups = jnResponse["groups"];
if (jnGroups)
for (auto &it : jnGroups) {
VKUserID_t iUserId = it.as_int();
if (lufUsers.find((VKUserID_t*) &iUserId))
continue;
lufUsers.insert(new VKUserID_t(iUserId));
}
CMStringA szGroupIds;
for (auto& it : jnDlgs) {
if (!it)
break;
const JSONNode& jnConversation = it["conversation"];
const JSONNode& jnLastMessage = it["last_message"];
if (!jnConversation)
break;
int iUnreadCount = jnConversation["unread_count"].as_int();
const JSONNode& jnPeer = jnConversation["peer"];
if (!jnPeer)
break;
VKUserID_t iUserId = 0;
MCONTACT hContact(0);
CMStringW wszPeerType(jnPeer["type"].as_mstring());
if (wszPeerType == L"user" || wszPeerType == L"group") {
iUserId = jnPeer["id"].as_int();
VKUserID_t *pUserID = lufUsers.find((VKUserID_t*) &iUserId);
debugLogA("CVkProto::OnReceiveDlgs UserId = %d, iIndex = %p, numUnread = %d", iUserId, pUserID, iUnreadCount);
if (m_vkOptions.bLoadOnlyFriends && iUnreadCount == 0 && !pUserID)
continue;
hContact = FindUser(iUserId, true);
debugLogA("CVkProto::OnReceiveDlgs add UserId = %d", iUserId);
if (IsGroupUser(hContact))
szGroupIds.AppendFormat(szGroupIds.IsEmpty() ? "%d" : ",%d", -1 * iUserId);
WriteQSWord(hContact, "in_read", jnConversation["in_read"].as_int());
WriteQSWord(hContact, "out_read", jnConversation["out_read"].as_int());
}
if (wszPeerType == L"chat") {
VKUserID_t iChatId = jnPeer["local_id"].as_int();
debugLogA("CVkProto::OnReceiveDlgs chatid = %d", iChatId);
if (m_chats.find((CVkChatInfo*)&iChatId) == nullptr)
AppendConversationChat(iChatId, it);
hContact = FindChat(iChatId);
}
if (g_bMessageState) {
bool bIsOut = jnLastMessage["out"].as_bool();
bool bIsRead = (jnLastMessage["id"].as_int() <= jnConversation["in_read"].as_int());
if (bIsRead && bIsOut)
CallService(MS_MESSAGESTATE_UPDATE, hContact, MRD_TYPE_DELIVERED);
}
if (m_vkOptions.iSyncHistoryMetod) {
VKMessageID_t iMessageId = jnLastMessage["id"].as_int();
m_bNotifyForEndLoadingHistory = false;
if (ReadQSWord(hContact, "lastmsgid", -1) == -1 && iUnreadCount && !getBool(hContact, "ActiveHistoryTask")) {
setByte(hContact, "ActiveHistoryTask", 1);
GetServerHistory(hContact, 0, iUnreadCount, 0, 0, true);
}
else
GetHistoryDlg(hContact, iMessageId);
if (m_vkOptions.iMarkMessageReadOn == MarkMsgReadOn::markOnReceive && iUnreadCount)
MarkMessagesRead(hContact);
}
else if (iUnreadCount && !getBool(hContact, "ActiveHistoryTask")) {
m_bNotifyForEndLoadingHistory = false;
setByte(hContact, "ActiveHistoryTask", 1);
GetServerHistory(hContact, 0, iUnreadCount, 0, 0, true);
if (m_vkOptions.iMarkMessageReadOn == MarkMsgReadOn::markOnReceive)
MarkMessagesRead(hContact);
}
if (jnConversation["can_write"] && jnConversation["can_write"]["allowed"])
Contact::Readonly(hContact, !jnConversation["can_write"]["allowed"].as_bool());
}
lufUsers.destroy();
RetrieveUsersInfo();
RetrieveGroupInfo(szGroupIds);
}
|