chore: Move to prettier standard double quotes (#1309)

This commit is contained in:
Tom Moor
2020-06-20 13:59:15 -07:00
committed by GitHub
parent 2a3b9e2104
commit f43deb7940
444 changed files with 5988 additions and 5977 deletions

View File

@ -1,14 +1,14 @@
// @flow
import Router from 'koa-router';
import Sequelize from 'sequelize';
import auth from '../middlewares/authentication';
import pagination from './middlewares/pagination';
import documentMover from '../commands/documentMover';
import Router from "koa-router";
import Sequelize from "sequelize";
import auth from "../middlewares/authentication";
import pagination from "./middlewares/pagination";
import documentMover from "../commands/documentMover";
import {
presentDocument,
presentCollection,
presentPolicies,
} from '../presenters';
} from "../presenters";
import {
Collection,
Document,
@ -19,23 +19,23 @@ import {
Revision,
Backlink,
User,
} from '../models';
import { InvalidRequestError } from '../errors';
import policy from '../policies';
import { sequelize } from '../sequelize';
} from "../models";
import { InvalidRequestError } from "../errors";
import policy from "../policies";
import { sequelize } from "../sequelize";
const Op = Sequelize.Op;
const { authorize, cannot } = policy;
const router = new Router();
router.post('documents.list', auth(), pagination(), async ctx => {
const { sort = 'updatedAt', backlinkDocumentId, parentDocumentId } = ctx.body;
router.post("documents.list", auth(), pagination(), async ctx => {
const { sort = "updatedAt", backlinkDocumentId, parentDocumentId } = ctx.body;
// collection and user are here for backwards compatablity
const collectionId = ctx.body.collectionId || ctx.body.collection;
const createdById = ctx.body.userId || ctx.body.user;
let direction = ctx.body.direction;
if (direction !== 'ASC') direction = 'DESC';
if (direction !== "ASC") direction = "DESC";
// always filter by the current team
const user = ctx.state.user;
@ -44,19 +44,19 @@ router.post('documents.list', auth(), pagination(), async ctx => {
// if a specific user is passed then add to filters. If the user doesn't
// exist in the team then nothing will be returned, so no need to check auth
if (createdById) {
ctx.assertUuid(createdById, 'user must be a UUID');
ctx.assertUuid(createdById, "user must be a UUID");
where = { ...where, createdById };
}
// if a specific collection is passed then we need to check auth to view it
if (collectionId) {
ctx.assertUuid(collectionId, 'collection must be a UUID');
ctx.assertUuid(collectionId, "collection must be a UUID");
where = { ...where, collectionId };
const collection = await Collection.scope({
method: ['withMembership', user.id],
method: ["withMembership", user.id],
}).findByPk(collectionId);
authorize(user, 'read', collection);
authorize(user, "read", collection);
// otherwise, filter by all collections the user has access to
} else {
@ -65,15 +65,15 @@ router.post('documents.list', auth(), pagination(), async ctx => {
}
if (parentDocumentId) {
ctx.assertUuid(parentDocumentId, 'parentDocumentId must be a UUID');
ctx.assertUuid(parentDocumentId, "parentDocumentId must be a UUID");
where = { ...where, parentDocumentId };
}
if (backlinkDocumentId) {
ctx.assertUuid(backlinkDocumentId, 'backlinkDocumentId must be a UUID');
ctx.assertUuid(backlinkDocumentId, "backlinkDocumentId must be a UUID");
const backlinks = await Backlink.findAll({
attributes: ['reverseDocumentId'],
attributes: ["reverseDocumentId"],
where: {
documentId: backlinkDocumentId,
},
@ -86,10 +86,10 @@ router.post('documents.list', auth(), pagination(), async ctx => {
}
// add the users starred state to the response by default
const starredScope = { method: ['withStarred', user.id] };
const collectionScope = { method: ['withCollection', user.id] };
const starredScope = { method: ["withStarred", user.id] };
const collectionScope = { method: ["withCollection", user.id] };
const documents = await Document.scope(
'defaultScope',
"defaultScope",
starredScope,
collectionScope
).findAll({
@ -112,23 +112,23 @@ router.post('documents.list', auth(), pagination(), async ctx => {
};
});
router.post('documents.pinned', auth(), pagination(), async ctx => {
const { collectionId, sort = 'updatedAt' } = ctx.body;
router.post("documents.pinned", auth(), pagination(), async ctx => {
const { collectionId, sort = "updatedAt" } = ctx.body;
let direction = ctx.body.direction;
if (direction !== 'ASC') direction = 'DESC';
ctx.assertUuid(collectionId, 'collectionId is required');
if (direction !== "ASC") direction = "DESC";
ctx.assertUuid(collectionId, "collectionId is required");
const user = ctx.state.user;
const collection = await Collection.scope({
method: ['withMembership', user.id],
method: ["withMembership", user.id],
}).findByPk(collectionId);
authorize(user, 'read', collection);
authorize(user, "read", collection);
const starredScope = { method: ['withStarred', user.id] };
const collectionScope = { method: ['withCollection', user.id] };
const starredScope = { method: ["withStarred", user.id] };
const collectionScope = { method: ["withCollection", user.id] };
const documents = await Document.scope(
'defaultScope',
"defaultScope",
starredScope,
collectionScope
).findAll({
@ -157,17 +157,17 @@ router.post('documents.pinned', auth(), pagination(), async ctx => {
};
});
router.post('documents.archived', auth(), pagination(), async ctx => {
const { sort = 'updatedAt' } = ctx.body;
router.post("documents.archived", auth(), pagination(), async ctx => {
const { sort = "updatedAt" } = ctx.body;
let direction = ctx.body.direction;
if (direction !== 'ASC') direction = 'DESC';
if (direction !== "ASC") direction = "DESC";
const user = ctx.state.user;
const collectionIds = await user.collectionIds();
const collectionScope = { method: ['withCollection', user.id] };
const collectionScope = { method: ["withCollection", user.id] };
const documents = await Document.scope(
'defaultScope',
"defaultScope",
collectionScope
).findAll({
where: {
@ -195,15 +195,15 @@ router.post('documents.archived', auth(), pagination(), async ctx => {
};
});
router.post('documents.deleted', auth(), pagination(), async ctx => {
const { sort = 'deletedAt' } = ctx.body;
router.post("documents.deleted", auth(), pagination(), async ctx => {
const { sort = "deletedAt" } = ctx.body;
let direction = ctx.body.direction;
if (direction !== 'ASC') direction = 'DESC';
if (direction !== "ASC") direction = "DESC";
const user = ctx.state.user;
const collectionIds = await user.collectionIds();
const collectionScope = { method: ['withCollection', user.id] };
const collectionScope = { method: ["withCollection", user.id] };
const documents = await Document.scope(collectionScope).findAll({
where: {
teamId: user.teamId,
@ -213,8 +213,8 @@ router.post('documents.deleted', auth(), pagination(), async ctx => {
},
},
include: [
{ model: User, as: 'createdBy', paranoid: false },
{ model: User, as: 'updatedBy', paranoid: false },
{ model: User, as: "createdBy", paranoid: false },
{ model: User, as: "updatedBy", paranoid: false },
],
paranoid: false,
order: [[sort, direction]],
@ -235,9 +235,9 @@ router.post('documents.deleted', auth(), pagination(), async ctx => {
};
});
router.post('documents.viewed', auth(), pagination(), async ctx => {
let { sort = 'updatedAt', direction } = ctx.body;
if (direction !== 'ASC') direction = 'DESC';
router.post("documents.viewed", auth(), pagination(), async ctx => {
let { sort = "updatedAt", direction } = ctx.body;
if (direction !== "ASC") direction = "DESC";
const user = ctx.state.user;
const collectionIds = await user.collectionIds();
@ -255,7 +255,7 @@ router.post('documents.viewed', auth(), pagination(), async ctx => {
include: [
{
model: Star,
as: 'starred',
as: "starred",
where: { userId: user.id },
required: false,
},
@ -280,9 +280,9 @@ router.post('documents.viewed', auth(), pagination(), async ctx => {
};
});
router.post('documents.starred', auth(), pagination(), async ctx => {
let { sort = 'updatedAt', direction } = ctx.body;
if (direction !== 'ASC') direction = 'DESC';
router.post("documents.starred", auth(), pagination(), async ctx => {
let { sort = "updatedAt", direction } = ctx.body;
if (direction !== "ASC") direction = "DESC";
const user = ctx.state.user;
const collectionIds = await user.collectionIds();
@ -301,11 +301,11 @@ router.post('documents.starred', auth(), pagination(), async ctx => {
include: [
{
model: Collection,
as: 'collection',
as: "collection",
},
{
model: Star,
as: 'starred',
as: "starred",
where: {
userId: user.id,
},
@ -331,16 +331,16 @@ router.post('documents.starred', auth(), pagination(), async ctx => {
};
});
router.post('documents.drafts', auth(), pagination(), async ctx => {
let { sort = 'updatedAt', direction } = ctx.body;
if (direction !== 'ASC') direction = 'DESC';
router.post("documents.drafts", auth(), pagination(), async ctx => {
let { sort = "updatedAt", direction } = ctx.body;
if (direction !== "ASC") direction = "DESC";
const user = ctx.state.user;
const collectionIds = await user.collectionIds();
const collectionScope = { method: ['withCollection', user.id] };
const collectionScope = { method: ["withCollection", user.id] };
const documents = await Document.scope(
'defaultScope',
"defaultScope",
collectionScope
).findAll({
where: {
@ -366,9 +366,9 @@ router.post('documents.drafts', auth(), pagination(), async ctx => {
};
});
router.post('documents.info', auth({ required: false }), async ctx => {
router.post("documents.info", auth({ required: false }), async ctx => {
const { id, shareId } = ctx.body;
ctx.assertPresent(id || shareId, 'id or shareId is required');
ctx.assertPresent(id || shareId, "id or shareId is required");
const user = ctx.state.user;
let document;
@ -384,16 +384,16 @@ router.post('documents.info', auth({ required: false }), async ctx => {
// unscoping here allows us to return unpublished documents
model: Document.unscoped(),
include: [
{ model: User, as: 'createdBy', paranoid: false },
{ model: User, as: 'updatedBy', paranoid: false },
{ model: User, as: "createdBy", paranoid: false },
{ model: User, as: "updatedBy", paranoid: false },
],
required: true,
as: 'document',
as: "document",
},
],
});
if (!share || share.document.archivedAt) {
throw new InvalidRequestError('Document could not be found for shareId');
throw new InvalidRequestError("Document could not be found for shareId");
}
document = share.document;
} else {
@ -401,10 +401,10 @@ router.post('documents.info', auth({ required: false }), async ctx => {
id,
user ? { userId: user.id } : undefined
);
authorize(user, 'read', document);
authorize(user, "read", document);
}
const isPublic = cannot(user, 'read', document);
const isPublic = cannot(user, "read", document);
ctx.body = {
data: await presentDocument(document, { isPublic }),
@ -412,9 +412,9 @@ router.post('documents.info', auth({ required: false }), async ctx => {
};
});
router.post('documents.restore', auth(), async ctx => {
router.post("documents.restore", auth(), async ctx => {
const { id, revisionId } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(id, "id is required");
const user = ctx.state.user;
const document = await Document.findByPk(id, {
@ -423,13 +423,13 @@ router.post('documents.restore', auth(), async ctx => {
});
if (document.deletedAt) {
authorize(user, 'restore', document);
authorize(user, "restore", document);
// restore a previously deleted document
await document.unarchive(user.id);
await Event.create({
name: 'documents.restore',
name: "documents.restore",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -438,13 +438,13 @@ router.post('documents.restore', auth(), async ctx => {
ip: ctx.request.ip,
});
} else if (document.archivedAt) {
authorize(user, 'unarchive', document);
authorize(user, "unarchive", document);
// restore a previously archived document
await document.unarchive(user.id);
await Event.create({
name: 'documents.unarchive',
name: "documents.unarchive",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -454,17 +454,17 @@ router.post('documents.restore', auth(), async ctx => {
});
} else if (revisionId) {
// restore a document to a specific revision
authorize(user, 'update', document);
authorize(user, "update", document);
const revision = await Revision.findByPk(revisionId);
authorize(document, 'restore', revision);
authorize(document, "restore", revision);
document.text = revision.text;
document.title = revision.title;
await document.save();
await Event.create({
name: 'documents.restore',
name: "documents.restore",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -473,7 +473,7 @@ router.post('documents.restore', auth(), async ctx => {
ip: ctx.request.ip,
});
} else {
ctx.assertPresent(revisionId, 'revisionId is required');
ctx.assertPresent(revisionId, "revisionId is required");
}
ctx.body = {
@ -482,7 +482,7 @@ router.post('documents.restore', auth(), async ctx => {
};
});
router.post('documents.search', auth(), pagination(), async ctx => {
router.post("documents.search", auth(), pagination(), async ctx => {
const {
query,
includeArchived,
@ -493,34 +493,34 @@ router.post('documents.search', auth(), pagination(), async ctx => {
} = ctx.body;
const { offset, limit } = ctx.state.pagination;
const user = ctx.state.user;
ctx.assertPresent(query, 'query is required');
ctx.assertPresent(query, "query is required");
if (collectionId) {
ctx.assertUuid(collectionId, 'collectionId must be a UUID');
ctx.assertUuid(collectionId, "collectionId must be a UUID");
const collection = await Collection.scope({
method: ['withMembership', user.id],
method: ["withMembership", user.id],
}).findByPk(collectionId);
authorize(user, 'read', collection);
authorize(user, "read", collection);
}
let collaboratorIds = undefined;
if (userId) {
ctx.assertUuid(userId, 'userId must be a UUID');
ctx.assertUuid(userId, "userId must be a UUID");
collaboratorIds = [userId];
}
if (dateFilter) {
ctx.assertIn(
dateFilter,
['day', 'week', 'month', 'year'],
'dateFilter must be one of day,week,month,year'
["day", "week", "month", "year"],
"dateFilter must be one of day,week,month,year"
);
}
const results = await Document.searchForUser(user, query, {
includeArchived: includeArchived === 'true',
includeDrafts: includeDrafts === 'true',
includeArchived: includeArchived === "true",
includeDrafts: includeDrafts === "true",
collaboratorIds,
collectionId,
dateFilter,
@ -545,19 +545,19 @@ router.post('documents.search', auth(), pagination(), async ctx => {
};
});
router.post('documents.pin', auth(), async ctx => {
router.post("documents.pin", auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(id, "id is required");
const user = ctx.state.user;
const document = await Document.findByPk(id, { userId: user.id });
authorize(user, 'pin', document);
authorize(user, "pin", document);
document.pinnedById = user.id;
await document.save();
await Event.create({
name: 'documents.pin',
name: "documents.pin",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -572,19 +572,19 @@ router.post('documents.pin', auth(), async ctx => {
};
});
router.post('documents.unpin', auth(), async ctx => {
router.post("documents.unpin", auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(id, "id is required");
const user = ctx.state.user;
const document = await Document.findByPk(id, { userId: user.id });
authorize(user, 'unpin', document);
authorize(user, "unpin", document);
document.pinnedById = null;
await document.save();
await Event.create({
name: 'documents.unpin',
name: "documents.unpin",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -599,20 +599,20 @@ router.post('documents.unpin', auth(), async ctx => {
};
});
router.post('documents.star', auth(), async ctx => {
router.post("documents.star", auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(id, "id is required");
const user = ctx.state.user;
const document = await Document.findByPk(id, { userId: user.id });
authorize(user, 'read', document);
authorize(user, "read", document);
await Star.findOrCreate({
where: { documentId: document.id, userId: user.id },
});
await Event.create({
name: 'documents.star',
name: "documents.star",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -626,20 +626,20 @@ router.post('documents.star', auth(), async ctx => {
};
});
router.post('documents.unstar', auth(), async ctx => {
router.post("documents.unstar", auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(id, "id is required");
const user = ctx.state.user;
const document = await Document.findByPk(id, { userId: user.id });
authorize(user, 'read', document);
authorize(user, "read", document);
await Star.destroy({
where: { documentId: document.id, userId: user.id },
});
await Event.create({
name: 'documents.unstar',
name: "documents.unstar",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -653,46 +653,46 @@ router.post('documents.unstar', auth(), async ctx => {
};
});
router.post('documents.create', auth(), async ctx => {
router.post("documents.create", auth(), async ctx => {
const {
title = '',
text = '',
title = "",
text = "",
publish,
collectionId,
parentDocumentId,
index,
} = ctx.body;
const editorVersion = ctx.headers['x-editor-version'];
const editorVersion = ctx.headers["x-editor-version"];
ctx.assertUuid(collectionId, 'collectionId must be an uuid');
ctx.assertUuid(collectionId, "collectionId must be an uuid");
if (parentDocumentId) {
ctx.assertUuid(parentDocumentId, 'parentDocumentId must be an uuid');
ctx.assertUuid(parentDocumentId, "parentDocumentId must be an uuid");
}
if (index) ctx.assertPositiveInteger(index, 'index must be an integer (>=0)');
if (index) ctx.assertPositiveInteger(index, "index must be an integer (>=0)");
const user = ctx.state.user;
authorize(user, 'create', Document);
authorize(user, "create", Document);
const collection = await Collection.scope({
method: ['withMembership', user.id],
method: ["withMembership", user.id],
}).findOne({
where: {
id: collectionId,
teamId: user.teamId,
},
});
authorize(user, 'publish', collection);
authorize(user, "publish", collection);
let parentDocument;
if (parentDocumentId && collection.type === 'atlas') {
if (parentDocumentId && collection.type === "atlas") {
parentDocument = await Document.findOne({
where: {
id: parentDocumentId,
collectionId: collection.id,
},
});
authorize(user, 'read', parentDocument, { collection });
authorize(user, "read", parentDocument, { collection });
}
let document = await Document.create({
@ -708,7 +708,7 @@ router.post('documents.create', auth(), async ctx => {
});
await Event.create({
name: 'documents.create',
name: "documents.create",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -721,7 +721,7 @@ router.post('documents.create', auth(), async ctx => {
await document.publish();
await Event.create({
name: 'documents.publish',
name: "documents.publish",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -745,7 +745,7 @@ router.post('documents.create', auth(), async ctx => {
};
});
router.post('documents.update', auth(), async ctx => {
router.post("documents.update", auth(), async ctx => {
const {
id,
title,
@ -756,18 +756,18 @@ router.post('documents.update', auth(), async ctx => {
lastRevision,
append,
} = ctx.body;
const editorVersion = ctx.headers['x-editor-version'];
const editorVersion = ctx.headers["x-editor-version"];
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(title || text, 'title or text is required');
if (append) ctx.assertPresent(text, 'Text is required while appending');
ctx.assertPresent(id, "id is required");
ctx.assertPresent(title || text, "title or text is required");
if (append) ctx.assertPresent(text, "Text is required while appending");
const user = ctx.state.user;
const document = await Document.findByPk(id, { userId: user.id });
authorize(user, 'update', document);
authorize(user, "update", document);
if (lastRevision && lastRevision !== document.revisionCount) {
throw new InvalidRequestError('Document has changed since last revision');
throw new InvalidRequestError("Document has changed since last revision");
}
// Update document
@ -801,7 +801,7 @@ router.post('documents.update', auth(), async ctx => {
if (publish) {
await Event.create({
name: 'documents.publish',
name: "documents.publish",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -811,7 +811,7 @@ router.post('documents.update', auth(), async ctx => {
});
} else {
await Event.create({
name: 'documents.update',
name: "documents.update",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -834,31 +834,31 @@ router.post('documents.update', auth(), async ctx => {
};
});
router.post('documents.move', auth(), async ctx => {
router.post("documents.move", auth(), async ctx => {
const { id, collectionId, parentDocumentId, index } = ctx.body;
ctx.assertUuid(id, 'id must be a uuid');
ctx.assertUuid(collectionId, 'collectionId must be a uuid');
ctx.assertUuid(id, "id must be a uuid");
ctx.assertUuid(collectionId, "collectionId must be a uuid");
if (parentDocumentId) {
ctx.assertUuid(parentDocumentId, 'parentDocumentId must be a uuid');
ctx.assertUuid(parentDocumentId, "parentDocumentId must be a uuid");
}
if (index) {
ctx.assertPositiveInteger(index, 'index must be a positive integer');
ctx.assertPositiveInteger(index, "index must be a positive integer");
}
if (parentDocumentId === id) {
throw new InvalidRequestError(
'Infinite loop detected, cannot nest a document inside itself'
"Infinite loop detected, cannot nest a document inside itself"
);
}
const user = ctx.state.user;
const document = await Document.findByPk(id, { userId: user.id });
authorize(user, 'move', document);
authorize(user, "move", document);
const { collection } = document;
if (collection.type !== 'atlas' && parentDocumentId) {
if (collection.type !== "atlas" && parentDocumentId) {
throw new InvalidRequestError(
'Document cannot be nested in this collection type'
"Document cannot be nested in this collection type"
);
}
@ -866,7 +866,7 @@ router.post('documents.move', auth(), async ctx => {
const parent = await Document.findByPk(parentDocumentId, {
userId: user.id,
});
authorize(user, 'update', parent);
authorize(user, "update", parent);
}
const { documents, collections } = await documentMover({
@ -891,18 +891,18 @@ router.post('documents.move', auth(), async ctx => {
};
});
router.post('documents.archive', auth(), async ctx => {
router.post("documents.archive", auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(id, "id is required");
const user = ctx.state.user;
const document = await Document.findByPk(id, { userId: user.id });
authorize(user, 'archive', document);
authorize(user, "archive", document);
await document.archive(user.id);
await Event.create({
name: 'documents.archive',
name: "documents.archive",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,
@ -917,18 +917,18 @@ router.post('documents.archive', auth(), async ctx => {
};
});
router.post('documents.delete', auth(), async ctx => {
router.post("documents.delete", auth(), async ctx => {
const { id } = ctx.body;
ctx.assertPresent(id, 'id is required');
ctx.assertPresent(id, "id is required");
const user = ctx.state.user;
const document = await Document.findByPk(id, { userId: user.id });
authorize(user, 'delete', document);
authorize(user, "delete", document);
await document.delete();
await Event.create({
name: 'documents.delete',
name: "documents.delete",
documentId: document.id,
collectionId: document.collectionId,
teamId: document.teamId,