Skip to content

Commit

Permalink
chore: close room
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 8, 2024
1 parent ed47c8e commit 7dd5559
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
44 changes: 34 additions & 10 deletions src/bot/mtproto-api/workrooms.ts
Original file line number Diff line number Diff line change
@@ -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<CallbackResult> {
Expand Down Expand Up @@ -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<CallbackResult> {
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],
})
);
}
}



Expand All @@ -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 });
Expand Down
2 changes: 1 addition & 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/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";
Expand Down
5 changes: 0 additions & 5 deletions src/handlers/repository-dispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7dd5559

Please sign in to comment.