Skip to content

Commit

Permalink
chore: delete existing sessions during yarn sms-auth
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Oct 28, 2024
1 parent 282398f commit 009f22e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
6 changes: 1 addition & 5 deletions src/adapters/supabase/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,8 @@ export class SuperbaseStorage extends StorageGetters implements Storage {
}
}

async handleSession<TAction extends "create" | "delete">(session: string, action: TAction) {
async handleSession<TAction extends "create" | "delete">(action: TAction, session: string) {
if (action === "create") {
const { error } = await this.supabase.from("tg-bot-sessions").delete().single();
if (error) {
throw new Error("Failed to delete existing session");
}
await this.storeData({ session });
} else {
await this.storeData({ session: null });
Expand Down
21 changes: 19 additions & 2 deletions src/bot/mtproto-api/bot/scripts/sms-auth/auth-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,25 @@ export class AuthHandler {
onError: (err: unknown) => console.error("Error during login:", { err }),
});

const data = await this._supabase?.from("tg-bot-sessions").insert([{ session_data: mtProto.session?.save() }]);
if (!this._supabase) {
throw new Error("Supabase client is not initialized");
}

const { data: existingSessions } = await this._supabase.from("tg-bot-sessions").select("*");

if (existingSessions?.length) {
for (const sessionData of existingSessions || []) {
const { error } = await this._supabase.from("tg-bot-sessions").delete().eq("id", sessionData.id);

if (error) {
logger.error("Failed to delete session", { sessionData, er: error });
}
}
}

const { error } = await this._supabase.from("tg-bot-sessions").insert([{ session_data: mtProto.session?.save() }]);

if (data?.error) {
if (error) {
throw new Error("Failed to save session data to Supabase.");
}

Expand All @@ -97,5 +113,6 @@ export class AuthHandler {
} catch (err) {
logger.error("Failed to log in:", { err });
}
process.exit(1);
}
}
7 changes: 2 additions & 5 deletions src/bot/setcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,11 @@ function getGroupChatCommands(): BotCommand[] {
}

export async function setCommandsHandler(ctx: CommandContext<GrammyContext>) {

const updatedCommands = {
private_chat: false,
group_chat: false,
owner: false,
}
};

// set private chat commands
try {
Expand All @@ -140,7 +139,6 @@ export async function setCommandsHandler(ctx: CommandContext<GrammyContext>) {
logger.error("Error setting private chat commands", { err });
}


try {
// set group chat commands
await ctx.api.setMyCommands(getGroupChatCommands(), {
Expand All @@ -154,7 +152,6 @@ export async function setCommandsHandler(ctx: CommandContext<GrammyContext>) {
logger.error("Error setting group chat commands", { err });
}


// set private chat commands for owner
try {
await ctx.api.setMyCommands([...getPrivateChatCommands(), ...getPrivateChatAdminCommands()], {
Expand All @@ -174,7 +171,7 @@ export async function setCommandsHandler(ctx: CommandContext<GrammyContext>) {
`Private chat commands: ${updatedCommands.private_chat ? "✅" : "❌"}`,
`Group chat commands: ${updatedCommands.group_chat ? "✅" : "❌"}`,
`Owner commands: ${updatedCommands.owner ? "✅" : "❌"}`,
]
];

return ctx.reply(msgParts.join("\n"));
}

0 comments on commit 009f22e

Please sign in to comment.