Files
cobble/src/pages/admin/bots/[bot_id]/index.server.ts
T
tben 16f1e7904a Open the bot last visited, else the coordinator
`/admin/bots` sent the super admin to the coordinator and everyone else to
their latest bot, which lost the thread each time the tab was reopened. The
conversation pages now record where the browser is in two HttpOnly cookies
(`cobble_last_visited_bot_id`, `..._workspace_id`, a year, `Secure` only over
HTTPS), and the landing page opens that conversation again, in its workspace
when it was one.

A stale cookie can't loop: `grabReachableConversation` re-checks it with
`grabUserConversation`, the gate the conversation pages use, so a revoked
grant, a deleted bot or an archived workspace falls back to the coordinator
(super admin) or the latest bot, first workspace, empty page. Signing out
clears both cookies so the next user on that browser doesn't land in the
previous one's conversation.
2026-10-22 11:18:19 +01:00

45 lines
1.3 KiB
TypeScript

import userAuth from "@/src/functions/backend/auth/user-auth";
import grabConversationProps from "@/src/functions/backend/db/bot-sessions/grab-conversation-props";
import withLastVisitedBotCookies from "@/src/functions/server/with-last-visited-bot-cookies";
import type { PagePropsType, PageQueryObject } from "@/src/types";
import type { BunextPageServerFn } from "@moduletrace/bunext/types";
/**
* A bot's own chat. Rendering it records the conversation in the
* last-visited cookies, so `/admin/bots` opens it again.
*/
export const server: BunextPageServerFn<PagePropsType> = async (params) => {
const query: PageQueryObject = params.query;
const { user } = await userAuth({ req: params.req });
if (!user?.id) {
return {
redirect: {
destination: "/auth/login",
},
};
}
const props = await grabConversationProps({
user_id: Number(user.id),
bot_id: String(query.bot_id),
});
if (!props?.bot?.id) {
return {
redirect: {
destination: "/admin/bots",
},
};
}
const bot_id = props.bot.id;
return {
props,
res_transform(res) {
return withLastVisitedBotCookies({ req: params.req, res, bot_id });
},
};
};