From 7dd5559ed42ec653da56bd13e29bafaebc559eed Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Sun, 8 Sep 2024 14:20:22 +0100 Subject: [PATCH] chore: close room --- src/bot/mtproto-api/workrooms.ts | 44 ++++++++++++++++++++++------- src/handlers/callbacks-proxy.ts | 2 +- src/handlers/repository-dispatch.ts | 5 ---- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/bot/mtproto-api/workrooms.ts b/src/bot/mtproto-api/workrooms.ts index 0452730..2ad2339 100644 --- a/src/bot/mtproto-api/workrooms.ts +++ b/src/bot/mtproto-api/workrooms.ts @@ -1,5 +1,6 @@ import { Context, SupportedEvents } from "#root/types/context"; import { CallbackResult } from "#root/types/proxy.js"; +import { Api } from "telegram"; import { MtProto } from "./bot/mtproto"; export async function createChat(context: Context<"issues.labeled", SupportedEvents["issues.labeled"]>): Promise { @@ -28,20 +29,50 @@ export async function createChat(context: Context<"issues.labeled", SupportedEve export async function closeChat(context: Context<"issues.closed", SupportedEvents["issues.closed"]>): Promise { try { - const { payload, env, config } = context; + const { payload, adapters: { supabase: { chats } } } = context; const chatName = payload.issue.title; const mtProto = new MtProto(context); await mtProto.initialize(); context.logger.info("Closing chat with name: ", { chatName }); + const chat = await chats.getChatByTaskNodeId(payload.issue.node_id); - const chatMembers = await mtProto.client.invoke( + const fetchChat = await mtProto.client.invoke( new mtProto.api.messages.GetFullChat({ - chatId: payload.issue.number, + chatId: chat.chatId, }) ); + let chatParticipants; + + if ("participants" in fetchChat.fullChat) { + chatParticipants = fetchChat.fullChat.participants; + } else { + throw new Error("Failed to fetch chat participants"); + } + + if (chatParticipants.className === "ChatParticipantsForbidden") { + console.log("ChatParticipantsForbidden"); + } + + if (chatParticipants.className === "ChatParticipants") { + const userIDs = chatParticipants.participants.map((participant) => { + return participant.userId; + }); + + for (let i = 0; i < userIDs.length; i++) { + if (userIDs[i].toJSNumber() === context.config.botId) { + continue; + } + await mtProto.client.invoke( + new mtProto.api.messages.DeleteChatUser({ + chatId: chat.chatId, + userId: userIDs[i], + }) + ); + } + } @@ -60,13 +91,6 @@ export async function reopenChat(context: Context<"issues.reopened", SupportedEv 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 }); diff --git a/src/handlers/callbacks-proxy.ts b/src/handlers/callbacks-proxy.ts index ae38dde..1251037 100644 --- a/src/handlers/callbacks-proxy.ts +++ b/src/handlers/callbacks-proxy.ts @@ -1,4 +1,4 @@ -import { createChat } from "#root/bot/mtproto-api/workrooms.js"; +import { closeChat, createChat, reopenChat } 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"; diff --git a/src/handlers/repository-dispatch.ts b/src/handlers/repository-dispatch.ts index df5df44..5c6b9b6 100644 --- a/src/handlers/repository-dispatch.ts +++ b/src/handlers/repository-dispatch.ts @@ -7,11 +7,6 @@ import { Context } from "../types"; * * These workflows are extensions of the worker allowing for more complex operations * to be performed outside of Cloudflare Workers' limitations. - * - * @param env The environment variables for the worker instance. These - * will be taken from the repository's secrets. - * @param args The arguments passed to the workflow. - * */ export async function repositoryDispatch(context: Context, workflow: string) {