summaryrefslogtreecommitdiff
path: root/protocols/Discord
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2018-10-27 20:32:44 +0300
committerGeorge Hazan <ghazan@miranda.im>2018-10-27 20:32:44 +0300
commit60abc2fbd6fc10083e821340f9edc3593de5637e (patch)
tree6c7851c199c28938e88863de5db31297bffbe104 /protocols/Discord
parentd5931de04d23f15930e915bc03fe59a127cc5f16 (diff)
Discord: list of guild's channels separated for each guild
Diffstat (limited to 'protocols/Discord')
-rw-r--r--protocols/Discord/src/dispatch.cpp12
-rw-r--r--protocols/Discord/src/groupchat.cpp2
-rw-r--r--protocols/Discord/src/guilds.cpp24
-rw-r--r--protocols/Discord/src/proto.h5
4 files changed, 30 insertions, 13 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp
index bca40644a2..002f45fb36 100644
--- a/protocols/Discord/src/dispatch.cpp
+++ b/protocols/Discord/src/dispatch.cpp
@@ -194,7 +194,7 @@ void CDiscordProto::OnCommandGuildDeleted(const JSONNode &pRoot)
auto T = arUsers.rev_iter();
for (auto &it : T)
- if (it->guildId == pGuild->id) {
+ if (it->pGuild == pGuild) {
Chat_Terminate(m_szModuleName, it->wszUsername, true);
arUsers.remove(T.indexOf(&it));
}
@@ -220,7 +220,7 @@ void CDiscordProto::OnCommandGuildMemberRemoved(const JSONNode &pRoot)
CMStringW wszUserId = pRoot["user"]["id"].as_mstring();
for (auto &pUser : arUsers) {
- if (pUser->guildId != pGuild->id)
+ if (pUser->pGuild != pGuild)
continue;
GCEVENT gce = { m_szModuleName, pUser->wszUsername, GC_EVENT_PART };
@@ -246,7 +246,7 @@ void CDiscordProto::OnCommandGuildMemberUpdated(const JSONNode &pRoot)
gm->wszNick = pRoot["user"]["username"].as_mstring() + L"#" + pRoot["user"]["discriminator"].as_mstring();
for (auto &it : arUsers) {
- if (it->guildId != pGuild->id)
+ if (it->pGuild != pGuild)
continue;
CMStringW wszOldNick;
@@ -295,7 +295,7 @@ void CDiscordProto::OnCommandRoleDeleted(const JSONNode &pRoot)
it->position--;
for (auto &it : arUsers) {
- if (it->guildId != pGuild->id)
+ if (it->pGuild != pGuild)
continue;
SESSION_INFO *si = g_chatApi.SM_FindSession(it->wszUsername, m_szModuleName);
@@ -368,9 +368,9 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot)
return;
}
- CDiscordGuild *pGuild = FindGuild(pUser->guildId);
+ CDiscordGuild *pGuild = pUser->pGuild;
if (pGuild == nullptr) {
- debugLogA("message to unknown guild %lld ignored", pUser->guildId);
+ debugLogA("message to unknown guild ignored");
return;
}
diff --git a/protocols/Discord/src/groupchat.cpp b/protocols/Discord/src/groupchat.cpp
index 942100d58a..d8ac8eb4dc 100644
--- a/protocols/Discord/src/groupchat.cpp
+++ b/protocols/Discord/src/groupchat.cpp
@@ -140,7 +140,7 @@ void CDiscordProto::Chat_ProcessLogMenu(GCHOOK *gch)
es.recentCount = 5;
if (EnterString(&es)) {
JSONNode root; root << WCHAR_PARAM("nick", es.ptszResult);
- CMStringA szUrl(FORMAT, "/guilds/%lld/members/@me/nick", pUser->guildId);
+ CMStringA szUrl(FORMAT, "/guilds/%lld/members/@me/nick", pUser->pGuild->id);
Push(new AsyncHttpRequest(this, REQUEST_PATCH, szUrl, nullptr, &root));
mir_free(es.ptszResult);
}
diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp
index 751ecf54da..c494d141ac 100644
--- a/protocols/Discord/src/guilds.cpp
+++ b/protocols/Discord/src/guilds.cpp
@@ -17,6 +17,8 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "stdafx.h"
+int compareUsers(const CDiscordUser *p1, const CDiscordUser *p2);
+
static int compareRoles(const CDiscordRole *p1, const CDiscordRole *p2)
{
return compareInt64(p1->id, p2->id);
@@ -29,6 +31,7 @@ static int compareChatUsers(const CDiscordGuildMember *p1, const CDiscordGuildMe
CDiscordGuild::CDiscordGuild(SnowFlake _id)
: id(_id),
+ arChannels(10, compareUsers),
arChatUsers(30, compareChatUsers),
arRoles(10, compareRoles)
{
@@ -38,6 +41,12 @@ CDiscordGuild::~CDiscordGuild()
{
}
+CDiscordUser::~CDiscordUser()
+{
+ if (pGuild != nullptr)
+ pGuild->arChannels.remove(this);
+}
+
/////////////////////////////////////////////////////////////////////////////////////////
// reads a role from json
@@ -70,8 +79,8 @@ void CDiscordProto::BatchChatCreate(void *param)
{
CDiscordGuild *pGuild = (CDiscordGuild*)param;
- for (auto &it : arUsers)
- if (it->guildId == pGuild->id && !it->bIsPrivate)
+ for (auto &it : pGuild->arChannels)
+ if (!it->bIsPrivate)
CreateChat(pGuild, it);
}
@@ -161,6 +170,7 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS
pUser->channelId = channelId;
pUser->bIsGroup = true;
arUsers.insert(pUser);
+ pGuild->arChannels.insert(pUser);
MGROUP grpId = Clist_GroupCreate(pGuild->groupId, wszName);
pUser->wszChannelName = Clist_GroupGetName(grpId);
@@ -176,10 +186,14 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS
pUser->channelId = channelId;
arUsers.insert(pUser);
}
+
+ if (pGuild->arChannels.find(pUser) == nullptr)
+ pGuild->arChannels.insert(pUser);
+
pUser->wszUsername = wszChannelId;
pUser->wszChannelName = L"#" + wszName;
pUser->wszTopic = pch["topic"].as_mstring();
- pUser->guildId = pGuild->id;
+ pUser->pGuild = pGuild;
pUser->lastMsg = CDiscordMessage(::getId(pch["last_message_id"]));
pUser->parentId = _wtoi64(pch["parent_id"].as_mstring());
@@ -273,8 +287,8 @@ void CDiscordProto::ParseGuildContents(CDiscordGuild *pGuild, const JSONNode &pR
AddGuildUser(pGuild, *pm);
// retrieve missing histories
- for (auto &it : arUsers) {
- if (it->bIsPrivate || it->guildId != pGuild->id)
+ for (auto &it : pGuild->arChannels) {
+ if (it->bIsPrivate)
continue;
SnowFlake oldMsgId = getId(it->hContact, DB_KEY_LASTMSGID);
diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h
index 7f31402a2a..debd6db7e7 100644
--- a/protocols/Discord/src/proto.h
+++ b/protocols/Discord/src/proto.h
@@ -79,16 +79,18 @@ struct CDiscordUser : public MZeroedObject
id(_id)
{}
+ ~CDiscordUser();
+
SnowFlake id;
MCONTACT hContact;
- SnowFlake guildId;
SnowFlake channelId;
SnowFlake lastReadId;
SnowFlake parentId;
bool bIsPrivate;
bool bIsGroup;
+ struct CDiscordGuild *pGuild;
CDiscordMessage lastMsg;
CMStringW wszUsername, wszChannelName, wszTopic;
@@ -125,6 +127,7 @@ struct CDiscordGuild : public MZeroedObject
CMStringW wszName;
MCONTACT hContact;
MGROUP groupId;
+ LIST<CDiscordUser> arChannels;
GCSessionInfoBase *pParentSi;
OBJLIST<CDiscordGuildMember> arChatUsers;