Skip to content

Commit

Permalink
chore: test
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 11, 2024
1 parent 77e392d commit 9095add
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
20 changes: 20 additions & 0 deletions src/adapters/supabase/helpers/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ export class Chats extends Super {
super(supabase, context);
}

async userSnapshot(chatId: number, userIds: number[]) {
const { error } = await this.supabase.from("chats").upsert({ chatId, userIds })
if (error) {
this.context.logger.error("Failed to save chat users", { chatId, userIds, er: error });
} else {
this.context.logger.info("Successfully saved chat users", { chatId, userIds });
}
}

async getChatUsers(chatId: number) {
const { data, error } = (await this.supabase.from("chats").select("userIds").eq("chatId", chatId).single())
if (error || !data) {
this.context.logger.error("No chat users found", { chatId });
} else {
this.context.logger.info("Successfully fetched chat users", { chatId });
}

return data;
}

async updateChatStatus(status: string, taskNodeId?: string, chatId?: number) {
if (!taskNodeId && !chatId) {
this.context.logger.error("No taskNodeId or chatId provided to update chat status");
Expand Down
28 changes: 20 additions & 8 deletions src/bot/mtproto-api/workrooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,6 @@ export async function closeChat(context: Context<"issues.closed", SupportedEvent
throw new Error("Failed to fetch chat participants");
}

console.log("chatParticipants", chatParticipants);
console.log("fetchChat users:", fetchChat.users);

// archive it
await mtProto.client.invoke(
new mtProto.api.folders.EditPeerFolders({
Expand All @@ -136,6 +133,8 @@ export async function closeChat(context: Context<"issues.closed", SupportedEvent
return participant.userId;
});

await chats.userSnapshot(chat.chatId, userIDs.map((id) => id.toJSNumber()));

for (let i = 0; i < userIDs.length; i++) {
if (userIDs[i].toJSNumber() === context.config.botId) {
continue;
Expand Down Expand Up @@ -197,8 +196,6 @@ export async function reopenChat(context: Context<"issues.reopened", SupportedEv
chatFull = fetchChat.fullChat as Api.ChatFull
participants = chatFull.participants as Api.ChatParticipantsForbidden;

console.log("participants", participants);

const chatCreator = participants.selfParticipant?.userId;
if (!chatCreator) {
throw new Error("Failed to get chat creator");
Expand All @@ -224,11 +221,26 @@ export async function reopenChat(context: Context<"issues.reopened", SupportedEv

await chats.updateChatStatus("reopened", payload.issue.node_id);

// Now we must re-invite all previously banned users
// can we get a list of banned users from the chat object?
const users = await chats.getChatUsers(chat.chatId);

if (!users) {
throw new Error("Failed to get chat users");
}

const { userIds } = users;

return { status: 200, reason: "chat_reopened" };
for (let i = 0; i < userIds.length; i++) {
if (userIds[i] === context.config.botId) {
continue;
}
await mtProto.client.invoke(
new mtProto.api.messages.AddChatUser({
chatId: chat.chatId,
userId: userIds[i].userId,
fwdLimit: 50,
})
);
}

return { status: 200, reason: "chat_reopened" };
}

0 comments on commit 9095add

Please sign in to comment.