chore: Remove over-usage of invariant (#3719)
This commit is contained in:
@@ -3,7 +3,6 @@ import {
|
|||||||
onLoadDocumentPayload,
|
onLoadDocumentPayload,
|
||||||
Extension,
|
Extension,
|
||||||
} from "@hocuspocus/server";
|
} from "@hocuspocus/server";
|
||||||
import invariant from "invariant";
|
|
||||||
import * as Y from "yjs";
|
import * as Y from "yjs";
|
||||||
import { sequelize } from "@server/database/sequelize";
|
import { sequelize } from "@server/database/sequelize";
|
||||||
import Logger from "@server/logging/Logger";
|
import Logger from "@server/logging/Logger";
|
||||||
@@ -30,11 +29,11 @@ export default class PersistenceExtension implements Extension {
|
|||||||
const document = await Document.scope("withState").findOne({
|
const document = await Document.scope("withState").findOne({
|
||||||
transaction,
|
transaction,
|
||||||
lock: transaction.LOCK.UPDATE,
|
lock: transaction.LOCK.UPDATE,
|
||||||
|
rejectOnEmpty: true,
|
||||||
where: {
|
where: {
|
||||||
id: documentId,
|
id: documentId,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
invariant(document, "Document not found");
|
|
||||||
|
|
||||||
if (document.state) {
|
if (document.state) {
|
||||||
const ydoc = new Y.Doc();
|
const ydoc = new Y.Doc();
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { yDocToProsemirrorJSON } from "@getoutline/y-prosemirror";
|
import { yDocToProsemirrorJSON } from "@getoutline/y-prosemirror";
|
||||||
import invariant from "invariant";
|
|
||||||
import { uniq } from "lodash";
|
import { uniq } from "lodash";
|
||||||
import { Node } from "prosemirror-model";
|
import { Node } from "prosemirror-model";
|
||||||
import * as Y from "yjs";
|
import * as Y from "yjs";
|
||||||
@@ -25,9 +24,9 @@ export default async function documentCollaborativeUpdater({
|
|||||||
of: Document,
|
of: Document,
|
||||||
level: transaction.LOCK.UPDATE,
|
level: transaction.LOCK.UPDATE,
|
||||||
},
|
},
|
||||||
|
rejectOnEmpty: true,
|
||||||
paranoid: false,
|
paranoid: false,
|
||||||
});
|
});
|
||||||
invariant(document, "document not found");
|
|
||||||
|
|
||||||
const state = Y.encodeStateAsUpdate(ydoc);
|
const state = Y.encodeStateAsUpdate(ydoc);
|
||||||
const node = Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, "default"));
|
const node = Node.fromJSON(schema, yDocToProsemirrorJSON(ydoc, "default"));
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import invariant from "invariant";
|
|
||||||
import { Transaction } from "sequelize";
|
import { Transaction } from "sequelize";
|
||||||
import { Document, Event, User } from "@server/models";
|
import { Document, Event, User } from "@server/models";
|
||||||
|
|
||||||
@@ -105,14 +104,12 @@ export default async function documentCreator({
|
|||||||
// reload to get all of the data needed to present (user, collection etc)
|
// reload to get all of the data needed to present (user, collection etc)
|
||||||
// we need to specify publishedAt to bypass default scope that only returns
|
// we need to specify publishedAt to bypass default scope that only returns
|
||||||
// published documents
|
// published documents
|
||||||
const doc = await Document.findOne({
|
return await Document.findOne({
|
||||||
where: {
|
where: {
|
||||||
id: document.id,
|
id: document.id,
|
||||||
publishedAt: document.publishedAt,
|
publishedAt: document.publishedAt,
|
||||||
},
|
},
|
||||||
|
rejectOnEmpty: true,
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
invariant(doc, "Document must exist");
|
|
||||||
|
|
||||||
return doc;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -149,8 +149,7 @@ export default async function loadDocument({
|
|||||||
}
|
}
|
||||||
|
|
||||||
// It is possible to disable sharing at the team level so we must check
|
// It is possible to disable sharing at the team level so we must check
|
||||||
const team = await Team.findByPk(document.teamId);
|
const team = await Team.findByPk(document.teamId, { rejectOnEmpty: true });
|
||||||
invariant(team, "team not found");
|
|
||||||
|
|
||||||
if (!team.sharing) {
|
if (!team.sharing) {
|
||||||
throw AuthorizationError();
|
throw AuthorizationError();
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import invariant from "invariant";
|
|
||||||
import { uniqBy } from "lodash";
|
import { uniqBy } from "lodash";
|
||||||
import { Role } from "@shared/types";
|
import { Role } from "@shared/types";
|
||||||
import InviteEmail from "@server/emails/templates/InviteEmail";
|
import InviteEmail from "@server/emails/templates/InviteEmail";
|
||||||
@@ -25,8 +24,7 @@ export default async function userInviter({
|
|||||||
sent: Invite[];
|
sent: Invite[];
|
||||||
users: User[];
|
users: User[];
|
||||||
}> {
|
}> {
|
||||||
const team = await Team.findByPk(user.teamId);
|
const team = await Team.findByPk(user.teamId, { rejectOnEmpty: true });
|
||||||
invariant(team, "team not found");
|
|
||||||
|
|
||||||
// filter out empties and obvious non-emails
|
// filter out empties and obvious non-emails
|
||||||
const compactedInvites = invites.filter(
|
const compactedInvites = invites.filter(
|
||||||
|
|||||||
@@ -32,8 +32,8 @@ export default class RevisionsProcessor extends BaseProcessor {
|
|||||||
|
|
||||||
const user = await User.findByPk(event.actorId, {
|
const user = await User.findByPk(event.actorId, {
|
||||||
paranoid: false,
|
paranoid: false,
|
||||||
|
rejectOnEmpty: true,
|
||||||
});
|
});
|
||||||
invariant(user, "User should exist");
|
|
||||||
await revisionCreator({
|
await revisionCreator({
|
||||||
user,
|
user,
|
||||||
document,
|
document,
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import fetch from "fetch-with-proxy";
|
import fetch from "fetch-with-proxy";
|
||||||
import invariant from "invariant";
|
|
||||||
import env from "@server/env";
|
import env from "@server/env";
|
||||||
import Logger from "@server/logging/Logger";
|
import Logger from "@server/logging/Logger";
|
||||||
import {
|
import {
|
||||||
@@ -73,8 +72,9 @@ type Props = {
|
|||||||
|
|
||||||
export default class DeliverWebhookTask extends BaseTask<Props> {
|
export default class DeliverWebhookTask extends BaseTask<Props> {
|
||||||
public async perform({ subscriptionId, event }: Props) {
|
public async perform({ subscriptionId, event }: Props) {
|
||||||
const subscription = await WebhookSubscription.findByPk(subscriptionId);
|
const subscription = await WebhookSubscription.findByPk(subscriptionId, {
|
||||||
invariant(subscription, "Subscription not found");
|
rejectOnEmpty: true,
|
||||||
|
});
|
||||||
|
|
||||||
Logger.info(
|
Logger.info(
|
||||||
"task",
|
"task",
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import invariant from "invariant";
|
|
||||||
import { truncate } from "lodash";
|
import { truncate } from "lodash";
|
||||||
import ExportFailureEmail from "@server/emails/templates/ExportFailureEmail";
|
import ExportFailureEmail from "@server/emails/templates/ExportFailureEmail";
|
||||||
import ExportSuccessEmail from "@server/emails/templates/ExportSuccessEmail";
|
import ExportSuccessEmail from "@server/emails/templates/ExportSuccessEmail";
|
||||||
@@ -22,15 +21,14 @@ export default class ExportMarkdownZipTask extends BaseTask<Props> {
|
|||||||
* @param props The props
|
* @param props The props
|
||||||
*/
|
*/
|
||||||
public async perform({ fileOperationId }: Props) {
|
public async perform({ fileOperationId }: Props) {
|
||||||
const fileOperation = await FileOperation.findByPk(fileOperationId);
|
const fileOperation = await FileOperation.findByPk(fileOperationId, {
|
||||||
invariant(fileOperation, "fileOperation not found");
|
rejectOnEmpty: true,
|
||||||
|
});
|
||||||
|
|
||||||
const [team, user] = await Promise.all([
|
const [team, user] = await Promise.all([
|
||||||
Team.findByPk(fileOperation.teamId),
|
Team.findByPk(fileOperation.teamId, { rejectOnEmpty: true }),
|
||||||
User.findByPk(fileOperation.userId),
|
User.findByPk(fileOperation.userId, { rejectOnEmpty: true }),
|
||||||
]);
|
]);
|
||||||
invariant(team, "team operation not found");
|
|
||||||
invariant(user, "user operation not found");
|
|
||||||
|
|
||||||
const collectionIds = fileOperation.collectionId
|
const collectionIds = fileOperation.collectionId
|
||||||
? [fileOperation.collectionId]
|
? [fileOperation.collectionId]
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import invariant from "invariant";
|
|
||||||
import { truncate } from "lodash";
|
import { truncate } from "lodash";
|
||||||
import attachmentCreator from "@server/commands/attachmentCreator";
|
import attachmentCreator from "@server/commands/attachmentCreator";
|
||||||
import documentCreator from "@server/commands/documentCreator";
|
import documentCreator from "@server/commands/documentCreator";
|
||||||
@@ -79,8 +78,9 @@ export default abstract class ImportTask extends BaseTask<Props> {
|
|||||||
* @param props The props
|
* @param props The props
|
||||||
*/
|
*/
|
||||||
public async perform({ fileOperationId }: Props) {
|
public async perform({ fileOperationId }: Props) {
|
||||||
const fileOperation = await FileOperation.findByPk(fileOperationId);
|
const fileOperation = await FileOperation.findByPk(fileOperationId, {
|
||||||
invariant(fileOperation, "fileOperation not found");
|
rejectOnEmpty: true,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Logger.info("task", `ImportTask fetching data for ${fileOperationId}`);
|
Logger.info("task", `ImportTask fetching data for ${fileOperationId}`);
|
||||||
@@ -200,8 +200,8 @@ export default abstract class ImportTask extends BaseTask<Props> {
|
|||||||
return sequelize.transaction(async (transaction) => {
|
return sequelize.transaction(async (transaction) => {
|
||||||
const user = await User.findByPk(fileOperation.userId, {
|
const user = await User.findByPk(fileOperation.userId, {
|
||||||
transaction,
|
transaction,
|
||||||
|
rejectOnEmpty: true,
|
||||||
});
|
});
|
||||||
invariant(user, "User not found");
|
|
||||||
|
|
||||||
const ip = user.lastActiveIp || undefined;
|
const ip = user.lastActiveIp || undefined;
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import invariant from "invariant";
|
|
||||||
import Router from "koa-router";
|
import Router from "koa-router";
|
||||||
import { find } from "lodash";
|
import { find } from "lodash";
|
||||||
import { parseDomain } from "@shared/utils/domains";
|
import { parseDomain } from "@shared/utils/domains";
|
||||||
@@ -108,8 +107,9 @@ router.post("auth.config", async (ctx) => {
|
|||||||
|
|
||||||
router.post("auth.info", auth(), async (ctx) => {
|
router.post("auth.info", auth(), async (ctx) => {
|
||||||
const { user } = ctx.state;
|
const { user } = ctx.state;
|
||||||
const team = await Team.scope("withDomains").findByPk(user.teamId);
|
const team = await Team.scope("withDomains").findByPk(user.teamId, {
|
||||||
invariant(team, "Team not found");
|
rejectOnEmpty: true,
|
||||||
|
});
|
||||||
|
|
||||||
await ValidateSSOAccessTask.schedule({ userId: user.id });
|
await ValidateSSOAccessTask.schedule({ userId: user.id });
|
||||||
|
|
||||||
|
|||||||
@@ -641,8 +641,7 @@ router.post("collections.update", auth(), async (ctx) => {
|
|||||||
// if the privacy level has changed. Otherwise skip this query for speed.
|
// if the privacy level has changed. Otherwise skip this query for speed.
|
||||||
if (privacyChanged || sharingChanged) {
|
if (privacyChanged || sharingChanged) {
|
||||||
await collection.reload();
|
await collection.reload();
|
||||||
const team = await Team.findByPk(user.teamId);
|
const team = await Team.findByPk(user.teamId, { rejectOnEmpty: true });
|
||||||
invariant(team, "team not found");
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
collection.permission === null &&
|
collection.permission === null &&
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import {
|
|||||||
Star,
|
Star,
|
||||||
User,
|
User,
|
||||||
View,
|
View,
|
||||||
Team,
|
|
||||||
} from "@server/models";
|
} from "@server/models";
|
||||||
import { authorize, cannot } from "@server/policies";
|
import { authorize, cannot } from "@server/policies";
|
||||||
import {
|
import {
|
||||||
@@ -626,7 +625,7 @@ router.post(
|
|||||||
}
|
}
|
||||||
|
|
||||||
teamId = share.teamId;
|
teamId = share.teamId;
|
||||||
const team = await Team.findByPk(teamId);
|
const team = await share.$get("team");
|
||||||
invariant(team, "Share must belong to a team");
|
invariant(team, "Share must belong to a team");
|
||||||
|
|
||||||
response = await Document.searchForTeam(team, query, {
|
response = await Document.searchForTeam(team, query, {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import invariant from "invariant";
|
|
||||||
import Router from "koa-router";
|
import Router from "koa-router";
|
||||||
import { WhereOptions } from "sequelize/types";
|
import { WhereOptions } from "sequelize/types";
|
||||||
import fileOperationDeleter from "@server/commands/fileOperationDeleter";
|
import fileOperationDeleter from "@server/commands/fileOperationDeleter";
|
||||||
@@ -18,8 +17,9 @@ router.post("fileOperations.info", auth(), async (ctx) => {
|
|||||||
assertUuid(id, "id is required");
|
assertUuid(id, "id is required");
|
||||||
const { user } = ctx.state;
|
const { user } = ctx.state;
|
||||||
const team = await Team.findByPk(user.teamId);
|
const team = await Team.findByPk(user.teamId);
|
||||||
const fileOperation = await FileOperation.findByPk(id);
|
const fileOperation = await FileOperation.findByPk(id, {
|
||||||
invariant(fileOperation, "File operation not found");
|
rejectOnEmpty: true,
|
||||||
|
});
|
||||||
|
|
||||||
authorize(user, fileOperation.type, team);
|
authorize(user, fileOperation.type, team);
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import invariant from "invariant";
|
|
||||||
import Router from "koa-router";
|
import Router from "koa-router";
|
||||||
import { Op } from "sequelize";
|
import { Op } from "sequelize";
|
||||||
import { MAX_AVATAR_DISPLAY } from "@shared/constants";
|
import { MAX_AVATAR_DISPLAY } from "@shared/constants";
|
||||||
@@ -80,8 +79,7 @@ router.post("groups.create", auth(), async (ctx) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// reload to get default scope
|
// reload to get default scope
|
||||||
const group = await Group.findByPk(g.id);
|
const group = await Group.findByPk(g.id, { rejectOnEmpty: true });
|
||||||
invariant(group, "group not found");
|
|
||||||
|
|
||||||
await Event.create({
|
await Event.create({
|
||||||
name: "groups.create",
|
name: "groups.create",
|
||||||
@@ -231,12 +229,11 @@ router.post("groups.add_user", auth(), async (ctx) => {
|
|||||||
groupId: id,
|
groupId: id,
|
||||||
userId,
|
userId,
|
||||||
},
|
},
|
||||||
|
rejectOnEmpty: true,
|
||||||
});
|
});
|
||||||
invariant(membership, "membership not found");
|
|
||||||
|
|
||||||
// reload to get default scope
|
// reload to get default scope
|
||||||
group = await Group.findByPk(id);
|
group = await Group.findByPk(id, { rejectOnEmpty: true });
|
||||||
invariant(group, "group not found");
|
|
||||||
|
|
||||||
await Event.create({
|
await Event.create({
|
||||||
name: "groups.add_user",
|
name: "groups.add_user",
|
||||||
@@ -287,8 +284,7 @@ router.post("groups.remove_user", auth(), async (ctx) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// reload to get default scope
|
// reload to get default scope
|
||||||
group = await Group.findByPk(id);
|
group = await Group.findByPk(id, { rejectOnEmpty: true });
|
||||||
invariant(group, "group not found");
|
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: {
|
data: {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import invariant from "invariant";
|
|
||||||
import Router from "koa-router";
|
import Router from "koa-router";
|
||||||
import { escapeRegExp } from "lodash";
|
import { escapeRegExp } from "lodash";
|
||||||
import env from "@server/env";
|
import env from "@server/env";
|
||||||
@@ -103,8 +102,7 @@ router.post("hooks.interactive", async (ctx) => {
|
|||||||
throw InvalidRequestError("Invalid callback_id");
|
throw InvalidRequestError("Invalid callback_id");
|
||||||
}
|
}
|
||||||
|
|
||||||
const team = await Team.findByPk(document.teamId);
|
const team = await Team.findByPk(document.teamId, { rejectOnEmpty: true });
|
||||||
invariant(team, "team not found");
|
|
||||||
|
|
||||||
// respond with a public message that will be posted in the original channel
|
// respond with a public message that will be posted in the original channel
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import invariant from "invariant";
|
|
||||||
import Router from "koa-router";
|
import Router from "koa-router";
|
||||||
import { Sequelize, Op } from "sequelize";
|
import { Sequelize, Op } from "sequelize";
|
||||||
import pinCreator from "@server/commands/pinCreator";
|
import pinCreator from "@server/commands/pinCreator";
|
||||||
@@ -106,8 +105,7 @@ router.post("pins.update", auth(), async (ctx) => {
|
|||||||
assertIndexCharacters(index);
|
assertIndexCharacters(index);
|
||||||
|
|
||||||
const { user } = ctx.state;
|
const { user } = ctx.state;
|
||||||
let pin = await Pin.findByPk(id);
|
let pin = await Pin.findByPk(id, { rejectOnEmpty: true });
|
||||||
invariant(pin, "pin not found");
|
|
||||||
|
|
||||||
const document = await Document.findByPk(pin.documentId, {
|
const document = await Document.findByPk(pin.documentId, {
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
@@ -137,8 +135,7 @@ router.post("pins.delete", auth(), async (ctx) => {
|
|||||||
assertUuid(id, "id is required");
|
assertUuid(id, "id is required");
|
||||||
|
|
||||||
const { user } = ctx.state;
|
const { user } = ctx.state;
|
||||||
const pin = await Pin.findByPk(id);
|
const pin = await Pin.findByPk(id, { rejectOnEmpty: true });
|
||||||
invariant(pin, "pin not found");
|
|
||||||
|
|
||||||
const document = await Document.findByPk(pin.documentId, {
|
const document = await Document.findByPk(pin.documentId, {
|
||||||
userId: user.id,
|
userId: user.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user