Skip to content

Commit

Permalink
chore: moving all workroom fn into mtproto
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 8, 2024
1 parent 0b4e971 commit ed47c8e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 93 deletions.
5 changes: 0 additions & 5 deletions src/bot/callback-data/change-language.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/bot/mtproto-api/create-chat.ts

This file was deleted.

75 changes: 75 additions & 0 deletions src/bot/mtproto-api/workrooms.ts
Original file line number Diff line number Diff line change
@@ -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<CallbackResult> {
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<CallbackResult> {
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<CallbackResult> {
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 } };
}
}
8 changes: 7 additions & 1 deletion src/handlers/callbacks-proxy.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -40,6 +40,12 @@ const callbacks = {
const workflowCallbacks = {
"issues.labeled": [
createChat
],
"issues.closed": [
closeChat
],
"issues.reopened": [
reopenChat
]
} as ProxyCallbacks;

Expand Down
65 changes: 4 additions & 61 deletions src/handlers/github/workrooms.ts
Original file line number Diff line number Diff line change
@@ -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<CallbackResult> {
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<CallbackResult> {
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<CallbackResult> {
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" };
}

0 comments on commit ed47c8e

Please sign in to comment.