diff options
author | George Hazan <ghazan@miranda.im> | 2020-02-24 12:23:07 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2020-02-24 12:23:07 +0300 |
commit | 2f8a2b2964dbf1f36db628661978ecd9189c1b1d (patch) | |
tree | 1470581e2a2a39e9f308e28d37155614ec548859 /protocols/Discord | |
parent | e01b0dc97ef826a3f3ee6c2649df3a32af402484 (diff) |
Discord:
- fixes a problem with 400 BAD REQUEST error when sending mark as read request;
- code cleaning;
- version bump
Diffstat (limited to 'protocols/Discord')
-rw-r--r-- | protocols/Discord/src/http.cpp | 31 | ||||
-rw-r--r-- | protocols/Discord/src/proto.cpp | 11 | ||||
-rw-r--r-- | protocols/Discord/src/proto.h | 15 | ||||
-rw-r--r-- | protocols/Discord/src/server.cpp | 74 | ||||
-rw-r--r-- | protocols/Discord/src/version.h | 2 |
5 files changed, 82 insertions, 51 deletions
diff --git a/protocols/Discord/src/http.cpp b/protocols/Discord/src/http.cpp index 7dda5ae565..3cd80d01fd 100644 --- a/protocols/Discord/src/http.cpp +++ b/protocols/Discord/src/http.cpp @@ -27,6 +27,16 @@ void CDiscordProto::Push(AsyncHttpRequest *pReq, int iTimeout) SetEvent(m_evRequestsQueue); } +void CDiscordProto::SaveToken(const JSONNode &data) +{ + CMStringA szToken = data["token"].as_mstring(); + if (szToken.IsEmpty()) + return; + + m_szAccessToken = szToken.Detach(); + setString("AccessToken", m_szAccessToken); +} + ///////////////////////////////////////////////////////////////////////////////////////// static LONG g_reqNum = 0; @@ -64,6 +74,27 @@ AsyncHttpRequest::AsyncHttpRequest(CDiscordProto *ppro, int iRequestType, LPCSTR m_iReqNum = ::InterlockedIncrement(&g_reqNum); } +JsonReply::JsonReply(NETLIBHTTPREQUEST *pReply) +{ + if (pReply == nullptr) { + m_errorCode = 500; + return; + } + + m_errorCode = pReply->resultCode; + if (m_errorCode != 200) + return; + + m_root = json_parse(pReply->pData); + if (m_root == nullptr) + m_errorCode = 500; +} + +JsonReply::~JsonReply() +{ + json_delete(m_root); +} + ///////////////////////////////////////////////////////////////////////////////////////// void CDiscordProto::ServerThread(void*) diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index 626b30088e..1789963959 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -482,13 +482,22 @@ int CDiscordProto::UserIsTyping(MCONTACT hContact, int type) ///////////////////////////////////////////////////////////////////////////////////////// +void CDiscordProto::OnReceiveMarkRead(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *) +{ + JsonReply root(pReply); + if (root) + SaveToken(root.data()); +} + void CDiscordProto::SendMarkRead() { mir_cslock lck(csMarkReadQueue); while (arMarkReadQueue.getCount()) { CDiscordUser *pUser = arMarkReadQueue[0]; + JSONNode payload; payload << CHAR_PARAM("token", m_szAccessToken); CMStringA szUrl(FORMAT, "/channels/%lld/messages/%lld/ack", pUser->channelId, pUser->lastMsgId); - Push(new AsyncHttpRequest(this, REQUEST_POST, szUrl, nullptr)); + auto *pReq = new AsyncHttpRequest(this, REQUEST_POST, szUrl, &CDiscordProto::OnReceiveMarkRead, &payload); + Push(pReq); arMarkReadQueue.remove(0); } } diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index e42fe720a1..14451dce1f 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -21,6 +21,19 @@ struct AsyncHttpRequest : public MTHttpRequest<CDiscordProto> bool m_bMainSite; }; +class JsonReply +{ + JSONNode *m_root = nullptr; + int m_errorCode = 0; + +public: + JsonReply(NETLIBHTTPREQUEST *); + ~JsonReply(); + + __forceinline JSONNode& data() const { return *m_root; } + __forceinline operator bool() const { return m_errorCode == 200; } +}; + ///////////////////////////////////////////////////////////////////////////////////////// struct CDiscordRole : public MZeroedObject @@ -178,6 +191,7 @@ class CDiscordProto : public PROTO<CDiscordProto> void ExecuteRequest(AsyncHttpRequest *pReq); void Push(AsyncHttpRequest *pReq, int iTimeout = 10000); + void SaveToken(const JSONNode &data); HANDLE m_hWorkerThread; // worker thread handle HNETLIBCONN m_hAPIConnection; // working connection @@ -385,6 +399,7 @@ public: void OnReceiveCreateChannel(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void OnReceiveFile(NETLIBHTTPREQUEST*, AsyncHttpRequest*); void OnReceiveGateway(NETLIBHTTPREQUEST*, AsyncHttpRequest*); + void OnReceiveMarkRead(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 3afe70de99..414c22815d 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -52,10 +52,7 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest { CDiscordUser *pUser = (CDiscordUser*)pReq->pUserInfo; - if (pReply->resultCode != 200) - return; - - JSONNode root = JSONNode::parse(pReply->pData); + JsonReply root(pReply); if (!root) return; @@ -72,7 +69,7 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest LIST<JSONNode> arNodes(10, compareMsgHistory); int iNumMessages = 0; - for (auto &it : root) { + for (auto &it : root.data()) { arNodes.insert(&it); iNumMessages++; } @@ -153,24 +150,20 @@ void CDiscordProto::RetrieveMyInfo() void CDiscordProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) { - if (pReply->resultCode != 200) { - ConnectionFailed(LOGINERR_WRONGPASSWORD); - return; - } - - JSONNode root = JSONNode::parse(pReply->pData); + JsonReply root(pReply); if (!root) { - ConnectionFailed(LOGINERR_NOSERVER); + ConnectionFailed(LOGINERR_WRONGPASSWORD); return; } - SnowFlake id = ::getId(root["id"]); + auto &data = root.data(); + SnowFlake id = ::getId(data["id"]); setId(0, DB_KEY_ID, id); - setByte(0, DB_KEY_MFA, root["mfa_enabled"].as_bool()); - setDword(0, DB_KEY_DISCR, _wtoi(root["discriminator"].as_mstring())); - setWString(0, DB_KEY_NICK, root["username"].as_mstring()); - m_wszEmail = root["email"].as_mstring(); + setByte(0, DB_KEY_MFA, data["mfa_enabled"].as_bool()); + setDword(0, DB_KEY_DISCR, _wtoi(data["discriminator"].as_mstring())); + setWString(0, DB_KEY_NICK, data["username"].as_mstring()); + m_wszEmail = data["email"].as_mstring(); m_ownId = id; @@ -188,7 +181,7 @@ void CDiscordProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest* OnLoggedIn(); - CheckAvatarChange(0, root["avatar"].as_mstring()); + CheckAvatarChange(0, data["avatar"].as_mstring()); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -196,18 +189,14 @@ void CDiscordProto::OnReceiveMyInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest* void CDiscordProto::OnReceiveGateway(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) { - if (pReply->resultCode != 200) { - ShutdownSession(); - return; - } - - JSONNode root = JSONNode::parse(pReply->pData); + JsonReply root(pReply); if (!root) { ShutdownSession(); return; } - m_szGateway = root["url"].as_mstring(); + auto &data = root.data(); + m_szGateway = data["url"].as_mstring(); ForkThread(&CDiscordProto::GatewayThread, nullptr); } @@ -246,30 +235,25 @@ void CDiscordProto::SetServerStatus(int iStatus) void CDiscordProto::OnReceiveCreateChannel(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) { - if (pReply->resultCode != 200) - return; - - JSONNode root = JSONNode::parse(pReply->pData); + JsonReply root(pReply); if (root) - OnCommandChannelCreated(root); + OnCommandChannelCreated(root.data()); } ///////////////////////////////////////////////////////////////////////////////////////// void CDiscordProto::OnReceiveMessageAck(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) { - if (pReply->resultCode != 200) - return; - - JSONNode root = JSONNode::parse(pReply->pData); + JsonReply root(pReply); if (!root) return; - CMStringW wszToken(root["token"].as_mstring()); + auto &data = root.data(); + CMStringW wszToken(data["token"].as_mstring()); if (!wszToken.IsEmpty()) { JSONNode props; props.set_name("properties"); JSONNode reply; reply << props; - reply << CHAR_PARAM("event", "ack_messages") << WCHAR_PARAM("token", root["token"].as_mstring()); + reply << CHAR_PARAM("event", "ack_messages") << WCHAR_PARAM("token", data["token"].as_mstring()); Push(new AsyncHttpRequest(this, REQUEST_POST, "/track", nullptr, &reply)); } } @@ -295,19 +279,11 @@ void CDiscordProto::OnReceiveToken(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest*) return; } - JSONNode root = JSONNode::parse(pReply->pData); - if (!root) { -LBL_Error: + JsonReply root(pReply); + if (!root) ConnectionFailed(LOGINERR_NOSERVER); - return; + else { + SaveToken(root.data()); + RetrieveMyInfo(); } - - CMStringA szToken = root["token"].as_mstring(); - if (szToken.IsEmpty()) - goto LBL_Error; - - m_szAccessToken = szToken.Detach(); - setString("AccessToken", m_szAccessToken); - - RetrieveMyInfo(); } diff --git a/protocols/Discord/src/version.h b/protocols/Discord/src/version.h index bdc120275b..eb2be75625 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 2 -#define __BUILD_NUM 3 +#define __BUILD_NUM 4 #include <stdver.h> |