From b7e7695a016663f8ec0f8fa57de9e2d3dfd466a2 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Sun, 12 May 2024 19:36:10 +0300 Subject: completely fixes #629 (Support for Discord) - reactions --- include/m_database.h | 3 +++ libs/win32/mir_app.lib | Bin 294360 -> 295252 bytes libs/win64/mir_app.lib | Bin 294138 -> 295056 bytes protocols/Discord/src/dispatch.cpp | 29 +++++++++++++++++++++++++++ protocols/Discord/src/proto.h | 2 ++ src/mir_app/src/db_events.cpp | 40 +++++++++++++++++++++++++++++++++++++ src/mir_app/src/mir_app.def | 2 ++ src/mir_app/src/mir_app64.def | 2 ++ 8 files changed, 78 insertions(+) diff --git a/include/m_database.h b/include/m_database.h index 169ac00eb3..c967d6700e 100644 --- a/include/m_database.h +++ b/include/m_database.h @@ -738,6 +738,9 @@ namespace DB wchar_t* getString(const char *str) const; + void addReaction(const char *emoji); + void delReaction(const char *emoji); + void flushJson(); JSONNode& setJson(); }; diff --git a/libs/win32/mir_app.lib b/libs/win32/mir_app.lib index d79165e112..f6f98e8974 100644 Binary files a/libs/win32/mir_app.lib and b/libs/win32/mir_app.lib differ diff --git a/libs/win64/mir_app.lib b/libs/win64/mir_app.lib index 3315b2119c..9f508b4622 100644 Binary files a/libs/win64/mir_app.lib and b/libs/win64/mir_app.lib differ diff --git a/protocols/Discord/src/dispatch.cpp b/protocols/Discord/src/dispatch.cpp index 09a3c3deab..8963d4fa7c 100644 --- a/protocols/Discord/src/dispatch.cpp +++ b/protocols/Discord/src/dispatch.cpp @@ -52,6 +52,8 @@ static handlers[] = // these structures must me sorted alphabetically { L"MESSAGE_ACK", &CDiscordProto::OnCommandMessageAck }, { L"MESSAGE_CREATE", &CDiscordProto::OnCommandMessageCreate }, { L"MESSAGE_DELETE", &CDiscordProto::OnCommandMessageDelete }, + { L"MESSAGE_REACTION_ADD", &CDiscordProto::OnCommandMessageAddReaction }, + { L"MESSAGE_REACTION_REMOVE", &CDiscordProto::OnCommandMessageRemoveReaction }, { L"MESSAGE_UPDATE", &CDiscordProto::OnCommandMessageUpdate }, { L"PRESENCE_UPDATE", &CDiscordProto::OnCommandPresence }, @@ -529,6 +531,33 @@ void CDiscordProto::OnCommandMessage(const JSONNode &pRoot, bool bIsNew) } } +///////////////////////////////////////////////////////////////////////////////////////// +// message reactions + +void CDiscordProto::OnCommandMessageAddReaction(const JSONNode &pRoot) +{ + std::string msgId(pRoot["message_id"].as_string()); + if (MEVENT hEvent = db_event_getById(m_szModuleName, msgId.c_str())) { + DB::EventInfo dbei(hEvent); + if (dbei) { + dbei.addReaction(pRoot["emoji"]["name"].as_string().c_str()); + db_event_edit(hEvent, &dbei); + } + } +} + +void CDiscordProto::OnCommandMessageRemoveReaction(const JSONNode &pRoot) +{ + CMStringA msgId(pRoot["message_id"].as_mstring()); + if (MEVENT hEvent = db_event_getById(m_szModuleName, msgId.c_str())) { + DB::EventInfo dbei(hEvent); + if (dbei) { + dbei.delReaction(pRoot["emoji"]["name"].as_string().c_str()); + db_event_edit(hEvent, &dbei); + } + } +} + ///////////////////////////////////////////////////////////////////////////////////////// // someone changed its status diff --git a/protocols/Discord/src/proto.h b/protocols/Discord/src/proto.h index 438c97d6e1..aaa00c0cd5 100644 --- a/protocols/Discord/src/proto.h +++ b/protocols/Discord/src/proto.h @@ -506,6 +506,8 @@ public: void OnCommandMessageCreate(const JSONNode &json); void OnCommandMessageDelete(const JSONNode &json); void OnCommandMessageUpdate(const JSONNode &json); + void OnCommandMessageAddReaction(const JSONNode &json); + void OnCommandMessageRemoveReaction(const JSONNode &json); void OnCommandMessageAck(const JSONNode &json); void OnCommandPresence(const JSONNode &json); void OnCommandReady(const JSONNode &json); diff --git a/src/mir_app/src/db_events.cpp b/src/mir_app/src/db_events.cpp index f1500bc937..502c02882f 100644 --- a/src/mir_app/src/db_events.cpp +++ b/src/mir_app/src/db_events.cpp @@ -373,6 +373,46 @@ JSONNode& DB::EventInfo::setJson() return *m_json; } +void DB::EventInfo::addReaction(const char *emoji) +{ + if (!mir_strlen(emoji)) + return; + + auto &json = setJson(); + JSONNode &reactions = json["r"]; + if (!reactions) { + reactions = JSONNode(JSON_NODE); reactions.set_name("r"); + json.push_back(reactions); + } + + auto it = reactions.find(emoji); + if (it == reactions.end()) + reactions << INT_PARAM(emoji, 1); + else + (*it) = JSONNode(emoji, (*it).as_int() + 1); + + flushJson(); +} + +void DB::EventInfo::delReaction(const char *emoji) +{ + if (!mir_strlen(emoji)) + return; + + auto &json = setJson(); + auto &reactions = json["r"]; + auto it = reactions.find(emoji); + if (it != reactions.end()) { + JSONNode &n = *it; + if (n.as_int() > 1) + n = JSONNode(emoji, n.as_int() - 1); + else + reactions.erase(it); + + flushJson(); + } +} + ///////////////////////////////////////////////////////////////////////////////////////// // File blob helper diff --git a/src/mir_app/src/mir_app.def b/src/mir_app/src/mir_app.def index 709f5a075b..1e528c19e3 100644 --- a/src/mir_app/src/mir_app.def +++ b/src/mir_app/src/mir_app.def @@ -974,3 +974,5 @@ g_hevEventSetJson @1109 NONAME ?getJson@EventInfo@DB@@QBEAAVJSONNode@@XZ @1110 NONAME ?setJson@EventInfo@DB@@QAEAAVJSONNode@@XZ @1111 NONAME ?flushJson@EventInfo@DB@@QAEXXZ @1112 NONAME +?addReaction@EventInfo@DB@@QAEXPBD@Z @1113 NONAME +?delReaction@EventInfo@DB@@QAEXPBD@Z @1114 NONAME diff --git a/src/mir_app/src/mir_app64.def b/src/mir_app/src/mir_app64.def index 37c4dd3e83..08a35f4c16 100644 --- a/src/mir_app/src/mir_app64.def +++ b/src/mir_app/src/mir_app64.def @@ -974,3 +974,5 @@ g_hevEventSetJson @1103 NONAME ?getJson@EventInfo@DB@@QEBAAEAVJSONNode@@XZ @1104 NONAME ?setJson@EventInfo@DB@@QEAAAEAVJSONNode@@XZ @1105 NONAME ?flushJson@EventInfo@DB@@QEAAXXZ @1106 NONAME +?addReaction@EventInfo@DB@@QEAAXPEBD@Z @1107 NONAME +?delReaction@EventInfo@DB@@QEAAXPEBD@Z @1108 NONAME -- cgit v1.2.3