Skip to content

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

Show Delete on a room document only to people who can write to the room

Abel Dantasd1df3de1bbf7
diff --git a/gateway/src/pages/rooms/RoomScopedDocumentView.test.tsx b/gateway/src/pages/rooms/RoomScopedDocumentView.test.tsx
new file mode 100644
index 000000000..bf08e281c
--- /dev/null
+++ b/gateway/src/pages/rooms/RoomScopedDocumentView.test.tsx
@@ -0,0 +1,94 @@
+import { cleanup, render, screen } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import type { DocumentDetail, RoomDetail } from "../../data/api";
+import { useAuth } from "../../data/AuthContext";
+import * as hooks from "../../data/hooks";
+import RoomScopedDocumentView from "./RoomScopedDocumentView";
+
+// Deleting a room's document is a write to the room: its owner, an admin, or
+// anyone in a publicly writable room. A reader sees the document only.
+
+vi.mock("react-router", () => ({
+ useNavigate: () => vi.fn(),
+ useParams: () => ({ roomId: "r1", documentId: "d1" }),
+}));
+
+vi.mock("../../data/AuthContext", () => ({
+ useAuth: vi.fn(),
+}));
+
+vi.mock("../../data/hooks", () => ({
+ useRoom: vi.fn(),
+ useRoomDocument: vi.fn(),
+}));
+
+const doc = {
+ id: "d1",
+ name: "notes",
+ description: "",
+ instructions: "",
+ content: "hello",
+ scope: "room",
+} as unknown as DocumentDetail;
+
+const query = <T,>(data: T | null) => ({
+ data,
+ loading: false,
+ error: null,
+ refetch: vi.fn(),
+});
+
+function showDocument(room: Partial<RoomDetail> | null, canAdminTenant = false) {
+ vi.mocked(useAuth).mockReturnValue({
+ user: { id: "u1" },
+ canAdminTenant,
+ } as unknown as ReturnType<typeof useAuth>);
+ vi.mocked(hooks.useRoomDocument).mockReturnValue(query(doc));
+ vi.mocked(hooks.useRoom).mockReturnValue(
+ query(
+ room && ({
+ id: "r1",
+ owner_id: "someone",
+ read_visibility: "public",
+ write_visibility: "private",
+ ...room,
+ } as RoomDetail),
+ ),
+ );
+ render(<RoomScopedDocumentView />);
+}
+
+afterEach(cleanup);
+
+describe("room document delete", () => {
+ it("is not offered to a reader of the room", () => {
+ showDocument({});
+
+ expect(screen.getByText("hello")).toBeTruthy();
+ expect(screen.queryByRole("button", { name: "Delete" })).toBeNull();
+ });
+
+ it("is not offered before the room has loaded", () => {
+ showDocument(null);
+
+ expect(screen.queryByRole("button", { name: "Delete" })).toBeNull();
+ });
+
+ it("is offered to the room's owner", () => {
+ showDocument({ owner_id: "u1" });
+
+ expect(screen.getByRole("button", { name: "Delete" })).toBeTruthy();
+ });
+
+ it("is offered in a publicly writable room", () => {
+ showDocument({ write_visibility: "public" });
+
+ expect(screen.getByRole("button", { name: "Delete" })).toBeTruthy();
+ });
+
+ it("is offered to an admin", () => {
+ showDocument({}, true);
+
+ expect(screen.getByRole("button", { name: "Delete" })).toBeTruthy();
+ });
+});
diff --git a/gateway/src/pages/rooms/RoomScopedDocumentView.tsx b/gateway/src/pages/rooms/RoomScopedDocumentView.tsx
index c1e51e03c..ab0be1c81 100644
--- a/gateway/src/pages/rooms/RoomScopedDocumentView.tsx
+++ b/gateway/src/pages/rooms/RoomScopedDocumentView.tsx
@@ -15,7 +15,9 @@ import {
import { useState } from "react";
import { useNavigate, useParams } from "react-router";
import { detachDocumentFromRoom } from "../../data/api";
-import { useRoomDocument } from "../../data/hooks";
+import { useAuth } from "../../data/AuthContext";
+import { useRoom, useRoomDocument } from "../../data/hooks";
+import { canWriteRoom } from "./roomAccess";
export default function RoomScopedDocumentView() {
const { roomId, documentId } = useParams<{
@@ -24,6 +26,11 @@ export default function RoomScopedDocumentView() {
}>();
const navigate = useNavigate();
const { data: doc, loading, error } = useRoomDocument(roomId, documentId);
+ // Deleting a room document (or detaching a shared one) is a write to the
+ // room, so it is offered only to those who may write to it.
+ const { data: room } = useRoom(roomId);
+ const { user, canAdminTenant } = useAuth();
+ const canWrite = !!room && canWriteRoom(room, user?.id, canAdminTenant);
const [deleting, setDeleting] = useState(false);
const [deleteError, setDeleteError] = useState<string | null>(null);
@@ -132,19 +139,23 @@ export default function RoomScopedDocumentView() {
</Paper>
</Stack>
- <Divider />
+ {canWrite && (
+ <>
+ <Divider />
- {deleteError && <Alert severity="error">{deleteError}</Alert>}
- <Stack direction="row" justifyContent="flex-end">
- <Button
- color="error"
- startIcon={<DeleteOutline />}
- onClick={handleDelete}
- disabled={deleting}
- >
- Delete
- </Button>
- </Stack>
+ {deleteError && <Alert severity="error">{deleteError}</Alert>}
+ <Stack direction="row" justifyContent="flex-end">
+ <Button
+ color="error"
+ startIcon={<DeleteOutline />}
+ onClick={handleDelete}
+ disabled={deleting}
+ >
+ Delete
+ </Button>
+ </Stack>
+ </>
+ )}
</Stack>
</Paper>
</Box>