diff options
author | George Hazan <ghazan@miranda.im> | 2017-02-15 20:22:29 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2017-02-15 20:22:29 +0300 |
commit | ad212509efd1de495c7a4ad08504bc062bfe8e2a (patch) | |
tree | 47cbe3a37747cf2dd107878ff85082d368d7521b | |
parent | acd9edbd6413e9ff25378bd4e2cbc515d2a620f7 (diff) |
Discord: groupchat history support
version bump
-rw-r--r-- | protocols/Discord/src/dispatch.cpp | 26 | ||||
-rw-r--r-- | protocols/Discord/src/server.cpp | 72 | ||||
-rw-r--r-- | protocols/Discord/src/utils.cpp | 22 | ||||
-rw-r--r-- | protocols/Discord/src/version.h | 6 |
4 files changed, 92 insertions, 34 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index 615cb6d3de..e43b70d0fc 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -210,6 +210,9 @@ void CDiscordProto::ProcessGuild(const JSONNode &readState, const JSONNode &p) setId(si->hContact, DB_KEY_OWNERID, ownerId); BuildStatusList(guildId, wszChannelId); + Chat_Control(m_szModuleName, wszChannelId, WINDOW_HIDDEN); + Chat_Control(m_szModuleName, wszChannelId, SESSION_ONLINE); + if (!wszTopic.IsEmpty()) { Chat_SetStatusbarText(m_szModuleName, wszChannelId, wszTopic); @@ -226,6 +229,7 @@ void CDiscordProto::ProcessGuild(const JSONNode &readState, const JSONNode &p) pUser = new CDiscordUser(channelId); pUser->bIsPrivate = false; pUser->hContact = si->hContact; + pUser->id = channelId; pUser->channelId = channelId; arUsers.insert(pUser); } @@ -234,7 +238,12 @@ void CDiscordProto::ProcessGuild(const JSONNode &readState, const JSONNode &p) pUser->lastMessageId = ::getId(pch["last_message_id"]); pUser->lastReadId = sttGetLastRead(readState, wszChannelId); + setId(pUser->hContact, DB_KEY_ID, channelId); setId(pUser->hContact, DB_KEY_CHANNELID, channelId); + + SnowFlake oldMsgId = getId(pUser->hContact, DB_KEY_LASTMSGID); + if (oldMsgId != 0 && pUser->lastMessageId > oldMsgId) + RetrieveHistory(pUser->hContact, MSG_AFTER, oldMsgId, 99); } } @@ -256,9 +265,14 @@ void CDiscordProto::OnCommandGuildSync(const JSONNode &pRoot) if (pUser.guildId != guildId) continue; + SESSION_INFO *si = pci->SM_FindSession(pUser.wszUsername, m_szModuleName); + if (si == NULL) + continue; + GCDEST gcd = { m_szModuleName, pUser.wszUsername, GC_EVENT_JOIN }; GCEVENT gce = { &gcd }; gce.time = time(0); + gce.dwFlags = GCEF_SILENT; for (auto it = pMembers.begin(); it != pMembers.end(); ++it) { const JSONNode &m = *it; @@ -295,10 +309,6 @@ void CDiscordProto::OnCommandGuildSync(const JSONNode &pRoot) } Chat_SetStatusEx(m_szModuleName, pUser.wszUsername, flags, wszUserId); } - - // okay, users added & topic set, now we can move a room online - Chat_Control(m_szModuleName, pUser.wszUsername, WINDOW_HIDDEN); - Chat_Control(m_szModuleName, pUser.wszUsername, SESSION_ONLINE); } } @@ -447,10 +457,6 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot) recv.szMessage = buf; recv.lParam = (LPARAM)wszMessageId.c_str(); ProtoChainRecvMsg(pUser->hContact, &recv); - - SnowFlake lastId = getId(pUser->hContact, DB_KEY_LASTMSGID); // as stored in a database - if (lastId < messageId) - setId(pUser->hContact, DB_KEY_LASTMSGID, messageId); } else { debugLogA("store a message into the group channel id %lld", channelId); @@ -472,6 +478,10 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot) gce.bIsMe = _wtoi64(wszUserId) == m_ownId; Chat_Event(&gce); } + + SnowFlake lastId = getId(pUser->hContact, DB_KEY_LASTMSGID); // as stored in a database + if (lastId < messageId) + setId(pUser->hContact, DB_KEY_LASTMSGID, messageId); } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index bd23659b3e..1765d2b610 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -63,10 +63,14 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest if (!root) return; - DBEVENTINFO dbei = {}; - dbei.szModule = m_szModuleName; - dbei.flags = DBEF_UTF; - dbei.eventType = EVENTTYPE_MESSAGE; + SESSION_INFO *si = nullptr; + if (!pUser->bIsPrivate) { + si = pci->SM_FindSession(pUser->wszUsername, m_szModuleName); + if (si == NULL) { + debugLogA("nessage to unknown channal %lld ignored", pUser->channelId); + return; + } + } SnowFlake lastId = getId(pUser->hContact, DB_KEY_LASTMSGID); // as stored in a database @@ -80,26 +84,48 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest for (int i = 0; i < arNodes.getCount(); i++) { JSONNode &p = *arNodes[i]; - SnowFlake authorid = ::getId(p["author"]["id"]); - if (authorid == m_ownId) - dbei.flags |= DBEF_SENT; - else - dbei.flags &= ~DBEF_SENT; - - SnowFlake msgid = ::getId(p["id"]); - if (msgid <= pUser->lastReadId) - dbei.flags |= DBEF_READ; - else - dbei.flags &= ~DBEF_READ; - CMStringW wszText = PrepareMessageText(p); - CMStringA szBody(ptrA(mir_utf8encodeW(wszText))); - szBody.AppendFormat("%c%lld", 0, msgid); - - dbei.timestamp = StringToDate(p["timestamp"].as_mstring()); - dbei.pBlob = (PBYTE)szBody.GetBuffer(); - dbei.cbBlob = szBody.GetLength(); - db_event_add(pUser->hContact, &dbei); + CMStringW wszUserId = p["author"]["id"].as_mstring(); + SnowFlake msgid = ::getId(p["id"]); + SnowFlake authorid = _wtoi64(wszUserId); + DWORD dwTimeStamp = StringToDate(p["timestamp"].as_mstring()); + + if (pUser->bIsPrivate) { + DBEVENTINFO dbei = {}; + dbei.szModule = m_szModuleName; + dbei.flags = DBEF_UTF; + dbei.eventType = EVENTTYPE_MESSAGE; + + if (authorid == m_ownId) + dbei.flags |= DBEF_SENT; + else + dbei.flags &= ~DBEF_SENT; + + if (msgid <= pUser->lastReadId) + dbei.flags |= DBEF_READ; + else + dbei.flags &= ~DBEF_READ; + + CMStringA szBody(ptrA(mir_utf8encodeW(wszText))); + szBody.AppendFormat("%c%lld", 0, msgid); + + dbei.timestamp = dwTimeStamp; + dbei.pBlob = (PBYTE)szBody.GetBuffer(); + dbei.cbBlob = szBody.GetLength(); + db_event_add(pUser->hContact, &dbei); + } + else { + ParseSpecialChars(si, wszText); + + GCDEST gcd = { m_szModuleName, pUser->wszUsername, GC_EVENT_MESSAGE }; + GCEVENT gce = { &gcd }; + gce.dwFlags = GCEF_ADDTOLOG; + gce.ptszUID = wszUserId; + gce.ptszText = wszText; + gce.time = dwTimeStamp; + gce.bIsMe = authorid == m_ownId; + Chat_Event(&gce); + } if (lastId < msgid) lastId = msgid; diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp index 4666ab188f..40911369d2 100644 --- a/protocols/Discord/src/utils.cpp +++ b/protocols/Discord/src/utils.cpp @@ -238,6 +238,28 @@ CMStringW PrepareMessageText(const JSONNode &pRoot) } } + const JSONNode &pEmbeds = pRoot["embeds"]; + for (auto it = pEmbeds.begin(); it != pEmbeds.end(); ++it) { + const JSONNode &p = *it; + + wszText.Append(L"\n-----------------"); + + CMStringW str = p["url"].as_mstring(); + wszText.AppendFormat(L"\n%s: %s", TranslateT("Embed"), str); + + str = p["provider"]["name"].as_mstring() + L" " + p["type"].as_mstring(); + if (str.GetLength() > 1) + wszText.AppendFormat(L"\n\t%s", str); + + str = p["description"].as_mstring(); + if (!str.IsEmpty()) + wszText.AppendFormat(L"\n\t%s", str); + + str = p["thumbnail"]["url"].as_mstring(); + if (!str.IsEmpty()) + wszText.AppendFormat(L"\n%s: %s", TranslateT("Preview"), str); + } + return wszText; } diff --git a/protocols/Discord/src/version.h b/protocols/Discord/src/version.h index b57f31665b..3014fd20d8 100644 --- a/protocols/Discord/src/version.h +++ b/protocols/Discord/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 -#define __MINOR_VERSION 3 -#define __RELEASE_NUM 1 -#define __BUILD_NUM 3 +#define __MINOR_VERSION 4 +#define __RELEASE_NUM 0 +#define __BUILD_NUM 1 #include <stdver.h> |