summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <ghazan@miranda.im>2021-04-11 19:05:56 +0300
committerGeorge Hazan <ghazan@miranda.im>2021-04-11 19:05:56 +0300
commit7af11cc0f8e7b23891e24fad89cbd53e7d9e4f8f (patch)
treee030b1d69fbfc491bdc3225aa9aef05ebc9fe052
parentdafd9a18a4b1573ff536e3db851e9e40a345c201 (diff)
Discord: added guild contacts cache
-rw-r--r--protocols/Discord/src/guilds.cpp47
-rw-r--r--protocols/Discord/src/proto.cpp3
-rw-r--r--protocols/Discord/src/proto.h10
3 files changed, 59 insertions, 1 deletions
diff --git a/protocols/Discord/src/guilds.cpp b/protocols/Discord/src/guilds.cpp
index 356fada12b..83922575f9 100644
--- a/protocols/Discord/src/guilds.cpp
+++ b/protocols/Discord/src/guilds.cpp
@@ -160,6 +160,7 @@ void CDiscordProto::ProcessGuild(const JSONNode &pRoot)
CDiscordGuild *pGuild = FindGuild(guildId);
if (pGuild == nullptr) {
pGuild = new CDiscordGuild(guildId);
+ pGuild->LoadFromFile();
arGuilds.insert(pGuild);
}
@@ -327,3 +328,49 @@ void CDiscordProto::AddGuildUser(CDiscordGuild *pGuild, const CDiscordGuildMembe
if (pUser.userId == m_ownId)
pGuild->pParentSi->pMe = pu;
}
+
+/////////////////////////////////////////////////////////////////////////////////////////
+
+void CDiscordGuild ::LoadFromFile()
+{
+ int fileNo = _wopen(GetCacheFile(), O_TEXT | O_RDONLY);
+ if (fileNo == -1)
+ return;
+
+ int fSize = ::filelength(fileNo);
+ ptrA json((char*)mir_alloc(fSize + 1));
+ read(fileNo, json, fSize);
+ close(fileNo);
+
+ JSONNode cached = JSONNode::parse(json);
+ for (auto &it : cached) {
+ SnowFlake userId = getId(it["id"]);
+ auto *pUser = FindUser(userId);
+ if (pUser == nullptr) {
+ pUser = new CDiscordGuildMember(userId);
+ arChatUsers.insert(pUser);
+ }
+
+ pUser->wszNick = it["n"].as_mstring();
+ pUser->wszRole = it["r"].as_mstring();
+ }
+}
+
+void CDiscordGuild ::SaveToFile()
+{
+ JSONNode members(JSON_ARRAY);
+ for (auto &it : arChatUsers) {
+ JSONNode member;
+ member << INT64_PARAM("id", it->userId) << WCHAR_PARAM("n", it->wszNick) << WCHAR_PARAM("r", it->wszRole);
+ members << member;
+ }
+
+ CMStringW wszFileName(GetCacheFile());
+ CreatePathToFileW(wszFileName);
+ int fileNo = _wopen(wszFileName, O_CREAT | O_TRUNC | O_TEXT | O_WRONLY);
+ if (fileNo != -1) {
+ std::string json = members.write_formatted();
+ write(fileNo, json.c_str(), (int)json.size());
+ close(fileNo);
+ }
+}
diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp
index cbcb5dca5c..63a8170b6a 100644
--- a/protocols/Discord/src/proto.cpp
+++ b/protocols/Discord/src/proto.cpp
@@ -198,6 +198,9 @@ void CDiscordProto::OnShutdown()
m_bTerminated = true;
SetEvent(m_evRequestsQueue);
+ for (auto &it : arGuilds)
+ it->SaveToFile();
+
if (m_hGatewayConnection)
Netlib_Shutdown(m_hGatewayConnection);
diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h
index 4b023fde05..d9013c261e 100644
--- a/protocols/Discord/src/proto.h
+++ b/protocols/Discord/src/proto.h
@@ -114,7 +114,12 @@ struct CDiscordGuild : public MZeroedObject
__forceinline CDiscordGuildMember* FindUser(SnowFlake userId)
{
- return arChatUsers.find((CDiscordGuildMember*)&userId);
+ return arChatUsers.find((CDiscordGuildMember *)&userId);
+ }
+
+ __inline CMStringW GetCacheFile() const
+ {
+ return CMStringW(FORMAT, L"%s\\DiscordCache\\%lld.json", VARSW(L"%miranda_userdata%").get(), id);
}
SnowFlake id, ownerId;
@@ -127,6 +132,9 @@ struct CDiscordGuild : public MZeroedObject
SESSION_INFO *pParentSi;
OBJLIST<CDiscordGuildMember> arChatUsers;
OBJLIST<CDiscordRole> arRoles; // guild roles
+
+ void LoadFromFile();
+ void SaveToFile();
};
struct CDiscordVoiceCall