mirror of
https://github.com/outline/outline.git
synced 2026-12-04 10:24:43 +03:00
remove SubscriptionType.Collection enum
This commit is contained in:
1 parent
ac3b913fbe
commit
08f73a8836
14 files changed
+233
-175
No files matched your search
@@ -79,7 +79,7 @@ function CollectionMenu({
|
||||
} = useRequest(() =>
|
||||
subscriptions.fetchOne({
|
||||
collectionId: collection.id,
|
||||
event: SubscriptionType.Collection,
|
||||
event: SubscriptionType.Document,
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@@ -108,7 +108,7 @@ const MenuTrigger: React.FC<MenuTriggerProps> = ({ label, onTrigger }) => {
|
||||
document.collectionId
|
||||
? subscriptions.fetchOne({
|
||||
collectionId: document.collectionId,
|
||||
event: SubscriptionType.Collection,
|
||||
event: SubscriptionType.Document,
|
||||
})
|
||||
: noop,
|
||||
])
|
||||
|
||||
@@ -217,7 +217,7 @@ export default class CollectionsStore extends Store<Collection> {
|
||||
subscribe = (collection: Collection) =>
|
||||
this.rootStore.subscriptions.create({
|
||||
collectionId: collection.id,
|
||||
event: SubscriptionType.Collection,
|
||||
event: SubscriptionType.Document,
|
||||
});
|
||||
|
||||
unsubscribe = (collection: Collection) => {
|
||||
|
||||
@@ -16,12 +16,13 @@ export default class SubscriptionsStore extends Store<Subscription> {
|
||||
|
||||
@action
|
||||
async fetchOne(
|
||||
options:
|
||||
| { documentId: string; event: SubscriptionType.Document }
|
||||
| { collectionId: string; event: SubscriptionType.Collection }
|
||||
options: { event: SubscriptionType } & (
|
||||
| { documentId: string }
|
||||
| { collectionId: string }
|
||||
)
|
||||
) {
|
||||
const subscription =
|
||||
options.event === SubscriptionType.Collection
|
||||
"collectionId" in options
|
||||
? this.getByCollectionId(options.collectionId)
|
||||
: this.getByDocumentId(options.documentId);
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ describe("subscriptionCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
const subscribedEvent = SubscriptionType.Document;
|
||||
|
||||
it("should create a collection subscription", async () => {
|
||||
it("should create a document subscription for the whole collection", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const collection = await buildCollection({
|
||||
@@ -25,7 +25,7 @@ describe("subscriptionCreator", () => {
|
||||
subscriptionCreator({
|
||||
ctx: createContext({ user, transaction, ip }),
|
||||
collectionId: collection.id,
|
||||
event: SubscriptionType.Collection,
|
||||
event: SubscriptionType.Document,
|
||||
})
|
||||
);
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { NotificationEventType, SubscriptionType } from "@shared/types";
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import {
|
||||
buildDocument,
|
||||
buildSubscription,
|
||||
@@ -8,7 +8,7 @@ import NotificationHelper from "./NotificationHelper";
|
||||
|
||||
describe("NotificationHelper", () => {
|
||||
describe("getDocumentNotificationRecipients", () => {
|
||||
it("should return all users who have notification enabled for the event when subscription type is not provided", async () => {
|
||||
it("should return all users who have notification enabled for the event", async () => {
|
||||
const documentAuthor = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: documentAuthor.id,
|
||||
@@ -23,6 +23,7 @@ describe("NotificationHelper", () => {
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
onlySubscribers: false,
|
||||
actorId: documentAuthor.id,
|
||||
});
|
||||
|
||||
@@ -46,7 +47,7 @@ describe("NotificationHelper", () => {
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
subscriptionTypes: [SubscriptionType.Document],
|
||||
onlySubscribers: true,
|
||||
actorId: documentAuthor.id,
|
||||
});
|
||||
|
||||
@@ -70,7 +71,7 @@ describe("NotificationHelper", () => {
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
subscriptionTypes: [SubscriptionType.Collection],
|
||||
onlySubscribers: true,
|
||||
actorId: documentAuthor.id,
|
||||
});
|
||||
|
||||
@@ -108,10 +109,7 @@ describe("NotificationHelper", () => {
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
subscriptionTypes: [
|
||||
SubscriptionType.Collection,
|
||||
SubscriptionType.Document,
|
||||
],
|
||||
onlySubscribers: true,
|
||||
actorId: documentAuthor.id,
|
||||
});
|
||||
|
||||
@@ -122,52 +120,6 @@ describe("NotificationHelper", () => {
|
||||
expect(recipientIds).toContain(documentSubscribedUser.id);
|
||||
});
|
||||
|
||||
it("should return no users when the user is subscribed to the document, but the requested subscription is for the containing collection", async () => {
|
||||
const documentAuthor = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: documentAuthor.id,
|
||||
teamId: documentAuthor.teamId,
|
||||
});
|
||||
const subscribedUser = await buildUser({ teamId: document.teamId });
|
||||
await buildSubscription({
|
||||
userId: subscribedUser.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
const recipients =
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
subscriptionTypes: [SubscriptionType.Collection],
|
||||
actorId: documentAuthor.id,
|
||||
});
|
||||
|
||||
expect(recipients.length).toEqual(0);
|
||||
});
|
||||
|
||||
it("should return no users when the user is subscribed to the containing collection, but the requested subscription is for the document", async () => {
|
||||
const documentAuthor = await buildUser();
|
||||
const document = await buildDocument({
|
||||
userId: documentAuthor.id,
|
||||
teamId: documentAuthor.teamId,
|
||||
});
|
||||
const subscribedUser = await buildUser({ teamId: document.teamId });
|
||||
await buildSubscription({
|
||||
userId: subscribedUser.id,
|
||||
collectionId: document.collectionId!,
|
||||
});
|
||||
|
||||
const recipients =
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
subscriptionTypes: [SubscriptionType.Document],
|
||||
actorId: documentAuthor.id,
|
||||
});
|
||||
|
||||
expect(recipients.length).toEqual(0);
|
||||
});
|
||||
|
||||
it("should not return suspended users", async () => {
|
||||
const documentAuthor = await buildUser();
|
||||
const document = await buildDocument({
|
||||
@@ -189,6 +141,7 @@ describe("NotificationHelper", () => {
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
onlySubscribers: false,
|
||||
actorId: documentAuthor.id,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import uniq from "lodash/uniq";
|
||||
import { Op, WhereOptions } from "sequelize";
|
||||
import { Op } from "sequelize";
|
||||
import {
|
||||
NotificationEventType,
|
||||
MentionType,
|
||||
@@ -63,9 +63,7 @@ export default class NotificationHelper {
|
||||
let recipients = await this.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
subscriptionTypes: !comment.parentCommentId
|
||||
? [SubscriptionType.Document]
|
||||
: undefined,
|
||||
onlySubscribers: !comment.parentCommentId,
|
||||
actorId,
|
||||
});
|
||||
|
||||
@@ -134,19 +132,19 @@ export default class NotificationHelper {
|
||||
*
|
||||
* @param document The document to get recipients for.
|
||||
* @param notificationType The notification type for which to find the recipients.
|
||||
* @param subscriptionTypes The active subscription types to consider for the recipient, if any.
|
||||
* @param onlySubscribers Whether to consider only the users who have active subscription to the document.
|
||||
* @param actorId The id of the user that performed the action.
|
||||
* @returns A list of recipients
|
||||
*/
|
||||
public static getDocumentNotificationRecipients = async ({
|
||||
document,
|
||||
notificationType,
|
||||
subscriptionTypes,
|
||||
onlySubscribers,
|
||||
actorId,
|
||||
}: {
|
||||
document: Document;
|
||||
notificationType: NotificationEventType;
|
||||
subscriptionTypes?: SubscriptionType[];
|
||||
onlySubscribers: boolean;
|
||||
actorId: string;
|
||||
}): Promise<User[]> => {
|
||||
// First find all the users that have notifications enabled for this event
|
||||
@@ -165,26 +163,18 @@ export default class NotificationHelper {
|
||||
);
|
||||
|
||||
// Filter further to only those that have a subscription to the document…
|
||||
if (subscriptionTypes) {
|
||||
const subscriptionFilters = subscriptionTypes.map<
|
||||
WhereOptions<Subscription>
|
||||
>((subscriptionType) =>
|
||||
subscriptionType === SubscriptionType.Collection
|
||||
? {
|
||||
collectionId: document.collectionId,
|
||||
event: SubscriptionType.Collection,
|
||||
}
|
||||
: {
|
||||
documentId: document.id,
|
||||
event: SubscriptionType.Document,
|
||||
}
|
||||
);
|
||||
|
||||
if (onlySubscribers) {
|
||||
const subscriptions = await Subscription.findAll({
|
||||
attributes: ["userId"],
|
||||
where: {
|
||||
userId: recipients.map((recipient) => recipient.id),
|
||||
[Op.or]: subscriptionFilters,
|
||||
[Op.or]: [
|
||||
{
|
||||
collectionId: document.collectionId,
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
{ documentId: document.id, event: SubscriptionType.Document },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -54,6 +54,7 @@ export default class DocumentPublishedNotificationsTask extends BaseTask<Documen
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.PublishDocument,
|
||||
onlySubscribers: false,
|
||||
actorId: document.lastModifiedById,
|
||||
})
|
||||
).filter((recipient) => !userIdsMentioned.includes(recipient.id));
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { subHours } from "date-fns";
|
||||
import differenceBy from "lodash/differenceBy";
|
||||
import { Op } from "sequelize";
|
||||
import {
|
||||
MentionType,
|
||||
NotificationEventType,
|
||||
SubscriptionType,
|
||||
} from "@shared/types";
|
||||
import { MentionType, NotificationEventType } from "@shared/types";
|
||||
import { createSubscriptionsForDocument } from "@server/commands/subscriptionCreator";
|
||||
import env from "@server/env";
|
||||
import Logger from "@server/logging/Logger";
|
||||
@@ -80,10 +76,7 @@ export default class RevisionCreatedNotificationsTask extends BaseTask<RevisionE
|
||||
await NotificationHelper.getDocumentNotificationRecipients({
|
||||
document,
|
||||
notificationType: NotificationEventType.UpdateDocument,
|
||||
subscriptionTypes: [
|
||||
SubscriptionType.Collection,
|
||||
SubscriptionType.Document,
|
||||
],
|
||||
onlySubscribers: true,
|
||||
actorId: document.lastModifiedById,
|
||||
})
|
||||
).filter((recipient) => !userIdsMentioned.includes(recipient.id));
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
import isEmpty from "lodash/isEmpty";
|
||||
import { z } from "zod";
|
||||
import { SubscriptionType } from "@shared/types";
|
||||
import { ValidateDocumentId } from "@server/validation";
|
||||
import { BaseSchema } from "../schema";
|
||||
|
||||
const SubscriptionBody = z.discriminatedUnion("event", [
|
||||
z.object({
|
||||
event: z.literal(SubscriptionType.Collection),
|
||||
collectionId: z.string().uuid(),
|
||||
}),
|
||||
z.object({
|
||||
const SubscriptionBody = z
|
||||
.object({
|
||||
event: z.literal(SubscriptionType.Document),
|
||||
documentId: z.string().refine(ValidateDocumentId.isValid, {
|
||||
message: ValidateDocumentId.message,
|
||||
}),
|
||||
}),
|
||||
]);
|
||||
collectionId: z.string().uuid().optional(),
|
||||
documentId: z
|
||||
.string()
|
||||
.refine(ValidateDocumentId.isValid, {
|
||||
message: ValidateDocumentId.message,
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
.refine((obj) => !(isEmpty(obj.collectionId) && isEmpty(obj.documentId)), {
|
||||
message: "one of collectionId or documentId is required",
|
||||
});
|
||||
|
||||
export const SubscriptionsListSchema = BaseSchema.extend({
|
||||
body: SubscriptionBody,
|
||||
|
||||
@@ -1,15 +1,41 @@
|
||||
import { SubscriptionType } from "@shared/types";
|
||||
import { Event } from "@server/models";
|
||||
import {
|
||||
buildUser,
|
||||
buildSubscription,
|
||||
buildDocument,
|
||||
buildCollection,
|
||||
} from "@server/test/factories";
|
||||
import { getTestServer } from "@server/test/support";
|
||||
|
||||
const server = getTestServer();
|
||||
|
||||
describe("#subscriptions.create", () => {
|
||||
it("should create a subscription", async () => {
|
||||
it("should create a document subscription for the whole collection", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const collection = await buildCollection({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const res = await server.post("/api/subscriptions.create", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
collectionId: collection.id,
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.id).toBeDefined();
|
||||
expect(body.data.userId).toEqual(user.id);
|
||||
expect(body.data.collectionId).toEqual(collection.id);
|
||||
});
|
||||
|
||||
it("should create a document subscription", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
@@ -21,7 +47,7 @@ describe("#subscriptions.create", () => {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -45,7 +71,7 @@ describe("#subscriptions.create", () => {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -79,7 +105,7 @@ describe("#subscriptions.create", () => {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -88,7 +114,7 @@ describe("#subscriptions.create", () => {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -97,17 +123,16 @@ describe("#subscriptions.create", () => {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
// List subscriptions associated with
|
||||
// `document.id`
|
||||
// List subscriptions associated with `document.id`
|
||||
const res = await server.post("/api/subscriptions.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -132,8 +157,7 @@ describe("#subscriptions.create", () => {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
documentId: document.id,
|
||||
// Subscription on event
|
||||
// that cannot be subscribed to.
|
||||
// Subscription on event that cannot be subscribed to.
|
||||
event: "documents.publish",
|
||||
},
|
||||
});
|
||||
@@ -144,13 +168,65 @@ describe("#subscriptions.create", () => {
|
||||
expect(body.ok).toEqual(false);
|
||||
expect(body.error).toEqual("validation_error");
|
||||
expect(body.message).toEqual(
|
||||
"event: Invalid discriminator value. Expected 'collections.update' | 'documents.update'"
|
||||
`event: Invalid literal value, expected "documents.update"`
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw 400 when neither documentId nor collectionId is provided", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const res = await server.post("/api/subscriptions.create", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(400);
|
||||
expect(body.ok).toEqual(false);
|
||||
expect(body.error).toEqual("validation_error");
|
||||
expect(body.message).toEqual(
|
||||
"body: one of collectionId or documentId is required"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#subscriptions.info", () => {
|
||||
it("should provide info about a subscription", async () => {
|
||||
it("should provide info about a document subscription for the collection", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const subscriber = await buildUser({ teamId: user.teamId });
|
||||
|
||||
const collection = await buildCollection({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
await server.post("/api/subscriptions.create", {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
collectionId: collection.id,
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
const res = await server.post("/api/subscriptions.info", {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
collectionId: collection.id,
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
const subscription = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(subscription.data.id).toBeDefined();
|
||||
expect(subscription.data.userId).toEqual(subscriber.id);
|
||||
expect(subscription.data.collectionId).toEqual(collection.id);
|
||||
});
|
||||
|
||||
it("should provide info about a document subscription", async () => {
|
||||
const creator = await buildUser();
|
||||
|
||||
const subscriber = await buildUser({ teamId: creator.teamId });
|
||||
@@ -171,7 +247,7 @@ describe("#subscriptions.info", () => {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
documentId: document0.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -180,7 +256,7 @@ describe("#subscriptions.info", () => {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
documentId: document1.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -190,7 +266,7 @@ describe("#subscriptions.info", () => {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
documentId: document0.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -202,6 +278,25 @@ describe("#subscriptions.info", () => {
|
||||
expect(response0.data.documentId).toEqual(document0.id);
|
||||
});
|
||||
|
||||
it("should throw 400 when neither documentId nor collectionId is provided", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const res = await server.post("/api/subscriptions.info", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(400);
|
||||
expect(body.ok).toEqual(false);
|
||||
expect(body.error).toEqual("validation_error");
|
||||
expect(body.message).toEqual(
|
||||
"body: one of collectionId or documentId is required"
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw 404 if no subscription found", async () => {
|
||||
const author = await buildUser();
|
||||
const subscriber = await buildUser({ teamId: author.teamId });
|
||||
@@ -214,7 +309,7 @@ describe("#subscriptions.info", () => {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -243,7 +338,7 @@ describe("#subscriptions.info", () => {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
documentId: document0.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -252,17 +347,16 @@ describe("#subscriptions.info", () => {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
documentId: document1.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
// `viewer` wants info about `subscriber`'s
|
||||
// subscription on `document0`.
|
||||
// `viewer` wants info about `subscriber`'s subscription on `document0`.
|
||||
const subscription0 = await server.post("/api/subscriptions.info", {
|
||||
body: {
|
||||
token: viewer.getJwtToken(),
|
||||
documentId: document0.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -274,13 +368,12 @@ describe("#subscriptions.info", () => {
|
||||
expect(response0.error).toEqual("authorization_error");
|
||||
expect(response0.message).toEqual("Authorization error");
|
||||
|
||||
// `viewer` wants info about `subscriber`'s
|
||||
// subscription on `document0`.
|
||||
// `viewer` wants info about `subscriber`'s subscription on `document0`.
|
||||
const subscription1 = await server.post("/api/subscriptions.info", {
|
||||
body: {
|
||||
token: viewer.getJwtToken(),
|
||||
documentId: document1.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -316,7 +409,7 @@ describe("#subscriptions.info", () => {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
documentId: document0.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -325,13 +418,11 @@ describe("#subscriptions.info", () => {
|
||||
body: {
|
||||
token: subscriber.getJwtToken(),
|
||||
documentId: document1.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
// `viewer` wants info about `subscriber`'s
|
||||
// subscription on `document0`.
|
||||
// They have requested an invalid event.
|
||||
// `viewer` wants info about `subscriber`'s subscription on `document0` - they have requested an invalid event.
|
||||
const subscription0 = await server.post("/api/subscriptions.info", {
|
||||
body: {
|
||||
token: viewer.getJwtToken(),
|
||||
@@ -346,7 +437,7 @@ describe("#subscriptions.info", () => {
|
||||
expect(response0.ok).toEqual(false);
|
||||
expect(response0.error).toEqual("validation_error");
|
||||
expect(response0.message).toEqual(
|
||||
"event: Invalid discriminator value. Expected 'collections.update' | 'documents.update'"
|
||||
`event: Invalid literal value, expected "documents.update"`
|
||||
);
|
||||
|
||||
// `viewer` wants info about `subscriber`'s
|
||||
@@ -366,13 +457,13 @@ describe("#subscriptions.info", () => {
|
||||
expect(response1.ok).toEqual(false);
|
||||
expect(response1.error).toEqual("validation_error");
|
||||
expect(response1.message).toEqual(
|
||||
"event: Invalid discriminator value. Expected 'collections.update' | 'documents.update'"
|
||||
`event: Invalid literal value, expected "documents.update"`
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#subscriptions.list", () => {
|
||||
it("should list user subscriptions", async () => {
|
||||
it("should list user subscriptions for the document", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
@@ -389,7 +480,7 @@ describe("#subscriptions.list", () => {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
documentId: document.id,
|
||||
event: "documents.update",
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -535,7 +626,7 @@ describe("#subscriptions.list", () => {
|
||||
expect(body.ok).toEqual(false);
|
||||
expect(body.error).toEqual("validation_error");
|
||||
expect(body.message).toEqual(
|
||||
"event: Invalid discriminator value. Expected 'collections.update' | 'documents.update'"
|
||||
`event: Invalid literal value, expected "documents.update"`
|
||||
);
|
||||
});
|
||||
|
||||
@@ -591,6 +682,25 @@ describe("#subscriptions.list", () => {
|
||||
expect(body.error).toEqual("authorization_error");
|
||||
expect(body.message).toEqual("Authorization error");
|
||||
});
|
||||
|
||||
it("should throw 400 when neither documentId nor collectionId is provided", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const res = await server.post("/api/subscriptions.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
event: SubscriptionType.Document,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(400);
|
||||
expect(body.ok).toEqual(false);
|
||||
expect(body.error).toEqual("validation_error");
|
||||
expect(body.message).toEqual(
|
||||
"body: one of collectionId or documentId is required"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("#subscriptions.delete", () => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import Router from "koa-router";
|
||||
import { Transaction, WhereOptions } from "sequelize";
|
||||
import { QueryNotices, SubscriptionType } from "@shared/types";
|
||||
import { QueryNotices } from "@shared/types";
|
||||
import subscriptionCreator from "@server/commands/subscriptionCreator";
|
||||
import { createContext } from "@server/context";
|
||||
import env from "@server/env";
|
||||
@@ -26,16 +26,14 @@ router.post(
|
||||
validate(T.SubscriptionsListSchema),
|
||||
async (ctx: APIContext<T.SubscriptionsListReq>) => {
|
||||
const { user } = ctx.state.auth;
|
||||
const { event } = ctx.input.body;
|
||||
const { event, collectionId, documentId } = ctx.input.body;
|
||||
|
||||
const where: WhereOptions<Subscription> = {
|
||||
userId: user.id,
|
||||
event,
|
||||
};
|
||||
|
||||
if (event === SubscriptionType.Collection) {
|
||||
const { collectionId } = ctx.input.body;
|
||||
|
||||
if (collectionId) {
|
||||
const collection = await Collection.scope({
|
||||
method: ["withMembership", user.id],
|
||||
}).findByPk(collectionId);
|
||||
@@ -43,9 +41,10 @@ router.post(
|
||||
|
||||
where.collectionId = collectionId;
|
||||
} else {
|
||||
const { documentId } = ctx.input.body;
|
||||
|
||||
const document = await Document.findByPk(documentId, { userId: user.id });
|
||||
// documentId will be available here
|
||||
const document = await Document.findByPk(documentId!, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
|
||||
where.documentId = documentId;
|
||||
@@ -71,16 +70,14 @@ router.post(
|
||||
validate(T.SubscriptionsInfoSchema),
|
||||
async (ctx: APIContext<T.SubscriptionsInfoReq>) => {
|
||||
const { user } = ctx.state.auth;
|
||||
const { event } = ctx.input.body;
|
||||
const { event, collectionId, documentId } = ctx.input.body;
|
||||
|
||||
const where: WhereOptions<Subscription> = {
|
||||
userId: user.id,
|
||||
event,
|
||||
};
|
||||
|
||||
if (event === SubscriptionType.Collection) {
|
||||
const { collectionId } = ctx.input.body;
|
||||
|
||||
if (collectionId) {
|
||||
const collection = await Collection.scope({
|
||||
method: ["withMembership", user.id],
|
||||
}).findByPk(collectionId);
|
||||
@@ -88,9 +85,10 @@ router.post(
|
||||
|
||||
where.collectionId = collectionId;
|
||||
} else {
|
||||
const { documentId } = ctx.input.body;
|
||||
|
||||
const document = await Document.findByPk(documentId, { userId: user.id });
|
||||
// documentId will be available here
|
||||
const document = await Document.findByPk(documentId!, {
|
||||
userId: user.id,
|
||||
});
|
||||
authorize(user, "read", document);
|
||||
|
||||
where.documentId = documentId;
|
||||
@@ -115,22 +113,19 @@ router.post(
|
||||
transaction(),
|
||||
async (ctx: APIContext<T.SubscriptionsCreateReq>) => {
|
||||
const { user } = ctx.state.auth;
|
||||
const { event } = ctx.input.body;
|
||||
|
||||
let documentId, collectionId;
|
||||
|
||||
if (event === SubscriptionType.Collection) {
|
||||
collectionId = ctx.input.body.collectionId;
|
||||
const { event, collectionId, documentId } = ctx.input.body;
|
||||
|
||||
if (collectionId) {
|
||||
const collection = await Collection.scope({
|
||||
method: ["withMembership", user.id],
|
||||
}).findByPk(collectionId);
|
||||
|
||||
authorize(user, "subscribe", collection);
|
||||
} else {
|
||||
documentId = ctx.input.body.documentId;
|
||||
|
||||
const document = await Document.findByPk(documentId, { userId: user.id });
|
||||
// documentId will be available here
|
||||
const document = await Document.findByPk(documentId!, {
|
||||
userId: user.id,
|
||||
});
|
||||
|
||||
authorize(user, "subscribe", document);
|
||||
}
|
||||
|
||||
@@ -109,16 +109,29 @@ export async function buildStar(overrides: Partial<Star> = {}) {
|
||||
});
|
||||
}
|
||||
|
||||
export async function buildSubscription(
|
||||
options:
|
||||
| { userId: string } & ({ collectionId: string } | { documentId: string })
|
||||
) {
|
||||
export async function buildSubscription(overrides: Partial<Subscription> = {}) {
|
||||
let user;
|
||||
|
||||
if (overrides.userId) {
|
||||
user = await User.findByPk(overrides.userId, {
|
||||
rejectOnEmpty: true,
|
||||
});
|
||||
} else {
|
||||
user = await buildUser();
|
||||
overrides.userId = user.id;
|
||||
}
|
||||
|
||||
if (!overrides.documentId && !overrides.collectionId) {
|
||||
const document = await buildDocument({
|
||||
createdById: overrides.userId,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
overrides.documentId = document.id;
|
||||
}
|
||||
|
||||
return Subscription.create({
|
||||
event:
|
||||
"collectionId" in options
|
||||
? SubscriptionType.Collection
|
||||
: SubscriptionType.Document,
|
||||
...options,
|
||||
event: SubscriptionType.Document,
|
||||
...overrides,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -271,7 +271,6 @@ export type CollectionSort = {
|
||||
};
|
||||
|
||||
export enum SubscriptionType {
|
||||
Collection = "collections.update",
|
||||
Document = "documents.update",
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user