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/src/server.cpp | |
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/src/server.cpp')
-rw-r--r-- | protocols/Discord/src/server.cpp | 74 |
1 files changed, 25 insertions, 49 deletions
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(); } |