From 6c050b9a3d3d3cd1e99607bcc54e5f16f32ca180 Mon Sep 17 00:00:00 2001 From: alexa Date: Mon, 3 Jun 2024 22:34:40 +0200 Subject: [PATCH] work on reactions --- src/enums/defaultReactions.ts | 132 +++++++++++------------ src/features/database/mariaDbDatabase.ts | 4 + src/features/liveFeature.ts | 49 +++++++++ updateDb.sql | 2 +- 4 files changed, 120 insertions(+), 67 deletions(-) diff --git a/src/enums/defaultReactions.ts b/src/enums/defaultReactions.ts index 439f19d..33c93c7 100644 --- a/src/enums/defaultReactions.ts +++ b/src/enums/defaultReactions.ts @@ -521,72 +521,72 @@ export const defaultReactions = [ {content: "🐌", groupId: 12, identifier: "snail"}, {content: "🐍", groupId: 12, identifier: "snake"}, {content: "🐎", groupId: 12, identifier: "horse"}, - {content: "🐏", groupId: 12, identifier: "emoji"}, - {content: "🐐", groupId: 12, identifier: "emoji"}, - {content: "🐑", groupId: 12, identifier: "emoji"}, - {content: "🐒", groupId: 12, identifier: "emoji"}, - {content: "🐓", groupId: 12, identifier: "emoji"}, - {content: "🐔", groupId: 12, identifier: "emoji"}, - {content: "🐕", groupId: 12, identifier: "emoji"}, - {content: "🐖", groupId: 12, identifier: "emoji"}, - {content: "🐗", groupId: 12, identifier: "emoji"}, - {content: "🐘", groupId: 12, identifier: "emoji"}, - {content: "🐙", groupId: 12, identifier: "emoji"}, - {content: "🐚", groupId: 12, identifier: "emoji"}, - {content: "🐛", groupId: 12, identifier: "emoji"}, - {content: "🐜", groupId: 12, identifier: "emoji"}, - {content: "🐝", groupId: 12, identifier: "emoji"}, - {content: "🐞", groupId: 12, identifier: "emoji"}, - {content: "🐟", groupId: 12, identifier: "emoji"}, - {content: "🐠", groupId: 12, identifier: "emoji"}, - {content: "🐡", groupId: 12, identifier: "emoji"}, - {content: "🐢", groupId: 12, identifier: "emoji"}, - {content: "🐣", groupId: 12, identifier: "emoji"}, - {content: "🐤", groupId: 12, identifier: "emoji"}, - {content: "🐥", groupId: 12, identifier: "emoji"}, - {content: "🐦", groupId: 12, identifier: "emoji"}, - {content: "🐧", groupId: 12, identifier: "emoji"}, - {content: "🐨", groupId: 12, identifier: "emoji"}, - {content: "🐩", groupId: 12, identifier: "emoji"}, - {content: "🐪", groupId: 12, identifier: "emoji"}, - {content: "🐫", groupId: 12, identifier: "emoji"}, - {content: "🐬", groupId: 12, identifier: "emoji"}, - {content: "🐭", groupId: 12, identifier: "emoji"}, - {content: "🐮", groupId: 12, identifier: "emoji"}, - {content: "🐯", groupId: 12, identifier: "emoji"}, - {content: "🐰", groupId: 12, identifier: "emoji"}, - {content: "🐱", groupId: 12, identifier: "emoji"}, - {content: "🐲", groupId: 12, identifier: "emoji"}, - {content: "🐳", groupId: 12, identifier: "emoji"}, - {content: "🐴", groupId: 12, identifier: "emoji"}, - {content: "🐵", groupId: 12, identifier: "emoji"}, - {content: "🐶", groupId: 12, identifier: "emoji"}, - {content: "🐷", groupId: 12, identifier: "emoji"}, - {content: "🐸", groupId: 12, identifier: "emoji"}, - {content: "🐹", groupId: 12, identifier: "emoji"}, - {content: "🐺", groupId: 12, identifier: "emoji"}, - {content: "🐻", groupId: 12, identifier: "emoji"}, - {content: "🐼", groupId: 12, identifier: "emoji"}, - {content: "🐽", groupId: 12, identifier: "emoji"}, - {content: "🐾", groupId: 0, identifier: "emoji"}, - {content: "🐿️", groupId: 0, identifier: "emoji"}, - {content: "👀", groupId: 0, identifier: "emoji"}, - {content: "👁️", groupId: 0, identifier: "emoji"}, - {content: "👂", groupId: 0, identifier: "emoji"}, - {content: "👃", groupId: 0, identifier: "emoji"}, - {content: "👄", groupId: 0, identifier: "emoji"}, - {content: "👅", groupId: 0, identifier: "emoji"}, - {content: "👆", groupId: 0, identifier: "emoji"}, - {content: "👇", groupId: 0, identifier: "emoji"}, - {content: "👈", groupId: 0, identifier: "emoji"}, - {content: "👉", groupId: 0, identifier: "emoji"}, - {content: "👊", groupId: 0, identifier: "emoji"}, - {content: "👋", groupId: 0, identifier: "emoji"}, - {content: "👌", groupId: 0, identifier: "emoji"}, - {content: "👍", groupId: 0, identifier: "emoji"}, - {content: "👎", groupId: 0, identifier: "emoji"}, - {content: "👏", groupId: 0, identifier: "emoji"}, - {content: "👐", groupId: 0, identifier: "emoji"}, + {content: "🐏", groupId: 12, identifier: "ram"}, + {content: "🐐", groupId: 12, identifier: "goat"}, + {content: "🐑", groupId: 12, identifier: "sheep"}, + {content: "🐒", groupId: 12, identifier: "monkey"}, + {content: "🐓", groupId: 12, identifier: "rooster"}, + {content: "🐔", groupId: 12, identifier: "chicken"}, + {content: "🐕", groupId: 12, identifier: "dog"}, + {content: "🐖", groupId: 12, identifier: "pig"}, + {content: "🐗", groupId: 12, identifier: "boar"}, + {content: "🐘", groupId: 12, identifier: "elephant"}, + {content: "🐙", groupId: 12, identifier: "octopus"}, + {content: "🐚", groupId: 4, identifier: "shell"}, + {content: "🐛", groupId: 12, identifier: "bug"}, + {content: "🐜", groupId: 12, identifier: "ant"}, + {content: "🐝", groupId: 12, identifier: "bee"}, + {content: "🐞", groupId: 12, identifier: "ladybug"}, + {content: "🐟", groupId: 12, identifier: "fish"}, + {content: "🐠", groupId: 12, identifier: "fish_tropical"}, + {content: "🐡", groupId: 12, identifier: "fish_blowfish"}, + {content: "🐢", groupId: 12, identifier: "turtle"}, + {content: "🐣", groupId: 12, identifier: "hatching_chick"}, + {content: "🐤", groupId: 12, identifier: "baby_chick"}, + {content: "🐥", groupId: 12, identifier: "chick"}, + {content: "🐦", groupId: 12, identifier: "bird"}, + {content: "🐧", groupId: 12, identifier: "penguin"}, + {content: "🐨", groupId: 12, identifier: "koala"}, + {content: "🐩", groupId: 12, identifier: "poodle"}, + {content: "🐪", groupId: 12, identifier: "dromedary_camel"}, + {content: "🐫", groupId: 12, identifier: "camel"}, + {content: "🐬", groupId: 12, identifier: "dolphin"}, + {content: "🐭", groupId: 12, identifier: "mouse"}, + {content: "🐮", groupId: 12, identifier: "cow"}, + {content: "🐯", groupId: 12, identifier: "tiger"}, + {content: "🐰", groupId: 12, identifier: "rabbit"}, + {content: "🐱", groupId: 12, identifier: "cat"}, + {content: "🐲", groupId: 12, identifier: "dragon"}, + {content: "🐳", groupId: 12, identifier: "whale"}, + {content: "🐴", groupId: 12, identifier: "horse"}, + {content: "🐵", groupId: 12, identifier: "monkey_face"}, + {content: "🐶", groupId: 12, identifier: "dog_face"}, + {content: "🐷", groupId: 12, identifier: "pig_face"}, + {content: "🐸", groupId: 12, identifier: "frog_face"}, + {content: "🐹", groupId: 12, identifier: "hamster_face"}, + {content: "🐺", groupId: 12, identifier: "wolf_face"}, + {content: "🐻", groupId: 12, identifier: "bear_face"}, + {content: "🐼", groupId: 12, identifier: "panda_face"}, + {content: "🐽", groupId: 12, identifier: "pig_nose"}, + {content: "🐾", groupId: 12, identifier: "paw_prints"}, + {content: "🐿️", groupId: 12, identifier: "chipmunk"}, + {content: "👀", groupId: 1, identifier: "eyes"}, + {content: "👁️", groupId: 1, identifier: "eye"}, + {content: "👂", groupId: 1, identifier: "ear"}, + {content: "👃", groupId: 1, identifier: "nose"}, + {content: "👄", groupId: 1, identifier: "mouth"}, + {content: "👅", groupId: 1, identifier: "tongue"}, + {content: "👆", groupId: 1, identifier: "point_up"}, + {content: "👇", groupId: 1, identifier: "point_down"}, + {content: "👈", groupId: 1, identifier: "point_left"}, + {content: "👉", groupId: 1, identifier: "point_right"}, + {content: "👊", groupId: 1, identifier: "fist"}, + {content: "👋", groupId: 1, identifier: "wave"}, + {content: "👌", groupId: 1, identifier: "ok_hand"}, + {content: "👍", groupId: 1, identifier: "emoji"}, + {content: "👎", groupId: 1, identifier: "emoji"}, + {content: "👏", groupId: 1, identifier: "emoji"}, + {content: "👐", groupId: 1, identifier: "emoji"}, {content: "👑", groupId: 0, identifier: "emoji"}, {content: "👒", groupId: 0, identifier: "emoji"}, {content: "👓", groupId: 0, identifier: "emoji"}, diff --git a/src/features/database/mariaDbDatabase.ts b/src/features/database/mariaDbDatabase.ts index e9cdd91..9ad4914 100644 --- a/src/features/database/mariaDbDatabase.ts +++ b/src/features/database/mariaDbDatabase.ts @@ -313,4 +313,8 @@ WHERE ur.userId = ?`, [userId]); async addReaction(userId: Id, messageId: Id, reactionId: Id) { await this.query("INSERT INTO venel.messageReactions (userId, messageId, reactionId) VALUES (?, ?, ?)", [userId, messageId, reactionId]); } + + async removeReaction(userId: Id, messageId: Id, reactionId: Id) { + await this.query("DELETE FROM venel.messageReactions WHERE userId = ? AND messageId = ? AND reactionId = ?", [userId, messageId, reactionId]); + } } \ No newline at end of file diff --git a/src/features/liveFeature.ts b/src/features/liveFeature.ts index 9609e4e..47d9c3f 100644 --- a/src/features/liveFeature.ts +++ b/src/features/liveFeature.ts @@ -57,6 +57,9 @@ export class LiveFeature { case "addReaction": await LiveFeature.addReaction(data, ws.user, clients, ws, db); break; + case "removeReaction": + await LiveFeature.removeReaction(data, ws.user, clients, ws, db); + break; case "updateAvatar": await LiveFeature.updateAvatar(userMap, data, ws.user, clients, ws, db); break; @@ -213,6 +216,52 @@ export class LiveFeature { } } + static async removeReaction(data: any, user: User, clients: Set, client: UserWebSocket, db: MariaDbDatabase) { + const messageId = data.messageId; + if (!messageId) { + client.send(JSON.stringify({error: "Message ID is required"})); + return; + } + + const message = await db.getMessageById(messageId); + if (!message) { + client.send(JSON.stringify({error: "Message not found"})); + return; + } + + const invalid = await MessagingEndpoints.checkChannelAccess(db, user, message.channelId); + if (invalid !== null) { + client.send(JSON.stringify({error: invalid})); + return; + } + + const reactionId = data.reactionId; + if (!reactionId) { + client.send(JSON.stringify({error: "Reaction is required"})); + return; + } + + await db.removeReaction(user.id, messageId, reactionId); + const payload = JSON.stringify({ + type: "removeReaction", + messageId, + reactionId, + userId: user.id + }); + CLI.debug(`Propagating reaction removal from ${user.id} to ${clients.size} clients.`); + for (const ws of clients) { + if (ws.readyState !== ws.OPEN) { + continue; + } + + const invalid = await MessagingEndpoints.checkChannelAccess(db, ws.user, message.channelId); + if (invalid !== null) { + continue; + } + ws.send(payload); + } + } + private static async updateAvatar(userMap: Map, data: any, user: User, clients: Set, client: UserWebSocket, db: MariaDbDatabase) { let avatar = data.avatar; diff --git a/updateDb.sql b/updateDb.sql index d245dd8..d6edcd6 100644 --- a/updateDb.sql +++ b/updateDb.sql @@ -183,7 +183,7 @@ create table if not exists venel.messageReactions messageId bigint not null, reactionId bigint not null, userId bigint not null, - primary key (messageId, reactionId), + primary key (messageId, reactionId, userId), constraint messageReactions_ibfk_1 foreign key (messageId) references venel.messages (id) on delete cascade,