updates and fixes

* seo friendly urls
* custom client serve-static module
* database fixes
* fix recent pages
* other fixes
This commit is contained in:
Philipinho
2024-05-18 03:19:42 +01:00
parent eefe63d1cd
commit 9c7c2f1163
102 changed files with 921 additions and 536 deletions

View File

@ -4,10 +4,13 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>docmost</title>
<title>Docmost</title>
</head>
<body>
<div id="root"></div>
<!--window-config-->
<script type="module" src="/src/main.tsx"></script>
</body>
</html>

View File

@ -1,7 +1,7 @@
{
"name": "client",
"private": true,
"version": "0.0.0",
"version": "0.1.0",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
@ -31,6 +31,7 @@
"react-arborist": "^3.4.0",
"react-dom": "^18.2.0",
"react-error-boundary": "^4.0.13",
"react-helmet-async": "^2.0.5",
"react-router-dom": "^6.22.3",
"socket.io-client": "^4.7.5",
"tippy.js": "^6.3.7",

View File

@ -65,7 +65,7 @@ export default function App() {
<Route element={<DashboardLayout />}>
<Route path={"/home"} element={<Home />} />
<Route path={"/p/:pageId"} element={<Page />} />
<Route path={"/p/:slugId/:slug?"} element={<Page />} />
</Route>
<Route path={"/settings"} element={<SettingsLayout />}>

View File

@ -14,6 +14,8 @@ import { IconDots } from "@tabler/icons-react";
import { Link, useParams } from "react-router-dom";
import classes from "./breadcrumb.module.css";
import { SpaceTreeNode } from "@/features/page/tree/types.ts";
import { buildPageSlug } from "@/features/page/page.utils.ts";
import { usePageQuery } from "@/features/page/queries/page-query.ts";
function getTitle(name: string, icon: string) {
if (icon) {
@ -27,23 +29,17 @@ export default function Breadcrumb() {
const [breadcrumbNodes, setBreadcrumbNodes] = useState<
SpaceTreeNode[] | null
>(null);
const { pageId } = useParams();
const { slugId } = useParams();
const { data: currentPage } = usePageQuery(slugId);
useEffect(() => {
if (treeData.length) {
const breadcrumb = findBreadcrumbPath(treeData, pageId);
if (treeData?.length > 0 && currentPage) {
const breadcrumb = findBreadcrumbPath(treeData, currentPage.id);
if (breadcrumb) {
setBreadcrumbNodes(breadcrumb);
}
}
}, [pageId, treeData]);
useEffect(() => {
if (treeData.length) {
const breadcrumb = findBreadcrumbPath(treeData, pageId);
if (breadcrumb) setBreadcrumbNodes(breadcrumb);
}
}, [pageId, treeData]);
}, [currentPage?.id, treeData]);
const HiddenNodesTooltipContent = () =>
breadcrumbNodes?.slice(1, -2).map((node) => (
@ -51,7 +47,7 @@ export default function Breadcrumb() {
<Button
justify="start"
component={Link}
to={`/p/${node.id}`}
to={buildPageSlug(node.slugId, node.name)}
variant="default"
style={{ border: "none" }}
>
@ -63,16 +59,14 @@ export default function Breadcrumb() {
const getLastNthNode = (n: number) =>
breadcrumbNodes && breadcrumbNodes[breadcrumbNodes.length - n];
// const getTitle = (title: string) => (title?.length > 0 ? title : "untitled");
const getBreadcrumbItems = () => {
if (breadcrumbNodes?.length > 3) {
return [
<Anchor
component={Link}
to={`/p/${breadcrumbNodes[0].id}`}
to={buildPageSlug(breadcrumbNodes[0].slugId, breadcrumbNodes[0].name)}
underline="never"
key={breadcrumbNodes[0].id}
key={breadcrumbNodes[0].slugId}
>
{getTitle(breadcrumbNodes[0].name, breadcrumbNodes[0].icon)}
</Anchor>,
@ -94,17 +88,17 @@ export default function Breadcrumb() {
</Popover>,
<Anchor
component={Link}
to={`/p/${getLastNthNode(2)?.id}`}
to={buildPageSlug(getLastNthNode(2)?.slugId, getLastNthNode(2)?.name)}
underline="never"
key={getLastNthNode(2)?.id}
key={getLastNthNode(2)?.slugId}
>
{getTitle(getLastNthNode(2)?.name, getLastNthNode(2)?.icon)}
</Anchor>,
<Anchor
component={Link}
to={`/p/${getLastNthNode(1)?.id}`}
to={buildPageSlug(getLastNthNode(1)?.slugId, getLastNthNode(1)?.name)}
underline="never"
key={getLastNthNode(1)?.id}
key={getLastNthNode(1)?.slugId}
>
{getTitle(getLastNthNode(1)?.name, getLastNthNode(1)?.icon)}
</Anchor>,
@ -115,7 +109,7 @@ export default function Breadcrumb() {
return breadcrumbNodes.map((node) => (
<Anchor
component={Link}
to={`/p/${node.id}`}
to={buildPageSlug(node.slugId, node.name)}
underline="never"
key={node.id}
>

View File

@ -1,11 +1,15 @@
import { UserProvider } from "@/features/user/user-provider.tsx";
import Shell from "./shell.tsx";
import { Outlet } from "react-router-dom";
import { Helmet } from "react-helmet-async";
export default function DashboardLayout() {
return (
<UserProvider>
<Shell>
<Helmet>
<title>Home</title>
</Helmet>
<Outlet />
</Shell>
</UserProvider>

View File

@ -1,18 +1,19 @@
import { ActionIcon, Menu, Button, Tooltip } from "@mantine/core";
import { ActionIcon, Menu, Tooltip } from "@mantine/core";
import {
IconDots,
IconFileInfo,
IconHistory,
IconLink,
IconLock,
IconShare,
IconTrash,
IconMessage,
} from "@tabler/icons-react";
import React from "react";
import useToggleAside from "@/hooks/use-toggle-aside.tsx";
import { useAtom } from "jotai";
import { historyAtoms } from "@/features/page-history/atoms/history-atoms.ts";
import { useClipboard } from "@mantine/hooks";
import { useParams } from "react-router-dom";
import { usePageQuery } from "@/features/page/queries/page-query.ts";
import { buildPageSlug } from "@/features/page/page.utils.ts";
import { notifications } from "@mantine/notifications";
export default function Header() {
const toggleAside = useToggleAside();
@ -42,6 +43,16 @@ export default function Header() {
function PageActionMenu() {
const [, setHistoryModalOpen] = useAtom(historyAtoms);
const clipboard = useClipboard({ timeout: 500 });
const { slugId } = useParams();
const { data: page, isLoading, isError } = usePageQuery(slugId);
const handleCopyLink = () => {
const pageLink =
window.location.host + buildPageSlug(page.slugId, page.title);
clipboard.copy(pageLink);
notifications.show({ message: "Link copied" });
};
const openHistoryModal = () => {
setHistoryModalOpen(true);
@ -63,9 +74,13 @@ function PageActionMenu() {
</Menu.Target>
<Menu.Dropdown>
<Menu.Item leftSection={<IconLink size={16} stroke={2} />}>
<Menu.Item
leftSection={<IconLink size={16} stroke={2} />}
onClick={handleCopyLink}
>
Copy link
</Menu.Item>
<Menu.Divider />
<Menu.Item
leftSection={<IconHistory size={16} stroke={2} />}
onClick={openHistoryModal}
@ -73,10 +88,12 @@ function PageActionMenu() {
Page history
</Menu.Item>
{/*
<Menu.Divider />
<Menu.Item leftSection={<IconTrash size={16} stroke={2} />}>
Delete
</Menu.Item>
*/}
</Menu.Dropdown>
</Menu>
);

View File

@ -1,11 +1,15 @@
import { UserProvider } from "@/features/user/user-provider.tsx";
import { Outlet } from "react-router-dom";
import SettingsShell from "@/components/layouts/settings/settings-shell.tsx";
import { Helmet } from "react-helmet-async";
export default function SettingsLayout() {
return (
<UserProvider>
<SettingsShell>
<Helmet>
<title>Settings</title>
</Helmet>
<Outlet />
</SettingsShell>
</UserProvider>

View File

@ -60,7 +60,7 @@ export function InviteSignUpForm() {
<Container size={420} my={40} className={classes.container}>
<Box p="xl" mt={200}>
<Title order={2} ta="center" fw={500} mb="md">
Complete your signup
Join the workspace
</Title>
<Stack align="stretch" justify="center" gap="xl">

View File

@ -0,0 +1,97 @@
import * as React from "react";
import * as z from "zod";
import { useForm, zodResolver } from "@mantine/form";
import {
Container,
Title,
TextInput,
Button,
PasswordInput,
Box,
} from "@mantine/core";
import { ISetupWorkspace } from "@/features/auth/types/auth.types";
import useAuth from "@/features/auth/hooks/use-auth";
import classes from "@/features/auth/components/auth.module.css";
const formSchema = z.object({
workspaceName: z.string().min(2).max(60),
name: z.string().min(2).max(60),
email: z
.string()
.min(1, { message: "email is required" })
.email({ message: "Invalid email address" }),
password: z.string().min(8),
});
export function SetupWorkspaceForm() {
const { setupWorkspace, isLoading } = useAuth();
// useRedirectIfAuthenticated();
const form = useForm<ISetupWorkspace>({
validate: zodResolver(formSchema),
initialValues: {
workspaceName: "",
name: "",
email: "",
password: "",
},
});
async function onSubmit(data: ISetupWorkspace) {
await setupWorkspace(data);
}
return (
<Container size={420} my={40} className={classes.container}>
<Box p="xl" mt={200}>
<Title order={2} ta="center" fw={500} mb="md">
Create workspace
</Title>
<form onSubmit={form.onSubmit(onSubmit)}>
<TextInput
id="workspaceName"
type="text"
label="Workspace Name"
placeholder="e.g ACME Inc"
variant="filled"
mt="md"
{...form.getInputProps("workspaceName")}
/>
<TextInput
id="name"
type="text"
label="Your Name"
placeholder="enter your full name"
variant="filled"
mt="md"
{...form.getInputProps("name")}
/>
<TextInput
id="email"
type="email"
label="Your Email"
placeholder="email@example.com"
variant="filled"
mt="md"
{...form.getInputProps("email")}
/>
<PasswordInput
label="Password"
placeholder="Enter a strong password"
variant="filled"
mt="md"
{...form.getInputProps("password")}
/>
<Button type="submit" fullWidth mt="xl" loading={isLoading}>
Setup workspace
</Button>
</form>
</Box>
</Container>
);
}

View File

@ -1,10 +1,18 @@
import { useState } from "react";
import { login, register } from "@/features/auth/services/auth-service";
import {
login,
register,
setupWorkspace,
} from "@/features/auth/services/auth-service";
import { useNavigate } from "react-router-dom";
import { useAtom } from "jotai";
import { authTokensAtom } from "@/features/auth/atoms/auth-tokens-atom";
import { currentUserAtom } from "@/features/user/atoms/current-user-atom";
import { ILogin, IRegister } from "@/features/auth/types/auth.types";
import {
ILogin,
IRegister,
ISetupWorkspace,
} from "@/features/auth/types/auth.types";
import { notifications } from "@mantine/notifications";
import { IAcceptInvite } from "@/features/workspace/types/workspace.types.ts";
import { acceptInvitation } from "@/features/workspace/services/workspace-service.ts";
@ -76,6 +84,25 @@ export default function useAuth() {
}
};
const handleSetupWorkspace = async (data: ISetupWorkspace) => {
setIsLoading(true);
try {
const res = await setupWorkspace(data);
setIsLoading(false);
setAuthToken(res.tokens);
navigate("/home");
} catch (err) {
setIsLoading(false);
notifications.show({
message: err.response?.data.message,
color: "red",
});
}
};
const handleIsAuthenticated = async () => {
if (!authToken) {
return false;
@ -109,6 +136,7 @@ export default function useAuth() {
signIn: handleSignIn,
signUp: handleSignUp,
invitationSignup: handleInvitationSignUp,
setupWorkspace: handleSetupWorkspace,
isAuthenticated: handleIsAuthenticated,
logout: handleLogout,
hasTokens,

View File

@ -3,6 +3,7 @@ import {
IChangePassword,
ILogin,
IRegister,
ISetupWorkspace,
ITokenResponse,
} from "@/features/auth/types/auth.types";
@ -22,3 +23,10 @@ export async function changePassword(
const req = await api.post<IChangePassword>("/auth/change-password", data);
return req.data;
}
export async function setupWorkspace(
data: ISetupWorkspace,
): Promise<ITokenResponse> {
const req = await api.post<ITokenResponse>("/auth/setup", data);
return req.data;
}

View File

@ -9,6 +9,13 @@ export interface IRegister {
password: string;
}
export interface ISetupWorkspace {
workspaceName: string;
name: string;
email: string;
password: string;
}
export interface ITokens {
accessToken: string;
refreshToken: string;

View File

@ -1,4 +1,4 @@
import React, { useState, useRef } from "react";
import React, { useState, useRef, useCallback, memo } from "react";
import { useParams } from "react-router-dom";
import { Divider, Paper } from "@mantine/core";
import CommentListItem from "@/features/comment/components/comment-list-item";
@ -11,16 +11,67 @@ import CommentEditor from "@/features/comment/components/comment-editor";
import CommentActions from "@/features/comment/components/comment-actions";
import { useFocusWithin } from "@mantine/hooks";
import { IComment } from "@/features/comment/types/comment.types.ts";
import { usePageQuery } from "@/features/page/queries/page-query.ts";
import { IPagination } from "@/lib/types.ts";
function CommentList() {
const { pageId } = useParams();
const { slugId } = useParams();
const { data: page } = usePageQuery(slugId);
const {
data: comments,
isLoading: isCommentsLoading,
isError,
} = useCommentsQuery({ pageId, limit: 100 });
const [isLoading, setIsLoading] = useState(false);
} = useCommentsQuery({ pageId: page?.id, limit: 100 });
const createCommentMutation = useCreateCommentMutation();
const [isLoading, setIsLoading] = useState(false);
const handleAddReply = useCallback(
async (commentId: string, content: string) => {
try {
setIsLoading(true);
const commentData = {
pageId: page?.id,
parentCommentId: commentId,
content: JSON.stringify(content),
};
await createCommentMutation.mutateAsync(commentData);
} catch (error) {
console.error("Failed to post comment:", error);
} finally {
setIsLoading(false);
}
},
[createCommentMutation, page?.id],
);
const renderComments = useCallback(
(comment: IComment) => (
<Paper
shadow="sm"
radius="md"
p="sm"
mb="sm"
withBorder
key={comment.id}
data-comment-id={comment.id}
>
<div>
<CommentListItem comment={comment} />
<MemoizedChildComments comments={comments} parentId={comment.id} />
</div>
<Divider my={4} />
<CommentEditorWithActions
commentId={comment.id}
onSave={handleAddReply}
isLoading={isLoading}
/>
</Paper>
),
[comments, handleAddReply, isLoading],
);
if (isCommentsLoading) {
return <></>;
@ -34,50 +85,6 @@ function CommentList() {
return <>No comments yet.</>;
}
const renderComments = (comment: IComment) => {
const handleAddReply = async (commentId: string, content: string) => {
try {
setIsLoading(true);
const commentData = {
pageId: comment.pageId,
parentCommentId: comment.id,
content: JSON.stringify(content),
};
await createCommentMutation.mutateAsync(commentData);
} catch (error) {
console.error("Failed to post comment:", error);
} finally {
setIsLoading(false);
}
};
return (
<Paper
shadow="sm"
radius="md"
p="sm"
mb="sm"
withBorder
key={comment.id}
data-comment-id={comment.id}
>
<div>
<CommentListItem comment={comment} />
<ChildComments comments={comments} parentId={comment.id} />
</div>
<Divider my={4} />
<CommentEditorWithActions
commentId={comment.id}
onSave={handleAddReply}
isLoading={isLoading}
/>
</Paper>
);
};
return (
<>
{comments.items
@ -87,35 +94,46 @@ function CommentList() {
);
}
const ChildComments = ({ comments, parentId }) => {
const getChildComments = (parentId: string) => {
return comments.items.filter(
(comment: IComment) => comment.parentCommentId === parentId,
);
};
interface ChildCommentsProps {
comments: IPagination<IComment>;
parentId: string;
}
const ChildComments = ({ comments, parentId }: ChildCommentsProps) => {
const getChildComments = useCallback(
(parentId: string) =>
comments.items.filter(
(comment: IComment) => comment.parentCommentId === parentId,
),
[comments.items],
);
return (
<div>
{getChildComments(parentId).map((childComment) => (
<div key={childComment.id}>
<CommentListItem comment={childComment} />
<ChildComments comments={comments} parentId={childComment.id} />
<MemoizedChildComments
comments={comments}
parentId={childComment.id}
/>
</div>
))}
</div>
);
};
const MemoizedChildComments = memo(ChildComments);
const CommentEditorWithActions = ({ commentId, onSave, isLoading }) => {
const [content, setContent] = useState("");
const { ref, focused } = useFocusWithin();
const commentEditorRef = useRef(null);
const handleSave = () => {
const handleSave = useCallback(() => {
onSave(commentId, content);
setContent("");
commentEditorRef.current?.clearContent();
};
}, [commentId, content, onSave]);
return (
<div ref={ref}>

View File

@ -1,20 +1,22 @@
import classes from '@/features/editor/styles/editor.module.css';
import React from 'react';
import { TitleEditor } from '@/features/editor/title-editor';
import PageEditor from '@/features/editor/page-editor';
import classes from "@/features/editor/styles/editor.module.css";
import React from "react";
import { TitleEditor } from "@/features/editor/title-editor";
import PageEditor from "@/features/editor/page-editor";
const MemoizedTitleEditor = React.memo(TitleEditor);
const MemoizedPageEditor = React.memo(PageEditor);
export interface FullEditorProps {
pageId: string;
title: any;
slugId: string;
title: string;
}
export function FullEditor({ pageId, title }: FullEditorProps) {
export function FullEditor({ pageId, title, slugId }: FullEditorProps) {
return (
<div className={classes.editor}>
<TitleEditor pageId={pageId} title={title} />
<PageEditor pageId={pageId} />
<MemoizedTitleEditor pageId={pageId} slugId={slugId} title={title} />
<MemoizedPageEditor pageId={pageId} />
</div>
);
}

View File

@ -36,12 +36,9 @@ export default function PageEditor({
const [currentUser] = useAtom(currentUserAtom);
const [, setEditor] = useAtom(pageEditorAtom);
const [, setAsideState] = useAtom(asideStateAtom);
const [, setActiveCommentId] = useAtom(activeCommentIdAtom);
const [showCommentPopup, setShowCommentPopup] = useAtom(showCommentPopupAtom);
const ydoc = useMemo(() => new Y.Doc(), [pageId]);
const [isLocalSynced, setLocalSynced] = useState(false);
const [isRemoteSynced, setRemoteSynced] = useState(false);
const documentName = `page.${pageId}`;

View File

@ -10,27 +10,34 @@ import {
pageEditorAtom,
titleEditorAtom,
} from "@/features/editor/atoms/editor-atoms";
import { useUpdatePageMutation } from "@/features/page/queries/page-query";
import {
usePageQuery,
useUpdatePageMutation,
} from "@/features/page/queries/page-query";
import { useDebouncedValue } from "@mantine/hooks";
import { useAtom } from "jotai";
import { treeDataAtom } from "@/features/page/tree/atoms/tree-data-atom";
import { updateTreeNodeName } from "@/features/page/tree/utils";
import { useQueryEmit } from "@/features/websocket/use-query-emit.ts";
import { History } from "@tiptap/extension-history";
import { buildPageSlug } from "@/features/page/page.utils.ts";
import { useNavigate } from "react-router-dom";
export interface TitleEditorProps {
pageId: string;
slugId: string;
title: string;
}
export function TitleEditor({ pageId, title }: TitleEditorProps) {
const [debouncedTitleState, setDebouncedTitleState] = useState("");
export function TitleEditor({ pageId, slugId, title }: TitleEditorProps) {
const [debouncedTitleState, setDebouncedTitleState] = useState(null);
const [debouncedTitle] = useDebouncedValue(debouncedTitleState, 1000);
const updatePageMutation = useUpdatePageMutation();
const pageEditor = useAtomValue(pageEditorAtom);
const [, setTitleEditor] = useAtom(titleEditorAtom);
const [treeData, setTreeData] = useAtom(treeDataAtom);
const emit = useQueryEmit();
const navigate = useNavigate();
const titleEditor = useEditor({
extensions: [
@ -62,15 +69,23 @@ export function TitleEditor({ pageId, title }: TitleEditorProps) {
});
useEffect(() => {
if (debouncedTitle !== "") {
updatePageMutation.mutate({ pageId, title: debouncedTitle });
const pageSlug = buildPageSlug(slugId, title);
navigate(pageSlug, { replace: true });
}, [title]);
useEffect(() => {
if (debouncedTitle !== null) {
updatePageMutation.mutate({
pageId: pageId,
title: debouncedTitle,
});
setTimeout(() => {
emit({
operation: "updateOne",
entity: ["pages"],
id: pageId,
payload: { title: debouncedTitle },
payload: { title: debouncedTitle, slugId: slugId },
});
}, 50);

View File

@ -1,5 +1,5 @@
import { Text, Tabs, Space } from "@mantine/core";
import { IconClockHour3, IconStar } from "@tabler/icons-react";
import { IconClockHour3 } from "@tabler/icons-react";
import RecentChanges from "@/features/home/components/recent-changes";
export default function HomeTabs() {
@ -16,7 +16,7 @@ export default function HomeTabs() {
<Space my="md" />
<Tabs.Panel value="recent">
<div>Recent</div>
<RecentChanges />
</Tabs.Panel>
</Tabs>
);

View File

@ -1,9 +1,10 @@
import { Text, Group, Stack, UnstyledButton, Divider } from '@mantine/core';
import { format } from 'date-fns';
import classes from './home.module.css';
import { Link } from 'react-router-dom';
import PageListSkeleton from '@/features/home/components/page-list-skeleton';
import { useRecentChangesQuery } from '@/features/page/queries/page-query';
import { Text, Group, Stack, UnstyledButton, Divider } from "@mantine/core";
import { format } from "date-fns";
import classes from "./home.module.css";
import { Link } from "react-router-dom";
import PageListSkeleton from "@/features/home/components/page-list-skeleton";
import { useRecentChangesQuery } from "@/features/page/queries/page-query";
import { buildPageSlug } from "@/features/page/page.utils.ts";
function RecentChanges() {
const { data, isLoading, isError } = useRecentChangesQuery();
@ -18,21 +19,23 @@ function RecentChanges() {
return (
<div>
{data
.map((page) => (
{data.items.map((page) => (
<div key={page.id}>
<UnstyledButton component={Link} to={`/p/${page.id}`}
className={classes.page} p="xs">
<UnstyledButton
component={Link}
to={buildPageSlug(page.slugId, page.title)}
className={classes.page}
p="xs"
>
<Group wrap="nowrap">
<Stack gap="xs" style={{ flex: 1 }}>
<Text fw={500} size="md" lineClamp={1}>
{page.title || 'Untitled'}
{page.title || "Untitled"}
</Text>
</Stack>
<Text c="dimmed" size="xs" fw={500}>
{format(new Date(page.updatedAt), 'PP')}
{format(new Date(page.updatedAt), "PP")}
</Text>
</Group>
</UnstyledButton>

View File

@ -18,9 +18,12 @@ import {
import { modals } from "@mantine/modals";
import { notifications } from "@mantine/notifications";
function HistoryList() {
interface Props {
pageId: string;
}
function HistoryList({ pageId }: Props) {
const [activeHistoryId, setActiveHistoryId] = useAtom(activeHistoryIdAtom);
const { pageId } = useParams();
const {
data: pageHistoryList,
isLoading,

View File

@ -1,18 +1,22 @@
import { ScrollArea } from '@mantine/core';
import HistoryList from '@/features/page-history/components/history-list';
import classes from './history.module.css';
import { useAtom } from 'jotai';
import { activeHistoryIdAtom } from '@/features/page-history/atoms/history-atoms';
import HistoryView from '@/features/page-history/components/history-view';
import { ScrollArea } from "@mantine/core";
import HistoryList from "@/features/page-history/components/history-list";
import classes from "./history.module.css";
import { useAtom } from "jotai";
import { activeHistoryIdAtom } from "@/features/page-history/atoms/history-atoms";
import HistoryView from "@/features/page-history/components/history-view";
export default function HistoryModalBody() {
interface Props {
pageId: string;
}
export default function HistoryModalBody({ pageId }: Props) {
const [activeHistoryId] = useAtom(activeHistoryIdAtom);
return (
<div className={classes.sidebarFlex}>
<nav className={classes.sidebar}>
<div className={classes.sidebarMain}>
<HistoryList />
<HistoryList pageId={pageId} />
</div>
</nav>
@ -21,7 +25,6 @@ export default function HistoryModalBody() {
{activeHistoryId && <HistoryView historyId={activeHistoryId} />}
</div>
</ScrollArea>
</div>
);
}

View File

@ -1,24 +1,33 @@
import { Modal, Text } from '@mantine/core';
import { useAtom } from 'jotai';
import { historyAtoms } from '@/features/page-history/atoms/history-atoms';
import HistoryModalBody from '@/features/page-history/components/history-modal-body';
import { Modal, Text } from "@mantine/core";
import { useAtom } from "jotai";
import { historyAtoms } from "@/features/page-history/atoms/history-atoms";
import HistoryModalBody from "@/features/page-history/components/history-modal-body";
export default function HistoryModal() {
interface Props {
pageId: string;
}
export default function HistoryModal({ pageId }: Props) {
const [isModalOpen, setModalOpen] = useAtom(historyAtoms);
return (
<>
<Modal.Root size={1200} opened={isModalOpen} onClose={() => setModalOpen(false)}>
<Modal.Root
size={1200}
opened={isModalOpen}
onClose={() => setModalOpen(false)}
>
<Modal.Overlay />
<Modal.Content style={{ overflow: 'hidden' }}>
<Modal.Content style={{ overflow: "hidden" }}>
<Modal.Header>
<Modal.Title>
<Text size="md" fw={500}>Page history</Text>
<Text size="md" fw={500}>
Page history
</Text>
</Modal.Title>
<Modal.CloseButton />
</Modal.Header>
<Modal.Body>
<HistoryModalBody />
<HistoryModalBody pageId={pageId} />
</Modal.Body>
</Modal.Content>
</Modal.Root>

View File

@ -0,0 +1,15 @@
import slugify from "@sindresorhus/slugify";
export const buildPageSlug = (
pageShortId: string,
pageTitle?: string,
): string => {
const titleSlug = slugify(pageTitle?.substring(0, 99) || "untitled", {
customReplacements: [
["♥", ""],
["🦄", ""],
],
});
return `/p/${pageShortId}/${titleSlug}`;
};

View File

@ -27,16 +27,21 @@ import { buildTree } from "@/features/page/tree/utils";
const RECENT_CHANGES_KEY = ["recentChanges"];
export function usePageQuery(pageId: string): UseQueryResult<IPage, Error> {
export function usePageQuery(
pageIdOrSlugId: string,
): UseQueryResult<IPage, Error> {
return useQuery({
queryKey: ["pages", pageId],
queryFn: () => getPageById(pageId),
enabled: !!pageId,
queryKey: ["pages", pageIdOrSlugId],
queryFn: () => getPageById(pageIdOrSlugId),
enabled: !!pageIdOrSlugId,
staleTime: 5 * 60 * 1000,
});
}
export function useRecentChangesQuery(): UseQueryResult<IPage[], Error> {
export function useRecentChangesQuery(): UseQueryResult<
IPagination<IPage>,
Error
> {
return useQuery({
queryKey: RECENT_CHANGES_KEY,
queryFn: () => getRecentChanges(),
@ -60,7 +65,7 @@ export function useUpdatePageMutation() {
mutationFn: (data) => updatePage(data),
onSuccess: (data) => {
// update page in cache
queryClient.setQueryData(["pages", data.id], data);
queryClient.setQueryData(["pages", data.slugId], data);
},
});
}

View File

@ -29,8 +29,8 @@ export async function movePage(data: IMovePage): Promise<void> {
await api.post<void>("/pages/move", data);
}
export async function getRecentChanges(): Promise<IPage[]> {
const req = await api.post<IPage[]>("/pages/recent");
export async function getRecentChanges(): Promise<IPagination<IPage>> {
const req = await api.post("/pages/recent");
return req.data;
}

View File

@ -4,12 +4,13 @@ import { treeApiAtom } from "@/features/page/tree/atoms/tree-api-atom.ts";
import {
fetchAncestorChildren,
useGetRootSidebarPagesQuery,
usePageQuery,
useUpdatePageMutation,
} from "@/features/page/queries/page-query.ts";
import React, { useEffect, useRef } from "react";
import { useNavigate, useParams } from "react-router-dom";
import classes from "@/features/page/tree/styles/tree.module.css";
import { ActionIcon, Menu, rem } from "@mantine/core";
import { ActionIcon, Menu, rem, Text } from "@mantine/core";
import {
IconChevronDown,
IconChevronRight,
@ -18,7 +19,6 @@ import {
IconLink,
IconPlus,
IconPointFilled,
IconStar,
IconTrash,
} from "@tabler/icons-react";
import { treeDataAtom } from "@/features/page/tree/atoms/tree-data-atom.ts";
@ -39,9 +39,12 @@ import {
import { IPage, SidebarPagesParams } from "@/features/page/types/page.types.ts";
import { queryClient } from "@/main.tsx";
import { OpenMap } from "react-arborist/dist/main/state/open-slice";
import { useElementSize, useMergedRef } from "@mantine/hooks";
import { useClipboard, useElementSize, useMergedRef } from "@mantine/hooks";
import { dfs } from "react-arborist/dist/module/utils";
import { useQueryEmit } from "@/features/websocket/use-query-emit.ts";
import { buildPageSlug } from "@/features/page/page.utils.ts";
import { notifications } from "@mantine/notifications";
import { modals } from "@mantine/modals";
interface SpaceTreeProps {
spaceId: string;
@ -50,7 +53,7 @@ interface SpaceTreeProps {
const openTreeNodesAtom = atom<OpenMap>({});
export default function SpaceTree({ spaceId }: SpaceTreeProps) {
const { pageId } = useParams();
const { slugId } = useParams();
const { data, setData, controllers } =
useTreeMutation<TreeApi<SpaceTreeNode>>(spaceId);
const {
@ -68,6 +71,7 @@ export default function SpaceTree({ spaceId }: SpaceTreeProps) {
const { ref: sizeRef, width, height } = useElementSize();
const mergedRef = useMergedRef(rootElement, sizeRef);
const isDataLoaded = useRef(false);
const { data: currentPage } = usePageQuery(slugId);
useEffect(() => {
if (hasNextPage && !isFetching) {
@ -94,24 +98,24 @@ export default function SpaceTree({ spaceId }: SpaceTreeProps) {
useEffect(() => {
const fetchData = async () => {
if (isDataLoaded.current) {
if (isDataLoaded.current && currentPage) {
// check if pageId node is present in the tree
const node = dfs(treeApiRef.current.root, pageId);
const node = dfs(treeApiRef.current.root, currentPage.id);
if (node) {
// if node is found, no need to traverse its ancestors
return;
}
// if not found, fetch and build its ancestors and their children
if (!pageId) return;
const ancestors = await getPageBreadcrumbs(pageId);
if (!currentPage.id) return;
const ancestors = await getPageBreadcrumbs(currentPage.id);
if (ancestors && ancestors?.length > 1) {
let flatTreeItems = [...buildTree(ancestors)];
const fetchAndUpdateChildren = async (ancestor: IPage) => {
// we don't want to fetch the children of the opened page
if (ancestor.id === pageId) {
if (ancestor.id === currentPage.id) {
return;
}
const children = await fetchAncestorChildren({
@ -148,7 +152,7 @@ export default function SpaceTree({ spaceId }: SpaceTreeProps) {
setTimeout(() => {
// focus on node and open all parents
treeApiRef.current.select(pageId);
treeApiRef.current.select(currentPage.id);
}, 100);
});
}
@ -156,13 +160,15 @@ export default function SpaceTree({ spaceId }: SpaceTreeProps) {
};
fetchData();
}, [isDataLoaded.current, pageId]);
}, [isDataLoaded.current, currentPage?.id]);
useEffect(() => {
setTimeout(() => {
treeApiRef.current?.select(pageId, { align: "auto" });
}, 200);
}, [pageId]);
if (currentPage) {
setTimeout(() => {
treeApiRef.current?.select(currentPage.id, { align: "auto" });
}, 200);
}
}, [currentPage?.id]);
useEffect(() => {
if (treeApiRef.current) {
@ -241,7 +247,7 @@ function Node({ node, style, dragHandle, tree }: NodeRendererProps<any>) {
}
const handleClick = () => {
navigate(`/p/${node.id}`);
navigate(buildPageSlug(node.data.slugId, node.data.name));
};
const handleUpdateNodeIcon = (nodeId: string, newIcon: string) => {
@ -333,6 +339,7 @@ interface CreateNodeProps {
treeApi: TreeApi<SpaceTreeNode>;
onExpandTree?: () => void;
}
function CreateNode({ node, treeApi, onExpandTree }: CreateNodeProps) {
function handleCreate() {
if (node.data.hasChildren && node.children.length === 0) {
@ -366,7 +373,32 @@ interface NodeMenuProps {
node: NodeApi<SpaceTreeNode>;
treeApi: TreeApi<SpaceTreeNode>;
}
function NodeMenu({ node, treeApi }: NodeMenuProps) {
const clipboard = useClipboard({ timeout: 500 });
const handleCopyLink = () => {
const pageLink =
window.location.host + buildPageSlug(node.data.id, node.data.name);
clipboard.copy(pageLink);
notifications.show({ message: "Link copied" });
};
const openDeleteModal = () =>
modals.openConfirmModal({
title: "Are you sure you want to delete this page?",
children: (
<Text size="sm">
Are you sure you want to delete this page? This action is
irreversible.
</Text>
),
centered: true,
labels: { confirm: "Delete", cancel: "Cancel" },
confirmProps: { color: "red" },
onConfirm: () => treeApi?.delete(node),
});
return (
<Menu shadow="md" width={200}>
<Menu.Target>
@ -386,13 +418,12 @@ function NodeMenu({ node, treeApi }: NodeMenuProps) {
</Menu.Target>
<Menu.Dropdown>
<Menu.Divider />
<Menu.Item
leftSection={<IconLink style={{ width: rem(14), height: rem(14) }} />}
onClick={(e) => {
e.preventDefault();
e.stopPropagation();
handleCopyLink();
}}
>
Copy link
@ -404,7 +435,7 @@ function NodeMenu({ node, treeApi }: NodeMenuProps) {
leftSection={
<IconTrash style={{ width: rem(14), height: rem(14) }} />
}
onClick={() => treeApi?.delete(node)}
onClick={openDeleteModal}
>
Delete
</Menu.Item>
@ -417,6 +448,7 @@ interface PageArrowProps {
node: NodeApi<SpaceTreeNode>;
onExpandTree?: () => void;
}
function PageArrow({ node, onExpandTree }: PageArrowProps) {
return (
<ActionIcon

View File

@ -10,7 +10,7 @@ import {
import { IconChevronRight } from "@tabler/icons-react";
import classes from "./tree-collapse.module.css";
interface LinksGroupProps {
interface TreeCollapseProps {
icon?: React.FC<any>;
label: string;
initiallyOpened?: boolean;
@ -22,7 +22,7 @@ export function TreeCollapse({
label,
initiallyOpened,
children,
}: LinksGroupProps) {
}: TreeCollapseProps) {
const [opened, setOpened] = useState(initiallyOpened || false);
return (

View File

@ -19,6 +19,7 @@ import {
} from "@/features/page/queries/page-query.ts";
import { generateJitteredKeyBetween } from "fractional-indexing-jittered";
import { SpaceTreeNode } from "@/features/page/tree/types.ts";
import { buildPageSlug } from "@/features/page/page.utils.ts";
export function useTreeMutation<T>(spaceId: string) {
const [data, setData] = useAtom(treeDataAtom);
@ -46,6 +47,7 @@ export function useTreeMutation<T>(spaceId: string) {
const data = {
id: createdPage.id,
slugId: createdPage.slugId,
name: "",
position: createdPage.position,
children: [],
@ -63,7 +65,7 @@ export function useTreeMutation<T>(spaceId: string) {
tree.create({ parentId, index, data });
setData(tree.data);
navigate(`/p/${createdPage.id}`);
navigate(buildPageSlug(createdPage.slugId, createdPage.title));
return data;
};

View File

@ -1,9 +1,9 @@
export type SpaceTreeNode = {
id: string;
slugId: string;
name: string;
icon?: string;
position: string;
slug?: string;
spaceId: string;
parentPageId: string;
hasChildren: boolean;

View File

@ -17,6 +17,7 @@ export function buildTree(pages: IPage[]): SpaceTreeNode[] {
pages.forEach((page) => {
pageMap[page.id] = {
id: page.id,
slugId: page.slugId,
name: page.title,
icon: page.icon,
position: page.position,

View File

@ -1,28 +1,23 @@
export interface IPage {
pageId: string;
id: string;
slugId: string;
title: string;
content: string;
html: string;
slug: string;
icon: string;
coverPhoto: string;
editor: string;
shareId: string;
parentPageId: string;
creatorId: string;
spaceId: string;
workspaceId: string;
children: [];
childrenIds: [];
isLocked: boolean;
status: string;
publishedAt: Date;
isPublic: boolean;
lastModifiedById: Date;
createdAt: Date;
updatedAt: Date;
deletedAt: Date;
position: string;
hasChildren: boolean;
pageId: string;
}
export interface IMovePage {

View File

@ -57,7 +57,6 @@ export default function SpaceMembersList({ spaceId }: SpaceMembersProps) {
};
const onRemove = async (memberId: string, type: MemberType) => {
console.log("remove", spaceId);
const memberToRemove: IRemoveSpaceMember = {
spaceId: spaceId,
};

View File

@ -8,14 +8,18 @@ export interface IUser {
avatarUrl: string;
timezone: string;
settings: any;
invitedById: string;
lastLoginAt: string;
lastActiveAt: Date;
createdAt: Date;
updatedAt: Date;
role: string;
workspaceId: string;
deactivatedAt: Date;
deletedAt: Date;
}
export interface ICurrentUser {
user: IUser,
workspace: IWorkspace
user: IUser;
workspace: IWorkspace;
}

View File

@ -12,6 +12,9 @@ export const useQuerySubscription = () => {
socket?.on("message", (event) => {
const data: WebSocketEvent = event;
let entity = null;
let queryKeyId = null;
switch (data.operation) {
case "invalidate":
queryClient.invalidateQueries({
@ -19,8 +22,16 @@ export const useQuerySubscription = () => {
});
break;
case "updateOne":
queryClient.setQueryData([...data.entity, data.id], {
...queryClient.getQueryData([...data.entity, data.id]),
entity = data.entity[0];
if (entity === "pages") {
// we have to do this because the usePageQuery cache key is the slugId.
queryKeyId = data.payload.slugId;
} else {
queryKeyId = data.id;
}
queryClient.setQueryData([...data.entity, queryKeyId], {
...queryClient.getQueryData([...data.entity, queryKeyId]),
...data.payload,
});

View File

@ -8,11 +8,12 @@ import {
changeMemberRole,
getInvitationById,
getPendingInvitations,
getWorkspace,
getWorkspaceMembers,
createInvitation,
resendInvitation,
revokeInvitation,
getWorkspace,
getWorkspacePublicData,
} from "@/features/workspace/services/workspace-service";
import { IPagination, QueryParams } from "@/lib/types.ts";
import { notifications } from "@mantine/notifications";
@ -22,13 +23,23 @@ import {
IWorkspace,
} from "@/features/workspace/types/workspace.types.ts";
export function useWorkspace(): UseQueryResult<IWorkspace, Error> {
export function useWorkspaceQuery(): UseQueryResult<IWorkspace, Error> {
return useQuery({
queryKey: ["workspace"],
queryFn: () => getWorkspace(),
});
}
export function useWorkspacePublicDataQuery(): UseQueryResult<
IWorkspace,
Error
> {
return useQuery({
queryKey: ["workspace-public"],
queryFn: () => getWorkspacePublicData(),
});
}
export function useWorkspaceMembersQuery(params?: QueryParams) {
return useQuery({
queryKey: ["workspaceMembers", params],
@ -69,7 +80,7 @@ export function useCreateInvitationMutation() {
return useMutation<void, Error, ICreateInvite>({
mutationFn: (data) => createInvitation(data),
onSuccess: (data, variables) => {
notifications.show({ message: "Invitation successfully" });
notifications.show({ message: "Invitation sent" });
// TODO: mutate cache
queryClient.invalidateQueries({
queryKey: ["invitations"],
@ -92,7 +103,7 @@ export function useResendInvitationMutation() {
>({
mutationFn: (data) => resendInvitation(data),
onSuccess: (data, variables) => {
notifications.show({ message: "Invitation mail sent" });
notifications.show({ message: "Invitation resent" });
},
onError: (error) => {
const errorMessage = error["response"]?.data?.message;

View File

@ -14,6 +14,11 @@ export async function getWorkspace(): Promise<IWorkspace> {
return req.data;
}
export async function getWorkspacePublicData(): Promise<IWorkspace> {
const req = await api.post<IWorkspace>("/workspace/public");
return req.data;
}
// Todo: fix all paginated types
export async function getWorkspaceMembers(
params?: QueryParams,

View File

@ -38,15 +38,22 @@ api.interceptors.response.use(
switch (error.response.status) {
case 401:
// Handle unauthorized error
if (window.location.pathname != Routes.AUTH.LOGIN) {
window.location.href = Routes.AUTH.LOGIN;
}
Cookies.remove("authTokens");
redirectToLogin();
break;
case 403:
// Handle forbidden error
break;
case 404:
// Handle not found error
if (
error.response.data.message
.toLowerCase()
.includes("workspace not found")
) {
Cookies.remove("authTokens");
redirectToLogin();
}
break;
case 500:
// Handle internal server error
@ -59,4 +66,13 @@ api.interceptors.response.use(
},
);
function redirectToLogin() {
if (
window.location.pathname != Routes.AUTH.LOGIN &&
window.location.pathname != Routes.AUTH.SIGNUP
) {
window.location.href = Routes.AUTH.LOGIN;
}
}
export default api;

View File

@ -10,6 +10,7 @@ import { BrowserRouter } from "react-router-dom";
import { ModalsProvider } from "@mantine/modals";
import { Notifications } from "@mantine/notifications";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { HelmetProvider } from "react-helmet-async";
export const queryClient = new QueryClient({
defaultOptions: {
@ -30,8 +31,10 @@ root.render(
<MantineProvider theme={theme}>
<ModalsProvider>
<QueryClientProvider client={queryClient}>
<Notifications position="top-right" limit={3} />
<App />
<Notifications position="bottom-center" limit={3} />
<HelmetProvider>
<App />
</HelmetProvider>
</QueryClientProvider>
</ModalsProvider>
</MantineProvider>

View File

@ -1,5 +1,13 @@
import { LoginForm } from '@/features/auth/components/login-form';
import { LoginForm } from "@/features/auth/components/login-form";
import { Helmet } from "react-helmet-async";
export default function LoginPage() {
return <LoginForm />;
return (
<>
<Helmet>
<title>Login</title>
</Helmet>
<LoginForm />
</>
);
}

View File

@ -1,5 +1,42 @@
import { SignUpForm } from '@/features/auth/components/sign-up-form';
import { SignUpForm } from "@/features/auth/components/sign-up-form";
import { useWorkspacePublicDataQuery } from "@/features/workspace/queries/workspace-query.ts";
import { SetupWorkspaceForm } from "@/features/auth/components/setup-workspace-form.tsx";
import { Helmet } from "react-helmet-async";
import React from "react";
export default function SignUpPage() {
return <SignUpForm />;
const {
data: workspace,
isLoading,
isError,
error,
} = useWorkspacePublicDataQuery();
if (isLoading) {
return <div></div>;
}
if (
isError &&
error?.["response"]?.status === 404 &&
error?.["response"]?.data.message.includes("Workspace not found")
) {
return (
<>
<Helmet>
<title>Setup workspace</title>
</Helmet>
<SetupWorkspaceForm />
</>
);
}
return workspace ? (
<>
<Helmet>
<title>Signup</title>
</Helmet>
<SignUpForm />
</>
) : null;
}

View File

@ -1,27 +1,31 @@
import { useParams } from 'react-router-dom';
import { usePageQuery } from '@/features/page/queries/page-query';
import { FullEditor } from '@/features/editor/full-editor';
import HistoryModal from '@/features/page-history/components/history-modal';
import { useParams } from "react-router-dom";
import { usePageQuery } from "@/features/page/queries/page-query";
import { FullEditor } from "@/features/editor/full-editor";
import HistoryModal from "@/features/page-history/components/history-modal";
import { Helmet } from "react-helmet-async";
export default function Page() {
const { pageId } = useParams();
const { data, isLoading, isError } = usePageQuery(pageId);
const { slugId } = useParams();
const { data: page, isLoading, isError } = usePageQuery(slugId);
if (isLoading) {
return <></>;
}
if (isError || !data) { // TODO: fix this
if (isError || !page) {
// TODO: fix this
return <div>Error fetching page data.</div>;
}
return (
data && (
page && (
<div>
<FullEditor pageId={pageId} title={data.title} />
<HistoryModal />
<Helmet>
<title>{page.title}</title>
</Helmet>
<FullEditor pageId={page.id} title={page.title} slugId={page.slugId} />
<HistoryModal pageId={page.id} />
</div>
)
);
}

View File

@ -1,3 +1,4 @@
/storage
.env
package-lock.json
# compiled output

View File

@ -1,6 +1,6 @@
{
"name": "server",
"version": "0.0.1",
"version": "0.1.0",
"description": "",
"author": "",
"private": true,
@ -13,13 +13,13 @@
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"email:dev": "email dev -p 5019 -d ./src/integrations/transactional/emails",
"migration:create": "tsx ./src/kysely/migrate.ts create",
"migration:up": "tsx ./src/kysely/migrate.ts up",
"migration:down": "tsx ./src/kysely/migrate.ts down",
"migration:latest": "tsx ./src/kysely/migrate.ts latest",
"migration:redo": "tsx ./src/kysely/migrate.ts redo",
"migration:reset": "tsx ./src/kysely/migrate.ts down-to NO_MIGRATIONS",
"migration:codegen": "kysely-codegen --dialect=postgres --camel-case --env-file=../../.env --out-file=./src/kysely/types/db.d.ts",
"migration:create": "tsx src/database/migrate.ts create",
"migration:up": "tsx src/database/migrate.ts up",
"migration:down": "tsx src/database/migrate.ts down",
"migration:latest": "tsx src/database/migrate.ts latest",
"migration:redo": "tsx src/database/migrate.ts redo",
"migration:reset": "tsx src/database/migrate.ts down-to NO_MIGRATIONS",
"migration:codegen": "kysely-codegen --dialect=postgres --camel-case --env-file=../../.env --out-file=./src/database/types/db.d.ts",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
@ -42,7 +42,6 @@
"@nestjs/passport": "^10.0.3",
"@nestjs/platform-fastify": "^10.3.8",
"@nestjs/platform-socket.io": "^10.3.8",
"@nestjs/serve-static": "^4.0.2",
"@nestjs/websockets": "^10.3.8",
"@react-email/components": "0.0.17",
"@react-email/render": "^0.0.13",
@ -68,7 +67,6 @@
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"sanitize-filename-ts": "^1.0.2",
"slugify": "^1.6.6",
"socket.io": "^4.7.5",
"tsx": "^4.8.2",
"uuid": "^9.0.1",

View File

@ -5,26 +5,11 @@ import { CoreModule } from './core/core.module';
import { EnvironmentModule } from './integrations/environment/environment.module';
import { CollaborationModule } from './collaboration/collaboration.module';
import { WsModule } from './ws/ws.module';
import { ServeStaticModule } from '@nestjs/serve-static';
import { join } from 'path';
import { DatabaseModule } from '@docmost/db/database.module';
import * as fs from 'fs';
import { StorageModule } from './integrations/storage/storage.module';
import { MailModule } from './integrations/mail/mail.module';
import { QueueModule } from './integrations/queue/queue.module';
const clientDistPath = join(__dirname, '..', '..', 'client/dist');
function getServeStaticModule() {
if (fs.existsSync(clientDistPath)) {
return [
ServeStaticModule.forRoot({
rootPath: clientDistPath,
}),
];
}
return [];
}
import { StaticModule } from './integrations/static/static.module';
@Module({
imports: [
@ -34,7 +19,7 @@ function getServeStaticModule() {
CollaborationModule,
WsModule,
QueueModule,
...getServeStaticModule(),
StaticModule,
StorageModule.forRootAsync({
imports: [EnvironmentModule],
}),

View File

@ -78,6 +78,7 @@ export class PersistenceExtension implements Extension {
textContent: textContent,
ydoc: ydocState,
lastUpdatedById: context.user.id,
updatedAt: new Date(),
},
pageId,
);

View File

@ -9,6 +9,7 @@ import { executeTx } from '@docmost/db/utils';
import { InjectKysely } from 'nestjs-kysely';
import { User } from '@docmost/db/types/entity.types';
import { GroupUserRepo } from '@docmost/db/repos/group/group-user.repo';
import { UserRole } from '../../../helpers/types/permission';
@Injectable()
export class SignupService {
@ -75,7 +76,11 @@ export class SignupService {
this.db,
async (trx) => {
// create user
const user = await this.userRepo.insertUser(createAdminUserDto, trx);
const user = await this.userRepo.insertUser(
{ ...createAdminUserDto, role: UserRole.OWNER },
trx,
);
// create workspace with full setup
const workspaceData: CreateWorkspaceDto = {

View File

@ -1,4 +1,8 @@
import { ForbiddenException, Injectable } from '@nestjs/common';
import {
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import {
AbilityBuilder,
createMongoAbility,
@ -33,9 +37,7 @@ export default class SpaceAbilityFactory {
case SpaceRole.READER:
return buildSpaceReaderAbility();
default:
throw new ForbiddenException(
'You do not have permission to access this space',
);
throw new NotFoundException('Space permissions not found');
}
}
}

View File

@ -52,7 +52,12 @@ export class CommentController {
throw new ForbiddenException();
}
return this.commentService.create(user.id, workspace.id, createCommentDto);
return this.commentService.create(
user.id,
page.id,
workspace.id,
createCommentDto,
);
}
@HttpCode(HttpStatus.OK)
@ -73,7 +78,7 @@ export class CommentController {
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) {
throw new ForbiddenException();
}
return this.commentService.findByPageId(input.pageId, pagination);
return this.commentService.findByPageId(page.id, pagination);
}
@HttpCode(HttpStatus.OK)
@ -84,7 +89,6 @@ export class CommentController {
throw new NotFoundException('Comment not found');
}
// TODO: add spaceId to comment entity.
const page = await this.pageRepo.findById(comment.pageId);
if (!page) {
throw new NotFoundException('Page not found');
@ -104,6 +108,7 @@ export class CommentController {
return this.commentService.update(
updateCommentDto.commentId,
updateCommentDto,
user,
);
}
@ -111,6 +116,6 @@ export class CommentController {
@Post('delete')
remove(@Body() input: CommentIdDto, @AuthUser() user: User) {
// TODO: only comment creators and admins can delete their comments
return this.commentService.remove(input.commentId);
return this.commentService.remove(input.commentId, user);
}
}

View File

@ -1,12 +1,13 @@
import {
BadRequestException,
ForbiddenException,
Injectable,
NotFoundException,
} from '@nestjs/common';
import { CreateCommentDto } from './dto/create-comment.dto';
import { UpdateCommentDto } from './dto/update-comment.dto';
import { CommentRepo } from '@docmost/db/repos/comment/comment.repo';
import { Comment } from '@docmost/db/types/entity.types';
import { Comment, User } from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { PaginationResult } from '@docmost/db/pagination/pagination';
import { PageRepo } from '@docmost/db/repos/page/page.repo';
@ -30,24 +31,18 @@ export class CommentService {
async create(
userId: string,
pageId: string,
workspaceId: string,
createCommentDto: CreateCommentDto,
) {
const commentContent = JSON.parse(createCommentDto.content);
const page = await this.pageRepo.findById(createCommentDto.pageId);
// const spaceId = null; // todo, get from page
if (!page) {
throw new BadRequestException('Page not found');
}
if (createCommentDto.parentCommentId) {
const parentComment = await this.commentRepo.findById(
createCommentDto.parentCommentId,
);
if (!parentComment) {
if (!parentComment || parentComment.pageId !== pageId) {
throw new BadRequestException('Parent comment not found');
}
@ -57,10 +52,10 @@ export class CommentService {
}
const createdComment = await this.commentRepo.insertComment({
pageId: createCommentDto.pageId,
pageId: pageId,
content: commentContent,
selection: createCommentDto?.selection?.substring(0, 250),
type: 'inline', // for now
type: 'inline',
parentCommentId: createCommentDto?.parentCommentId,
creatorId: userId,
workspaceId: workspaceId,
@ -90,6 +85,7 @@ export class CommentService {
async update(
commentId: string,
updateCommentDto: UpdateCommentDto,
authUser: User,
): Promise<Comment> {
const commentContent = JSON.parse(updateCommentDto.content);
@ -98,6 +94,10 @@ export class CommentService {
throw new NotFoundException('Comment not found');
}
if (comment.creatorId !== authUser.id) {
throw new ForbiddenException('You can only edit your own comments');
}
const editedAt = new Date();
await this.commentRepo.updateComment(
@ -113,12 +113,17 @@ export class CommentService {
return comment;
}
async remove(commentId: string): Promise<void> {
async remove(commentId: string, authUser: User): Promise<void> {
const comment = await this.commentRepo.findById(commentId);
if (!comment) {
throw new NotFoundException('Comment not found');
}
if (comment.creatorId !== authUser.id) {
throw new ForbiddenException('You can only delete your own comments');
}
await this.commentRepo.deleteComment(commentId);
}
}

View File

@ -1,7 +1,7 @@
import { IsUUID } from 'class-validator';
import { IsString, IsUUID } from 'class-validator';
export class PageIdDto {
@IsUUID()
@IsString()
pageId: string;
}

View File

@ -1,7 +1,7 @@
import { IsJSON, IsOptional, IsString, IsUUID } from 'class-validator';
export class CreateCommentDto {
@IsUUID()
@IsString()
pageId: string;
@IsJSON()

View File

@ -10,7 +10,7 @@ export class CreatePageDto {
icon?: string;
@IsOptional()
@IsUUID()
@IsString()
parentPageId?: string;
@IsUUID()

View File

@ -1,13 +1,7 @@
import {
IsString,
IsUUID,
IsOptional,
MinLength,
MaxLength,
} from 'class-validator';
import { IsString, IsOptional, MinLength, MaxLength } from 'class-validator';
export class MovePageDto {
@IsUUID()
@IsString()
pageId: string;
@IsString()

View File

@ -1,3 +0,0 @@
import { Page } from '@docmost/db/types/entity.types';
export type PageWithOrderingDto = Page & { childrenIds?: string[] };

View File

@ -1,7 +1,7 @@
import { IsUUID } from 'class-validator';
import { IsString, IsUUID } from 'class-validator';
export class PageIdDto {
@IsUUID()
@IsString()
pageId: string;
}

View File

@ -1,8 +1,8 @@
import { IsOptional, IsUUID } from 'class-validator';
import { IsOptional, IsString } from 'class-validator';
import { SpaceIdDto } from './page.dto';
export class SidebarPageDto extends SpaceIdDto {
@IsOptional()
@IsUUID()
@IsString()
pageId: string;
}

View File

@ -1,8 +1,8 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreatePageDto } from './create-page.dto';
import { IsUUID } from 'class-validator';
import { IsString } from 'class-validator';
export class UpdatePageDto extends PartialType(CreatePageDto) {
@IsUUID()
@IsString()
pageId: string;
}

View File

@ -12,7 +12,7 @@ import { PageService } from './services/page.service';
import { CreatePageDto } from './dto/create-page.dto';
import { UpdatePageDto } from './dto/update-page.dto';
import { MovePageDto } from './dto/move-page.dto';
import { PageHistoryIdDto, PageIdDto, SpaceIdDto } from './dto/page.dto';
import { PageHistoryIdDto, PageIdDto } from './dto/page.dto';
import { PageHistoryService } from './services/page-history.service';
import { AuthUser } from '../../decorators/auth-user.decorator';
import { AuthWorkspace } from '../../decorators/auth-workspace.decorator';
@ -118,18 +118,23 @@ export class PageController {
@HttpCode(HttpStatus.OK)
@Post('recent')
async getRecentSpacePages(
@Body() spaceIdDto: SpaceIdDto,
@Body() pagination: PaginationOptions,
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
const ability = await this.spaceAbility.createForUser(
user,
spaceIdDto.spaceId,
workspace.defaultSpaceId,
);
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) {
throw new ForbiddenException();
}
return this.pageService.getRecentSpacePages(spaceIdDto.spaceId, pagination);
return this.pageService.getRecentSpacePages(
workspace.defaultSpaceId,
pagination,
);
}
// TODO: scope to workspaces
@ -146,7 +151,7 @@ export class PageController {
throw new ForbiddenException();
}
return this.pageHistoryService.findHistoryByPageId(dto.pageId, pagination);
return this.pageHistoryService.findHistoryByPageId(page.id, pagination);
}
@HttpCode(HttpStatus.OK)
@ -181,7 +186,17 @@ export class PageController {
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) {
throw new ForbiddenException();
}
return this.pageService.getSidebarPages(dto, pagination);
let pageId = null;
if (dto.pageId) {
const page = await this.pageRepo.findById(dto.pageId);
if (page.spaceId !== dto.spaceId) {
throw new ForbiddenException();
}
pageId = page.id;
}
return this.pageService.getSidebarPages(dto.spaceId, pagination, pageId);
}
@HttpCode(HttpStatus.OK)
@ -207,10 +222,14 @@ export class PageController {
@Post('/breadcrumbs')
async getPageBreadcrumbs(@Body() dto: PageIdDto, @AuthUser() user: User) {
const page = await this.pageRepo.findById(dto.pageId);
if (!page) {
throw new NotFoundException('Page not found');
}
const ability = await this.spaceAbility.createForUser(user, page.spaceId);
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Page)) {
throw new ForbiddenException();
}
return this.pageService.getPageBreadCrumbs(dto.pageId);
return this.pageService.getPageBreadCrumbs(page.id);
}
}

View File

@ -18,7 +18,7 @@ import { generateJitteredKeyBetween } from 'fractional-indexing-jittered';
import { MovePageDto } from '../dto/move-page.dto';
import { ExpressionBuilder } from 'kysely';
import { DB } from '@docmost/db/types/db';
import { SidebarPageDto } from '../dto/sidebar-page.dto';
import { genPageShortId } from '../../../helpers/nanoid.utils';
@Injectable()
export class PageService {
@ -40,14 +40,19 @@ export class PageService {
workspaceId: string,
createPageDto: CreatePageDto,
): Promise<Page> {
let parentPageId = undefined;
// check if parent page exists
if (createPageDto.parentPageId) {
const parentPage = await this.pageRepo.findById(
createPageDto.parentPageId,
);
if (!parentPage || parentPage.spaceId !== createPageDto.spaceId)
if (!parentPage || parentPage.spaceId !== createPageDto.spaceId) {
throw new NotFoundException('Parent page not found');
}
parentPageId = parentPage.id;
}
let pagePosition: string;
@ -59,10 +64,10 @@ export class PageService {
.orderBy('position', 'desc')
.limit(1);
if (createPageDto.parentPageId) {
if (parentPageId) {
// check for children of this page
const lastPage = await lastPageQuery
.where('parentPageId', '=', createPageDto.parentPageId)
.where('parentPageId', '=', parentPageId)
.executeTakeFirst();
if (!lastPage) {
@ -87,10 +92,11 @@ export class PageService {
}
const createdPage = await this.pageRepo.insertPage({
slugId: genPageShortId(),
title: createPageDto.title,
position: pagePosition,
icon: createPageDto.icon,
parentPageId: createPageDto.parentPageId,
parentPageId: parentPageId,
spaceId: createPageDto.spaceId,
creatorId: userId,
workspaceId: workspaceId,
@ -110,6 +116,7 @@ export class PageService {
title: updatePageDto.title,
icon: updatePageDto.icon,
lastUpdatedById: userId,
updatedAt: new Date(),
},
pageId,
);
@ -135,13 +142,15 @@ export class PageService {
}
async getSidebarPages(
dto: SidebarPageDto,
spaceId: string,
pagination: PaginationOptions,
pageId?: string,
): Promise<any> {
let query = this.db
.selectFrom('pages')
.select([
'id',
'slugId',
'title',
'icon',
'position',
@ -151,10 +160,10 @@ export class PageService {
])
.select((eb) => this.withHasChildren(eb))
.orderBy('position', 'asc')
.where('spaceId', '=', dto.spaceId);
.where('spaceId', '=', spaceId);
if (dto.pageId) {
query = query.where('parentPageId', '=', dto.pageId);
if (pageId) {
query = query.where('parentPageId', '=', pageId);
} else {
query = query.where('parentPageId', 'is', null);
}
@ -185,8 +194,8 @@ export class PageService {
if (!parentPage || parentPage.spaceId !== movedPage.spaceId) {
throw new NotFoundException('Parent page not found');
}
parentPageId = parentPage.id;
}
parentPageId = dto.parentPageId;
}
await this.pageRepo.updatePage(
@ -205,6 +214,7 @@ export class PageService {
.selectFrom('pages')
.select([
'id',
'slugId',
'title',
'icon',
'position',
@ -218,6 +228,7 @@ export class PageService {
.selectFrom('pages as p')
.select([
'p.id',
'p.slugId',
'p.title',
'p.icon',
'p.position',
@ -255,10 +266,7 @@ export class PageService {
spaceId: string,
pagination: PaginationOptions,
): Promise<PaginationResult<Page>> {
const pages = await this.pageRepo.getRecentPagesInSpace(
spaceId,
pagination,
);
const pages = await this.pageRepo.getRecentPageUpdates(spaceId, pagination);
return pages;
}
@ -267,6 +275,7 @@ export class PageService {
await this.pageRepo.deletePage(pageId);
}
}
/*
// TODO: page deletion and restoration
async delete(pageId: string): Promise<void> {

View File

@ -5,12 +5,13 @@ import {
} from '@nestjs/common';
import { CreateSpaceDto } from '../dto/create-space.dto';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import slugify from 'slugify';
import { SpaceRepo } from '@docmost/db/repos/space/space.repo';
import { KyselyTransaction } from '@docmost/db/types/kysely.types';
import { Space } from '@docmost/db/types/entity.types';
import { PaginationResult } from '@docmost/db/pagination/pagination';
import { UpdateSpaceDto } from '../dto/update-space.dto';
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { slugify } = require('fix-esm').require('@sindresorhus/slugify');
@Injectable()
export class SpaceService {

View File

@ -36,12 +36,16 @@ export class WorkspaceController {
private readonly workspaceInvitationService: WorkspaceInvitationService,
) {}
@Public()
@HttpCode(HttpStatus.OK)
@Post('/public')
async getWorkspacePublicInfo(@Req() req) {
return this.workspaceService.getWorkspacePublicData(req.raw.workspaceId);
}
@HttpCode(HttpStatus.OK)
@Post('/info')
async getWorkspace(
@AuthUser() user: User,
@AuthWorkspace() workspace: Workspace,
) {
async getWorkspace(@AuthWorkspace() workspace: Workspace) {
return this.workspaceService.getWorkspaceInfo(workspace.id);
}

View File

@ -46,6 +46,19 @@ export class WorkspaceService {
return workspace;
}
async getWorkspacePublicData(workspaceId: string) {
const workspace = await this.db
.selectFrom('workspaces')
.select(['id'])
.where('id', '=', workspaceId)
.executeTakeFirst();
if (!workspace) {
throw new NotFoundException('Workspace not found');
}
return workspace;
}
async create(
user: User,
createWorkspaceDto: CreateWorkspaceDto,

View File

@ -8,20 +8,17 @@ export async function up(db: Kysely<any>): Promise<void> {
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn('name', 'varchar', (col) => col)
.addColumn('description', 'text', (col) => col)
.addColumn('description', 'varchar', (col) => col)
.addColumn('logo', 'varchar', (col) => col)
.addColumn('hostname', 'varchar', (col) => col)
.addColumn('custom_domain', 'varchar', (col) => col)
.addColumn('enable_invite', 'boolean', (col) =>
col.defaultTo(true).notNull(),
)
.addColumn('invite_code', 'varchar', (col) =>
col.defaultTo(sql`gen_random_uuid()`),
)
.addColumn('settings', 'jsonb', (col) => col)
.addColumn('default_role', 'varchar', (col) =>
col.defaultTo(UserRole.MEMBER).notNull(),
)
.addColumn('allowed_email_domains', sql`varchar[]`, (col) =>
col.defaultTo('{}'),
)
.addColumn('default_space_id', 'uuid', (col) => col)
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
@ -31,7 +28,7 @@ export async function up(db: Kysely<any>): Promise<void> {
)
.addColumn('deleted_at', 'timestamptz', (col) => col)
.addUniqueConstraint('workspaces_hostname_unique', ['hostname'])
.addUniqueConstraint('workspaces_invite_code_unique', ['invite_code'])
.addUniqueConstraint('workspaces_custom_domain_unique', ['custom_domain'])
.execute();
}

View File

@ -9,10 +9,12 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('name', 'varchar', (col) => col)
.addColumn('email', 'varchar', (col) => col.notNull())
.addColumn('email_verified_at', 'timestamptz', (col) => col)
.addColumn('password', 'varchar', (col) => col.notNull())
.addColumn('password', 'varchar', (col) => col)
.addColumn('avatar_url', 'varchar', (col) => col)
.addColumn('role', 'varchar', (col) => col)
.addColumn('status', 'varchar', (col) => col)
.addColumn('role', 'varchar', (col) => col.notNull())
.addColumn('invited_by_id', 'uuid', (col) =>
col.references('users.id').onDelete('set null'),
)
.addColumn('workspace_id', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade'),
)
@ -21,12 +23,14 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('settings', 'jsonb', (col) => col)
.addColumn('last_active_at', 'timestamptz', (col) => col)
.addColumn('last_login_at', 'timestamptz', (col) => col)
.addColumn('deactivated_at', 'timestamptz', (col) => col)
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updated_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('deleted_at', 'timestamptz', (col) => col)
.addUniqueConstraint('users_email_workspace_id_unique', [
'email',
'workspace_id',

View File

@ -49,7 +49,7 @@ export async function up(db: Kysely<any>): Promise<void> {
col.references('spaces.id').onDelete('cascade').notNull(),
)
.addColumn('role', 'varchar', (col) => col.notNull())
.addColumn('addedById', 'uuid', (col) => col.references('users.id'))
.addColumn('added_by_id', 'uuid', (col) => col.references('users.id'))
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)

View File

@ -6,19 +6,24 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn('email', 'varchar', (col) => col)
.addColumn('role', 'varchar', (col) => col.notNull())
.addColumn('token', 'varchar', (col) => col.notNull())
.addColumn('group_ids', sql`uuid[]`, (col) => col)
.addColumn('invited_by_id', 'uuid', (col) => col.references('users.id'))
.addColumn('workspace_id', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('invited_by_id', 'uuid', (col) => col.references('users.id'))
.addColumn('email', 'varchar', (col) => col.notNull())
.addColumn('role', 'varchar', (col) => col.notNull())
.addColumn('status', 'varchar', (col) => col)
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updated_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addUniqueConstraint('invitations_email_workspace_id_unique', [
'email',
'workspace_id',
])
.execute();
}

View File

@ -6,17 +6,15 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn('slug_id', 'varchar', (col) => col.notNull())
.addColumn('title', 'varchar', (col) => col)
.addColumn('icon', 'varchar', (col) => col)
.addColumn('key', 'varchar', (col) => col)
.addColumn('cover_photo', 'varchar', (col) => col)
.addColumn('position', 'varchar', (col) => col)
.addColumn('content', 'jsonb', (col) => col)
.addColumn('html', 'text', (col) => col)
.addColumn('ydoc', 'bytea', (col) => col)
.addColumn('text_content', 'text', (col) => col)
.addColumn('tsv', sql`tsvector`, (col) => col)
.addColumn('ydoc', 'bytea', (col) => col)
.addColumn('slug', 'varchar', (col) => col)
.addColumn('cover_photo', 'varchar', (col) => col)
.addColumn('editor', 'varchar', (col) => col)
.addColumn('parent_page_id', 'uuid', (col) =>
col.references('pages.id').onDelete('cascade'),
)
@ -32,8 +30,6 @@ export async function up(db: Kysely<any>): Promise<void> {
col.references('workspaces.id').onDelete('cascade').notNull(),
)
.addColumn('is_locked', 'boolean', (col) => col.defaultTo(false).notNull())
.addColumn('status', 'varchar', (col) => col)
.addColumn('published_at', 'date', (col) => col)
.addColumn('created_at', 'timestamptz', (col) =>
col.notNull().defaultTo(sql`now()`),
)
@ -41,6 +37,7 @@ export async function up(db: Kysely<any>): Promise<void> {
col.notNull().defaultTo(sql`now()`),
)
.addColumn('deleted_at', 'timestamptz', (col) => col)
.addUniqueConstraint('pages_slug_id_unique', ['slug_id'])
.execute();
await db.schema
@ -49,38 +46,14 @@ export async function up(db: Kysely<any>): Promise<void> {
.using('GIN')
.column('tsv')
.execute();
await db.schema
.createIndex('pages_slug_id_idx')
.on('pages')
.column('slug_id')
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('pages')
.dropConstraint('pages_creator_id_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_last_updated_by_id_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_deleted_by_id_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_space_id_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_workspace_id_fkey')
.execute();
await db.schema
.alterTable('pages')
.dropConstraint('pages_parent_page_id_fkey')
.execute();
await db.schema.dropTable('pages').execute();
}

View File

@ -9,12 +9,13 @@ export async function up(db: Kysely<any>): Promise<void> {
.addColumn('page_id', 'uuid', (col) =>
col.references('pages.id').onDelete('cascade').notNull(),
)
.addColumn('slug_id', 'varchar', (col) => col)
.addColumn('title', 'varchar', (col) => col)
.addColumn('content', 'jsonb', (col) => col)
.addColumn('slug', 'varchar', (col) => col)
.addColumn('icon', 'varchar', (col) => col)
.addColumn('cover_photo', 'varchar', (col) => col)
.addColumn('version', 'int4', (col) => col.notNull())
.addColumn('version', 'int4', (col) => col)
.addColumn('last_updated_by_id', 'uuid', (col) =>
col.references('users.id'),
)

View File

@ -6,7 +6,6 @@ import {
InsertablePageHistory,
Page,
PageHistory,
UpdatablePageHistory,
} from '@docmost/db/types/entity.types';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { executeWithPagination } from '@docmost/db/pagination/pagination';
@ -27,19 +26,6 @@ export class PageHistoryRepo {
.executeTakeFirst();
}
async updatePageHistory(
updatablePageHistory: UpdatablePageHistory,
pageHistoryId: string,
trx?: KyselyTransaction,
) {
const db = dbOrTx(this.db, trx);
return db
.updateTable('pageHistory')
.set(updatablePageHistory)
.where('id', '=', pageHistoryId)
.execute();
}
async insertPageHistory(
insertablePageHistory: InsertablePageHistory,
trx?: KyselyTransaction,
@ -55,11 +41,10 @@ export class PageHistoryRepo {
async saveHistory(page: Page): Promise<void> {
await this.insertPageHistory({
pageId: page.id,
slugId: page.slugId,
title: page.title,
content: page.content,
slug: page.slug,
icon: page.icon,
version: 1, // TODO: make incremental
coverPhoto: page.coverPhoto,
lastUpdatedById: page.lastUpdatedById ?? page.creatorId,
spaceId: page.spaceId,

View File

@ -7,22 +7,20 @@ import {
Page,
UpdatablePage,
} from '@docmost/db/types/entity.types';
import { sql } from 'kysely';
import { PaginationOptions } from '@docmost/db/pagination/pagination-options';
import { executeWithPagination } from '@docmost/db/pagination/pagination';
import { validate as isValidUUID } from 'uuid';
// TODO: scope to space/workspace
@Injectable()
export class PageRepo {
constructor(@InjectKysely() private readonly db: KyselyDB) {}
private baseFields: Array<keyof Page> = [
'id',
'slugId',
'title',
'slug',
'icon',
'coverPhoto',
'key',
'position',
'parentPageId',
'creatorId',
@ -30,8 +28,6 @@ export class PageRepo {
'spaceId',
'workspaceId',
'isLocked',
'status',
'publishedAt',
'createdAt',
'updatedAt',
'deletedAt',
@ -44,21 +40,19 @@ export class PageRepo {
includeYdoc?: boolean;
},
): Promise<Page> {
return await this.db
let query = this.db
.selectFrom('pages')
.select(this.baseFields)
.where('id', '=', pageId)
.$if(opts?.includeContent, (qb) => qb.select('content'))
.$if(opts?.includeYdoc, (qb) => qb.select('ydoc'))
.executeTakeFirst();
}
.$if(opts?.includeYdoc, (qb) => qb.select('ydoc'));
async slug(slug: string): Promise<Page> {
return await this.db
.selectFrom('pages')
.selectAll()
.where(sql`LOWER(slug)`, '=', sql`LOWER(${slug})`)
.executeTakeFirst();
if (isValidUUID(pageId)) {
query = query.where('id', '=', pageId);
} else {
query = query.where('slugId', '=', pageId);
}
return query.executeTakeFirst();
}
async updatePage(
@ -67,11 +61,15 @@ export class PageRepo {
trx?: KyselyTransaction,
) {
const db = dbOrTx(this.db, trx);
return db
.updateTable('pages')
.set(updatablePage)
.where('id', '=', pageId)
.executeTakeFirst();
let query = db.updateTable('pages').set(updatablePage);
if (isValidUUID(pageId)) {
query = query.where('id', '=', pageId);
} else {
query = query.where('slugId', '=', pageId);
}
return query.executeTakeFirst();
}
async insertPage(
@ -87,10 +85,20 @@ export class PageRepo {
}
async deletePage(pageId: string): Promise<void> {
await this.db.deleteFrom('pages').where('id', '=', pageId).execute();
let query = this.db.deleteFrom('pages');
if (isValidUUID(pageId)) {
query = query.where('id', '=', pageId);
} else {
query = query.where('slugId', '=', pageId);
}
await query.execute();
}
async getRecentPagesInSpace(spaceId: string, pagination: PaginationOptions) {
async getRecentPageUpdates(spaceId: string, pagination: PaginationOptions) {
//TODO: should fetch pages from all spaces the user is member of
// for now, fetch from default space
const query = this.db
.selectFrom('pages')
.select(this.baseFields)

View File

@ -28,8 +28,10 @@ export class UserRepo {
'timezone',
'settings',
'lastLoginAt',
'deactivatedAt',
'createdAt',
'updatedAt',
'deletedAt',
];
async findById(
@ -97,6 +99,7 @@ export class UserRepo {
email: insertableUser.email.toLowerCase(),
password: await hashPassword(insertableUser.password),
locale: 'en',
role: insertableUser?.role,
lastLoginAt: new Date(),
};

View File

@ -79,10 +79,11 @@ export interface PageHistory {
lastUpdatedById: string | null;
pageId: string;
slug: string | null;
slugId: string | null;
spaceId: string;
title: string | null;
updatedAt: Generated<Timestamp>;
version: number;
version: number | null;
workspaceId: string;
}
@ -93,19 +94,14 @@ export interface Pages {
creatorId: string | null;
deletedAt: Timestamp | null;
deletedById: string | null;
editor: string | null;
html: string | null;
icon: string | null;
id: Generated<string>;
isLocked: Generated<boolean>;
key: string | null;
lastUpdatedById: string | null;
parentPageId: string | null;
position: string | null;
publishedAt: Timestamp | null;
slug: string | null;
slugId: string;
spaceId: string;
status: string | null;
textContent: string | null;
title: string | null;
tsv: string | null;
@ -115,8 +111,8 @@ export interface Pages {
}
export interface SpaceMembers {
addedById: string | null;
createdAt: Generated<Timestamp>;
creatorId: string | null;
groupId: string | null;
id: Generated<string>;
role: string;
@ -143,6 +139,8 @@ export interface Spaces {
export interface Users {
avatarUrl: string | null;
createdAt: Generated<Timestamp>;
deactivatedAt: Timestamp | null;
deletedAt: Timestamp | null;
email: string;
emailVerifiedAt: Timestamp | null;
id: Generated<string>;
@ -151,10 +149,9 @@ export interface Users {
lastLoginAt: Timestamp | null;
locale: string | null;
name: string | null;
password: string;
role: string | null;
password: string | null;
role: string;
settings: Json | null;
status: string | null;
timezone: string | null;
updatedAt: Generated<Timestamp>;
workspaceId: string | null;
@ -162,27 +159,26 @@ export interface Users {
export interface WorkspaceInvitations {
createdAt: Generated<Timestamp>;
email: string;
email: string | null;
groupIds: string[] | null;
id: Generated<string>;
invitedById: string | null;
role: string;
token: string | null;
token: string;
updatedAt: Generated<Timestamp>;
workspaceId: string;
}
export interface Workspaces {
allowedEmailDomains: Generated<string[] | null>;
createdAt: Generated<Timestamp>;
customDomain: string | null;
defaultRole: Generated<string>;
defaultSpaceId: string | null;
deletedAt: Timestamp | null;
description: string | null;
enableInvite: Generated<boolean>;
hostname: string | null;
id: Generated<string>;
inviteCode: Generated<string | null>;
logo: string | null;
name: string | null;
settings: Json | null;

View File

@ -3,3 +3,7 @@ const { customAlphabet } = require('fix-esm').require('nanoid');
const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
export const nanoIdGen = customAlphabet(alphabet, 10);
const slugIdAlphabet =
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
export const genPageShortId = customAlphabet(slugIdAlphabet, 12);

View File

@ -0,0 +1,57 @@
import { Module, OnModuleInit } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { join } from 'path';
import * as fs from 'node:fs';
import fastifyStatic from '@fastify/static';
import { EnvironmentService } from '../environment/environment.service';
@Module({})
export class StaticModule implements OnModuleInit {
constructor(
private readonly httpAdapterHost: HttpAdapterHost,
private readonly environmentService: EnvironmentService,
) {}
public async onModuleInit() {
const httpAdapter = this.httpAdapterHost.httpAdapter;
const app = httpAdapter.getInstance();
const clientDistPath = join(
__dirname,
'..',
'..',
'..',
'..',
'client/dist',
);
if (fs.existsSync(clientDistPath)) {
const indexFilePath = join(clientDistPath, 'index.html');
const windowVar = '<!--window-config-->';
const configString = {
env: this.environmentService.getEnv(),
appUrl: this.environmentService.getAppUrl(),
isCloud: this.environmentService.isCloud(),
};
const windowScriptContent = `<script>window.CONFIG=${JSON.stringify(configString)};</script>`;
const html = fs.readFileSync(indexFilePath, 'utf8');
const transformedHtml = html.replace(windowVar, windowScriptContent);
fs.writeFileSync(indexFilePath, transformedHtml);
const RENDER_PATH = '*';
await app.register(fastifyStatic, {
root: clientDistPath,
wildcard: false,
});
app.get(RENDER_PATH, (req: any, res: any) => {
const stream = fs.createReadStream(indexFilePath);
res.type('text/html').send(stream);
});
}
}
}

View File

@ -1,12 +0,0 @@
import { type Kysely } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('pages')
.addColumn('position', 'varchar', (col) => col)
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.alterTable('pages').dropColumn('position').execute();
}

View File

@ -1,43 +0,0 @@
import { Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('workspace_invitations')
.addColumn('token', 'varchar', (col) => col)
.addColumn('group_ids', sql`uuid[]`, (col) => col)
.execute();
await db.schema
.alterTable('workspace_invitations')
.dropColumn('status')
.execute();
await db.schema
.alterTable('workspace_invitations')
.addUniqueConstraint('invitation_email_workspace_id_unique', [
'email',
'workspace_id',
])
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('workspace_invitations')
.dropColumn('token')
.execute();
await db.schema
.alterTable('workspace_invitations')
.dropColumn('group_ids')
.execute();
await db.schema
.alterTable('workspace_invitations')
.addColumn('status', 'varchar', (col) => col)
.execute();
await db.schema
.alterTable('workspace_invitations')
.dropConstraint('invitation_email_workspace_id_unique')
.execute();
}

View File

@ -1,14 +0,0 @@
import { type Kysely } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('users')
.addColumn('invited_by_id', 'uuid', (col) =>
col.references('users.id').onDelete('set null'),
)
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema.alterTable('users').dropColumn('invited_by_id').execute();
}

View File

@ -4,7 +4,7 @@ import {
FastifyAdapter,
NestFastifyApplication,
} from '@nestjs/platform-fastify';
import { ValidationPipe } from '@nestjs/common';
import { NotFoundException, ValidationPipe } from '@nestjs/common';
import { TransformHttpResponseInterceptor } from './interceptors/http-response.interceptor';
import fastifyMultipart from '@fastify/multipart';
@ -14,12 +14,29 @@ async function bootstrap() {
new FastifyAdapter({
ignoreTrailingSlash: true,
ignoreDuplicateSlashes: true,
} as any),
}),
);
app.setGlobalPrefix('api');
await app.register(fastifyMultipart as any);
await app.register(fastifyMultipart);
app
.getHttpAdapter()
.getInstance()
.addHook('preHandler', function (req, reply, done) {
if (
req.originalUrl.startsWith('/api') &&
!req.originalUrl.startsWith('/api/auth/setup')
) {
if (!req.raw?.['workspaceId']) {
throw new NotFoundException('Workspace not found');
}
done();
} else {
done();
}
});
app.useGlobalPipes(
new ValidationPipe({

View File

@ -17,7 +17,9 @@ export class DomainMiddleware implements NestMiddleware {
if (this.environmentService.isSelfHosted()) {
const workspace = await this.workspaceRepo.findFirst();
if (!workspace) {
throw new NotFoundException('Workspace not found');
//throw new NotFoundException('Workspace not found');
(req as any).workspaceId = null;
return next();
}
(req as any).workspaceId = workspace.id;

View File

@ -20,8 +20,7 @@
"strict": true,
"jsx": "react",
"paths": {
"@docmost/db": ["./src/kysely"],
"@docmost/db/*": ["./src/kysely/*"],
"@docmost/db/*": ["./src/database/*"],
"@docmost/transactional/*": ["./src/integrations/transactional/*"]
}
}

Some files were not shown because too many files have changed in this diff Show More