Skip to content

Commit

Permalink
chore: leftover db refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Sep 13, 2024
1 parent 590a54a commit 99ac8cc
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/adapters/supabase/helpers/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Chats extends Super {
*/
async userSnapshot(chatId: number, userIds: number[]) {
const chat = await this.getChatByChatId(chatId);
const { error } = await this.supabase.from("chats").upsert({ ...chat, user_ids: userIds });
const { error } = await this.supabase.from("chats").update({ user_ids: userIds }).eq("chat_id", chat.chat_id);
if (error) {
this.logger.error("Failed to save chat users", { chatId, userIds, er: error });
} else {
Expand Down Expand Up @@ -63,7 +63,7 @@ export class Chats extends Super {
return;
}

const { error } = await this.supabase.from("chats").upsert({ ...chat, status });
const { error } = await this.supabase.from("chats").update({ status }).eq("chat_id", chat.chat_id);

if (error) {
this.logger.error("Failed to update chat status", { chatId, taskNodeId, er: error });
Expand Down
1 change: 1 addition & 0 deletions src/bot/mtproto-api/bot/mtproto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { SupabaseSession } from "./session";
import { createClient, SupabaseClient } from "@supabase/supabase-js";
dotenv.config();


/**
* This class MUST ONLY be used in the context of workflow-functions as
* it requires a Node.js environment which is not available with Cloudflare Workers.
Expand Down
2 changes: 2 additions & 0 deletions src/bot/mtproto-api/bot/scripts/sms-auth/base-mtproto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { StringSession } from "telegram/sessions";
import { Context } from "#root/types/context.js";
dotenv.config();



/**
* @dev Not abstract because we need it to be instantiated for sms-auth
*
Expand Down
1 change: 0 additions & 1 deletion src/bot/mtproto-api/bot/scripts/sms-auth/setup-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { exit, kill, nextTick } from "node:process";
import { Octokit } from "@octokit/rest";
import dotenv from "dotenv";
import { writeFile } from "node:fs/promises";
import { spawn, spawnSync } from "node:child_process";
dotenv.config();
/**
* This script is used to help guide the user through setting up the environment variables.
Expand Down
11 changes: 6 additions & 5 deletions src/bot/mtproto-api/workrooms/close-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ export async function closeChat(context: Context<"issues.closed", SupportedEvent
logger.info("Closing chat with name: ", { chatName: payload.issue.title });
const chat = await chats.getChatByTaskNodeId(payload.issue.node_id);

await mtProto.client.getDialogs();
const fetchedChat = await mtProto.client.invoke(
new mtProto.api.messages.GetFullChat({
chatId: chat.chatId,
chatId: chat.chat_id,
})
);

Expand All @@ -41,7 +42,7 @@ export async function closeChat(context: Context<"issues.closed", SupportedEvent
new mtProto.api.folders.EditPeerFolders({
folderPeers: [
new mtProto.api.InputFolderPeer({
peer: new mtProto.api.InputPeerChat({ chatId: chat.chatId }),
peer: new mtProto.api.InputPeerChat({ chatId: chat.chat_id }),
folderId: 1, // 0 is active, 1 is archived
}),
],
Expand All @@ -52,7 +53,7 @@ export async function closeChat(context: Context<"issues.closed", SupportedEvent
await mtProto.client.invoke(
new mtProto.api.messages.SendMessage({
message: "This task has been closed and this chat has been archived.",
peer: new mtProto.api.InputPeerChat({ chatId: chat.chatId }),
peer: new mtProto.api.InputPeerChat({ chatId: chat.chat_id }),
})
);

Expand All @@ -74,10 +75,10 @@ export async function closeChat(context: Context<"issues.closed", SupportedEvent
}

userIds.push(creatorId);
const chatInput = await mtProto.client.getInputEntity(chat.chatId);
const chatInput = await mtProto.client.getInputEntity(chat.chat_id);

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

Expand Down
2 changes: 1 addition & 1 deletion src/bot/mtproto-api/workrooms/create-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export async function createChat(context: Context<"issues.labeled", SupportedEve
logger.info("Creating chat with name: ", { chatName });

try {
await mtProto.client.getDialogs();
const botIdString = await mtProto.client.getPeerId(config.botId, true);

const chat = await mtProto.client.invoke(
new mtProto.api.messages.CreateChat({
title: chatName,
Expand Down
12 changes: 6 additions & 6 deletions src/bot/mtproto-api/workrooms/reopen-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function reopenChat(context: Context<"issues.reopened", SupportedEv

const fetchedChat = await mtProto.client.invoke(
new mtProto.api.messages.GetFullChat({
chatId: chat.chatId,
chatId: chat.chat_id,
})
);

Expand All @@ -33,7 +33,7 @@ export async function reopenChat(context: Context<"issues.reopened", SupportedEv
new mtProto.api.folders.EditPeerFolders({
folderPeers: [
new mtProto.api.InputFolderPeer({
peer: new mtProto.api.InputPeerChat({ chatId: chat.chatId }),
peer: new mtProto.api.InputPeerChat({ chatId: chat.chat_id }),
folderId: 0,
}),
],
Expand All @@ -51,20 +51,20 @@ export async function reopenChat(context: Context<"issues.reopened", SupportedEv
// add the creator back to obtain control of the chat
await mtProto.client.invoke(
new mtProto.api.messages.AddChatUser({
chatId: chat.chatId,
chatId: chat.chat_id,
userId: chatCreator,
fwdLimit: 50,
})
);

await chats.updateChatStatus("reopened", payload.issue.node_id);
const users = await chats.getChatUsers(chat.chatId);
const users = await chats.getChatUsers(chat.chat_id);
if (!users) {
throw new Error("Failed to get chat users");
}

const { user_ids: userIds } = users;
const chatInput = await mtProto.client.getInputEntity(chat.chatId);
const chatInput = await mtProto.client.getInputEntity(chat.chat_id);

for (const userId of userIds) {
/**
Expand Down Expand Up @@ -96,7 +96,7 @@ export async function reopenChat(context: Context<"issues.reopened", SupportedEv
await mtProto.client.invoke(
new mtProto.api.messages.SendMessage({
message: "This task has been reopened and this chat has been unarchived.",
peer: new mtProto.api.InputPeerChat({ chatId: chat.chatId }),
peer: new mtProto.api.InputPeerChat({ chatId: chat.chat_id }),
})
);
return { status: 200, reason: "chat_reopened" };
Expand Down
1 change: 1 addition & 0 deletions supabase/migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CREATE TABLE IF NOT EXISTS "chats" (
created_at TIMESTAMPTZ DEFAULT now(),
updated_at TIMESTAMPTZ DEFAULT now(),
chat_id BIGINT NOT NULL,
chat_name TEXT NOT NULL,
task_node_id TEXT UNIQUE NOT NULL,
status TEXT NOT NULL CHECK (status IN ('open', 'closed', 'reopened')),
user_ids BIGINT[]
Expand Down

0 comments on commit 99ac8cc

Please sign in to comment.