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
|
/*
Facebook plugin for Miranda NG
Copyright © 2019-20 Miranda NG team
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, either version 2 of the License, or
(at your option) any later version.
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"
void FacebookProto::ConnectionFailed()
{
ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_FAILED, (HANDLE)m_iStatus, m_iDesiredStatus);
m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE;
ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, m_iDesiredStatus);
OnShutdown();
}
void FacebookProto::OnLoggedIn()
{
m_mid = 0;
MqttPublish("/foreground_state", "{\"foreground\":true, \"keepalive_timeout\":60}");
MqttSubscribe("/inbox", "/mercury", "/messaging_events", "/orca_presence", "/orca_typing_notifications", "/pp", "/t_ms", "/t_p", "/t_rtc", "/webrtc", "/webrtc_response", 0);
MqttUnsubscribe("/orca_message_notifications", 0);
// if sequence is not initialized, request SID from the server
if (m_sid == 0) {
auto *pReq = CreateRequestGQL(FB_API_QUERY_SEQ_ID);
pReq << CHAR_PARAM("query_params", "{\"1\":\"0\"}");
pReq->CalcSig();
JsonReply reply(ExecuteRequest(pReq));
if (reply.error()) {
ConnectionFailed();
return;
}
auto &n = reply.data()["viewer"]["message_threads"];
CMStringW wszSid(n["sync_sequence_id"].as_mstring());
setWString(DBKEY_SID, wszSid);
m_sid = _wtoi64(wszSid);
m_iUnread = n["unread_count"].as_int();
}
// point of no return;
ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, m_iDesiredStatus);
m_iStatus = m_iDesiredStatus;
m_bOnline = true;
m_impl.m_heartBeat.Start(60000);
// connect message queue
JSONNode query;
query << INT_PARAM("delta_batch_size", 125) << INT_PARAM("max_deltas_able_to_process", 1000) << INT_PARAM("sync_api_version", 3) << CHAR_PARAM("encoding", "JSON");
if (m_szSyncToken.IsEmpty()) {
JSONNode hashes; hashes.set_name("graphql_query_hashes"); hashes << CHAR_PARAM("xma_query_id", __STRINGIFY(FB_API_QUERY_XMA));
JSONNode xma; xma.set_name(__STRINGIFY(FB_API_QUERY_XMA)); xma << CHAR_PARAM("xma_id", "<ID>");
JSONNode hql; hql.set_name("graphql_query_params"); hql << xma;
JSONNode params; params.set_name("queue_params");
params << CHAR_PARAM("buzz_on_deltas_enabled", "false") << hashes << hql;
query << INT64_PARAM("initial_titan_sequence_id", m_sid) << CHAR_PARAM("device_id", m_szDeviceID) << INT64_PARAM("entity_fbid", m_uid) << params;
MqttPublish("/messenger_sync_create_queue", query.write().c_str());
}
else {
query << INT64_PARAM("last_seq_id", m_sid) << CHAR_PARAM("sync_token", m_szSyncToken);
MqttPublish("/messenger_sync_get_diffs", query.write().c_str());
}
}
void FacebookProto::OnLoggedOut()
{
m_impl.m_heartBeat.Stop();
m_bOnline = false;
ProtoBroadcastAck(0, ACKTYPE_STATUS, ACKRESULT_SUCCESS, (HANDLE)m_iStatus, ID_STATUS_OFFLINE);
m_iStatus = m_iDesiredStatus = ID_STATUS_OFFLINE;
setAllContactStatuses(ID_STATUS_OFFLINE);
}
int FacebookProto::RefreshContacts()
{
auto *pReq = CreateRequestGQL(FB_API_QUERY_CONTACTS);
pReq->flags |= NLHRF_NODUMPSEND;
pReq << CHAR_PARAM("query_params", "{\"0\":[\"user\"],\"1\":\"" FB_API_CONTACTS_COUNT "\"}");
pReq->CalcSig();
JsonReply reply(ExecuteRequest(pReq));
if (int iErrorCode = reply.error())
return iErrorCode; // unknown error
bool bNeedUpdate = false;
for (auto &it : reply.data()["viewer"]["messenger_contacts"]["nodes"]) {
auto &n = it["represented_profile"];
CMStringW wszId(n["id"].as_mstring());
__int64 id = _wtoi64(wszId);
MCONTACT hContact;
if (id != m_uid) {
auto *pUser = FindUser(id);
if (pUser == nullptr) {
hContact = db_add_contact();
Proto_AddToContact(hContact, m_szModuleName);
setWString(hContact, DBKEY_ID, wszId);
Clist_SetGroup(hContact, m_wszDefaultGroup);
m_users.insert(new FacebookUser(id, hContact));
}
else hContact = pUser->hContact;
}
else hContact = 0;
if (auto &nName = it["structured_name"]) {
CMStringW wszName(nName["text"].as_mstring());
setWString(hContact, DBKEY_NICK, wszName);
for (auto &nn : nName["parts"]) {
CMStringW wszPart(nn["part"].as_mstring());
int offset = nn["offset"].as_int(), length = nn["length"].as_int();
if (wszPart == L"first")
setWString(hContact, DBKEY_FIRST_NAME, wszName.Mid(offset, length));
else if (wszPart == L"last")
setWString(hContact, DBKEY_LAST_NAME, wszName.Mid(offset, length));
}
}
if (auto &nBirth = n["birthdate"]) {
setDword(hContact, "BirthDay", nBirth["day"].as_int());
setDword(hContact, "BirthMonth", nBirth["month"].as_int());
}
if (auto &nCity = n["current_city"])
setWString(hContact, "City", nCity["name"].as_mstring());
if (auto &nAva = it[(m_bUseBigAvatars) ? "hugePictureUrl" : "bigPictureUrl"]) {
CMStringW wszOldUrl(getMStringW(hContact, DBKEY_AVATAR)), wszNewUrl(nAva["uri"].as_mstring());
if (wszOldUrl != wszNewUrl) {
bNeedUpdate = true;
setByte(hContact, "UpdateNeeded", 1);
setWString(hContact, DBKEY_AVATAR, wszNewUrl);
}
}
}
if (bNeedUpdate)
ForkThread(&FacebookProto::AvatarsUpdate);
return 0;
}
bool FacebookProto::RefreshToken()
{
auto *pReq = CreateRequest(FB_API_URL_AUTH, "authenticate", "auth.login");
pReq->flags |= NLHRF_NODUMP;
pReq << CHAR_PARAM("email", getMStringA(DBKEY_LOGIN));
pReq << CHAR_PARAM("password", getMStringA(DBKEY_PASS));
pReq->CalcSig();
JsonReply reply(ExecuteRequest(pReq));
if (reply.error())
return false;
m_szAuthToken = reply.data()["access_token"].as_mstring();
setString(DBKEY_TOKEN, m_szAuthToken);
m_uid = reply.data()["uid"].as_int();
CMStringA m_szUid = reply.data()["uid"].as_mstring();
setString(DBKEY_ID, m_szUid);
return true;
}
/////////////////////////////////////////////////////////////////////////////////////////
void FacebookProto::ServerThread(void *)
{
LBL_Begin:
m_szAuthToken = getMStringA(DBKEY_TOKEN);
if (m_szAuthToken.IsEmpty()) {
if (!RefreshToken()) {
ConnectionFailed();
return;
}
}
int iErrorCode = RefreshContacts();
if (iErrorCode != 0) {
if (iErrorCode == 401) {
delSetting(DBKEY_TOKEN);
goto LBL_Begin;
}
ConnectionFailed();
return;
}
// connect to MQTT server
NETLIBOPENCONNECTION nloc = {};
nloc.szHost = "mqtt.facebook.com";
nloc.wPort = 443;
nloc.flags = NLOCF_SSL | NLOCF_V2;
m_mqttConn = Netlib_OpenConnection(m_hNetlibUser, &nloc);
if (m_mqttConn == nullptr) {
debugLogA("connection failed, exiting");
ConnectionFailed();
return;
}
// send initial packet
MqttLogin();
while (!Miranda_IsTerminated()) {
NETLIBSELECT nls = {};
nls.hReadConns[0] = m_mqttConn;
nls.dwTimeout = 1000;
int ret = Netlib_Select(&nls);
if (ret == SOCKET_ERROR) {
debugLogA("Netlib_Recv() failed, error=%d", WSAGetLastError());
break;
}
// no data, continue waiting
if (ret == 0)
continue;
MqttMessage msg;
if (!MqttRead(msg)) {
debugLogA("MqttRead() failed");
break;
}
if (!MqttParse(msg)) {
debugLogA("MqttParse() failed");
break;
}
}
debugLogA("exiting ServerThread");
Netlib_CloseHandle(m_mqttConn);
m_mqttConn = nullptr;
OnLoggedOut();
}
/////////////////////////////////////////////////////////////////////////////////////////
void FacebookProto::OnPublish(const char *topic, const uint8_t *p, size_t cbLen)
{
FbThriftReader rdr;
// that might be a zipped buffer
if (cbLen >= 2) {
if ((((p[0] << 8) | p[1]) % 31) == 0 && (p[0] & 0x0F) == 8) { // zip header ok
size_t dataSize;
void *pData = doUnzip(cbLen, p, dataSize);
if (pData != nullptr) {
debugLogA("UNZIP: <%s>", CMStringA((const char *)pData, (int)dataSize).c_str());
rdr.reset(dataSize, pData);
mir_free(pData);
}
}
}
if (rdr.size() == 0)
rdr.reset(cbLen, (void*)p);
if (!strcmp(topic, "/t_p"))
OnPublishPresence(rdr);
else if (!strcmp(topic, "/t_ms"))
OnPublishMessage(rdr);
else if (!strcmp(topic, "/orca_typing_notifications"))
OnPublishUtn(rdr);
}
void FacebookProto::OnPublishPresence(FbThriftReader &rdr)
{
char *str;
rdr.readStr(str);
mir_free(str);
bool bVal;
uint8_t fieldType;
uint16_t fieldId;
rdr.readField(fieldType, fieldId);
assert(fieldType == FB_THRIFT_TYPE_BOOL);
assert(fieldId == 1);
rdr.readBool(bVal);
rdr.readField(fieldType, fieldId);
assert(fieldType == FB_THRIFT_TYPE_LIST);
assert(fieldId == 1);
uint32_t size;
rdr.readList(fieldType, size);
assert(fieldType == FB_THRIFT_TYPE_STRUCT);
debugLogA("Received list of presences: %d records", size);
for (uint32_t i = 0; i < size; i++) {
uint64_t userId, timestamp, voipBits;
rdr.readField(fieldType, fieldId);
assert(fieldType == FB_THRIFT_TYPE_I64);
assert(fieldId == 1);
rdr.readInt64(userId);
uint32_t u32;
rdr.readField(fieldType, fieldId);
assert(fieldType == FB_THRIFT_TYPE_I32);
assert(fieldId == 1);
rdr.readInt32(u32);
auto *pUser = FindUser(userId);
if (pUser == nullptr)
debugLogA("Skipping presence from unknown user %lld", userId);
else {
debugLogA("Presence from user %lld => %d", userId, u32);
setWord(pUser->hContact, "Status", (u32 != 0) ? ID_STATUS_ONLINE : ID_STATUS_OFFLINE);
}
rdr.readField(fieldType, fieldId);
assert(fieldType == FB_THRIFT_TYPE_I64);
assert(fieldId == 1 || fieldId == 4);
rdr.readInt64(timestamp);
while (!rdr.isStop()) {
rdr.readField(fieldType, fieldId);
assert(fieldType == FB_THRIFT_TYPE_I64 || fieldType == FB_THRIFT_TYPE_I16 || fieldType == FB_THRIFT_TYPE_I32);
rdr.readIntV(voipBits);
}
rdr.readByte(fieldType);
assert(fieldType == FB_THRIFT_TYPE_STOP);
}
rdr.readByte(fieldType);
assert(fieldType == FB_THRIFT_TYPE_STOP);
}
void FacebookProto::OnPublishUtn(FbThriftReader &rdr)
{
JSONNode root = JSONNode::parse((const char *)rdr.data());
auto *pUser = FindUser(_wtoi64(root["sender_fbid"].as_mstring()));
if (pUser != nullptr) {
int length = (root["state"].as_int() == 0) ? PROTOTYPE_CONTACTTYPING_OFF : 60;
CallService(MS_PROTO_CONTACTISTYPING, pUser->hContact, length);
}
}
/////////////////////////////////////////////////////////////////////////////////////////
struct
{
const char *messageType;
void (FacebookProto:: *pFunc)(const JSONNode &);
}
static MsgHandlers[] =
{
{ "deltaNewMessage", &FacebookProto::OnPublishPrivateMessage },
{ "deltaSentMessage", &FacebookProto::OnPublishSentMessage },
{ "deltaReadReceipt", &FacebookProto::OnPublishReadReceipt },
};
void FacebookProto::OnPublishMessage(FbThriftReader &rdr)
{
CMStringA szJson((const char *)rdr.data(), (int)rdr.size());
if (szJson[0] == 0)
szJson.Delete(0);
debugLogA("MS: <%s>", szJson.c_str());
JSONNode root = JSONNode::parse(szJson);
CMStringW str = root["lastIssuedSeqId"].as_mstring();
if (!str.IsEmpty()) {
setWString(DBKEY_SID, str);
m_sid = _wtoi64(str);
}
str = root["syncToken"].as_mstring();
if (!str.IsEmpty()) {
m_szSyncToken = str;
setString(DBKEY_SYNC_TOKEN, m_szSyncToken);
return;
}
for (auto &it : root["deltas"]) {
for (auto &handler : MsgHandlers) {
auto &json = it[handler.messageType];
if (json) {
(this->*(handler.pFunc))(json);
break;
}
}
}
}
// new message arrived
struct
{
const char *szTag, *szClientVersion;
}
static facebookClients[] =
{
{ "source:titan:web", "Facebook (website)" },
{ "app_id:256002347743983", "Facebook (Facebook Messenger)" }
};
void FacebookProto::OnPublishPrivateMessage(const JSONNode &root)
{
auto &metadata = root["messageMetadata"];
__int64 offlineId = _wtoi64(metadata["offlineThreadingId"].as_mstring());
if (!offlineId) {
debugLogA("We care about messages only, event skipped");
return;
}
__int64 otherUserFbId = _wtoi64(metadata["threadKey"]["otherUserFbId"].as_mstring());
if (!otherUserFbId) {
// TODO: handling thread/room/groupchat messages
debugLogA("We can't handle group chats at the moment, ignored");
return;
}
auto *pUser = FindUser(otherUserFbId);
if (pUser == nullptr) {
debugLogA("Message from unknown contact %lu, ignored", otherUserFbId);
return;
}
for (auto &it : metadata["tags"]) {
auto *szTagName = it.name();
for (auto &cli : facebookClients) {
if (!mir_strcmp(szTagName, cli.szTag)) {
setString(pUser->hContact, "MirVer", cli.szClientVersion);
break;
}
}
}
__int64 actorFbId = _wtoi64(metadata["actorFbId"].as_mstring());
CMStringA szId(metadata["messageId"].as_string().c_str());
CMStringA szBody(root["body"].as_string().c_str());
if (szBody.IsEmpty())
szBody = metadata["snippet"].as_mstring();
// parse stickers
CMStringA stickerId = root["stickerId"].as_mstring();
if (!stickerId.IsEmpty()) {
if (ServiceExists(MS_SMILEYADD_LOADCONTACTSMILEYS)) {
CMStringW wszPath(FORMAT, L"%s\\%S\\Stickers", VARSW(L"%miranda_avatarcache%").get(), m_szModuleName);
CreateDirectoryTreeW(wszPath);
bool bSuccess = false;
CMStringW wszFileName(FORMAT, L"%s\\STK{%S}.png", wszPath.c_str(), stickerId.c_str());
if (GetFileAttributesW(wszFileName) == INVALID_FILE_ATTRIBUTES) {
auto *pReq = CreateRequestGQL(FB_API_QUERY_STICKER);
pReq << CHAR_PARAM("query_params", CMStringA(FORMAT, "{\"0\":[\"%s\"]}", stickerId.c_str()));
pReq->CalcSig();
JsonReply reply(ExecuteRequest(pReq));
if (!reply.error()) {
for (auto &sticker : reply.data()) {
for (auto &img : sticker["thread_image"]) {
CMStringA szUrl(img.as_mstring());
NETLIBHTTPREQUEST req = {};
req.cbSize = sizeof(req);
req.flags = NLHRF_NODUMP | NLHRF_SSL | NLHRF_HTTP11 | NLHRF_REDIRECT;
req.requestType = REQUEST_GET;
req.szUrl = szUrl.GetBuffer();
NETLIBHTTPREQUEST *pReply = Netlib_HttpTransaction(m_hNetlibUser, &req);
if (pReply != nullptr && pReply->resultCode == 200 && pReply->pData && pReply->dataLength) {
bSuccess = true;
FILE *out = _wfopen(wszFileName, L"wb");
fwrite(pReply->pData, 1, pReply->dataLength, out);
fclose(out);
}
}
}
}
}
else bSuccess = true;
if (bSuccess) {
if (!szBody.IsEmpty())
szBody += "\r\n";
szBody += "STK{" + stickerId + "}";
SMADD_CONT cont;
cont.cbSize = sizeof(SMADD_CONT);
cont.hContact = pUser->hContact;
cont.type = 1;
cont.path = wszFileName.GetBuffer();
CallService(MS_SMILEYADD_LOADCONTACTSMILEYS, 0, (LPARAM)&cont);
}
else szBody += TranslateU("Sticker received");
}
else szBody += TranslateU("SmileyAdd plugin required to support stickers");
}
// parse stickers
for (auto &it : root["attachments"]) {
// madness... json inside json
CMStringA szJson(it["xmaGraphQL"].as_mstring());
if (szJson.IsEmpty()) {
__int64 fbid = _wtoi64(it["fbid"].as_mstring());
if (fbid == 0) {
debugLogA("Neither a GQL nor an inline attachment, nothing to do");
continue;
}
// inline attachment, request its description
auto *pReq = CreateRequest(FB_API_URL_ATTACH, "getAttachment", "messaging.getAttachment");
pReq << CHAR_PARAM("mid", szId) << INT64_PARAM("aid", fbid);
pReq->CalcSig();
JsonReply reply(ExecuteRequest(pReq));
if (!reply.error()) {
CMStringA uri = reply.data()["redirect_uri"].as_mstring();
CMStringA type = reply.data()["content_type"].as_mstring();
if (!uri.IsEmpty()) {
if (type.Find("image/") == 0)
szBody.Append("\r\nPicture attachment: ");
else
szBody.Append("\r\nFile attachment: ");
szBody.Append(uri);
}
}
continue;
}
JSONROOT nBody(szJson);
if (!nBody)
continue;
const JSONNode &attach = (*nBody).at((json_index_t)0)["story_attachment"];
szBody += "\r\n-----------------------------------";
CMStringA str = attach["url"].as_mstring();
if (!str.IsEmpty()) {
if (str.Left(8) == "fbrpc") {
int iStart = str.Find("target_url=");
if (iStart != 0) {
int iEnd = str.Find("&", iStart + 11);
if (iEnd == -1)
szBody.AppendFormat("\r\n\t%s: %s", TranslateU("URL"), str.c_str() + iStart);
else
szBody.AppendFormat("\r\n\t%s: %s", TranslateU("URL"), str.Mid(iStart, iEnd).c_str());
}
}
else szBody.AppendFormat("\r\n\t%s: %s", TranslateU("URL"), str.c_str());
}
str = attach["title"].as_mstring();
if (!str.IsEmpty())
szBody.AppendFormat("\r\n\t%s: %s", TranslateU("Title"), str.c_str());
str = attach["source"]["text"].as_mstring();
if (!str.IsEmpty())
szBody.AppendFormat("\r\n\t%s: %s", TranslateU("Source"), str.c_str());
str = attach["description"]["text"].as_mstring();
if (!str.IsEmpty())
szBody.AppendFormat("\r\n\t%s: %s", TranslateU("Description"), str.c_str());
str = attach["media"]["playable_url"].as_mstring();
if (!str.IsEmpty())
szBody.AppendFormat("\r\n\t%s: %s", TranslateU("Playable media"), str.c_str());
}
// store message
PROTORECVEVENT pre = {};
pre.timestamp = DWORD(_wtoi64(metadata["timestamp"].as_mstring()) / 1000);
pre.szMessage = (char *)szBody.c_str();
pre.szMsgId = (char *)szId.c_str();
if (m_uid == actorFbId)
pre.flags |= PREF_SENT;
ProtoChainRecvMsg(pUser->hContact, &pre);
}
// read notification
void FacebookProto::OnPublishReadReceipt(const JSONNode &root)
{
CMStringA wszUserId(root["threadKey"]["otherUserFbId"].as_mstring());
auto *pUser = FindUser(_atoi64(wszUserId));
if (pUser == nullptr) {
debugLogA("Message from unknown contact %s, ignored", wszUserId.c_str());
return;
}
DWORD timestamp = _wtoi64(root["watermarkTimestampMs"].as_mstring());
for (MEVENT ev = db_event_firstUnread(pUser->hContact); ev != 0; ev = db_event_next(pUser->hContact, ev)) {
DBEVENTINFO dbei = {};
if (db_event_get(ev, &dbei))
continue;
if (dbei.timestamp > timestamp)
break;
if (!dbei.markedRead())
db_event_markRead(pUser->hContact, ev);
}
}
// my own message was sent
void FacebookProto::OnPublishSentMessage(const JSONNode &root)
{
auto &metadata = root["messageMetadata"];
__int64 offlineId = _wtoi64(metadata["offlineThreadingId"].as_mstring());
std::string szId(metadata["messageId"].as_string());
CMStringA wszUserId(metadata["threadKey"]["otherUserFbId"].as_mstring());
auto *pUser = FindUser(_atoi64(wszUserId));
if (pUser == nullptr) {
debugLogA("Message from unknown contact %s, ignored", wszUserId.c_str());
return;
}
for (auto &it : arOwnMessages) {
if (it->msgId == offlineId) {
ProtoBroadcastAck(pUser->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)it->reqId, (LPARAM)szId.c_str());
arOwnMessages.remove(arOwnMessages.indexOf(&it));
break;
}
}
}
|