From 1ba1e51fc6fa7e30b0a627a141348f515ffaba76 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 13 Oct 2018 21:29:05 +0300 Subject: chat api: - GC_SHAREDUSERS flag added to share the same array of users for all group chats; - USERINFO.next removed; - MODULEINFO.arUsers & SESSION_INFO.arUsers introduced to maintain user lists; - MM_AddModule removed; - memory allocation model changed for MODULEINFO & SESSION_INFO - MM_CreateModule & SM_CreateSession members are added to g_chatApi --- protocols/Discord/src/dispatch.cpp | 17 ++++--------- protocols/Discord/src/guilds.cpp | 49 ++++++++++++++++++++------------------ protocols/Discord/src/proto.cpp | 2 +- protocols/Discord/src/proto.h | 3 ++- protocols/Discord/src/utils.cpp | 2 +- 5 files changed, 35 insertions(+), 38 deletions(-) (limited to 'protocols/Discord/src') diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index dbb30f382c..fa301ee37c 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -88,15 +88,10 @@ void CDiscordProto::OnCommandChannelCreated(const JSONNode &pRoot) } } else { - CDiscordGuild *pGuild = FindGuild(guildId); - if (pGuild == nullptr) - return; - // group channel for a guild - CDiscordUser *pUser = ProcessGuildChannel(pGuild, pRoot); - if (pUser != nullptr) - for (auto &it : pGuild->arChatUsers) - AddUserToChannel(*pUser, *it); + CDiscordGuild *pGuild = FindGuild(guildId); + if (pGuild) + ProcessGuildChannel(pGuild, pRoot); } } @@ -254,7 +249,7 @@ void CDiscordProto::OnCommandGuildMemberUpdated(const JSONNode &pRoot) CMStringW wszOldNick; SESSION_INFO *si = g_chatApi.SM_FindSession(it->wszUsername, m_szModuleName); if (si != nullptr) { - USERINFO *ui = g_chatApi.UM_FindUser(si->pUsers, wszUserId); + USERINFO *ui = g_chatApi.UM_FindUser(si, wszUserId); if (ui != nullptr) wszOldNick = ui->pszNick; } @@ -382,9 +377,7 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot) pm->wszNick = pRoot["user"]["username"].as_mstring() + L"#" + pRoot["user"]["discriminator"].as_mstring(); pGuild->arChatUsers.insert(pm); - for (auto &it : arUsers) - if (it->guildId == pGuild->id) - AddUserToChannel(*it, *pm); + AddGuildUser(pGuild->id, *pm); } ParseSpecialChars(si, wszText); diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp index 0bb61ed52a..1d0a137fea 100644 --- a/protocols/Discord/src/guilds.cpp +++ b/protocols/Discord/src/guilds.cpp @@ -98,9 +98,8 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS CMStringW wszChannelName = pGuild->wszName + L"#" + pch["name"].as_mstring(); // filter our all channels but the text ones - if (pch["type"].as_int() != 0) { + if (pch["type"].as_int() != 0) return nullptr; - } CMStringW wszTopic = pch["topic"].as_mstring(); @@ -139,18 +138,34 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS SnowFlake oldMsgId = getId(pUser->hContact, DB_KEY_LASTMSGID); if (oldMsgId != 0 && pUser->lastMsg.id > oldMsgId) RetrieveHistory(pUser->hContact, MSG_AFTER, oldMsgId, 99); - - for (auto &it : pGuild->arChatUsers) - AddUserToChannel(*pUser, *it); - return pUser; } ///////////////////////////////////////////////////////////////////////////////////////// -void CDiscordProto::AddUserToChannel(const CDiscordUser &pChannel, const CDiscordGuildMember &pUser) +void CDiscordProto::AddGuildUser(SnowFlake guildId, const CDiscordGuildMember &pUser) { - GCEVENT gce = { m_szModuleName, pChannel.wszUsername, GC_EVENT_JOIN }; + const CDiscordUser *pChannel = nullptr; + for (auto &it : arUsers) + if (it->guildId == guildId) { + pChannel = it; + break; + } + + if (pChannel == nullptr) + return; + + int flags = GC_SSE_ONLYLISTED; + switch (pUser.iStatus) { + case ID_STATUS_ONLINE: case ID_STATUS_NA: case ID_STATUS_DND: + flags += GC_SSE_ONLINE; + break; + + default: + return; + } + + GCEVENT gce = { m_szModuleName, pChannel->wszUsername, GC_EVENT_JOIN }; gce.time = time(0); gce.dwFlags = GCEF_SILENT; @@ -163,15 +178,7 @@ void CDiscordProto::AddUserToChannel(const CDiscordUser &pChannel, const CDiscor gce.ptszNick = pUser.wszNick; Chat_Event(&gce); - int flags = GC_SSE_ONLYLISTED; - switch (pUser.iStatus) { - case ID_STATUS_ONLINE: case ID_STATUS_NA: case ID_STATUS_DND: - flags += GC_SSE_ONLINE; - break; - default: - flags += GC_SSE_OFFLINE; - } - Chat_SetStatusEx(m_szModuleName, pChannel.wszUsername, flags, wszUserId); + Chat_SetStatusEx(m_szModuleName, pChannel->wszUsername, flags, wszUserId); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -222,10 +229,6 @@ void CDiscordProto::ParseGuildContents(CDiscordGuild *pGuild, const JSONNode &pR gm->iStatus = StrToStatus(s["status"].as_mstring()); } - for (auto &pm : newMembers) { - for (auto &pUser : arUsers) { - if (pUser->guildId == pGuild->id) - AddUserToChannel(*pUser, *pm); - } - } + for (auto &pm : newMembers) + AddGuildUser(pGuild->id, *pm); } diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index 574bd2be53..f6a91aca67 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -108,7 +108,7 @@ void CDiscordProto::OnModulesLoaded() } GCREGISTER gcr = {}; - gcr.dwFlags = GC_TYPNOTIF | GC_CHANMGR; + gcr.dwFlags = GC_TYPNOTIF | GC_CHANMGR | GC_SHAREDUSERS; gcr.ptszDispName = m_tszUserName; gcr.pszModule = m_szModuleName; Chat_Register(&gcr); diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 7874a8ffb6..5e6d537b24 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -196,6 +196,7 @@ class CDiscordProto : public PROTO OBJLIST arUsers; OBJLIST arOwnMessages; + CDiscordUser* FindUser(SnowFlake id); CDiscordUser* FindUser(const wchar_t *pwszUsername, int iDiscriminator); CDiscordUser* FindUserByChannel(SnowFlake channelId); @@ -226,7 +227,7 @@ class CDiscordProto : public PROTO } void ProcessGuild(const JSONNode&); - void AddUserToChannel(const CDiscordUser &pChannel, const CDiscordGuildMember &pUser); + void AddGuildUser(SnowFlake guildId, const CDiscordGuildMember &pUser); void ParseGuildContents(CDiscordGuild *guild, const JSONNode &); CDiscordUser* ProcessGuildChannel(CDiscordGuild *guild, const JSONNode&); void ProcessRole(CDiscordGuild *guild, const JSONNode&); diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp index e1867992db..d4e35b4090 100644 --- a/protocols/Discord/src/utils.cpp +++ b/protocols/Discord/src/utils.cpp @@ -265,7 +265,7 @@ void CDiscordProto::ParseSpecialChars(SESSION_INFO *si, CMStringW &str) if (wszWord[1] == '!') iStart++; - USERINFO *ui = g_chatApi.UM_FindUser(si->pUsers, wszWord.c_str() + iStart); + USERINFO *ui = g_chatApi.UM_FindUser(si, wszWord.c_str() + iStart); if (ui != nullptr) str.Replace(L"<" + wszWord + L">", CMStringW('@') + ui->pszNick); } -- cgit v1.2.3