summaryrefslogtreecommitdiff
path: root/protocols/Discord
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2019-11-05 22:19:15 +0300
committerGeorge Hazan <ghazan@miranda.im>2019-11-05 22:19:15 +0300
commit88de53b8a6c208f290ee780b46155d4626467fbc (patch)
treeb148a64ef9aaba82099166c777349dc03eae8cd1 /protocols/Discord
parent1c5692ba09e1917794d8cf1308f3c009073d4be8 (diff)
Discord:
- JSON cycles rewritten using iterators; - private channels processing code extracted to the separate method
Diffstat (limited to 'protocols/Discord')
-rw-r--r--protocols/Discord/src/dispatch.cpp72
-rw-r--r--protocols/Discord/src/guilds.cpp35
-rw-r--r--protocols/Discord/src/proto.h4
-rw-r--r--protocols/Discord/src/server.cpp6
-rw-r--r--protocols/Discord/src/utils.cpp43
5 files changed, 66 insertions, 94 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp
index e55c28ee1b..14ed7a6eba 100644
--- a/protocols/Discord/src/dispatch.cpp
+++ b/protocols/Discord/src/dispatch.cpp
@@ -77,20 +77,8 @@ GatewayHandlerFunc CDiscordProto::GetHandler(const wchar_t *pwszCommand)
void CDiscordProto::OnCommandChannelCreated(const JSONNode &pRoot)
{
SnowFlake guildId = ::getId(pRoot["guild_id"]);
- if (guildId == 0) {
- // private channel, created for a contact
- const JSONNode &members = pRoot["recipients"];
- for (auto it = members.begin(); it != members.end(); ++it) {
- bool bNewUser = FindUser(::getId((*it)["id"])) == nullptr;
- CDiscordUser *pUser = PrepareUser(*it);
- pUser->bIsPrivate = true;
- pUser->lastMsgId = ::getId(pRoot["last_message_id"]);
- pUser->channelId = ::getId(pRoot["id"]);
- setId(pUser->hContact, DB_KEY_CHANNELID, pUser->channelId);
- if (bNewUser)
- setWord(pUser->hContact, "ApparentMode", ID_STATUS_OFFLINE);
- }
- }
+ if (guildId == 0)
+ PreparePrivateChannel(pRoot);
else {
// group channel for a guild
CDiscordGuild *pGuild = FindGuild(guildId);
@@ -495,57 +483,27 @@ void CDiscordProto::OnCommandReady(const JSONNode &pRoot)
m_szGatewaySessionId = pRoot["session_id"].as_mstring();
- const JSONNode &guilds = pRoot["guilds"];
- for (auto it = guilds.begin(); it != guilds.end(); ++it)
- ProcessGuild(*it);
+ for (auto &it : pRoot["guilds"])
+ ProcessGuild(it);
- const JSONNode &relations = pRoot["relationships"];
- for (auto it = relations.begin(); it != relations.end(); ++it) {
- const JSONNode &p = *it;
-
- CDiscordUser *pUser = PrepareUser(p["user"]);
- ProcessType(pUser, p);
+ for (auto &it : pRoot["relationships"]) {
+ CDiscordUser *pUser = PrepareUser(it["user"]);
+ ProcessType(pUser, it);
}
- const JSONNode &pStatuses = pRoot["presences"];
- for (auto it = pStatuses.begin(); it != pStatuses.end(); ++it) {
- const JSONNode &p = *it;
-
- CDiscordUser *pUser = FindUser(::getId(p["user"]["id"]));
+ for (auto &it : pRoot["presences"]) {
+ CDiscordUser *pUser = FindUser(::getId(it["user"]["id"]));
if (pUser != nullptr)
- setWord(pUser->hContact, "Status", StrToStatus(p["status"].as_mstring()));
+ setWord(pUser->hContact, "Status", StrToStatus(it["status"].as_mstring()));
}
- const JSONNode &channels = pRoot["private_channels"];
- for (auto it = channels.begin(); it != channels.end(); ++it) {
- const JSONNode &p = *it;
-
- CDiscordUser *pUser = nullptr;
- const JSONNode &recipients = p["recipients"];
- for (auto it2 = recipients.begin(); it2 != recipients.end(); ++it2)
- pUser = PrepareUser(*it2);
-
- if (pUser == nullptr)
- continue;
-
- CMStringW wszChannelId = p["id"].as_mstring();
- pUser->channelId = _wtoi64(wszChannelId);
- 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->lastMsgId > oldMsgId)
- RetrieveHistory(pUser, MSG_AFTER, oldMsgId, 99);
- }
+ for (auto &it : pRoot["private_channels"])
+ PreparePrivateChannel(it);
- const JSONNode &readState = pRoot["read_state"];
- for (auto it = readState.begin(); it != readState.end(); ++it) {
- const JSONNode &p = *it;
- CDiscordUser *pUser = FindUserByChannel(::getId(p["id"]));
+ for (auto &it : pRoot["read_state"]) {
+ CDiscordUser *pUser = FindUserByChannel(::getId(it["id"]));
if (pUser != nullptr)
- pUser->lastReadId = ::getId(p["last_message_id"]);
+ pUser->lastReadId = ::getId(it["last_message_id"]);
}
}
diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp
index ba039497f9..6dbb3ff477 100644
--- a/protocols/Discord/src/guilds.cpp
+++ b/protocols/Discord/src/guilds.cpp
@@ -150,17 +150,16 @@ void CDiscordProto::ProcessGuild(const JSONNode &p)
Chat_Control(m_szModuleName, pGuild->wszName, WINDOW_HIDDEN);
Chat_Control(m_szModuleName, pGuild->wszName, SESSION_ONLINE);
- const JSONNode &roles = p["roles"];
- for (auto itr = roles.begin(); itr != roles.end(); ++itr)
- ProcessRole(pGuild, *itr);
+ for (auto &it : p["roles"])
+ ProcessRole(pGuild, it);
+
BuildStatusList(pGuild, si);
for (auto &it : pGuild->arChatUsers)
AddGuildUser(pGuild, *it);
- const JSONNode &channels = p["channels"];
- for (auto itc = channels.begin(); itc != channels.end(); ++itc)
- ProcessGuildChannel(pGuild, *itc);
+ for (auto &it : p["channels"])
+ ProcessGuildChannel(pGuild, it);
if (m_bUseGroupchats)
ForkThread(&CDiscordProto::BatchChatCreate, pGuild);
@@ -265,11 +264,8 @@ void CDiscordProto::ParseGuildContents(CDiscordGuild *pGuild, const JSONNode &pR
LIST<CDiscordGuildMember> newMembers(100);
// store all guild members
- const JSONNode &pMembers = pRoot["members"];
- for (auto it = pMembers.begin(); it != pMembers.end(); ++it) {
- const JSONNode &m = *it;
-
- CMStringW wszUserId = m["user"]["id"].as_mstring();
+ for (auto &it : pRoot["members"]) {
+ CMStringW wszUserId = it["user"]["id"].as_mstring();
SnowFlake userId = _wtoi64(wszUserId);
CDiscordGuildMember *pm = pGuild->FindUser(userId);
if (pm == nullptr) {
@@ -278,17 +274,16 @@ void CDiscordProto::ParseGuildContents(CDiscordGuild *pGuild, const JSONNode &pR
newMembers.insert(pm);
}
- pm->wszNick = m["nick"].as_mstring();
+ pm->wszNick = it["nick"].as_mstring();
if (pm->wszNick.IsEmpty())
- pm->wszNick = m["user"]["username"].as_mstring() + L"#" + m["user"]["discriminator"].as_mstring();
+ pm->wszNick = it["user"]["username"].as_mstring() + L"#" + it["user"]["discriminator"].as_mstring();
if (userId == pGuild->ownerId)
pm->wszRole = L"@owner";
else {
CDiscordRole *pRole = nullptr;
- const JSONNode &pRoles = m["roles"];
- for (auto itr = pRoles.begin(); itr != pRoles.end(); ++itr) {
- SnowFlake roleId = ::getId(*itr);
+ for (auto &itr : it["roles"]) {
+ SnowFlake roleId = ::getId(itr);
if (pRole = pGuild->arRoles.find((CDiscordRole*)&roleId))
break;
}
@@ -298,12 +293,10 @@ void CDiscordProto::ParseGuildContents(CDiscordGuild *pGuild, const JSONNode &pR
}
// parse online statuses
- const JSONNode &pStatuses = pRoot["presences"];
- for (auto it = pStatuses.begin(); it != pStatuses.end(); ++it) {
- const JSONNode &s = *it;
- CDiscordGuildMember *gm = pGuild->FindUser(::getId(s["user"]["id"]));
+ for (auto &it : pRoot["presences"]) {
+ CDiscordGuildMember *gm = pGuild->FindUser(::getId(it["user"]["id"]));
if (gm != nullptr)
- gm->iStatus = StrToStatus(s["status"].as_mstring());
+ gm->iStatus = StrToStatus(it["status"].as_mstring());
}
for (auto &pm : newMembers)
diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h
index 43f96a04a9..028829e91c 100644
--- a/protocols/Discord/src/proto.h
+++ b/protocols/Discord/src/proto.h
@@ -198,7 +198,9 @@ class CDiscordProto : public PROTO<CDiscordProto>
CDiscordUser* FindUser(SnowFlake id);
CDiscordUser* FindUser(const wchar_t *pwszUsername, int iDiscriminator);
CDiscordUser* FindUserByChannel(SnowFlake channelId);
- CDiscordUser* PrepareUser(const JSONNode&);
+
+ void PreparePrivateChannel(const JSONNode &);
+ CDiscordUser* PrepareUser(const JSONNode &);
//////////////////////////////////////////////////////////////////////////////////////
// menu items
diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp
index eef014b3fa..d62ff514c5 100644
--- a/protocols/Discord/src/server.cpp
+++ b/protocols/Discord/src/server.cpp
@@ -72,9 +72,9 @@ void CDiscordProto::OnReceiveHistory(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest
LIST<JSONNode> arNodes(10, compareMsgHistory);
int iNumMessages = 0;
- for (auto it = root.begin(); it != root.end(); ++it, ++iNumMessages) {
- JSONNode &p = *it;
- arNodes.insert(&p);
+ for (auto &it : root) {
+ arNodes.insert(&it);
+ iNumMessages++;
}
for (auto &it : arNodes) {
diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp
index e26317d0f8..07b293d1b4 100644
--- a/protocols/Discord/src/utils.cpp
+++ b/protocols/Discord/src/utils.cpp
@@ -129,6 +129,30 @@ CDiscordUser* CDiscordProto::FindUserByChannel(SnowFlake channelId)
return nullptr;
}
+/////////////////////////////////////////////////////////////////////////////////////////
+// Common JSON processing routines
+
+void CDiscordProto::PreparePrivateChannel(const JSONNode &root)
+{
+ CDiscordUser *pUser = nullptr;
+ for (auto &it : root["recipients"])
+ pUser = PrepareUser(it);
+
+ if (pUser == nullptr)
+ return;
+
+ CMStringW wszChannelId = root["id"].as_mstring();
+ pUser->channelId = _wtoi64(wszChannelId);
+ pUser->lastMsgId = ::getId(root["last_message_id"]);
+ pUser->bIsPrivate = true;
+
+ setId(pUser->hContact, DB_KEY_CHANNELID, pUser->channelId);
+
+ SnowFlake oldMsgId = getId(pUser->hContact, DB_KEY_LASTMSGID);
+ if (pUser->lastMsgId > oldMsgId)
+ RetrieveHistory(pUser, MSG_AFTER, oldMsgId, 99);
+}
+
CDiscordUser* CDiscordProto::PrepareUser(const JSONNode &user)
{
SnowFlake id = ::getId(user["id"]);
@@ -184,10 +208,8 @@ CMStringW PrepareMessageText(const JSONNode &pRoot)
CMStringW wszText = pRoot["content"].as_mstring();
bool bDelimiterAdded = false;
- const JSONNode &pAttaches = pRoot["attachments"];
- for (auto it = pAttaches.begin(); it != pAttaches.end(); ++it) {
- const JSONNode &p = *it;
- CMStringW wszUrl = p["url"].as_mstring();
+ for (auto &it : pRoot["attachments"]) {
+ CMStringW wszUrl = it["url"].as_mstring();
if (!wszUrl.IsEmpty()) {
if (!bDelimiterAdded) {
bDelimiterAdded = true;
@@ -197,24 +219,21 @@ CMStringW PrepareMessageText(const JSONNode &pRoot)
}
}
- const JSONNode &pEmbeds = pRoot["embeds"];
- for (auto it = pEmbeds.begin(); it != pEmbeds.end(); ++it) {
- const JSONNode &p = *it;
-
+ for (auto &it : pRoot["embeds"]) {
wszText.Append(L"\n-----------------");
- CMStringW str = p["url"].as_mstring();
+ CMStringW str = it["url"].as_mstring();
wszText.AppendFormat(L"\n%s: %s", TranslateT("Embed"), str.c_str());
- str = p["provider"]["name"].as_mstring() + L" " + p["type"].as_mstring();
+ str = it["provider"]["name"].as_mstring() + L" " + it["type"].as_mstring();
if (str.GetLength() > 1)
wszText.AppendFormat(L"\n\t%s", str.c_str());
- str = p["description"].as_mstring();
+ str = it["description"].as_mstring();
if (!str.IsEmpty())
wszText.AppendFormat(L"\n\t%s", str.c_str());
- str = p["thumbnail"]["url"].as_mstring();
+ str = it["thumbnail"]["url"].as_mstring();
if (!str.IsEmpty())
wszText.AppendFormat(L"\n%s: %s", TranslateT("Preview"), str.c_str());
}