diff options
author | George Hazan <ghazan@miranda.im> | 2018-10-26 22:23:42 +0300 |
---|---|---|
committer | George Hazan <ghazan@miranda.im> | 2018-10-26 22:23:42 +0300 |
commit | 7ef2e7c8737905941593fd73018070934b47116b (patch) | |
tree | 29141b450529109f44cf7a7fb594f3b78b75f2eb | |
parent | f17907dd5c586022660dd27fcd6397dd22b2e365 (diff) |
Discord: delayed chat creation + channel groups support
-rw-r--r-- | protocols/Discord/src/dispatch.cpp | 7 | ||||
-rw-r--r-- | protocols/Discord/src/guilds.cpp | 122 | ||||
-rw-r--r-- | protocols/Discord/src/proto.h | 7 | ||||
-rw-r--r-- | protocols/Discord/src/version.h | 2 |
4 files changed, 97 insertions, 41 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index f10114c884..bca40644a2 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -90,8 +90,11 @@ void CDiscordProto::OnCommandChannelCreated(const JSONNode &pRoot) else { // group channel for a guild CDiscordGuild *pGuild = FindGuild(guildId); - if (pGuild) - ProcessGuildChannel(pGuild, pRoot); + if (pGuild) { + CDiscordUser *pUser = ProcessGuildChannel(pGuild, pRoot); + if (pUser) + CreateChat(pGuild, pUser); + } } } diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp index 5434ce4592..751ecf54da 100644 --- a/protocols/Discord/src/guilds.cpp +++ b/protocols/Discord/src/guilds.cpp @@ -59,6 +59,50 @@ void CDiscordProto::ProcessRole(CDiscordGuild *guild, const JSONNode &role) ///////////////////////////////////////////////////////////////////////////////////////// +static void sttSetGroupName(MCONTACT hContact, const wchar_t *pwszGroupName) +{ + ptrW wszOldName(db_get_wsa(hContact, "CList", "Group")); + if (wszOldName == nullptr) + db_set_ws(hContact, "CList", "Group", pwszGroupName); +} + +void CDiscordProto::BatchChatCreate(void *param) +{ + CDiscordGuild *pGuild = (CDiscordGuild*)param; + + for (auto &it : arUsers) + if (it->guildId == pGuild->id && !it->bIsPrivate) + CreateChat(pGuild, it); +} + +void CDiscordProto::CreateChat(CDiscordGuild *pGuild, CDiscordUser *pUser) +{ + GCSessionInfoBase *si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, pUser->wszUsername, pUser->wszChannelName); + si->pParent = pGuild->pParentSi; + pUser->hContact = si->hContact; + + if (pUser->parentId) { + CDiscordUser *pParent = FindUserByChannel(pUser->parentId); + if (pParent != nullptr) + sttSetGroupName(pUser->hContact, pParent->wszChannelName); + } + else sttSetGroupName(pUser->hContact, Clist_GroupGetName(pGuild->groupId)); + + BuildStatusList(pGuild, pUser->wszUsername); + + Chat_Control(m_szModuleName, pUser->wszUsername, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE); + Chat_Control(m_szModuleName, pUser->wszUsername, SESSION_ONLINE); + + if (!pUser->wszTopic.IsEmpty()) { + Chat_SetStatusbarText(m_szModuleName, pUser->wszUsername, pUser->wszTopic); + + GCEVENT gce = { m_szModuleName, pUser->wszUsername, GC_EVENT_TOPIC }; + gce.time = time(0); + gce.ptszText = pUser->wszTopic; + Chat_Event(&gce); + } +} + void CDiscordProto::ProcessGuild(const JSONNode &p) { SnowFlake guildId = ::getId(p["id"]); @@ -72,6 +116,7 @@ void CDiscordProto::ProcessGuild(const JSONNode &p) } pGuild->ownerId = ::getId(p["owner_id"]); pGuild->wszName = p["name"].as_mstring(); + pGuild->groupId = Clist_GroupCreate(Clist_GroupExists(m_wszDefaultGroup), pGuild->wszName); GCSessionInfoBase *si = Chat_NewSession(GCW_SERVER, m_szModuleName, pGuild->wszName, pGuild->wszName, pGuild); Chat_Control(m_szModuleName, pGuild->wszName, WINDOW_HIDDEN); @@ -92,6 +137,8 @@ void CDiscordProto::ProcessGuild(const JSONNode &p) const JSONNode &channels = p["channels"]; for (auto itc = channels.begin(); itc != channels.end(); ++itc) ProcessGuildChannel(pGuild, *itc); + + ForkThread(&CDiscordProto::BatchChatCreate, pGuild); } ///////////////////////////////////////////////////////////////////////////////////////// @@ -100,47 +147,48 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS { CMStringW wszChannelId = pch["id"].as_mstring(); SnowFlake channelId = _wtoi64(wszChannelId); - CMStringW wszChannelName = pGuild->wszName + L"#" + pch["name"].as_mstring(); + CMStringW wszName = pch["name"].as_mstring(); + CDiscordUser *pUser; // filter our all channels but the text ones - if (pch["type"].as_int() != 0) - return nullptr; - - CMStringW wszTopic = pch["topic"].as_mstring(); - - GCSessionInfoBase *si = Chat_NewSession(GCW_CHATROOM, m_szModuleName, wszChannelId, wszChannelName); - si->pParent = pGuild->pParentSi; - BuildStatusList(pGuild, wszChannelId); - - Chat_Control(m_szModuleName, wszChannelId, m_bHideGroupchats ? WINDOW_HIDDEN : SESSION_INITDONE); - Chat_Control(m_szModuleName, wszChannelId, SESSION_ONLINE); - - if (!wszTopic.IsEmpty()) { - Chat_SetStatusbarText(m_szModuleName, wszChannelId, wszTopic); - - GCEVENT gce = { m_szModuleName, wszChannelId, GC_EVENT_TOPIC }; - gce.time = time(0); - gce.ptszText = wszTopic; - Chat_Event(&gce); - } - - CDiscordUser *pUser = FindUserByChannel(channelId); - if (pUser == nullptr) { - // missing channel - create it - pUser = new CDiscordUser(channelId); - pUser->bIsPrivate = false; - pUser->hContact = si->hContact; - pUser->id = channelId; - pUser->channelId = channelId; - arUsers.insert(pUser); + switch (pch["type"].as_int()) { + case 4: // channel group + pUser = FindUserByChannel(channelId); + if (pUser == nullptr) { + // missing channel - create it + pUser = new CDiscordUser(channelId); + pUser->bIsPrivate = false; + pUser->channelId = channelId; + pUser->bIsGroup = true; + arUsers.insert(pUser); + + MGROUP grpId = Clist_GroupCreate(pGuild->groupId, wszName); + pUser->wszChannelName = Clist_GroupGetName(grpId); + } + return pUser; + + case 0: // text channel + pUser = FindUserByChannel(channelId); + if (pUser == nullptr) { + // missing channel - create it + pUser = new CDiscordUser(channelId); + pUser->bIsPrivate = false; + pUser->channelId = channelId; + arUsers.insert(pUser); + } + pUser->wszUsername = wszChannelId; + pUser->wszChannelName = L"#" + wszName; + pUser->wszTopic = pch["topic"].as_mstring(); + pUser->guildId = pGuild->id; + pUser->lastMsg = CDiscordMessage(::getId(pch["last_message_id"])); + pUser->parentId = _wtoi64(pch["parent_id"].as_mstring()); + + setId(pUser->hContact, DB_KEY_ID, channelId); + setId(pUser->hContact, DB_KEY_CHANNELID, channelId); + return pUser; } - pUser->wszUsername = wszChannelId; - pUser->guildId = pGuild->id; - pUser->lastMsg = CDiscordMessage(::getId(pch["last_message_id"])); - setId(pUser->hContact, DB_KEY_ID, channelId); - setId(pUser->hContact, DB_KEY_CHANNELID, channelId); - return pUser; + return nullptr; } ///////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 4f99ac0735..7f31402a2a 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -85,11 +85,13 @@ struct CDiscordUser : public MZeroedObject SnowFlake guildId; SnowFlake channelId; SnowFlake lastReadId; + SnowFlake parentId; bool bIsPrivate; + bool bIsGroup; CDiscordMessage lastMsg; - CMStringW wszUsername; + CMStringW wszUsername, wszChannelName, wszTopic; int iDiscriminator; }; @@ -122,6 +124,7 @@ struct CDiscordGuild : public MZeroedObject SnowFlake id, ownerId; CMStringW wszName; MCONTACT hContact; + MGROUP groupId; GCSessionInfoBase *pParentSi; OBJLIST<CDiscordGuildMember> arChatUsers; @@ -142,6 +145,7 @@ class CDiscordProto : public PROTO<CDiscordProto> void __cdecl ServerThread(void*); void __cdecl SearchThread(void *param); void __cdecl SendMessageAckThread(void* param); + void __cdecl BatchChatCreate(void* param); ////////////////////////////////////////////////////////////////////////////////////// // session control @@ -255,6 +259,7 @@ class CDiscordProto : public PROTO<CDiscordProto> void Chat_ProcessLogMenu(GCHOOK *gch); void BuildStatusList(const CDiscordGuild *pGuild, const CMStringW &wszChannelId); + void CreateChat(CDiscordGuild *pGuild, CDiscordUser *pUser); void ParseSpecialChars(SESSION_INFO *si, CMStringW &str); ////////////////////////////////////////////////////////////////////////////////////// diff --git a/protocols/Discord/src/version.h b/protocols/Discord/src/version.h index 80d6ffba71..56d742b312 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 0 -#define __BUILD_NUM 2 +#define __BUILD_NUM 3 #include <stdver.h> |