mirror of
https://github.com/outline/outline.git
synced 2026-12-01 01:14:11 +03:00
fix: Address review feedback on sidebar links
This commit is contained in:
8 files changed
+196
-41
No files matched your search
@@ -631,7 +631,9 @@ export const addLink = createActionWithChildren({
|
||||
icon: <LinkIcon />,
|
||||
keywords: "url bookmark",
|
||||
visible: ({ activeDocumentId, stores }) =>
|
||||
canAddLinkBeside(stores, activeDocumentId),
|
||||
!!getLinkLocation(stores, activeDocumentId)?.collection.isManualSort &&
|
||||
(canAddLinkBeside(stores, activeDocumentId) ||
|
||||
canAddLinkInside(stores, activeDocumentId)),
|
||||
children: [addLinkAbove, addLinkBelow, addNestedLink],
|
||||
});
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { observer } from "mobx-react";
|
||||
import * as React from "react";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
import { isLinkNode } from "@shared/utils/collections";
|
||||
import { useActiveSidebarContext } from "~/hooks/useActiveSidebarContext";
|
||||
import useKeyDown from "~/hooks/useKeyDown";
|
||||
import useStores from "~/hooks/useStores";
|
||||
@@ -60,14 +61,10 @@ export const SidebarNavigationProvider = observer(
|
||||
}
|
||||
|
||||
// Moving up into a collapsed row goes to its last descendant.
|
||||
let target: Pick<SidebarNavigationItem, "url" | "title"> = item;
|
||||
if (offset < 0 && item.collapsedChildren?.length) {
|
||||
let node = item.collapsedChildren[item.collapsedChildren.length - 1];
|
||||
while (node.children.length) {
|
||||
node = node.children[node.children.length - 1];
|
||||
}
|
||||
target = node;
|
||||
}
|
||||
const target: Pick<SidebarNavigationItem, "url" | "title"> =
|
||||
(offset < 0 && item.collapsedChildren
|
||||
? lastDocumentDescendant(item.collapsedChildren)
|
||||
: undefined) ?? item;
|
||||
|
||||
event.preventDefault();
|
||||
history.push({
|
||||
@@ -159,3 +156,22 @@ const isAltArrow = (event: KeyboardEvent) =>
|
||||
|
||||
// The shortcut also works while the document editor has focus.
|
||||
const keyDownOptions = { allowInTextInput: true };
|
||||
|
||||
/**
|
||||
* Returns the last document shown below the given nodes when expanded,
|
||||
* skipping links, which are not pages in the app.
|
||||
*
|
||||
* @param nodes The child nodes of a collapsed row.
|
||||
* @returns the last document descendant, or undefined if there is none.
|
||||
*/
|
||||
function lastDocumentDescendant(
|
||||
nodes: NavigationNode[]
|
||||
): NavigationNode | undefined {
|
||||
for (let i = nodes.length - 1; i >= 0; i--) {
|
||||
const node = nodes[i];
|
||||
if (!isLinkNode(node)) {
|
||||
return lastDocumentDescendant(node.children) ?? node;
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -483,7 +483,10 @@ export function useDropToReparentDocument(
|
||||
if (monitor.getItemType() === LinkDragType) {
|
||||
const collectionId =
|
||||
collection?.id ?? node.collectionId ?? document?.collectionId;
|
||||
return item.collectionId === collectionId && !!document?.isActive;
|
||||
// Optimistic when the document is not loaded, the server validates.
|
||||
return (
|
||||
item.collectionId === collectionId && (!document || document.isActive)
|
||||
);
|
||||
}
|
||||
|
||||
if (!policies.abilities(item.id).move) {
|
||||
|
||||
@@ -399,7 +399,8 @@ export default class Collection extends ParanoidModel {
|
||||
invariant(res?.data, "Data should be available");
|
||||
|
||||
runInAction(() => {
|
||||
if (!this.documents) {
|
||||
// The websocket refresh may already contain the link.
|
||||
if (!this.documents || this.childrenByDocumentId.has(res.data.id)) {
|
||||
return;
|
||||
}
|
||||
const documents = insertNavigationNode(
|
||||
|
||||
@@ -1294,6 +1294,85 @@ describe("#collections.move_link", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("nested links with document access", () => {
|
||||
it("should allow a document member to change links below the document", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const collection = await buildCollection({
|
||||
teamId: admin.teamId,
|
||||
userId: admin.id,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: admin.teamId,
|
||||
userId: admin.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const member = await buildUser({ teamId: admin.teamId });
|
||||
await UserMembership.create({
|
||||
documentId: document.id,
|
||||
userId: member.id,
|
||||
createdById: admin.id,
|
||||
permission: DocumentPermission.ReadWrite,
|
||||
});
|
||||
|
||||
const created = await server.post("/api/collections.add_link", member, {
|
||||
body: {
|
||||
id: collection.id,
|
||||
parentDocumentId: document.id,
|
||||
title: "Status page",
|
||||
url: "https://status.example.com",
|
||||
},
|
||||
});
|
||||
expect(created.status).toEqual(200);
|
||||
const link = (await created.json()).data;
|
||||
|
||||
const updated = await server.post("/api/collections.update_link", member, {
|
||||
body: { id: collection.id, linkId: link.id, title: "Status" },
|
||||
});
|
||||
expect(updated.status).toEqual(200);
|
||||
|
||||
// The root of the private collection is not available to the member.
|
||||
const moved = await server.post("/api/collections.move_link", member, {
|
||||
body: { id: collection.id, linkId: link.id, parentDocumentId: null },
|
||||
});
|
||||
expect(moved.status).toEqual(403);
|
||||
|
||||
const removed = await server.post("/api/collections.remove_link", member, {
|
||||
body: { id: collection.id, linkId: link.id },
|
||||
});
|
||||
expect(removed.status).toEqual(200);
|
||||
});
|
||||
|
||||
it("should not allow other users to change a nested link", async () => {
|
||||
const admin = await buildAdmin();
|
||||
const collection = await buildCollection({
|
||||
teamId: admin.teamId,
|
||||
userId: admin.id,
|
||||
permission: null,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: admin.teamId,
|
||||
userId: admin.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const created = await server.post("/api/collections.add_link", admin, {
|
||||
body: {
|
||||
id: collection.id,
|
||||
parentDocumentId: document.id,
|
||||
title: "Status page",
|
||||
url: "https://status.example.com",
|
||||
},
|
||||
});
|
||||
const link = (await created.json()).data;
|
||||
const other = await buildUser({ teamId: admin.teamId });
|
||||
|
||||
const res = await server.post("/api/collections.remove_link", other, {
|
||||
body: { id: collection.id, linkId: link.id },
|
||||
});
|
||||
expect(res.status).toEqual(403);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#collections.remove_link", () => {
|
||||
it("should remove a link", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
@@ -1097,8 +1097,32 @@ router.post(
|
||||
|
||||
/**
|
||||
* Authorizes the user to change links at the given location, with the same
|
||||
* rules as creating a document there, and loads the collection with its
|
||||
* structure locked for update.
|
||||
* rules as creating a document there.
|
||||
*
|
||||
* @param ctx the API context.
|
||||
* @param collectionId the id of the collection.
|
||||
* @param parentDocumentId the id of the parent document, if any.
|
||||
* @throws AuthorizationError if the user may not change links there.
|
||||
*/
|
||||
async function authorizeLinkLocation(
|
||||
ctx: APIContext,
|
||||
collectionId: string,
|
||||
parentDocumentId: string | null | undefined
|
||||
) {
|
||||
const { collection } = await authorizeDocumentCreate(ctx, {
|
||||
collectionId,
|
||||
parentDocumentId,
|
||||
});
|
||||
|
||||
// With a parent document, the collection is resolved from the parent.
|
||||
if (collection?.id !== collectionId) {
|
||||
throw InvalidRequestError("Parent document is not in the collection");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Authorizes the user to add a link at the given location and loads the
|
||||
* collection with its structure locked for update.
|
||||
*
|
||||
* @param ctx the API context.
|
||||
* @param collectionId the id of the collection.
|
||||
@@ -1111,33 +1135,25 @@ async function loadCollectionForLink(
|
||||
parentDocumentId: string | null | undefined
|
||||
) {
|
||||
const { transaction } = ctx.state;
|
||||
const { collection } = await authorizeDocumentCreate(ctx, {
|
||||
collectionId,
|
||||
parentDocumentId,
|
||||
});
|
||||
await authorizeLinkLocation(ctx, collectionId, parentDocumentId);
|
||||
|
||||
// With a parent document, the collection is resolved from the parent.
|
||||
if (collection?.id !== collectionId) {
|
||||
throw InvalidRequestError("Parent document is not in the collection");
|
||||
}
|
||||
|
||||
const locked = await Collection.findByPk(collection.id, {
|
||||
const collection = await Collection.findByPk(collectionId, {
|
||||
includeDocumentStructure: true,
|
||||
rejectOnEmpty: true,
|
||||
transaction,
|
||||
lock: transaction.LOCK.NO_KEY_UPDATE,
|
||||
});
|
||||
|
||||
if (parentDocumentId && !locked.getDocumentTree(parentDocumentId)) {
|
||||
if (parentDocumentId && !collection.getDocumentTree(parentDocumentId)) {
|
||||
throw InvalidRequestError("Parent document is not in the collection");
|
||||
}
|
||||
|
||||
return locked;
|
||||
return collection;
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads the collection that contains the given link, after authorizing the
|
||||
* user to change links at its location.
|
||||
* Loads the collection that contains the given link, locked for update, and
|
||||
* authorizes the user to change links at the current location of the link.
|
||||
*
|
||||
* @param ctx the API context.
|
||||
* @param collectionId the id of the collection.
|
||||
@@ -1150,27 +1166,25 @@ async function loadCollectionForExistingLink(
|
||||
collectionId: string,
|
||||
linkId: string
|
||||
) {
|
||||
const { transaction } = ctx.state;
|
||||
const { user } = ctx.state.auth;
|
||||
const existing = await Collection.findByPk(collectionId, {
|
||||
userId: user.id,
|
||||
|
||||
// Locked before the lookup, so that the link cannot move to a parent that
|
||||
// was not authorized.
|
||||
const collection = await Collection.findByPk(collectionId, {
|
||||
includeDocumentStructure: true,
|
||||
transaction: ctx.state.transaction,
|
||||
transaction,
|
||||
lock: transaction.LOCK.NO_KEY_UPDATE,
|
||||
});
|
||||
authorize(user, "readDocument", existing);
|
||||
|
||||
const link = existing.findLinkInStructure(linkId);
|
||||
if (!link) {
|
||||
const link = collection?.findLinkInStructure(linkId);
|
||||
if (!collection || !link) {
|
||||
authorize(user, "readDocument", collection);
|
||||
throw NotFoundError("Link not found");
|
||||
}
|
||||
|
||||
const collection = await loadCollectionForLink(
|
||||
ctx,
|
||||
existing.id,
|
||||
link.parentDocumentId
|
||||
);
|
||||
if (!collection.findLinkInStructure(linkId)) {
|
||||
throw NotFoundError("Link not found");
|
||||
}
|
||||
// A nested link is authorized through its parent document, as when added.
|
||||
await authorizeLinkLocation(ctx, collection.id, link.parentDocumentId);
|
||||
return collection;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { NavigationNodeType } from "@shared/types";
|
||||
import { navigationNodeToSitemap } from "./sitemap";
|
||||
|
||||
describe("navigationNodeToSitemap", () => {
|
||||
it("should not include links", () => {
|
||||
const xml = navigationNodeToSitemap(
|
||||
{
|
||||
id: "root",
|
||||
title: "Root",
|
||||
url: "",
|
||||
children: [
|
||||
{
|
||||
id: "doc",
|
||||
title: "Doc",
|
||||
url: "/doc/doc-abc123defg",
|
||||
children: [
|
||||
{
|
||||
id: "link",
|
||||
title: "Link",
|
||||
url: "https://example.com",
|
||||
type: NavigationNodeType.Link,
|
||||
children: [],
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
"https://outline.example/s/share"
|
||||
);
|
||||
|
||||
expect(xml).toContain("https://outline.example/s/share/doc/doc-abc123defg");
|
||||
expect(xml).not.toContain("example.com<");
|
||||
expect(xml).not.toContain("sharehttps://");
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,6 @@
|
||||
import { escape } from "es-toolkit/compat";
|
||||
import type { NavigationNode } from "@shared/types";
|
||||
import { isLinkNode } from "@shared/utils/collections";
|
||||
|
||||
/**
|
||||
* Converts a navigation tree to a sitemap XML string, by traversing the nodes.
|
||||
@@ -15,6 +16,10 @@ export function navigationNodeToSitemap(
|
||||
const urls: string[] = [];
|
||||
|
||||
function collectUrls(node: NavigationNode, urls: string[]) {
|
||||
// Links point to other pages, which are not part of the share.
|
||||
if (isLinkNode(node)) {
|
||||
return;
|
||||
}
|
||||
urls.push(`${baseUrl}${node.url}`);
|
||||
if (node.children) {
|
||||
node.children.forEach((child) => collectUrls(child, urls));
|
||||
|
||||
Reference in new issue
Block a user