summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2020-12-27 19:49:39 +0300
committerGeorge Hazan <ghazan@miranda.im>2020-12-27 19:49:39 +0300
commiteae122c522acf00d2ee3d85988c811794a3b25fc (patch)
tree03b4015638b77bd9d13fcb2e3ef2dacb82586ecf
parent0a5de30626a30971cb99387a0624dc19c53f528a (diff)
unified guild members processing
-rw-r--r--protocols/Discord/src/dispatch.cpp14
-rw-r--r--protocols/Discord/src/guilds.cpp60
-rw-r--r--protocols/Discord/src/proto.h5
3 files changed, 44 insertions, 35 deletions
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp
index 1e1704eb4a..4ba7a1f7b2 100644
--- a/protocols/Discord/src/dispatch.cpp
+++ b/protocols/Discord/src/dispatch.cpp
@@ -217,25 +217,17 @@ void CDiscordProto::OnCommandGuildMemberListUpdate(const JSONNode &pRoot)
if (!mir_strcmp(item .name(), "member")) {
bool bNew = false;
- CMStringW wszUserId = item["user"]["id"].as_mstring();
- SnowFlake userId = _wtoi64(wszUserId);
- CDiscordGuildMember *pm = pGuild->FindUser(userId);
- if (pm == nullptr) {
- pm = new CDiscordGuildMember(userId);
- pGuild->arChatUsers.insert(pm);
- pm->wszNick = item["user"]["username"].as_mstring() + L"#" + item["user"]["discriminator"].as_mstring();
- bNew = true;
- }
-
+ auto *pm = ProcessGuildUser(pGuild, item, &bNew);
pm->iStatus = iStatus;
if (bNew)
AddGuildUser(pGuild, *pm);
else if (iStatus) {
+ CMStringW wszUserId(FORMAT, L"%lld", pm->userId);
+
GCEVENT gce = { m_szModuleName, 0, GC_EVENT_SETCONTACTSTATUS };
gce.time = time(0);
gce.pszUID.w = wszUserId;
- Chat_Event(&gce);
for (auto &cc : pGuild->arChannels) {
if (!cc->bIsGroup)
diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp
index 71ebd098b4..eeb03c40f3 100644
--- a/protocols/Discord/src/guilds.cpp
+++ b/protocols/Discord/src/guilds.cpp
@@ -189,29 +189,12 @@ void CDiscordProto::ProcessGuild(const JSONNode &pRoot)
// store all guild members
for (auto &it : pRoot["members"]) {
- CMStringW wszUserId = it["user"]["id"].as_mstring();
- SnowFlake userId = _wtoi64(wszUserId);
- CDiscordGuildMember *pm = pGuild->FindUser(userId);
- if (pm == nullptr) {
- pm = new CDiscordGuildMember(userId);
- pGuild->arChatUsers.insert(pm);
- }
+ auto *pm = ProcessGuildUser(pGuild, it);
+
+ CMStringW wszNick = it["nick"].as_mstring();
+ if (!wszNick.IsEmpty())
+ pm->wszNick = wszNick;
- pm->wszNick = it["nick"].as_mstring();
- if (pm->wszNick.IsEmpty())
- 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;
- for (auto &itr : it["roles"]) {
- SnowFlake roleId = ::getId(itr);
- if (pRole = pGuild->arRoles.find((CDiscordRole *)&roleId))
- break;
- }
- pm->wszRole = (pRole == nullptr) ? L"@everyone" : pRole->wszName;
- }
pm->iStatus = ID_STATUS_OFFLINE;
}
@@ -292,6 +275,39 @@ CDiscordUser* CDiscordProto::ProcessGuildChannel(CDiscordGuild *pGuild, const JS
/////////////////////////////////////////////////////////////////////////////////////////
+CDiscordGuildMember* CDiscordProto::ProcessGuildUser(CDiscordGuild *pGuild, const JSONNode &pUser, bool *pbNew)
+{
+ bool bNew = false;
+ CMStringW wszUserId = pUser["user"]["id"].as_mstring();
+ SnowFlake userId = _wtoi64(wszUserId);
+ CDiscordGuildMember *pm = pGuild->FindUser(userId);
+ if (pm == nullptr) {
+ pm = new CDiscordGuildMember(userId);
+ pGuild->arChatUsers.insert(pm);
+ bNew = true;
+ }
+
+ pm->wszNick = pUser["user"]["username"].as_mstring() + L"#" + pUser["user"]["discriminator"].as_mstring();
+
+ if (userId == pGuild->ownerId)
+ pm->wszRole = L"@owner";
+ else {
+ CDiscordRole *pRole = nullptr;
+ for (auto &itr : pUser["roles"]) {
+ SnowFlake roleId = ::getId(itr);
+ if (pRole = pGuild->arRoles.find((CDiscordRole *)&roleId))
+ break;
+ }
+ pm->wszRole = (pRole == nullptr) ? L"@everyone" : pRole->wszName;
+ }
+
+ if (pbNew)
+ *pbNew = bNew;
+ return pm;
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
void CDiscordProto::AddGuildUser(CDiscordGuild *pGuild, const CDiscordGuildMember &pUser)
{
int flags = 0;
diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h
index 5943e01500..dbc45b2512 100644
--- a/protocols/Discord/src/proto.h
+++ b/protocols/Discord/src/proto.h
@@ -300,13 +300,14 @@ class CDiscordProto : public PROTO<CDiscordProto>
}
void AddGuildUser(CDiscordGuild *guild, const CDiscordGuildMember &pUser);
-
void ProcessGuild(const JSONNode &json);
- CDiscordUser* ProcessGuildChannel(CDiscordGuild *guild, const JSONNode &json);
void ProcessPresence(const JSONNode &json);
void ProcessRole(CDiscordGuild *guild, const JSONNode &json);
void ProcessType(CDiscordUser *pUser, const JSONNode &json);
+ CDiscordUser* ProcessGuildChannel(CDiscordGuild *guild, const JSONNode &json);
+ CDiscordGuildMember* ProcessGuildUser(CDiscordGuild *pGuild, const JSONNode &json, bool *bNew = nullptr);
+
//////////////////////////////////////////////////////////////////////////////////////
// group chats