diff --git a/.circleci/config.yml b/.circleci/config.yml index ca6ffe4b47..ce3846fe3d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -70,6 +70,15 @@ jobs: - run: name: test command: yarn test:app + test-shared: + <<: *defaults + steps: + - checkout + - restore_cache: + key: dependency-cache-{{ checksum "package.json" }} + - run: + name: test + command: yarn test:shared test-server: <<: *defaults steps: @@ -81,7 +90,7 @@ jobs: command: ./node_modules/.bin/sequelize db:migrate --url $DATABASE_URL_TEST - run: name: test - command: yarn test:server + command: yarn test:server --forceExit bundle-size: <<: *defaults steps: @@ -140,6 +149,9 @@ workflows: - test-server: requires: - build + - test-shared: + requires: + - build - test-app: requires: - build @@ -149,6 +161,7 @@ workflows: - bundle-size: requires: - test-app + - test-shared - test-server build-docker: diff --git a/.env.sample b/.env.sample index 78508dac6e..954ddd554a 100644 --- a/.env.sample +++ b/.env.sample @@ -1,5 +1,7 @@ # –––––––––––––––– REQUIRED –––––––––––––––– +NODE_ENV=production + # Generate a hex-encoded 32-byte random key. You should use `openssl rand -hex 32` # in your terminal to generate a random value. SECRET_KEY=generate_a_new_key @@ -168,3 +170,10 @@ SMTP_SECURE=true # The default interface language. See translate.getoutline.com for a list of # available language codes and their rough percentage translated. DEFAULT_LANGUAGE=en_US + +# Optionally enable rate limiter at application web server +RATE_LIMITER_ENABLED=true + +# Configure default throttling paramaters for rate limiter +RATE_LIMITER_REQUESTS=5000 +RATE_LIMITER_DURATION_WINDOW=60 diff --git a/.eslintrc b/.eslintrc index 8ab3ee1755..b013cd5aa2 100644 --- a/.eslintrc +++ b/.eslintrc @@ -25,6 +25,7 @@ "rules": { "eqeqeq": 2, "curly": 2, + "object-shorthand": "error", "no-mixed-operators": "off", "no-useless-escape": "off", "es/no-regexp-lookbehind-assertions": "error", diff --git a/.jestconfig.json b/.jestconfig.json new file mode 100644 index 0000000000..4f23f6339a --- /dev/null +++ b/.jestconfig.json @@ -0,0 +1,85 @@ +{ + "projects": [ + { + "displayName": "server", + "verbose": false, + "roots": [ + "/server" + ], + "moduleNameMapper": { + "^@server/(.*)$": "/server/$1", + "^@shared/(.*)$": "/shared/$1" + }, + "setupFiles": [ + "/__mocks__/console.js", + "/server/test/setup.ts" + ], + "testEnvironment": "node", + "runner": "@getoutline/jest-runner-serial" + }, + { + "displayName": "app", + "verbose": false, + "roots": [ + "/app" + ], + "moduleNameMapper": { + "^~/(.*)$": "/app/$1", + "^@shared/(.*)$": "/shared/$1", + "^.*[.](gif|ttf|eot|svg)$": "/__test__/fileMock.js", + "^uuid$": "/node_modules/uuid/dist/index.js" + }, + "modulePaths": [ + "/app" + ], + "setupFiles": [ + "/__mocks__/window.js" + ], + "setupFilesAfterEnv": [ + "/app/test/setup.ts" + ], + "testEnvironment": "jsdom", + "testEnvironmentOptions": { + "url": "http://localhost" + } + }, + { + "displayName": "shared-node", + "verbose": false, + "roots": [ + "/shared" + ], + "moduleNameMapper": { + "^@server/(.*)$": "/server/$1", + "^@shared/(.*)$": "/shared/$1" + }, + "setupFiles": [ + "/__mocks__/console.js" + ], + "setupFilesAfterEnv": [ + "/shared/test/setup.ts" + ], + "testEnvironment": "node" + }, + { + "displayName": "shared-jsdom", + "verbose": false, + "roots": [ + "/shared" + ], + "moduleNameMapper": { + "^~/(.*)$": "/app/$1", + "^@shared/(.*)$": "/shared/$1", + "^.*[.](gif|ttf|eot|svg)$": "/__test__/fileMock.js", + "^uuid$": "/node_modules/uuid/dist/index.js" + }, + "setupFiles": [ + "/__mocks__/window.js" + ], + "testEnvironment": "jsdom", + "testEnvironmentOptions": { + "url": "http://localhost" + } + } + ] +} diff --git a/__mocks__/bull.js b/__mocks__/bull.js deleted file mode 100644 index 02c0a40b43..0000000000 --- a/__mocks__/bull.js +++ /dev/null @@ -1,17 +0,0 @@ -export default class Queue { - name; - - constructor(name) { - this.name = name; - } - - process = (fn) => { - console.log(`Registered function ${this.name}`); - this.processFn = fn; - }; - - add = (data) => { - console.log(`Running ${this.name}`); - return this.processFn({ data }); - }; -} diff --git a/__mocks__/console.js b/__mocks__/console.js index b0a2ab99a5..307dd1e595 100644 --- a/__mocks__/console.js +++ b/__mocks__/console.js @@ -1,2 +1 @@ -// Mock for node-uuid global.console.warn = () => {}; diff --git a/app/.jestconfig.json b/app/.jestconfig.json deleted file mode 100644 index ad8cf47c99..0000000000 --- a/app/.jestconfig.json +++ /dev/null @@ -1,27 +0,0 @@ -{ - "testURL": "http://localhost", - "verbose": false, - "rootDir": "..", - "roots": [ - "/app", - "/shared" - ], - "moduleNameMapper": { - "^~/(.*)$": "/app/$1", - "^@shared/(.*)$": "/shared/$1", - "^.*[.](gif|ttf|eot|svg)$": "/__test__/fileMock.js" - }, - "moduleDirectories": [ - "node_modules" - ], - "modulePaths": [ - "/app" - ], - "setupFiles": [ - "/__mocks__/window.js" - ], - "setupFilesAfterEnv": [ - "./app/test/setup.ts" - ], - "testEnvironment": "jsdom" -} \ No newline at end of file diff --git a/app/actions/definitions/documents.tsx b/app/actions/definitions/documents.tsx index 344012b1de..1092dbe495 100644 --- a/app/actions/definitions/documents.tsx +++ b/app/actions/definitions/documents.tsx @@ -11,9 +11,16 @@ import { ImportIcon, PinIcon, SearchIcon, + MoveIcon, + TrashIcon, + CrossIcon, + ArchiveIcon, } from "outline-icons"; import * as React from "react"; import { getEventFiles } from "@shared/utils/files"; +import DocumentDelete from "~/scenes/DocumentDelete"; +import DocumentMove from "~/scenes/DocumentMove"; +import DocumentPermanentDelete from "~/scenes/DocumentPermanentDelete"; import DocumentTemplatizeDialog from "~/components/DocumentTemplatizeDialog"; import { createAction } from "~/actions"; import { DocumentSection } from "~/actions/sections"; @@ -296,10 +303,11 @@ export const createTemplate = createAction({ return false; } const document = stores.documents.get(activeDocumentId); - return ( + return !!( !!activeCollectionId && stores.policies.abilities(activeCollectionId).update && - !document?.isTemplate + !document?.isTemplate && + !document?.isDeleted ); }, perform: ({ activeDocumentId, stores, t, event }) => { @@ -328,15 +336,144 @@ export const searchDocumentsForQuery = (searchQuery: string) => visible: ({ location }) => location.pathname !== searchPath(), }); +export const moveDocument = createAction({ + name: ({ t }) => t("Move"), + section: DocumentSection, + icon: , + visible: ({ activeDocumentId, stores }) => { + if (!activeDocumentId) { + return false; + } + return !!stores.policies.abilities(activeDocumentId).move; + }, + perform: ({ activeDocumentId, stores, t }) => { + if (activeDocumentId) { + const document = stores.documents.get(activeDocumentId); + if (!document) { + return; + } + + stores.dialogs.openModal({ + title: t("Move {{ documentName }}", { + documentName: document.noun, + }), + content: ( + + ), + }); + } + }, +}); + +export const archiveDocument = createAction({ + name: ({ t }) => t("Archive"), + section: DocumentSection, + icon: , + visible: ({ activeDocumentId, stores }) => { + if (!activeDocumentId) { + return false; + } + return !!stores.policies.abilities(activeDocumentId).archive; + }, + perform: async ({ activeDocumentId, stores, t }) => { + if (activeDocumentId) { + const document = stores.documents.get(activeDocumentId); + if (!document) { + return; + } + + await document.archive(); + stores.toasts.showToast(t("Document archived"), { + type: "success", + }); + } + }, +}); + +export const deleteDocument = createAction({ + name: ({ t }) => t("Delete"), + section: DocumentSection, + icon: , + dangerous: true, + visible: ({ activeDocumentId, stores }) => { + if (!activeDocumentId) { + return false; + } + return !!stores.policies.abilities(activeDocumentId).delete; + }, + perform: ({ activeDocumentId, stores, t }) => { + if (activeDocumentId) { + const document = stores.documents.get(activeDocumentId); + if (!document) { + return; + } + + stores.dialogs.openModal({ + title: t("Delete {{ documentName }}", { + documentName: document.noun, + }), + isCentered: true, + content: ( + + ), + }); + } + }, +}); + +export const permanentlyDeleteDocument = createAction({ + name: ({ t }) => t("Permanently delete"), + section: DocumentSection, + icon: , + dangerous: true, + visible: ({ activeDocumentId, stores }) => { + if (!activeDocumentId) { + return false; + } + return !!stores.policies.abilities(activeDocumentId).permanentDelete; + }, + perform: ({ activeDocumentId, stores, t }) => { + if (activeDocumentId) { + const document = stores.documents.get(activeDocumentId); + if (!document) { + return; + } + + stores.dialogs.openModal({ + title: t("Permanently delete {{ documentName }}", { + documentName: document.noun, + }), + isCentered: true, + content: ( + + ), + }); + } + }, +}); + export const rootDocumentActions = [ openDocument, + archiveDocument, createDocument, createTemplate, + deleteDocument, importDocument, downloadDocument, starDocument, unstarDocument, duplicateDocument, + moveDocument, + permanentlyDeleteDocument, printDocument, pinDocumentToCollection, pinDocumentToHome, diff --git a/app/actions/definitions/teams.tsx b/app/actions/definitions/teams.tsx new file mode 100644 index 0000000000..799c6a522d --- /dev/null +++ b/app/actions/definitions/teams.tsx @@ -0,0 +1,40 @@ +import * as React from "react"; +import styled from "styled-components"; +import { createAction } from "~/actions"; +import { loadSessionsFromCookie } from "~/hooks/useSessions"; + +export const changeTeam = createAction({ + name: ({ t }) => t("Switch team"), + placeholder: ({ t }) => t("Select a team"), + keywords: "change workspace organization", + section: "Account", + visible: ({ currentTeamId }) => { + const sessions = loadSessionsFromCookie(); + const otherSessions = sessions.filter( + (session) => session.teamId !== currentTeamId + ); + return otherSessions.length > 0; + }, + children: ({ currentTeamId }) => { + const sessions = loadSessionsFromCookie(); + const otherSessions = sessions.filter( + (session) => session.teamId !== currentTeamId + ); + + return otherSessions.map((session) => ({ + id: session.url, + name: session.name, + section: "Account", + icon: , + perform: () => (window.location.href = session.url), + })); + }, +}); + +const Logo = styled("img")` + border-radius: 2px; + width: 24px; + height: 24px; +`; + +export const rootTeamActions = [changeTeam]; diff --git a/app/actions/index.ts b/app/actions/index.ts index b13eaa99b5..6abb8ad873 100644 --- a/app/actions/index.ts +++ b/app/actions/index.ts @@ -56,6 +56,7 @@ export function actionToMenuItem( title, icon, visible, + dangerous: action.dangerous, onClick: () => action.perform && action.perform(context), selected: action.selected ? action.selected(context) : undefined, }; diff --git a/app/actions/root.ts b/app/actions/root.ts index fb19a38682..db4ee3746e 100644 --- a/app/actions/root.ts +++ b/app/actions/root.ts @@ -3,6 +3,7 @@ import { rootDeveloperActions } from "./definitions/developer"; import { rootDocumentActions } from "./definitions/documents"; import { rootNavigationActions } from "./definitions/navigation"; import { rootSettingsActions } from "./definitions/settings"; +import { rootTeamActions } from "./definitions/teams"; import { rootUserActions } from "./definitions/users"; export default [ @@ -12,4 +13,5 @@ export default [ ...rootNavigationActions, ...rootSettingsActions, ...rootDeveloperActions, + ...rootTeamActions, ]; diff --git a/app/components/Editor.tsx b/app/components/Editor.tsx index cbf69a373d..5359195926 100644 --- a/app/components/Editor.tsx +++ b/app/components/Editor.tsx @@ -3,7 +3,7 @@ import { deburr, sortBy } from "lodash"; import { DOMParser as ProsemirrorDOMParser } from "prosemirror-model"; import { TextSelection } from "prosemirror-state"; import * as React from "react"; -import mergeRefs from "react-merge-refs"; +import { mergeRefs } from "react-merge-refs"; import { Optional } from "utility-types"; import insertFiles from "@shared/editor/commands/insertFiles"; import embeds from "@shared/editor/embeds"; diff --git a/app/components/IconPicker.tsx b/app/components/IconPicker.tsx index c4ce124163..40415d5876 100644 --- a/app/components/IconPicker.tsx +++ b/app/components/IconPicker.tsx @@ -40,6 +40,7 @@ import { useTranslation } from "react-i18next"; import { useMenuState, MenuButton, MenuItem } from "reakit/Menu"; import styled, { useTheme } from "styled-components"; import breakpoint from "styled-components-breakpoint"; +import { colorPalette } from "@shared/utils/collections"; import ContextMenu from "~/components/ContextMenu"; import Flex from "~/components/Flex"; import { LabelText } from "~/components/Input"; @@ -200,18 +201,7 @@ export const icons = { keywords: "warning alert error", }, }; -const colors = [ - "#4E5C6E", - "#0366d6", - "#9E5CF7", - "#FF825C", - "#FF5C80", - "#FFBE0B", - "#42DED1", - "#00D084", - "#FF4DFA", - "#2F362F", -]; + type Props = { onOpen?: () => void; onClose?: () => void; @@ -272,7 +262,7 @@ function IconPicker({ onOpen, onClose, icon, color, onChange }: Props) { onChange(color.hex, icon)} - colors={colors} + colors={colorPalette} triangle="hide" styles={{ default: { diff --git a/app/editor/components/LinkEditor.tsx b/app/editor/components/LinkEditor.tsx index f444f6279c..e848cf1f04 100644 --- a/app/editor/components/LinkEditor.tsx +++ b/app/editor/components/LinkEditor.tsx @@ -11,7 +11,7 @@ import { setTextSelection } from "prosemirror-utils"; import { EditorView } from "prosemirror-view"; import * as React from "react"; import styled from "styled-components"; -import { isInternalUrl, sanitizeHref } from "@shared/utils/urls"; +import { isInternalUrl, sanitizeUrl } from "@shared/utils/urls"; import Flex from "~/components/Flex"; import { Dictionary } from "~/hooks/useDictionary"; import { ToastOptions } from "~/types"; @@ -70,7 +70,7 @@ class LinkEditor extends React.Component { }; get href(): string { - return sanitizeHref(this.props.mark?.attrs.href) ?? ""; + return sanitizeUrl(this.props.mark?.attrs.href) ?? ""; } get suggestedLinkTitle(): string { @@ -113,7 +113,7 @@ class LinkEditor extends React.Component { this.discardInputValue = true; const { from, to } = this.props; - href = sanitizeHref(href) ?? ""; + href = sanitizeUrl(href) ?? ""; this.props.onSelectLink({ href, title, from, to }); }; diff --git a/app/editor/index.tsx b/app/editor/index.tsx index 0de98128f5..f399890995 100644 --- a/app/editor/index.tsx +++ b/app/editor/index.tsx @@ -430,7 +430,7 @@ export class Editor extends React.PureComponent< state: this.createState(this.props.value), editable: () => !this.props.readOnly, nodeViews: this.nodeViews, - dispatchTransaction: function (transaction) { + dispatchTransaction(transaction) { // callback is bound to have the view instance as its this binding const { state, transactions } = this.state.applyTransaction( transaction diff --git a/app/hooks/useSessions.ts b/app/hooks/useSessions.ts index 32ddc10c69..83f64aa834 100644 --- a/app/hooks/useSessions.ts +++ b/app/hooks/useSessions.ts @@ -8,7 +8,7 @@ type Session = { teamId: string; }; -function loadSessionsFromCookie(): Session[] { +export function loadSessionsFromCookie(): Session[] { const sessions = JSON.parse(getCookie("sessions") || "{}"); return Object.keys(sessions).map((teamId) => ({ teamId, diff --git a/app/menus/DocumentMenu.tsx b/app/menus/DocumentMenu.tsx index d26a8780b7..2b84faa749 100644 --- a/app/menus/DocumentMenu.tsx +++ b/app/menus/DocumentMenu.tsx @@ -1,20 +1,11 @@ import { observer } from "mobx-react"; import { EditIcon, - StarredIcon, - UnstarredIcon, - DuplicateIcon, - ArchiveIcon, - TrashIcon, - MoveIcon, HistoryIcon, UnpublishIcon, PrintIcon, - ImportIcon, NewDocumentIcon, - DownloadIcon, RestoreIcon, - CrossIcon, } from "outline-icons"; import * as React from "react"; import { useTranslation } from "react-i18next"; @@ -25,19 +16,27 @@ import styled from "styled-components"; import breakpoint from "styled-components-breakpoint"; import { getEventFiles } from "@shared/utils/files"; import Document from "~/models/Document"; -import DocumentDelete from "~/scenes/DocumentDelete"; -import DocumentMove from "~/scenes/DocumentMove"; -import DocumentPermanentDelete from "~/scenes/DocumentPermanentDelete"; import CollectionIcon from "~/components/CollectionIcon"; import ContextMenu from "~/components/ContextMenu"; import OverflowMenuButton from "~/components/ContextMenu/OverflowMenuButton"; import Separator from "~/components/ContextMenu/Separator"; import Template from "~/components/ContextMenu/Template"; import Flex from "~/components/Flex"; -import Modal from "~/components/Modal"; import Switch from "~/components/Switch"; import { actionToMenuItem } from "~/actions"; -import { pinDocument, createTemplate } from "~/actions/definitions/documents"; +import { + pinDocument, + createTemplate, + moveDocument, + deleteDocument, + permanentlyDeleteDocument, + downloadDocument, + importDocument, + starDocument, + unstarDocument, + duplicateDocument, + archiveDocument, +} from "~/actions/definitions/documents"; import useActionContext from "~/hooks/useActionContext"; import useCurrentTeam from "~/hooks/useCurrentTeam"; import useMobile from "~/hooks/useMobile"; @@ -94,39 +93,8 @@ function DocumentMenu({ }); const { t } = useTranslation(); const isMobile = useMobile(); - const [renderModals, setRenderModals] = React.useState(false); - const [showDeleteModal, setShowDeleteModal] = React.useState(false); - const [ - showPermanentDeleteModal, - setShowPermanentDeleteModal, - ] = React.useState(false); - const [showMoveModal, setShowMoveModal] = React.useState(false); const file = React.useRef(null); - const handleOpen = React.useCallback(() => { - setRenderModals(true); - - if (onOpen) { - onOpen(); - } - }, [onOpen]); - - const handleDuplicate = React.useCallback(async () => { - const duped = await document.duplicate(); - // when duplicating, go straight to the duplicated document content - history.push(duped.url); - showToast(t("Document duplicated"), { - type: "success", - }); - }, [t, history, showToast, document]); - - const handleArchive = React.useCallback(async () => { - await document.archive(); - showToast(t("Document archived"), { - type: "success", - }); - }, [showToast, t, document]); - const handleRestore = React.useCallback( async ( ev: React.SyntheticEvent, @@ -154,24 +122,6 @@ function DocumentMenu({ window.print(); }, [menu]); - const handleStar = React.useCallback( - (ev: React.SyntheticEvent) => { - ev.preventDefault(); - ev.stopPropagation(); - document.star(); - }, - [document] - ); - - const handleUnstar = React.useCallback( - (ev: React.SyntheticEvent) => { - ev.preventDefault(); - ev.stopPropagation(); - document.unstar(); - }, - [document] - ); - const collection = collections.get(document.collectionId); const can = usePolicy(document.id); const canViewHistory = can.read && !can.restore; @@ -205,19 +155,6 @@ function DocumentMenu({ ev.stopPropagation(); }, []); - const handleImportDocument = React.useCallback( - (ev: React.SyntheticEvent) => { - ev.preventDefault(); - ev.stopPropagation(); - - // simulate a click on the file upload input element - if (file.current) { - file.current.click(); - } - }, - [file] - ); - const handleFilePicked = React.useCallback( async (ev: React.ChangeEvent) => { const files = getEventFiles(ev); @@ -280,7 +217,7 @@ function DocumentMenu({