diff options
-rw-r--r-- | protocols/Discord/src/dispatch.cpp | 20 | ||||
-rw-r--r-- | protocols/Discord/src/proto.h | 2 | ||||
-rw-r--r-- | protocols/Discord/src/server.cpp | 6 |
3 files changed, 24 insertions, 4 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index 3afa2f7a1c..6662163301 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -274,6 +274,17 @@ static void __stdcall sttStartTimer(void *param) SetTimer(g_hwndHeartbeat, (UINT_PTR)param, ppro->getHeartbeatInterval(), &CDiscordProto::HeartbeatTimerProc); } +static SnowFlake sttGetLastRead(const JSONNode &reads, const wchar_t *wszChannelId) +{ + for (auto it = reads.begin(); it != reads.end(); ++it) { + const JSONNode &p = *it; + + if (p["id"].as_mstring() == wszChannelId) + return _wtoi64(p["last_message_id"].as_mstring()); + } + return 0; +} + void CDiscordProto::OnCommandReady(const JSONNode &pRoot) { GatewaySendHeartbeat(); @@ -281,6 +292,8 @@ void CDiscordProto::OnCommandReady(const JSONNode &pRoot) m_szGatewaySessionId = pRoot["session_id"].as_mstring(); + const JSONNode &readState = pRoot["read_state"]; + const JSONNode &guilds = pRoot["guilds"]; for (auto it = guilds.begin(); it != guilds.end(); ++it) { const JSONNode &p = *it; @@ -320,6 +333,7 @@ void CDiscordProto::OnCommandReady(const JSONNode &pRoot) pUser->wszUsername = wszChannelId; pUser->guildId = guildId; pUser->lastMessageId = _wtoi64(pch["last_message_id"].as_mstring()); + pUser->lastReadId = sttGetLastRead(readState, wszChannelId); setId(pUser->hContact, DB_KEY_CHANNELID, channelId); } @@ -344,9 +358,11 @@ void CDiscordProto::OnCommandReady(const JSONNode &pRoot) if (pUser == NULL) continue; - - pUser->channelId = _wtoi64(p["id"].as_mstring()); + + CMStringW wszChannelId = p["id"].as_mstring(); + pUser->channelId = _wtoi64(wszChannelId); pUser->lastMessageId = _wtoi64(p["last_message_id"].as_mstring()); + pUser->lastReadId = sttGetLastRead(readState, wszChannelId); pUser->bIsPrivate = true; setId(pUser->hContact, DB_KEY_CHANNELID, pUser->channelId); diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index f0b76a2e27..96428bf2dd 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -97,7 +97,7 @@ struct CDiscordUser : public MZeroedObject SnowFlake guildId; SnowFlake channelId; - SnowFlake lastMessageId; + SnowFlake lastMessageId, lastReadId; bool bIsPrivate; CMStringW wszUsername; diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index a818622d67..b87bc4eaf8 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -60,7 +60,7 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest DBEVENTINFO dbei = {}; dbei.szModule = m_szModuleName; - dbei.flags = DBEF_READ | DBEF_UTF; + dbei.flags = DBEF_UTF; dbei.eventType = EVENTTYPE_MESSAGE; SnowFlake lastId = getId(pUser->hContact, DB_KEY_LASTMSGID); // as stored in a database @@ -76,6 +76,10 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest dbei.flags &= ~DBEF_SENT; SnowFlake msgid = _wtoi64(p["id"].as_mstring()); + if (msgid <= pUser->lastReadId) + dbei.flags |= DBEF_READ; + else + dbei.flags &= ~DBEF_READ; CMStringA szBody(ptrA(mir_utf8encodeW(p["content"].as_mstring()))); szBody.AppendFormat("%c%lld", 0, msgid); |