tgsnake
Version:
Telegram MTProto framework for nodejs.
50 lines (49 loc) • 1.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConversationManager = void 0;
const conversation_js_1 = require("./conversation.js");
const index_js_1 = require("../TL/Updates/index.js");
class ConversationManager {
conversation;
constructor() {
this.conversation = new Map();
}
middleware() {
return (context, next) => {
if (context instanceof index_js_1.Update) {
const peer = ConversationManager.getPeerId(context);
if (peer) {
const conversation = this.conversation.get(peer);
if (conversation) {
return conversation.middleware(context, next);
}
}
}
return next();
};
}
create(peer) {
const conversation = new conversation_js_1.Conversation();
this.conversation.set(peer, conversation);
return conversation;
}
remove(peer) {
return this.conversation.delete(peer);
}
static getPeerId(update) {
for (const entries of Object.values(update)) {
if (entries && typeof entries === 'object') {
if (entries.chat && entries.chat.id) {
return entries.chat.id;
}
if (entries.message &&
typeof entries.message === 'object' &&
entries.message.chat &&
entries.message.chat.id) {
return entries.message.chat.id;
}
}
}
}
}
exports.ConversationManager = ConversationManager;