From adc82c1bfa455a4f5482a7937e5bb6e4cf2a7ff2 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sat, 11 May 2024 16:50:58 +0300 Subject: fixes #724 ([Discord] Trying to add a new contact from group chat doesn't work) --- protocols/Discord/src/groupchat.cpp | 11 ++++++++++- protocols/Discord/src/proto.cpp | 8 ++++---- protocols/Discord/src/proto.h | 3 ++- protocols/Discord/src/server.cpp | 7 ++++++- 4 files changed, 22 insertions(+), 7 deletions(-) (limited to 'protocols/Discord/src') diff --git a/protocols/Discord/src/groupchat.cpp b/protocols/Discord/src/groupchat.cpp index 4a9d5bb457..60d9a52329 100644 --- a/protocols/Discord/src/groupchat.cpp +++ b/protocols/Discord/src/groupchat.cpp @@ -24,7 +24,7 @@ enum { IDM_CHANGENICK, IDM_CHANGETOPIC, IDM_RENAME, IDM_PASSOWNER, IDM_DESTROY, IDM_LEAVE, - IDM_KICK, IDM_INVITE + IDM_KICK, IDM_INVITE, IDM_ADD }; static void sttDisableMenuItem(int nItems, gc_item *items, uint32_t id, bool disabled) @@ -77,6 +77,7 @@ static gc_item sttLogListItems[] = static gc_item sttNicklistItems[] = { { LPGENW("Copy ID"), IDM_COPY_ID, MENU_ITEM }, + { LPGENW("Add friend"), IDM_ADD, MENU_ITEM }, { nullptr, 0, MENU_SEPARATOR }, { LPGENW("Kick user"), IDM_KICK, MENU_ITEM }, { LPGENW("Make group owner"), IDM_PASSOWNER, MENU_ITEM }, @@ -107,6 +108,10 @@ int CDiscordProto::GroupchatMenuHook(WPARAM, LPARAM lParam) Chat_AddMenuItems(gcmi->hMenu, _countof(sttLogListItems), sttLogListItems, &g_plugin); } else if (gcmi->Type == MENU_ON_NICKLIST) { + SnowFlake userId = (gcmi->pszUID) ? _wtoi64(gcmi->pszUID) : 0; + bool isFriend = (userId == m_ownId) ? true : FindUser(userId) != 0; + + sttDisableMenuItem(_countof(sttNicklistItems), sttNicklistItems, IDM_ADD, isFriend); sttDisableMenuItem(_countof(sttNicklistItems), sttNicklistItems, IDM_KICK, !isOwner); sttDisableMenuItem(_countof(sttNicklistItems), sttNicklistItems, IDM_PASSOWNER, !isOwner); @@ -315,6 +320,10 @@ void CDiscordProto::Chat_ProcessNickMenu(GCHOOK* gch) CopyId(gch->ptszUID); break; + case IDM_ADD: + AddFriend(_wtoi64(gch->ptszUID)); + break; + case IDM_KICK: KickChatUser(pChannel, gch->ptszUID); break; diff --git a/protocols/Discord/src/proto.cpp b/protocols/Discord/src/proto.cpp index 48141312f5..b05a46d330 100644 --- a/protocols/Discord/src/proto.cpp +++ b/protocols/Discord/src/proto.cpp @@ -280,7 +280,9 @@ int CDiscordProto::AuthRequest(MCONTACT hContact, const wchar_t *) if (wszUsername == nullptr || iDiscriminator == -1) return 1; // error - JSONNode root; root << WCHAR_PARAM("username", wszUsername) << INT_PARAM("discriminator", iDiscriminator); + JSONNode root; root << WCHAR_PARAM("username", wszUsername); + if (iDiscriminator ) + root << INT_PARAM("discriminator", iDiscriminator); Push(new AsyncHttpRequest(this, REQUEST_POST, "/users/@me/relationships", nullptr, &root)); return 0; } @@ -298,10 +300,8 @@ int CDiscordProto::Authorize(MEVENT hDbEvent) if (dbei.eventType != EVENTTYPE_AUTHREQUEST) return 1; if (mir_strcmp(dbei.szModule, m_szModuleName)) return 1; - JSONNode root; MCONTACT hContact = DbGetAuthEventContact(&dbei); - CMStringA szUrl(FORMAT, "/users/@me/relationships/%lld", getId(hContact, DB_KEY_ID)); - Push(new AsyncHttpRequest(this, REQUEST_PUT, szUrl, nullptr, &root)); + AddFriend(getId(hContact, DB_KEY_ID)); return 0; } diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 53eac9d78d..f48f2db66f 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -543,11 +543,12 @@ public: void SendMarkRead(void); void SetServerStatus(int iStatus); + + void AddFriend(SnowFlake id); void RemoveFriend(SnowFlake id); CMStringW GetAvatarFilename(MCONTACT hContact); void CheckAvatarChange(MCONTACT hContact, const CMStringW &wszNewHash); - void CheckChannelAvatar(MCONTACT hContact, const CMStringW &wszNewHash); }; typedef CProtoDlgBase CDiscordDlgBase; diff --git a/protocols/Discord/src/server.cpp b/protocols/Discord/src/server.cpp index 0b48dc3ab9..c60e6c23ed 100644 --- a/protocols/Discord/src/server.cpp +++ b/protocols/Discord/src/server.cpp @@ -18,7 +18,12 @@ along with this program. If not, see . #include "stdafx.h" ///////////////////////////////////////////////////////////////////////////////////////// -// removes a friend from the server +// add / removes a friend from the server + +void CDiscordProto::AddFriend(SnowFlake id) +{ + Push(new AsyncHttpRequest(this, REQUEST_PUT, CMStringA(FORMAT, "/users/@me/relationships/%lld", id), nullptr)); +} void CDiscordProto::RemoveFriend(SnowFlake id) { -- cgit v1.2.3