diff --git a/src/adapters/supabase/helpers/chats.ts b/src/adapters/supabase/helpers/chats.ts index c34a00f..8ef9db6 100644 --- a/src/adapters/supabase/helpers/chats.ts +++ b/src/adapters/supabase/helpers/chats.ts @@ -36,10 +36,10 @@ 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 }); } @@ -47,18 +47,16 @@ export class Chats extends Super { 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 { @@ -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 { @@ -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") diff --git a/src/bot/mtproto-api/workrooms.ts b/src/bot/mtproto-api/workrooms.ts index e982ed2..9da5194 100644 --- a/src/bot/mtproto-api/workrooms.ts +++ b/src/bot/mtproto-api/workrooms.ts @@ -32,6 +32,7 @@ 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(); @@ -39,6 +40,25 @@ export async function createChat(context: Context<"issues.labeled", SupportedEve 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, @@ -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" }; } @@ -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" };