Keep only the bot cookie for the last visited bot
CI / check (push) Canceled after 0s

A workspace conversation is never remembered, so /admin/bots always
reopens the bot's own chat.
This commit is contained in:
2026-10-22 13:39:36 +01:00
parent 133e6b87c9
commit 63a93a4262
13 changed files with 48 additions and 147 deletions
@@ -3,38 +3,30 @@ import grabUserConversation from "./grab-user-conversation";
type Params = {
user_id: number | string;
bot_id: number | string;
/**
* Null or undefined: the bot's own chat.
*/
workspace_id?: number | string | null;
};
export type ReachableConversation = {
bot_id: number;
workspace_id: number | null;
};
/**
* A conversation from the last-visited cookie, only while the user still
* The bot's own chat from the last-visited cookie, only while the user still
* reaches it: `grabUserConversation` is the same access check the
* conversation pages use, so a revoked grant, a deleted bot or an archived
* workspace answers null and the landing page falls back instead of
* redirecting into a page that would bounce it back.
* conversation pages use, so a revoked grant or a deleted bot answers null
* and the landing page falls back instead of redirecting into a page that
* would bounce it back.
*/
export default async function grabReachableConversation({
user_id,
bot_id,
workspace_id,
}: Params): Promise<ReachableConversation | null> {
const resolved_workspace_id = workspace_id ? Number(workspace_id) : null;
const conversation = await grabUserConversation({
user_id,
bot_id,
workspace_id: resolved_workspace_id,
workspace_id: null,
}).catch(() => null);
if (!conversation?.bot.id) return null;
return { bot_id: conversation.bot.id, workspace_id: conversation.workspace_id };
return { bot_id: conversation.bot.id };
}