Skip to content

Commit

Permalink
chore: test
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 8, 2024
1 parent 16746bd commit 68ac3b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 54 deletions.
16 changes: 7 additions & 9 deletions src/adapters/supabase/helpers/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,27 @@ export class Chats extends Super {
return;
}

const { data, error } = (await this.supabase.from("chats").upsert({ ...chat, status })) as { data: unknown; error: unknown };
const { error } = (await this.supabase.from("chats").upsert({ ...chat, status }))

if (error || !data) {
this.context.logger.error("Failed to update chat status", { chatId, taskNodeId });
if (error) {
this.context.logger.error("Failed to update chat status", { chatId, taskNodeId, er: error });
} else {
this.context.logger.info("Successfully updated chat status", { chatId, taskNodeId });
}
}


async saveChat(chatId: number, chatName: string, taskNodeId: string) {
const { data, error } = (await this.supabase.from("chats").insert([{ chatId, chatName, taskNodeId, status: "open" }]) as { data: unknown; error: unknown });
const { error } = await this.supabase.from("chats").insert([{ chatId, chatName, taskNodeId, status: "open" }])
if (error) {
this.context.logger.error("Failed to save chat", { chatId, chatName, taskNodeId, er: error });
} else {
this.context.logger.info("Successfully saved chat", { chatId, chatName });
}

return data;
}

async getChatByChatId(chatId: number) {
const { data, error } = (await this.supabase.from("chats").select("*").eq("chatId", chatId).single()) as { data: unknown; error: unknown };
const { data, error } = (await this.supabase.from("chats").select("*").eq("chatId", chatId).single())
if (error || !data) {
this.context.logger.error("No chat found", { chatId });
} else {
Expand All @@ -69,7 +67,7 @@ export class Chats extends Super {
}

async getChatByChatName(chatName: string) {
const { data, error } = (await this.supabase.from("chats").select("*").eq("chatName", chatName).single()) as { data: unknown; error: unknown };
const { data, error } = (await this.supabase.from("chats").select("*").eq("chatName", chatName).single())
if (error || !data) {
this.context.logger.error("No chat found", { chatName });
} else {
Expand All @@ -88,7 +86,7 @@ export class Chats extends Super {
this.context.logger.info("Successfully fetched chat", { taskNodeId });
}

return data;
return data
} catch (e) {
console.error(e)
throw new Error("Failed to fetch chat by task node id")
Expand Down
65 changes: 20 additions & 45 deletions src/bot/mtproto-api/workrooms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,33 @@ export async function createChat(context: Context<"issues.labeled", SupportedEve
users: [botIdString],
})
);
let inviteLink;

if ("chats" in chat.updates) {
chatId = chat.updates.chats[0].id.toJSNumber();
} else {
throw new Error("Failed to create chat");
}

if (chat.updates.chats[0].className === "Chat") {
inviteLink = await mtProto.client.invoke(
new mtProto.api.messages.ExportChatInvite({
peer: new mtProto.api.InputPeerChat({ chatId: chat.updates.chats[0].id }),
})
);
}

if (inviteLink) {
const [owner, repo] = payload.repository.full_name.split("/");
let link;

if ("link" in inviteLink) {
link = inviteLink.link;
}

await addCommentToIssue(context, `Workroom has been created for this task. [Join chat](${link})`, owner, repo, payload.issue.number);
}

const promoteBotToAdmin = await mtProto.client.invoke(
new mtProto.api.messages.EditChatAdmin({
chatId: chat.updates.chats[0].id,
Expand All @@ -56,29 +76,6 @@ export async function createChat(context: Context<"issues.labeled", SupportedEve
}

await context.adapters.supabase.chats.saveChat(chatId, payload.issue.title, payload.issue.node_id);

try {

const chatInviteLink = await mtProto.client.invoke(
new mtProto.api.messages.ExportChatInvite({
peer: await mtProto.client.getEntity(payload.issue.title)
})
);

const [owner, repo] = payload.repository.full_name.split("/");

let link;

if ("link" in chatInviteLink) {
link = chatInviteLink.link;
}

await addCommentToIssue(context, `Workroom has been created for this task. [Join chat](${link})`, owner, repo, payload.issue.number);
} catch (err) {
console.log("Error in creating chat invite link: ", err);
return { status: 500, reason: "chat_create_failed", content: { error: err } };
}

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

Expand Down Expand Up @@ -137,28 +134,6 @@ export async function closeChat(context: Context<"issues.closed", SupportedEvent
}
}

// const chatFull = fetchChat.fullChat as Api.ChatFull
// const participants = chatFull.participants as Api.ChatParticipants;

// for (const participant of participants.participants) {
// if (participant instanceof mtProto.api.ChatParticipant) {
// await mtProto.client.invoke(
// new mtProto.api.messages.DeleteChatUser({
// chatId: chat.chatId,
// userId: participant.userId,
// })
// );
// }
// }

// await mtProto.client.invoke(
// new mtProto.api.messages.DeleteChatUser({
// chatId: chat.chatId,
// userId: bigInt(0),
// })
// );


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

return { status: 200, reason: "chat_closed" };
Expand Down

0 comments on commit 68ac3b2

Please sign in to comment.