Files
tg-chat-bot/src/common/message-store.ts
T
melod1n b74e0a0f3e refactor(bot): centralize runtime state; support albums + safer vision handling
- make MessageStore.put() return StoredMessage and allow collectReplyChainText() to work with StoredMessage
- move muted users + answers loading into Environment (add Answers model + GEMINI_IMAGE_MODEL)
- extract message handling into processNewMessage() and add media-group (album) caching/downloading by unique_file_id
- for Ollama: check model capabilities before sending images; use replyToMessage helper consistently
- add /geminiGenImage command stub for Gemini image generation
2026-01-29 20:01:30 +03:00

43 lines
1.3 KiB
TypeScript

import {StoredMessage} from "../model/stored-message";
import {Message} from "typescript-telegram-bot-api";
import {extractTextMessage, isStoredMessage} from "../util/utils";
import {messageDao} from "../index";
export class MessageStore {
private static map = new Map<string, StoredMessage>();
private static key(chatId: number, messageId: number) {
return `${chatId}:${messageId}`;
}
static all(): Map<string, StoredMessage> {
return this.map;
}
static async put(m: Message | StoredMessage): Promise<StoredMessage> {
const msg: StoredMessage = isStoredMessage(m) ? m : {
chatId: m.chat.id,
id: m.message_id,
replyToMessageId: m.reply_to_message?.message_id ?? null,
fromId: m.from.id,
text: extractTextMessage(m),
date: m.date ?? 0,
};
this.map.set(this.key(msg.chatId, msg.id), msg);
await messageDao.insert(messageDao.mapStoredTo([msg]));
return msg;
}
static async get(chatId: number, messageId: number): Promise<StoredMessage | null> {
const message = await messageDao.getById({chatId: chatId, id: messageId});
if (!message) return null;
this.map.set(this.key(message.chatId, messageId), message);
return message;
}
static clear() {
this.map.clear();
}
}