summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGeorge Hazan <george.hazan@gmail.com>2024-05-08 19:16:42 +0300
committerGeorge Hazan <george.hazan@gmail.com>2024-05-08 19:16:42 +0300
commitddbb0219762e420d1336af28aed918dc8b54dcfc (patch)
tree54d4d15499808d4b41d265a539e908fca34e9d12
parentc695f9bdd30acd3064840918f1cc12107997197c (diff)
Discord: added support for reading private channels avatars
-rw-r--r--protocols/Discord/src/avatars.cpp22
-rw-r--r--protocols/Discord/src/dispatch.cpp25
-rw-r--r--protocols/Discord/src/proto.h2
-rw-r--r--protocols/Discord/src/server.cpp1
-rw-r--r--protocols/Discord/src/utils.cpp2
5 files changed, 42 insertions, 10 deletions
diff --git a/protocols/Discord/src/avatars.cpp b/protocols/Discord/src/avatars.cpp
index fd2d57181c..b58e9e5453 100644
--- a/protocols/Discord/src/avatars.cpp
+++ b/protocols/Discord/src/avatars.cpp
@@ -104,6 +104,20 @@ bool CDiscordProto::RetrieveAvatar(MCONTACT hContact)
return true;
}
+bool CDiscordProto::RetrieveChannelAvatar(MCONTACT hContact)
+{
+ ptrA szAvatarHash(getStringA(hContact, DB_KEY_AVHASH));
+ SnowFlake id = getId(hContact, DB_KEY_ID);
+ if (id == 0 || szAvatarHash == nullptr)
+ return false;
+
+ CMStringA szUrl(FORMAT, "https://cdn.discordapp.com/channel-icons/%lld/%s.jpg", id, szAvatarHash.get());
+ AsyncHttpRequest *pReq = new AsyncHttpRequest(this, REQUEST_GET, szUrl, &CDiscordProto::OnReceiveAvatar);
+ pReq->pUserInfo = (void *)hContact;
+ Push(pReq);
+ return true;
+}
+
INT_PTR CDiscordProto::GetAvatarInfo(WPARAM flags, LPARAM lParam)
{
PROTO_AVATAR_INFORMATION *pai = (PROTO_AVATAR_INFORMATION *)lParam;
@@ -198,6 +212,12 @@ void CDiscordProto::CheckAvatarChange(MCONTACT hContact, const CMStringW &wszNew
// if avatar's hash changed, we need to request a new one
if (mir_wstrcmp(wszNewHash, wszOldAvatar)) {
setWString(hContact, DB_KEY_AVHASH, wszNewHash);
- RetrieveAvatar(hContact);
+
+ if (auto *pUser = FindUser(getId(hContact, DB_KEY_ID))) {
+ if (pUser->bIsGroup)
+ RetrieveChannelAvatar(hContact);
+ else
+ RetrieveAvatar(hContact);
+ }
}
}
diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp
index 9ad3d83457..f251abaa53 100644
--- a/protocols/Discord/src/dispatch.cpp
+++ b/protocols/Discord/src/dispatch.cpp
@@ -205,6 +205,8 @@ void CDiscordProto::OnCommandChannelUpdated(const JSONNode &pRoot)
// reset members info for private channels
if (pUser->pGuild == nullptr) {
+ CheckAvatarChange(pUser->hContact, pRoot["icon"].as_mstring());
+
for (auto &it : pUser->si->arUsers) {
SnowFlake userId = _wtoi64(it->pszUID);
@@ -472,7 +474,7 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew)
debugLogA("skipping own message with nonce=%lld, id=%lld", ownMsg.nonce, msgId);
}
else {
- CMStringW wszText = PrepareMessageText(pRoot), wszMentioned;
+ CMStringW wszText = PrepareMessageText(pRoot), wszMentioned, wszAuthor = getName(pRoot["author"]);
SnowFlake mentionId = 0;
for (auto &it : pRoot["mentions"]) {
@@ -482,24 +484,31 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew)
}
switch (pRoot["type"].as_int()) {
- case 4: // chat was renamed
- if (pUser->si)
- setWString(pUser->si->hContact, "Nick", wszText);
- return;
-
case 1: // user was added to chat
if (mentionId != userId)
- wszText.Format(TranslateT("%s added %s to the group"), getName(pRoot["author"]).c_str(), wszMentioned.c_str());
+ wszText.Format(TranslateT("%s added %s to the group"), wszAuthor.c_str(), wszMentioned.c_str());
else
wszText.Format(TranslateT("%s joined the group"), wszMentioned.c_str());
break;
case 2: // user was removed from chat
if (mentionId != userId)
- wszText.Format(TranslateT("%s removed %s from the group"), getName(pRoot["author"]).c_str(), wszMentioned.c_str());
+ wszText.Format(TranslateT("%s removed %s from the group"), wszAuthor.c_str(), wszMentioned.c_str());
else
wszText.Format(TranslateT("%s left the group"), wszMentioned.c_str());
break;
+
+ case 3: // call
+ break;
+
+ case 4: // chat was renamed
+ if (pUser->si)
+ setWString(pUser->si->hContact, "Nick", wszText);
+ return;
+
+ case 5: // chat icon is changed
+ wszText.Format(TranslateT("%s changed the group icon"), wszAuthor.c_str());
+ break;
}
if (wszText.IsEmpty())
diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h
index c277731f7e..48664c424d 100644
--- a/protocols/Discord/src/proto.h
+++ b/protocols/Discord/src/proto.h
@@ -530,6 +530,7 @@ public:
void OnReceiveHistory(MHttpResponse*, AsyncHttpRequest*);
bool RetrieveAvatar(MCONTACT hContact);
+ bool RetrieveChannelAvatar(MCONTACT hContact);
void OnReceiveAvatar(MHttpResponse*, AsyncHttpRequest*);
void OnSendMsg(MHttpResponse*, AsyncHttpRequest*);
@@ -543,6 +544,7 @@ public:
CMStringW GetAvatarFilename(MCONTACT hContact);
void CheckAvatarChange(MCONTACT hContact, const CMStringW &wszNewHash);
+ void CheckChannelAvatar(MCONTACT hContact, const CMStringW &wszNewHash);
};
typedef CProtoDlgBase<CDiscordProto> CDiscordDlgBase;
diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp
index 3d538dda53..c083e9b91d 100644
--- a/protocols/Discord/src/server.cpp
+++ b/protocols/Discord/src/server.cpp
@@ -119,7 +119,6 @@ void CDiscordProto::OnReceiveHistory(MHttpResponse *pReply, AsyncHttpRequest *pR
dbei.pBlob = szBody;
dbei.cbBlob = (int)mir_strlen(szBody);
- bool bSucceeded = false;
char szMsgId[100];
_i64toa_s(msgid, szMsgId, _countof(szMsgId), 10);
dbei.szId = szMsgId;
diff --git a/protocols/Discord/src/utils.cpp b/protocols/Discord/src/utils.cpp
index bb4d6af363..952e5d8261 100644
--- a/protocols/Discord/src/utils.cpp
+++ b/protocols/Discord/src/utils.cpp
@@ -233,6 +233,8 @@ void CDiscordProto::PreparePrivateChannel(const JSONNode &root)
SnowFlake ownerId = _wtoi64(root["owner_id"].as_mstring());
setId(pUser->hContact, DB_KEY_OWNERID, ownerId);
+ CheckAvatarChange(si->hContact, root["icon"].as_mstring());
+
GCEVENT gce = { si, GC_EVENT_JOIN };
for (auto &it : root["recipients"]) {
CMStringW wszId = it["id"].as_mstring();