-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: moving all workroom fn into mtproto
- Loading branch information
Showing
5 changed files
with
86 additions
and
93 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" }; | ||
} |