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.
This commit is contained in:
2026-10-22 11:18:19 +01:00
parent a8d8b680ad
commit 16f1e7904a
14 changed files with 402 additions and 10 deletions
+14 -2
View File
@@ -1,8 +1,13 @@
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 });
@@ -20,7 +25,7 @@ export const server: BunextPageServerFn<PagePropsType> = async (params) => {
bot_id: String(query.bot_id),
});
if (!props) {
if (!props?.bot?.id) {
return {
redirect: {
destination: "/admin/bots",
@@ -28,5 +33,12 @@ export const server: BunextPageServerFn<PagePropsType> = async (params) => {
};
}
return { props };
const bot_id = props.bot.id;
return {
props,
res_transform(res) {
return withLastVisitedBotCookies({ req: params.req, res, bot_id });
},
};
};