summaryrefslogtreecommitdiff
path: root/protocols/Discord/src/guilds.cpp
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2020-02-11 11:38:25 +0300
committerGeorge Hazan <ghazan@miranda.im>2020-02-11 11:38:25 +0300
commite461037d7941988ec2d6f83742dd10eb375a1578 (patch)
tree8dab240c6a3068a5ad00b7f458ba80a08f82ed02 /protocols/Discord/src/guilds.cpp
parent968c4a1f08396b1734114e589f6abd54abe2c43d (diff)
Discord:
- gateway command 12 isn't available since Jan 13th, 2020; - GUILD_SYNC is also not available anymore; - better cookie control; - version bump
Diffstat (limited to 'protocols/Discord/src/guilds.cpp')
-rw-r--r--protocols/Discord/src/guilds.cpp55
1 files changed, 53 insertions, 2 deletions
diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp
index c19237c7e4..d517e9af0e 100644
--- a/protocols/Discord/src/guilds.cpp
+++ b/protocols/Discord/src/guilds.cpp
@@ -120,7 +120,7 @@ void CDiscordProto::CreateChat(CDiscordGuild *pGuild, CDiscordUser *pUser)
}
}
-void CDiscordProto::ProcessGuild(const JSONNode &p)
+CDiscordGuild* CDiscordProto::ProcessGuild(const JSONNode &p)
{
SnowFlake guildId = ::getId(p["id"]);
@@ -141,7 +141,7 @@ void CDiscordProto::ProcessGuild(const JSONNode &p)
setId(pGuild->hContact, DB_KEY_CHANNELID, guildId);
if (!pGuild->bSynced && getByte(si->hContact, "EnableSync"))
- GatewaySendGuildInfo(guildId);
+ LoadGuildInfo(pGuild);
Chat_Control(m_szModuleName, pGuild->wszName, WINDOW_HIDDEN);
Chat_Control(m_szModuleName, pGuild->wszName, SESSION_ONLINE);
@@ -159,6 +159,8 @@ void CDiscordProto::ProcessGuild(const JSONNode &p)
if (m_bUseGroupchats)
ForkThread(&CDiscordProto::BatchChatCreate, pGuild);
+
+ return pGuild;
}
/////////////////////////////////////////////////////////////////////////////////////////
@@ -255,6 +257,55 @@ void CDiscordProto::AddGuildUser(CDiscordGuild *pGuild, const CDiscordGuildMembe
/////////////////////////////////////////////////////////////////////////////////////////
+static CMStringW GetCacheFileName(SnowFlake guildId)
+{
+ VARSW wszCacheDir(L"%miranda_userdata%\\Discord");
+ CreateDirectoryTreeW(wszCacheDir);
+
+ return CMStringW(FORMAT, L"%s\\%lld.cache", wszCacheDir.get(), guildId);
+}
+
+void CDiscordProto::LoadGuildInfo(CDiscordGuild *pGuild)
+{
+ CMStringW wszCacheFile(GetCacheFileName(pGuild->id));
+ int fileId = _wopen(wszCacheFile, _O_BINARY | _O_RDONLY);
+ if (fileId != -1) {
+ size_t length = _filelength(fileId);
+ ptrA buf((char *)mir_alloc(length+1));
+ _read(fileId, buf, (unsigned)length);
+ _close(fileId);
+
+ JSONNode root(JSONNode::parse(buf));
+ for (auto &cc : root) {
+ auto *pUser = new CDiscordGuildMember(_wtoi64(cc["id"].as_mstring()));
+ pUser->wszNick = cc["nick"].as_mstring();
+ pUser->wszRole = cc["role"].as_mstring();
+ pGuild->arChatUsers.insert(pUser);
+
+ AddGuildUser(pGuild, *pUser);
+ }
+ }
+ else {
+ CMStringA szUrl(FORMAT, "/guilds/%lld/members", pGuild->id);
+ auto *pReq = new AsyncHttpRequest(this, REQUEST_GET, szUrl, &CDiscordProto::OnReceiveGuildInfo);
+ pReq << INT_PARAM("limit", 1000);
+ pReq->pUserInfo = pGuild;
+ // Push(pReq);
+ }
+}
+
+void CDiscordProto::OnReceiveGuildInfo(NETLIBHTTPREQUEST *pReply, AsyncHttpRequest *pReq)
+{
+ if (pReply->resultCode != 200)
+ return;
+
+ JSONNode root = JSONNode::parse(pReply->pData);
+ if (root)
+ ParseGuildContents((CDiscordGuild*)pReq->pUserInfo, root);
+}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
void CDiscordProto::ParseGuildContents(CDiscordGuild *pGuild, const JSONNode &pRoot)
{
LIST<CDiscordGuildMember> newMembers(100);