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:
@@ -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 });
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import userAuth from "@/src/functions/backend/auth/user-auth";
|
||||
import ensureCoordinatorBot from "@/src/functions/backend/db/bots/ensure-coordinator-bot";
|
||||
import grabBots from "@/src/functions/backend/db/bots/grab-bots";
|
||||
import grabReachableConversation from "@/src/functions/backend/db/bot-sessions/grab-reachable-conversation";
|
||||
import ensureMainComputer from "@/src/functions/backend/db/computers/ensure-main-computer";
|
||||
import grabUserWorkspaces from "@/src/functions/backend/db/workspaces/grab-user-workspaces";
|
||||
import type { PagePropsType } from "@/src/types";
|
||||
@@ -8,12 +9,15 @@ import crewRole from "@/src/utils/rbac/crew-role";
|
||||
import type { BunextPageServerFn } from "@moduletrace/bunext/types";
|
||||
import railBots from "@/src/utils/rail-bots";
|
||||
import railWorkspaces from "@/src/utils/rail-workspaces";
|
||||
import readLastVisitedBot from "@/src/utils/read-last-visited-bot";
|
||||
|
||||
/**
|
||||
* Opens a conversation. The super admin lands on the coordinator, which
|
||||
* (with the main computer) is created first if the install has none.
|
||||
* Anyone else lands on the latest bot they chat with, else their first
|
||||
* workspace; with neither, the page says to ask an admin.
|
||||
* Opens the conversation this browser was last in, from the last-visited
|
||||
* cookies the conversation pages set, while the user still reaches it.
|
||||
* Otherwise the super admin lands on the coordinator, which (with the main
|
||||
* computer) is created first if the install has none; anyone else lands on
|
||||
* the latest bot they chat with, else their first workspace; with neither,
|
||||
* the page says to ask an admin.
|
||||
*/
|
||||
export const server: BunextPageServerFn<PagePropsType> = async ({ req }) => {
|
||||
const { user, user_types } = await userAuth({ req });
|
||||
@@ -30,9 +34,17 @@ export const server: BunextPageServerFn<PagePropsType> = async ({ req }) => {
|
||||
const coordinator_id = await ensureCoordinatorBot({ user_id: user.id });
|
||||
await ensureMainComputer({ user_id: user.id, coordinator_id });
|
||||
|
||||
const last = await grabLastVisited({ user_id: user.id, req });
|
||||
|
||||
if (last) return { redirect: { destination: last } };
|
||||
|
||||
return { redirect: { destination: `/admin/bots/${coordinator_id}` } };
|
||||
}
|
||||
|
||||
const last = await grabLastVisited({ user_id: user.id, req });
|
||||
|
||||
if (last) return { redirect: { destination: last } };
|
||||
|
||||
const [bots, workspaces] = await Promise.all([
|
||||
grabBots({ user_id: user.id }),
|
||||
grabUserWorkspaces({ user_id: user.id }),
|
||||
@@ -48,3 +60,26 @@ export const server: BunextPageServerFn<PagePropsType> = async ({ req }) => {
|
||||
|
||||
return { props: { bots: railBots({ bots }), workspaces: railWorkspaces({ workspaces }) } };
|
||||
};
|
||||
|
||||
/**
|
||||
* The path of the conversation this browser was last in, if the user still
|
||||
* reaches it (`grabReachableConversation`, the conversation pages' own
|
||||
* access check). Null sends the page on to its usual fallback.
|
||||
*/
|
||||
async function grabLastVisited({ user_id, req }: { user_id: number | string; req: Request }) {
|
||||
const visited = readLastVisitedBot({ req });
|
||||
|
||||
if (!visited) return null;
|
||||
|
||||
const conversation = await grabReachableConversation({
|
||||
user_id,
|
||||
bot_id: visited.bot_id,
|
||||
workspace_id: visited.workspace_id,
|
||||
});
|
||||
|
||||
if (!conversation) return null;
|
||||
|
||||
return conversation.workspace_id
|
||||
? `/admin/workspaces/${conversation.workspace_id}/bots/${conversation.bot_id}`
|
||||
: `/admin/bots/${conversation.bot_id}`;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
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 canManageCrew from "@/src/utils/rbac/can-manage-crew";
|
||||
import type { BunextPageServerFn } from "@moduletrace/bunext/types";
|
||||
@@ -8,7 +9,8 @@ import type { BunextPageServerFn } from "@moduletrace/bunext/types";
|
||||
* A bot's conversation inside a workspace: the bot works with the
|
||||
* workspace's context, computers and models. Only the coordinator and the
|
||||
* workspace's bots have one; anyone else goes back to the workspace
|
||||
* settings (managers) or their bots.
|
||||
* settings (managers) or their bots. 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;
|
||||
@@ -24,7 +26,7 @@ export const server: BunextPageServerFn<PagePropsType> = async (params) => {
|
||||
workspace_id: String(query.workspace_id),
|
||||
});
|
||||
|
||||
if (!props) {
|
||||
if (!props?.bot?.id) {
|
||||
return {
|
||||
redirect: {
|
||||
destination: canManageCrew({ user_types })
|
||||
@@ -34,5 +36,13 @@ export const server: BunextPageServerFn<PagePropsType> = async (params) => {
|
||||
};
|
||||
}
|
||||
|
||||
return { props };
|
||||
const bot_id = props.bot.id;
|
||||
const workspace_id = props.workspace?.id ?? query.workspace_id;
|
||||
|
||||
return {
|
||||
props,
|
||||
res_transform(res) {
|
||||
return withLastVisitedBotCookies({ req: params.req, res, bot_id, workspace_id });
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user