Add per-document control over who can see viewer insights (#5594)

This commit is contained in:
Tom Moor
2023-07-23 12:01:36 -04:00
committed by GitHub
parent caf7333682
commit 479b805613
14 changed files with 192 additions and 73 deletions

View File

@@ -19,6 +19,8 @@ type Props = {
templateId?: string | null;
/** If the document should be displayed full-width on the screen */
fullWidth?: boolean;
/** Whether insights should be visible on the document */
insightsEnabled?: boolean;
/** Whether the text be appended to the end instead of replace */
append?: boolean;
/** Whether the document should be published to the collection */
@@ -46,6 +48,7 @@ export default async function documentUpdater({
editorVersion,
templateId,
fullWidth,
insightsEnabled,
append,
publish,
collectionId,
@@ -68,6 +71,9 @@ export default async function documentUpdater({
if (fullWidth !== undefined) {
document.fullWidth = fullWidth;
}
if (insightsEnabled !== undefined) {
document.insightsEnabled = insightsEnabled;
}
if (text !== undefined) {
document = DocumentHelper.applyMarkdownToDocument(document, text, append);
}

View File

@@ -0,0 +1,15 @@
'use strict';
module.exports = {
async up (queryInterface, Sequelize) {
await queryInterface.addColumn("documents", "insightsEnabled", {
type: Sequelize.BOOLEAN,
allowNull: false,
defaultValue: true,
});
},
async down (queryInterface, Sequelize) {
await queryInterface.removeColumn("documents", "insightsEnabled");
}
};

View File

@@ -196,6 +196,9 @@ class Document extends ParanoidModel {
@Column
fullWidth: boolean;
@Column
insightsEnabled: boolean;
@SimpleLength({
max: 255,
msg: `editorVersion must be 255 characters or less`,

View File

@@ -277,6 +277,20 @@ allow(User, "archive", Document, (user, document) => {
return user.teamId === document.teamId;
});
allow(User, "updateInsights", Document, (user, document) => {
if (!document || !document.isActive || document.isDraft) {
return false;
}
invariant(
document.collection,
"collection is missing, did you forget to include in the query scope?"
);
if (cannot(user, "update", document.collection)) {
return false;
}
return user.teamId === document.teamId;
});
allow(User, "unarchive", Document, (user, document) => {
if (!document) {
return false;

View File

@@ -41,6 +41,7 @@ async function presentDocument(
templateId: document.templateId,
collaboratorIds: [],
revision: document.revisionCount,
insightsEnabled: document.insightsEnabled,
fullWidth: document.fullWidth,
collectionId: undefined,
parentDocumentId: undefined,

View File

@@ -892,18 +892,8 @@ router.post(
auth(),
validate(T.DocumentsUpdateSchema),
async (ctx: APIContext<T.DocumentsUpdateReq>) => {
const {
id,
title,
text,
fullWidth,
publish,
templateId,
collectionId,
append,
apiVersion,
done,
} = ctx.input.body;
const { id, apiVersion, insightsEnabled, publish, collectionId, ...input } =
ctx.input.body;
const editorVersion = ctx.headers["x-editor-version"] as string | undefined;
const { user } = ctx.state.auth;
let collection: Collection | null | undefined;
@@ -915,6 +905,10 @@ router.post(
collection = document?.collection;
authorize(user, "update", document);
if (collection && insightsEnabled !== undefined) {
authorize(user, "updateInsights", document);
}
if (publish) {
if (!document.collectionId) {
assertPresent(
@@ -932,16 +926,12 @@ router.post(
await documentUpdater({
document,
user,
title,
text,
fullWidth,
...input,
publish,
collectionId,
append,
templateId,
insightsEnabled,
editorVersion,
transaction,
done,
ip: ctx.request.ip,
});

View File

@@ -189,6 +189,9 @@ export const DocumentsUpdateSchema = BaseSchema.extend({
/** Boolean to denote if the doc should occupy full width */
fullWidth: z.boolean().optional(),
/** Boolean to denote if insights should be visible on the doc */
insightsEnabled: z.boolean().optional(),
/** Boolean to denote if the doc should be published */
publish: z.boolean().optional(),

View File

@@ -1,4 +1,5 @@
import Router from "koa-router";
import { ValidationError } from "@server/errors";
import auth from "@server/middlewares/authentication";
import { rateLimiter } from "@server/middlewares/rateLimiter";
import validate from "@server/middlewares/validate";
@@ -23,6 +24,11 @@ router.post(
userId: user.id,
});
authorize(user, "read", document);
if (!document.insightsEnabled) {
throw ValidationError("Insights are not enabled for this document");
}
const views = await View.findByDocument(documentId, { includeSuspended });
ctx.body = {