diff options
author | George Hazan <ghazan@miranda.im> | 2018-11-23 22:14:03 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2018-11-23 22:14:03 +0300 |
commit | bc1277617376426080ca452f63a633c9e255fc4c (patch) | |
tree | bbff7db2a7b957799d4d627aa989c7c4e47cafde | |
parent | 1929e49d4407f275e470d2fe8cffb9b9d84ff010 (diff) |
Discord:
- fixes #1641 ([Discord] In some cases, sending a link in own message can cause that message to be attributed to conversation partner)
- old perversion with storing last user id dropped;
- class CDiscordMessage dropped as well, only message id is stored now;
- old useless handler OnReceiveMessage also dropped;
- version bump
-rw-r--r-- | protocols/Discord/src/dispatch.cpp | 34 | ||||
-rw-r--r-- | protocols/Discord/src/groupchat.cpp | 2 | ||||
-rw-r--r-- | protocols/Discord/src/guilds.cpp | 4 | ||||
-rw-r--r-- | protocols/Discord/src/proto.cpp | 6 | ||||
-rw-r--r-- | protocols/Discord/src/proto.h | 14 | ||||
-rw-r--r-- | protocols/Discord/src/server.cpp | 14 | ||||
-rw-r--r-- | protocols/Discord/src/version.h | 4 |
7 files changed, 26 insertions, 52 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index f48f789c44..13687a4b5f 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -82,7 +82,7 @@ void CDiscordProto::OnCommandChannelCreated(const JSONNode &pRoot) const JSONNode &members = pRoot["recipients"]; for (auto it = members.begin(); it != members.end(); ++it) { CDiscordUser *pUser = PrepareUser(*it); - pUser->lastMsg = CDiscordMessage(::getId(pRoot["last_message_id"])); + pUser->lastMsgId = ::getId(pRoot["last_message_id"]); pUser->channelId = ::getId(pRoot["id"]); setId(pUser->hContact, DB_KEY_CHANNELID, pUser->channelId); } @@ -106,8 +106,7 @@ void CDiscordProto::OnCommandChannelDeleted(const JSONNode &pRoot) SnowFlake guildId = ::getId(pRoot["guild_id"]); if (guildId == 0) { - pUser->channelId = 0; - pUser->lastMsg = CDiscordMessage(); + pUser->channelId = pUser->lastMsgId = 0; delSetting(pUser->hContact, DB_KEY_CHANNELID); } else { @@ -123,7 +122,7 @@ void CDiscordProto::OnCommandChannelUpdated(const JSONNode &pRoot) if (pUser == nullptr) return; - pUser->lastMsg = CDiscordMessage(::getId(pRoot["last_message_id"])); + pUser->lastMsgId = ::getId(pRoot["last_message_id"]); SnowFlake guildId = ::getId(pRoot["guild_id"]); if (guildId != 0) { @@ -325,7 +324,6 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) CMStringW wszUserId = pRoot["author"]["id"].as_mstring(); SnowFlake userId = _wtoi64(wszUserId); SnowFlake msgId = _wtoi64(wszMessageId); - CDiscordMessage msg(msgId, userId); // try to find a sender by his channel SnowFlake channelId = ::getId(pRoot["channel_id"]); @@ -338,19 +336,17 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) char szMsgId[100]; _i64toa_s(msgId, szMsgId, _countof(szMsgId), 10); - // restore partially received updated message - if (pUser->lastMsg.id == msg.id) - msg = pUser->lastMsg; - COwnMessage ownMsg(::getId(pRoot["nonce"]), 0); COwnMessage *p = arOwnMessages.find(&ownMsg); if (p != nullptr) { // own message? skip it ProtoBroadcastAck(pUser->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, (HANDLE)p->reqId, (LPARAM)szMsgId); - debugLogA("skipping own message with nonce=%lld, id=%lld", ownMsg.nonce, msg.id); + debugLogA("skipping own message with nonce=%lld, id=%lld", ownMsg.nonce, msgId); } else { CMStringW wszText = PrepareMessageText(pRoot); + bool bOurMessage = userId == m_ownId; + // old message? try to restore it from database if (!bIsNew) { MEVENT hOldEvent = db_event_getById(m_szModuleName, szMsgId); if (hOldEvent) { @@ -361,6 +357,8 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) ptrW wszOldText(DbEvent_GetTextW(&dbei, CP_UTF8)); if (wszOldText) wszText.Insert(0, wszOldText); + if (dbei.flags & DBEF_SENT) + bOurMessage = true; } mir_free(dbei.pBlob); } @@ -373,7 +371,7 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) if (pUser->bIsPrivate) { // if a message has myself as an author, add some flags PROTORECVEVENT recv = {}; - if (msg.authorId == m_ownId) + if (bOurMessage) recv.flags = PREF_CREATEREAD | PREF_SENT; debugLogA("store a message from private user %lld, channel id %lld", pUser->id, pUser->channelId); @@ -417,18 +415,18 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) gce.ptszUID = wszUserId; gce.ptszText = wszText; gce.time = (DWORD)StringToDate(pRoot["timestamp"].as_mstring()); - gce.bIsMe = msg.authorId == m_ownId; + gce.bIsMe = bOurMessage; Chat_Event(&gce); debugLogW(L"New channel %s message from %s: %s", si->ptszID, gce.ptszUID, gce.ptszText); } } - pUser->lastMsg = msg; + pUser->lastMsgId = msgId; SnowFlake lastId = getId(pUser->hContact, DB_KEY_LASTMSGID); // as stored in a database - if (lastId < msg.id) - setId(pUser->hContact, DB_KEY_LASTMSGID, msg.id); + if (lastId < msgId) + setId(pUser->hContact, DB_KEY_LASTMSGID, msgId); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -438,7 +436,7 @@ void CDiscordProto::OnCommandMessageAck(const JSONNode &pRoot) { CDiscordUser *pUser = FindUserByChannel(pRoot["channel_id"]); if (pUser != nullptr) - pUser->lastMsg = CDiscordMessage(::getId(pRoot["message_id"])); + pUser->lastMsgId = ::getId(pRoot["message_id"]); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -520,13 +518,13 @@ void CDiscordProto::OnCommandReady(const JSONNode &pRoot) CMStringW wszChannelId = p["id"].as_mstring(); pUser->channelId = _wtoi64(wszChannelId); - pUser->lastMsg = CDiscordMessage(::getId(p["last_message_id"])); + pUser->lastMsgId = ::getId(p["last_message_id"]); pUser->bIsPrivate = true; setId(pUser->hContact, DB_KEY_CHANNELID, pUser->channelId); SnowFlake oldMsgId = getId(pUser->hContact, DB_KEY_LASTMSGID); - if (pUser->lastMsg.id > oldMsgId) + if (pUser->lastMsgId > oldMsgId) RetrieveHistory(pUser->hContact, MSG_AFTER, oldMsgId, 99); } diff --git a/protocols/Discord/src/groupchat.cpp b/protocols/Discord/src/groupchat.cpp index d8ac8eb4dc..cc26dbb162 100644 --- a/protocols/Discord/src/groupchat.cpp +++ b/protocols/Discord/src/groupchat.cpp @@ -168,7 +168,7 @@ int CDiscordProto::GroupchatEventHook(WPARAM, LPARAM lParam) JSONNode body; body << WCHAR_PARAM("content", wszText); CMStringA szUrl(FORMAT, "/channels/%S/messages", gch->ptszID); - Push(new AsyncHttpRequest(this, REQUEST_POST, szUrl, &CDiscordProto::OnReceiveMessage, &body)); + Push(new AsyncHttpRequest(this, REQUEST_POST, szUrl, nullptr, &body)); } } break; diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp index 070b6e0ccf..31ce370f8d 100644 --- a/protocols/Discord/src/guilds.cpp +++ b/protocols/Discord/src/guilds.cpp @@ -203,7 +203,7 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS pUser->wszChannelName = pGuild->wszName + L"#" + wszName; pUser->wszTopic = pch["topic"].as_mstring(); pUser->pGuild = pGuild; - pUser->lastMsg = CDiscordMessage(::getId(pch["last_message_id"])); + pUser->lastMsgId = ::getId(pch["last_message_id"]); pUser->parentId = _wtoi64(pch["parent_id"].as_mstring()); setId(pUser->hContact, DB_KEY_ID, channelId); @@ -289,7 +289,7 @@ void CDiscordProto::ParseGuildContents(CDiscordGuild *pGuild, const JSONNode &pR continue; SnowFlake oldMsgId = getId(it->hContact, DB_KEY_LASTMSGID); - if (oldMsgId != 0 && it->lastMsg.id > oldMsgId) + if (oldMsgId != 0 && it->lastMsgId > oldMsgId) RetrieveHistory(it->hContact, MSG_AFTER, oldMsgId, 99); } } diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index a803488898..50e9481b12 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -106,7 +106,7 @@ void CDiscordProto::OnModulesLoaded() CDiscordUser *pNew = new CDiscordUser(getId(hContact, DB_KEY_ID)); pNew->hContact = hContact; pNew->channelId = getId(hContact, DB_KEY_CHANNELID); - pNew->lastMsg.id = getId(hContact, DB_KEY_LASTMSGID); + pNew->lastMsgId = getId(hContact, DB_KEY_LASTMSGID); pNew->wszUsername = ptrW(getWStringA(hContact, DB_KEY_NICK)); pNew->iDiscriminator = getDword(hContact, DB_KEY_DISCR); arUsers.insert(pNew); @@ -399,7 +399,7 @@ int CDiscordProto::SendMsg(MCONTACT hContact, int /*flags*/, const char *pszSrc) JSONNode body; body << WCHAR_PARAM("content", wszText) << INT64_PARAM("nonce", nonce); CMStringA szUrl(FORMAT, "/channels/%lld/messages", pUser->channelId); - AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_POST, szUrl, &CDiscordProto::OnReceiveMessage, &body); + AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_POST, szUrl, nullptr, &body); arOwnMessages.insert(new COwnMessage(nonce, pReq->m_iReqNum)); Push(pReq); return pReq->m_iReqNum; @@ -425,7 +425,7 @@ void CDiscordProto::MarkReadTimerProc(HWND hwnd, UINT, UINT_PTR id, DWORD) mir_cslock lck(ppro->csMarkReadQueue); while (ppro->arMarkReadQueue.getCount()) { CDiscordUser *pUser = ppro->arMarkReadQueue[0]; - CMStringA szUrl(FORMAT, "/channels/%lld/messages/%lld/ack", pUser->channelId, pUser->lastMsg.id); + CMStringA szUrl(FORMAT, "/channels/%lld/messages/%lld/ack", pUser->channelId, pUser->lastMsgId); ppro->Push(new AsyncHttpRequest(ppro, REQUEST_POST, szUrl, nullptr)); ppro->arMarkReadQueue.remove(0); } diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index b750b67b2d..9f42b4ad58 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -45,16 +45,6 @@ struct CDiscordRole : public MZeroedObject ///////////////////////////////////////////////////////////////////////////////////////// -struct CDiscordMessage -{ - SnowFlake id, authorId; - - CDiscordMessage(SnowFlake _id = 0, SnowFlake _authorId = 0) : - id(_id), - authorId(_authorId) - {} -}; - struct COwnMessage { SnowFlake nonce; @@ -85,13 +75,12 @@ struct CDiscordUser : public MZeroedObject MCONTACT hContact; SnowFlake channelId; - SnowFlake lastReadId; + SnowFlake lastReadId, lastMsgId; SnowFlake parentId; bool bIsPrivate; bool bIsGroup; struct CDiscordGuild *pGuild; - CDiscordMessage lastMsg; CMStringW wszUsername, wszChannelName, wszTopic; int iDiscriminator; @@ -356,7 +345,6 @@ public: void OnReceiveCreateChannel(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void OnReceiveFile(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void OnReceiveGateway(NETLIBHTTPREQUEST*, AsyncHttpRequest*); - void OnReceiveMessage(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void OnReceiveMessageAck(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void OnReceiveToken(NETLIBHTTPREQUEST*, AsyncHttpRequest*); diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index 615d7f96e9..e7737b61a5 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -137,7 +137,7 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest setId(pUser->hContact, DB_KEY_LASTMSGID, lastId); // if we fetched 99 messages, but have smth more to go, continue fetching - if (iNumMessages == 99 && lastId < pUser->lastMsg.id) + if (iNumMessages == 99 && lastId < pUser->lastMsgId) RetrieveHistory(pUser->hContact, MSG_AFTER, lastId, 99); } @@ -249,18 +249,6 @@ void CDiscordProto::OnReceiveCreateChannel(NETLIBHTTPREQUEST *pReply, AsyncHttpR ///////////////////////////////////////////////////////////////////////////////////////// -void CDiscordProto::OnReceiveMessage(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) -{ - JSONNode root = JSONNode::parse(pReply->pData); - if (root) { - CDiscordUser *pUser = FindUserByChannel(::getId(root["channel_id"])); - if (pUser != nullptr) - pUser->lastMsg = CDiscordMessage(::getId(root["id"]), ::getId(root["author"]["id"])); - } -} - -///////////////////////////////////////////////////////////////////////////////////////// - void CDiscordProto::OnReceiveMessageAck(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) { if (pReply->resultCode != 200) diff --git a/protocols/Discord/src/version.h b/protocols/Discord/src/version.h index 56d742b312..abd9c2e311 100644 --- a/protocols/Discord/src/version.h +++ b/protocols/Discord/src/version.h @@ -1,7 +1,7 @@ #define __MAJOR_VERSION 0 #define __MINOR_VERSION 6 -#define __RELEASE_NUM 0 -#define __BUILD_NUM 3 +#define __RELEASE_NUM 1 +#define __BUILD_NUM 1 #include <stdver.h> |