From ed47c8e8e6f7be037711861d480810174c07bc72 Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Sun, 8 Sep 2024 02:05:56 +0100 Subject: [PATCH] chore: moving all workroom fn into mtproto --- src/bot/callback-data/change-language.ts | 5 -- src/bot/mtproto-api/create-chat.ts | 26 -------- src/bot/mtproto-api/workrooms.ts | 75 ++++++++++++++++++++++++ src/handlers/callbacks-proxy.ts | 8 ++- src/handlers/github/workrooms.ts | 65 ++------------------ 5 files changed, 86 insertions(+), 93 deletions(-) delete mode 100644 src/bot/callback-data/change-language.ts delete mode 100644 src/bot/mtproto-api/create-chat.ts create mode 100644 src/bot/mtproto-api/workrooms.ts diff --git a/src/bot/callback-data/change-language.ts b/src/bot/callback-data/change-language.ts deleted file mode 100644 index 17ee539..0000000 --- a/src/bot/callback-data/change-language.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { createCallbackData } from 'callback-data' - -export const changeLanguageData = createCallbackData('language', { - code: String, -}) diff --git a/src/bot/mtproto-api/create-chat.ts b/src/bot/mtproto-api/create-chat.ts deleted file mode 100644 index 9f50585..0000000 --- a/src/bot/mtproto-api/create-chat.ts +++ /dev/null @@ -1,26 +0,0 @@ -import { Context, SupportedEvents } from "#root/types/context"; -import { CallbackResult } from "#root/types/proxy.js"; -import { MtProto } from "./bot/mtproto"; - -export async function createChat(context: Context<"issues.labeled", SupportedEvents["issues.labeled"]>): Promise { - try { - const { payload, env, config } = context; - const chatName = payload.issue.title; - - const mtProto = new MtProto(context); - await mtProto.initialize(); - - context.logger.info("Creating chat with name: ", { chatName }); - await mtProto.client.invoke( - new mtProto.api.messages.CreateChat({ - title: chatName, - users: [...env.BOT_ADMINS, config.botId], - }) - ); - - return { status: 200, reason: "chat_created" }; - } catch (er) { - context.logger.error("Failed to create chat", { er }); - return { status: 500, reason: "chat_creation_failed", content: { error: er } }; - } -} diff --git a/src/bot/mtproto-api/workrooms.ts b/src/bot/mtproto-api/workrooms.ts new file mode 100644 index 0000000..0452730 --- /dev/null +++ b/src/bot/mtproto-api/workrooms.ts @@ -0,0 +1,75 @@ +import { Context, SupportedEvents } from "#root/types/context"; +import { CallbackResult } from "#root/types/proxy.js"; +import { MtProto } from "./bot/mtproto"; + +export async function createChat(context: Context<"issues.labeled", SupportedEvents["issues.labeled"]>): Promise { + try { + const { payload, env, config } = context; + const chatName = payload.issue.title; + + const mtProto = new MtProto(context); + await mtProto.initialize(); + + context.logger.info("Creating chat with name: ", { chatName }); + + const chat = await mtProto.client.invoke( + new mtProto.api.messages.CreateChat({ + title: chatName, + users: [...env.BOT_ADMINS, config.botId], + }) + ); + + return { status: 200, reason: "chat_created" }; + } catch (er) { + context.logger.error("Failed to create chat", { er }); + return { status: 500, reason: "chat_creation_failed", content: { error: er } }; + } +} + +export async function closeChat(context: Context<"issues.closed", SupportedEvents["issues.closed"]>): Promise { + try { + const { payload, env, config } = context; + const chatName = payload.issue.title; + + const mtProto = new MtProto(context); + await mtProto.initialize(); + + context.logger.info("Closing chat with name: ", { chatName }); + + const chatMembers = await mtProto.client.invoke( + new mtProto.api.messages.GetFullChat({ + chatId: payload.issue.number, + }) + ); + + + + + return { status: 200, reason: "chat_closed" }; + } catch (er) { + context.logger.error("Failed to close chat", { er }); + return { status: 500, reason: "chat_close_failed", content: { error: er } }; + } +} + +export async function reopenChat(context: Context<"issues.reopened", SupportedEvents["issues.reopened"]>): Promise { + try { + const { payload, env, config } = context; + const chatName = payload.issue.title; + + const mtProto = new MtProto(context); + await mtProto.initialize(); + + context.logger.info("Reopening chat with name: ", { chatName }); + await mtProto.client.invoke( + new mtProto.api.messages.RestoreChat({ + chatId: payload.issue.number, + }) + ); + + return { status: 200, reason: "chat_reopened" }; + } catch (er) { + context.logger.error("Failed to reopen chat", { er }); + return { status: 500, reason: "chat_reopen_failed", content: { error: er } }; + } +} diff --git a/src/handlers/callbacks-proxy.ts b/src/handlers/callbacks-proxy.ts index a179b7f..ae38dde 100644 --- a/src/handlers/callbacks-proxy.ts +++ b/src/handlers/callbacks-proxy.ts @@ -1,4 +1,4 @@ -import { createChat } from "#root/bot/workflow-functions/create-chat.js"; +import { createChat } from "#root/bot/mtproto-api/workrooms.js"; import { ProxyCallbacks } from "#root/types/proxy.js"; import { Context, SupportedEventsU } from "../types"; import { closeWorkroom, createWorkroom, reOpenWorkroom } from "./github/workrooms"; @@ -40,6 +40,12 @@ const callbacks = { const workflowCallbacks = { "issues.labeled": [ createChat + ], + "issues.closed": [ + closeChat + ], + "issues.reopened": [ + reopenChat ] } as ProxyCallbacks; diff --git a/src/handlers/github/workrooms.ts b/src/handlers/github/workrooms.ts index 3dadabf..0c04f76 100644 --- a/src/handlers/github/workrooms.ts +++ b/src/handlers/github/workrooms.ts @@ -1,75 +1,18 @@ -import { Chat } from "#root/adapters/supabase/helpers/chats.js"; import { CallbackResult } from "#root/types/proxy.js"; -import { TelegramBotSingleton } from "#root/utils/telegram-bot-single.js"; import { Context, SupportedEvents } from "../../types"; import { repositoryDispatch } from "../repository-dispatch"; -import { addCommentToIssue } from "../../helpers/add-comment-to-issues"; -/** - * Dispatches a workflow in order to use the MTProto API to create a new chat - * for the task. - */ export async function createWorkroom(context: Context<"issues.labeled", SupportedEvents["issues.labeled"]>): Promise { await repositoryDispatch(context, "create-telegram-chat").catch(console.error); return { status: 200, reason: "workflow_dispatched" }; } -/** - * "Closes" the workroom by kicking all users from the chat and archiving it. - * - * - Does not delete the chat as it is required for later use. - * - Does not require MTProto API as we'll use the Bot API to kick users. - */ export async function closeWorkroom(context: Context<"issues.closed", SupportedEvents["issues.closed"]>): Promise { - const { logger, config, adapters: { supabase: { chats } } } = context; - const bot = TelegramBotSingleton.getInstance().getBot(); - const title = context.payload.issue.title - const { issue, repository } = context.payload; - const { full_name } = repository; - const [owner, repo] = full_name.split("/"); - - logger.info(`Closing workroom for issue ${title}`); - - const workroom = await chats.getChatByTaskNodeId(issue.node_id) as Chat - - if (!workroom) { - return { status: 404, reason: "workroom_not_found" }; - } - - try { - await bot.api?.closeForumTopic(config.supergroupChatId, workroom.chatId); - await chats.updateChatStatus("closed", issue.node_id); - await addCommentToIssue(context, `Workroom closed for issue ${title}`, owner, repo, issue.number); - return { status: 200, reason: "workroom_closed" }; - } catch (er) { - await addCommentToIssue(context, logger.error(`Failed to close workroom for issue ${title}`, { er }).logMessage.diff, owner, repo, issue.number); - return { status: 500, reason: "workroom_closing_failed" }; - } + await repositoryDispatch(context, "close-telegram-chat").catch(console.error); + return { status: 200, reason: "workflow_dispatched" }; } export async function reOpenWorkroom(context: Context<"issues.reopened", SupportedEvents["issues.reopened"]>): Promise { - const { config, logger, adapters: { supabase: { chats } } } = context; - const bot = TelegramBotSingleton.getInstance().getBot(); - const title = context.payload.issue.title - const { issue, repository } = context.payload; - const { full_name } = repository; - const [owner, repo] = full_name.split("/"); - - logger.info(`Reopening workroom for issue ${title}`); - - const workroom = await chats.getChatByTaskNodeId(issue.node_id) as Chat - - if (!workroom) { - return { status: 404, reason: "workroom_not_found" }; - } - - try { - await bot.api?.reopenForumTopic(config.supergroupChatId, workroom.chatId); - await chats.updateChatStatus("reopened", issue.node_id); - await addCommentToIssue(context, `Workroom reopened for issue ${title}`, owner, repo, issue.number); - return { status: 200, reason: "workroom_reopened" }; - } catch (er) { - await addCommentToIssue(context, logger.error(`Failed to reopen workroom for issue ${title}`, { er }).logMessage.diff, owner, repo, issue.number); - return { status: 500, reason: "workroom_reopening_failed" }; - } + await repositoryDispatch(context, "reopen-telegram-chat").catch(console.error); + return { status: 200, reason: "workflow_dispatched" }; }