Skip to content

Open-source AI teammates in Slack, Teams, Discord, Telegram and Mattermost. Self-host it, with any agent harness and any model.

Room resources: "Open in Resources" only for a document the viewer may open

Simon Flackfd5f7b48a60a
diff --git a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.rooms.test.ts b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.rooms.test.ts
index 976c3aa94..33b9e5332 100644
--- a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.rooms.test.ts
+++ b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.rooms.test.ts
@@ -311,6 +311,8 @@ describe('room resources', () => {
roomScoped: false,
createdByAgentName: null,
packageIds: ['pkg-1'],
+ ownerId: null,
+ readVisibility: 'private',
});
});
diff --git a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
index 8fb71b227..e52188c31 100644
--- a/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
+++ b/console/apps/switch-console-desktop/src/main/core/switch-servers/gateway-client.ts
@@ -5143,6 +5143,9 @@ type ResourceJson = {
created_by_agent_name?: string | null;
can_remove?: boolean;
content?: string;
+ owner_id?: string | null;
+ read_visibility?: string;
+ can_read?: boolean;
};
function mapResource(kind: RoomResourceKind, r: ResourceJson): RoomResource {
@@ -5161,6 +5164,9 @@ function mapResource(kind: RoomResourceKind, r: ResourceJson): RoomResource {
packageIds: r.packages ?? [],
...(r.can_remove !== undefined ? { canRemove: r.can_remove } : {}),
...(kind === 'documents' && typeof r.content === 'string' ? { content: r.content } : {}),
+ ownerId: r.owner_id ?? null,
+ readVisibility: r.read_visibility === 'public' ? 'public' : 'private',
+ ...(r.can_read !== undefined ? { canRead: r.can_read } : {}),
};
}
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources-panel.tsx b/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources-panel.tsx
index c83359eeb..fb4197e3c 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources-panel.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources-panel.tsx
@@ -5,6 +5,8 @@ import { type ReactNode, useState } from 'react';
import { opensAsConsoleChat } from '@renderer/features/chats/console-chat-redirect';
import { chatsStore } from '@renderer/features/chats/stores/chats';
import { switchRoomsStore } from '@renderer/features/switch-servers/switch-rooms-store';
+import { switchServersStore } from '@renderer/features/switch-servers/switch-servers-store';
+import { administersWorkspaceById } from '@renderer/features/switch-servers/workspace-admin';
import { workspacesStore } from '@renderer/features/workspaces/workspaces-store';
import { failureText } from '@renderer/lib/errors/describe-failure';
import { rpc } from '@renderer/lib/ipc';
@@ -22,7 +24,7 @@ import { Input } from '@renderer/lib/ui/input';
import { MarkdownRenderer } from '@renderer/lib/ui/markdown-renderer';
import type { RoomResource, RoomResourceKind } from '@shared/core/switch-servers/switch-servers';
import { useRoomDetail } from './room-participants';
-import { attachableResources, resourcesByOrigin } from './room-resources';
+import { attachableResources, opensInResources, resourcesByOrigin } from './room-resources';
const KIND_LABEL: Record<RoomResourceKind, { title: string; noun: string; none: string }> = {
references: { title: 'References', noun: 'reference', none: 'No references attached.' },
@@ -119,6 +121,11 @@ const ResourceSection = observer(function ResourceSection({
const queryClient = useQueryClient();
const { navigate } = useNavigate();
const serverId = workspacesStore.serverIdFor(workspaceId);
+ // Who is looking, to tell which attached items their Resources page opens for.
+ const viewer = {
+ id: serverId ? (switchServersStore.statusFor(serverId)?.user?.id ?? null) : null,
+ isAdmin: administersWorkspaceById(workspaceId),
+ };
const key = roomResourcesKey(workspaceId, roomId, kind);
const attached = useQuery({
queryKey: key,
@@ -251,7 +258,7 @@ const ResourceSection = observer(function ResourceSection({
document={reading}
onClose={() => setReading(null)}
onOpenInResources={
- serverId !== null && !reading.roomScoped
+ serverId !== null && opensInResources(reading, viewer)
? () => {
setReading(null);
navigate('libraryItem', { serverId, kind: 'documents', itemId: reading.id });
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources.test.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources.test.ts
index bbbcf3e88..0ecdd622a 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources.test.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import type { RoomResource } from '@shared/core/switch-servers/switch-servers';
-import { attachableResources, resourcesByOrigin } from './room-resources';
+import { attachableResources, opensInResources, resourcesByOrigin } from './room-resources';
const resource = (overrides: Partial<RoomResource>): RoomResource => ({
id: 'r',
@@ -12,6 +12,8 @@ const resource = (overrides: Partial<RoomResource>): RoomResource => ({
roomScoped: false,
createdByAgentName: null,
packageIds: [],
+ ownerId: 'owner',
+ readVisibility: 'public',
...overrides,
});
@@ -37,3 +39,29 @@ describe('a room’s resources', () => {
expect(attachableResources(library, attached, ' DESIGN ').map((r) => r.id)).toEqual(['a']);
});
});
+
+describe('opening an attached item in Resources', () => {
+ const viewer = { id: 'me', isAdmin: false };
+
+ it('opens one everyone may read, or the viewer owns, or for an admin', () => {
+ expect(opensInResources(resource({}), viewer)).toBe(true);
+ expect(opensInResources(resource({ readVisibility: 'private', ownerId: 'me' }), viewer)).toBe(
+ true
+ );
+ expect(
+ opensInResources(resource({ readVisibility: 'private' }), { id: 'me', isAdmin: true })
+ ).toBe(true);
+ });
+
+ it('does not open someone else’s private item, or a document written in the room', () => {
+ expect(opensInResources(resource({ readVisibility: 'private' }), viewer)).toBe(false);
+ expect(opensInResources(resource({ roomScoped: true }), viewer)).toBe(false);
+ });
+
+ it('takes the server’s word where it gives one', () => {
+ expect(opensInResources(resource({ canRead: false }), viewer)).toBe(false);
+ expect(opensInResources(resource({ canRead: true, readVisibility: 'private' }), viewer)).toBe(
+ true
+ );
+ });
+});
diff --git a/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources.ts b/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources.ts
index 95dca7087..1eabb89e1 100644
--- a/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources.ts
+++ b/console/apps/switch-console-desktop/src/renderer/features/switch-rooms/room-resources.ts
@@ -36,3 +36,22 @@ export function attachableResources(
)
.sort((a, b) => a.name.localeCompare(b.name));
}
+
+/**
+ * Whether the viewer may open an attached item's own page in Resources. A
+ * room shows everything attached to it to every reader, including an item
+ * its owner keeps private, whose page would refuse them. The server says so
+ * where it can; otherwise its own rule: owner, admin, or readable by all.
+ */
+export function opensInResources(
+ resource: Pick<RoomResource, 'roomScoped' | 'canRead' | 'ownerId' | 'readVisibility'>,
+ viewer: { id: string | null; isAdmin: boolean }
+): boolean {
+ if (resource.roomScoped) return false;
+ if (resource.canRead !== undefined) return resource.canRead;
+ return (
+ viewer.isAdmin ||
+ resource.readVisibility === 'public' ||
+ (viewer.id !== null && resource.ownerId === viewer.id)
+ );
+}
diff --git a/console/apps/switch-console-desktop/src/renderer/tests/browser/room-resources-panel.test.tsx b/console/apps/switch-console-desktop/src/renderer/tests/browser/room-resources-panel.test.tsx
index 8d7edb9f2..5534eed1b 100644
--- a/console/apps/switch-console-desktop/src/renderer/tests/browser/room-resources-panel.test.tsx
+++ b/console/apps/switch-console-desktop/src/renderer/tests/browser/room-resources-panel.test.tsx
@@ -51,6 +51,12 @@ vi.mock('@renderer/lib/layout/navigation-provider', () => ({
vi.mock('@renderer/features/workspaces/workspaces-store', () => ({
workspacesStore: { serverIdFor: () => 'srv-1', activeId: 'ws-1' },
}));
+vi.mock('@renderer/features/switch-servers/switch-servers-store', () => ({
+ switchServersStore: { statusFor: () => ({ user: { id: 'me' } }) },
+}));
+vi.mock('@renderer/features/switch-servers/workspace-admin', () => ({
+ administersWorkspaceById: () => false,
+}));
vi.mock('@renderer/lib/hooks/useTheme', () => ({
useTheme: () => ({ effectiveTheme: 'emlight' }),
@@ -59,6 +65,8 @@ vi.mock('@renderer/lib/hooks/useTheme', () => ({
import { RoomResourcesPanel } from '@renderer/features/switch-rooms/room-resources-panel';
const base = {
+ ownerId: 'u-2',
+ readVisibility: 'public',
description: '',
typeLabel: null,
urls: [],
@@ -249,4 +257,22 @@ describe('the Resources tab', () => {
itemId: 'd-1',
});
});
+
+ it('offers no Resources page for a document its owner keeps private', async () => {
+ listRoomResources.mockImplementation(async ({ kind }: { kind: keyof typeof RESOURCES }) =>
+ kind === 'documents'
+ ? [{ ...RESOURCES.documents[0], readVisibility: 'private', content: 'Secret plans' }]
+ : RESOURCES[kind]
+ );
+ const el = await renderPanel();
+ await act(async () =>
+ [...el.querySelectorAll('button')].find((b) => b.textContent?.includes('Guide'))!.click()
+ );
+ for (let i = 0; i < 4; i++) await act(async () => await Promise.resolve());
+ const dialog = document.querySelector('[data-slot="dialog-content"]')!;
+ expect(dialog.textContent).toContain('Secret plans');
+ expect(
+ [...dialog.querySelectorAll('button')].find((b) => b.textContent === 'Open in Resources')
+ ).toBeUndefined();
+ });
});
diff --git a/console/apps/switch-console-desktop/src/shared/core/switch-servers/switch-servers.ts b/console/apps/switch-console-desktop/src/shared/core/switch-servers/switch-servers.ts
index 409c4ebe9..2a2860483 100644
--- a/console/apps/switch-console-desktop/src/shared/core/switch-servers/switch-servers.ts
+++ b/console/apps/switch-console-desktop/src/shared/core/switch-servers/switch-servers.ts
@@ -1383,6 +1383,14 @@ export type RoomResource = {
canRemove?: boolean;
/** A document's content, when the listing carried it (a room's documents do). */
content?: string;
+ /** Who owns it, and whether everyone may read it, for working out if the viewer may open its own page. */
+ ownerId: string | null;
+ readVisibility: 'public' | 'private';
+ /**
+ * Whether the viewer may open it, when the server says (a room's packages:
+ * one its owner attached privately is still listed to every reader).
+ */
+ canRead?: boolean;
};
/** A document read in full, through the room it is attached to. */