Add replies and emoji reactions to chat messages

Every message has a row under it to reply or react. Replying quotes the
message above the composer, and the sent message stores
reply_to_message_id (a plain id, not a foreign key, so deleting the
quoted message only drops the quote). send-message accepts a quote from
any session of the same conversation, so a rollover or a thread that
moved on still sends; the conversation drops a picked quote when its
session changes. The bot reads the quote as "[Replying to ...]" before
the user's turn, file-only replies included, and relayed messages carry
it too.

Reactions are a fixed set (BotMessageReactions) in
bot_message_reactions, one per message, emoji and user or bot. Users
toggle their own through api/admin/bots/react-message, and every bot
gets react_to_message for the session it answers (the user's latest
message by default). Both push the fresh list as server:bot-reaction,
with mine worked out for the session's user.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-19 21:56:31 +01:00
co-authored by Claude Opus 5.5
parent bc499a7466
commit d1b5b6d68a
44 changed files with 1289 additions and 58 deletions
@@ -0,0 +1,70 @@
import BunSQLite from "@moduletrace/bun-sqlite";
import {
BotMessageReactions,
type BotMessageReactionEmoji,
} from "@/src/dict/bot-dict";
import type { BotMessageReaction } from "@/src/types";
type Params = {
message_ids: number[];
user_id: number | string;
};
type ReactionRow = {
message_id: number;
emoji: BotMessageReactionEmoji;
user_id: number;
};
/**
* A message's reactions, grouped by emoji, in the picker's order, with
* whether the given user reacted and how many did.
*/
export default async function grabMessagesReactions({ message_ids, user_id }: Params) {
const by_message = new Map<number, BotMessageReaction[]>();
const ids = [...new Set(message_ids.filter(Boolean))];
if (!ids.length) return by_message;
const rows =
(
await BunSQLite.sql<ReactionRow>({
sql: `SELECT message_id, emoji, user_id FROM bot_message_reactions
WHERE message_id IN (${ids.map(() => "?").join(", ")})
ORDER BY id ASC`,
values: ids,
})
).payload || [];
const order = new Map<string, number>(
BotMessageReactions.map((reaction, index) => [reaction.value, index]),
);
for (const row of rows) {
const list = by_message.get(row.message_id) || [];
const found = list.find((reaction) => reaction.emoji == row.emoji);
if (found) {
found.count += 1;
found.mine = found.mine || String(row.user_id) == String(user_id);
} else {
list.push({
emoji: row.emoji,
count: 1,
mine: String(row.user_id) == String(user_id),
});
}
by_message.set(row.message_id, list);
}
for (const list of by_message.values()) {
list.sort(
(a, b) =>
(order.get(a.emoji) ?? BotMessageReactions.length) -
(order.get(b.emoji) ?? BotMessageReactions.length),
);
}
return by_message;
}