feat: Document subscriptions (#3834)
Co-authored-by: Tom Moor <tom.moor@gmail.com>
This commit is contained in:
committed by
GitHub
parent
864f585e5b
commit
24c71c38a5
275
server/commands/subscriptionCreator.test.ts
Normal file
275
server/commands/subscriptionCreator.test.ts
Normal file
@@ -0,0 +1,275 @@
|
||||
import { sequelize } from "@server/database/sequelize";
|
||||
import { Subscription, Event } from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { getTestDatabase } from "@server/test/support";
|
||||
import subscriptionCreator from "./subscriptionCreator";
|
||||
import subscriptionDestroyer from "./subscriptionDestroyer";
|
||||
|
||||
const db = getTestDatabase();
|
||||
|
||||
beforeEach(db.flush);
|
||||
afterAll(db.disconnect);
|
||||
|
||||
describe("subscriptionCreator", () => {
|
||||
const ip = "127.0.0.1";
|
||||
const subscribedEvent = "documents.update";
|
||||
|
||||
it("should create a subscription", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const subscription = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
const event = await Event.findOne();
|
||||
|
||||
expect(subscription.documentId).toEqual(document.id);
|
||||
expect(subscription.userId).toEqual(user.id);
|
||||
expect(event?.name).toEqual("subscriptions.create");
|
||||
expect(event?.modelId).toEqual(subscription.id);
|
||||
expect(event?.actorId).toEqual(subscription.userId);
|
||||
expect(event?.userId).toEqual(subscription.userId);
|
||||
expect(event?.documentId).toEqual(subscription.documentId);
|
||||
});
|
||||
|
||||
it("should not create another subscription if one already exists", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const subscription0 = await Subscription.create({
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
});
|
||||
|
||||
const subscription1 = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
expect(subscription0.event).toEqual(subscribedEvent);
|
||||
expect(subscription1.event).toEqual(subscribedEvent);
|
||||
|
||||
expect(subscription0.userId).toEqual(user.id);
|
||||
expect(subscription1.userId).toEqual(user.id);
|
||||
|
||||
// Primary concern
|
||||
expect(subscription0.id).toEqual(subscription1.id);
|
||||
|
||||
// Edge cases
|
||||
expect(subscription0.documentId).toEqual(document.id);
|
||||
expect(subscription1.documentId).toEqual(document.id);
|
||||
|
||||
expect(subscription0.userId).toEqual(subscription1.userId);
|
||||
expect(subscription0.documentId).toEqual(subscription1.documentId);
|
||||
});
|
||||
|
||||
it("should enable subscription by overriding one that exists in disabled state", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const subscription0 = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
await sequelize.transaction(async (transaction) =>
|
||||
subscriptionDestroyer({
|
||||
user,
|
||||
subscription: subscription0,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
expect(subscription0.id).toBeDefined();
|
||||
expect(subscription0.userId).toEqual(user.id);
|
||||
expect(subscription0.documentId).toEqual(document.id);
|
||||
expect(subscription0.deletedAt).toBeDefined();
|
||||
|
||||
const subscription1 = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
const events = await Event.count();
|
||||
|
||||
// 3 events. 1 create, 1 destroy and 1 re-create.
|
||||
expect(events).toEqual(3);
|
||||
|
||||
expect(subscription0.id).toEqual(subscription1.id);
|
||||
expect(subscription0.documentId).toEqual(document.id);
|
||||
expect(subscription0.userId).toEqual(user.id);
|
||||
expect(subscription1.documentId).toEqual(document.id);
|
||||
expect(subscription1.userId).toEqual(user.id);
|
||||
expect(subscription0.id).toEqual(subscription1.id);
|
||||
expect(subscription0.userId).toEqual(subscription1.userId);
|
||||
expect(subscription0.documentId).toEqual(subscription1.documentId);
|
||||
});
|
||||
|
||||
it("should fetch already enabled subscription on create request", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const subscription0 = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
const subscription1 = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
// Should emit 1 event instead of 2.
|
||||
const events = await Event.count();
|
||||
expect(events).toEqual(1);
|
||||
|
||||
expect(subscription0.documentId).toEqual(document.id);
|
||||
expect(subscription0.userId).toEqual(user.id);
|
||||
expect(subscription1.documentId).toEqual(document.id);
|
||||
expect(subscription1.userId).toEqual(user.id);
|
||||
expect(subscription0.id).toEqual(subscription1.id);
|
||||
expect(subscription0.userId).toEqual(subscription1.userId);
|
||||
expect(subscription0.documentId).toEqual(subscription1.documentId);
|
||||
});
|
||||
|
||||
it("should emit event when re-creating subscription", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const subscription0 = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
await sequelize.transaction(async (transaction) =>
|
||||
subscriptionDestroyer({
|
||||
user,
|
||||
subscription: subscription0,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
expect(subscription0.id).toBeDefined();
|
||||
expect(subscription0.userId).toEqual(user.id);
|
||||
expect(subscription0.documentId).toEqual(document.id);
|
||||
expect(subscription0.deletedAt).toBeDefined();
|
||||
|
||||
const subscription1 = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
// Should emit 3 events.
|
||||
// 2 create, 1 destroy.
|
||||
const events = await Event.findAll();
|
||||
expect(events.length).toEqual(3);
|
||||
|
||||
expect(events[0].name).toEqual("subscriptions.create");
|
||||
expect(events[0].documentId).toEqual(document.id);
|
||||
expect(events[1].name).toEqual("subscriptions.delete");
|
||||
expect(events[1].documentId).toEqual(document.id);
|
||||
expect(events[2].name).toEqual("subscriptions.create");
|
||||
expect(events[2].documentId).toEqual(document.id);
|
||||
|
||||
expect(subscription0.documentId).toEqual(document.id);
|
||||
expect(subscription0.userId).toEqual(user.id);
|
||||
expect(subscription1.documentId).toEqual(document.id);
|
||||
expect(subscription1.userId).toEqual(user.id);
|
||||
expect(subscription0.id).toEqual(subscription1.id);
|
||||
expect(subscription0.userId).toEqual(subscription1.userId);
|
||||
expect(subscription0.documentId).toEqual(subscription1.documentId);
|
||||
});
|
||||
|
||||
it("should fetch deletedAt column with paranoid option", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const subscription0 = await sequelize.transaction(async (transaction) =>
|
||||
subscriptionCreator({
|
||||
user,
|
||||
documentId: document.id,
|
||||
event: subscribedEvent,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
const events = await Event.count();
|
||||
expect(events).toEqual(1);
|
||||
|
||||
expect(subscription0.documentId).toEqual(document.id);
|
||||
expect(subscription0.userId).toEqual(user.id);
|
||||
expect(subscription0.userId).toEqual(user.id);
|
||||
expect(subscription0.documentId).toEqual(document.id);
|
||||
expect(subscription0.deletedAt).toEqual(null);
|
||||
});
|
||||
});
|
||||
74
server/commands/subscriptionCreator.ts
Normal file
74
server/commands/subscriptionCreator.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
import { Transaction } from "sequelize";
|
||||
import { Subscription, Event, User } from "@server/models";
|
||||
|
||||
type Props = {
|
||||
/** The user creating the subscription */
|
||||
user: User;
|
||||
/** The document to subscribe to */
|
||||
documentId?: string;
|
||||
/** Event to subscribe to */
|
||||
event: string;
|
||||
/** The IP address of the incoming request */
|
||||
ip: string;
|
||||
/** Whether the subscription should be restored if it exists in a deleted state */
|
||||
resubscribe?: boolean;
|
||||
transaction: Transaction;
|
||||
};
|
||||
|
||||
/**
|
||||
* This command creates a subscription of a user to a document.
|
||||
*
|
||||
* @returns The subscription that was created
|
||||
*/
|
||||
export default async function subscriptionCreator({
|
||||
user,
|
||||
documentId,
|
||||
event,
|
||||
ip,
|
||||
resubscribe = true,
|
||||
transaction,
|
||||
}: Props): Promise<Subscription> {
|
||||
const [subscription, created] = await Subscription.findOrCreate({
|
||||
where: {
|
||||
userId: user.id,
|
||||
documentId,
|
||||
event,
|
||||
},
|
||||
transaction,
|
||||
// Previous subscriptions are soft-deleted, we want to know about them here
|
||||
paranoid: false,
|
||||
});
|
||||
|
||||
// If the subscription was deleted, then just restore the existing row.
|
||||
if (subscription.deletedAt && resubscribe) {
|
||||
subscription.restore({ transaction });
|
||||
|
||||
await Event.create(
|
||||
{
|
||||
name: "subscriptions.create",
|
||||
modelId: subscription.id,
|
||||
actorId: user.id,
|
||||
userId: user.id,
|
||||
documentId,
|
||||
ip,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
if (created) {
|
||||
await Event.create(
|
||||
{
|
||||
name: "subscriptions.create",
|
||||
modelId: subscription.id,
|
||||
actorId: user.id,
|
||||
userId: user.id,
|
||||
documentId,
|
||||
ip,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
}
|
||||
|
||||
return subscription;
|
||||
}
|
||||
101
server/commands/subscriptionDestroyer.test.ts
Normal file
101
server/commands/subscriptionDestroyer.test.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { sequelize } from "@server/database/sequelize";
|
||||
import { Subscription, Event } from "@server/models";
|
||||
import {
|
||||
buildDocument,
|
||||
buildSubscription,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { getTestDatabase } from "@server/test/support";
|
||||
import subscriptionDestroyer from "./subscriptionDestroyer";
|
||||
|
||||
const db = getTestDatabase();
|
||||
|
||||
beforeEach(db.flush);
|
||||
afterAll(db.disconnect);
|
||||
|
||||
describe("subscriptionDestroyer", () => {
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
it("should destroy existing subscription", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const subscription = await buildSubscription({
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
await sequelize.transaction(
|
||||
async (transaction) =>
|
||||
await subscriptionDestroyer({
|
||||
user,
|
||||
subscription,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
const count = await Subscription.count();
|
||||
|
||||
expect(count).toEqual(0);
|
||||
|
||||
const event = await Event.findOne();
|
||||
|
||||
expect(event?.name).toEqual("subscriptions.delete");
|
||||
expect(event?.modelId).toEqual(subscription.id);
|
||||
expect(event?.actorId).toEqual(subscription.userId);
|
||||
expect(event?.userId).toEqual(subscription.userId);
|
||||
expect(event?.documentId).toEqual(subscription.documentId);
|
||||
});
|
||||
|
||||
it("should soft delete row", async () => {
|
||||
const user = await buildUser();
|
||||
|
||||
const document = await buildDocument({
|
||||
userId: user.id,
|
||||
teamId: user.teamId,
|
||||
});
|
||||
|
||||
const subscription = await buildSubscription({
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
|
||||
await sequelize.transaction(
|
||||
async (transaction) =>
|
||||
await subscriptionDestroyer({
|
||||
user,
|
||||
subscription,
|
||||
ip,
|
||||
transaction,
|
||||
})
|
||||
);
|
||||
|
||||
const count = await Subscription.count();
|
||||
|
||||
expect(count).toEqual(0);
|
||||
|
||||
const event = await Event.findOne();
|
||||
|
||||
expect(event?.name).toEqual("subscriptions.delete");
|
||||
expect(event?.modelId).toEqual(subscription.id);
|
||||
expect(event?.actorId).toEqual(subscription.userId);
|
||||
expect(event?.userId).toEqual(subscription.userId);
|
||||
expect(event?.documentId).toEqual(subscription.documentId);
|
||||
|
||||
const deletedSubscription = await Subscription.findOne({
|
||||
where: {
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
},
|
||||
paranoid: false,
|
||||
});
|
||||
|
||||
expect(deletedSubscription).toBeDefined();
|
||||
expect(deletedSubscription?.deletedAt).toBeDefined();
|
||||
});
|
||||
});
|
||||
41
server/commands/subscriptionDestroyer.ts
Normal file
41
server/commands/subscriptionDestroyer.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { Transaction } from "sequelize";
|
||||
import { Event, Subscription, User } from "@server/models";
|
||||
|
||||
type Props = {
|
||||
/** The user destroying the subscription */
|
||||
user: User;
|
||||
/** The subscription to destroy */
|
||||
subscription: Subscription;
|
||||
/** The IP address of the incoming request */
|
||||
ip: string;
|
||||
transaction: Transaction;
|
||||
};
|
||||
|
||||
/**
|
||||
* This command destroys a user subscription to a document so they will no
|
||||
* longer receive notifications.
|
||||
*
|
||||
* @returns The subscription that was destroyed
|
||||
*/
|
||||
export default async function subscriptionDestroyer({
|
||||
user,
|
||||
subscription,
|
||||
ip,
|
||||
transaction,
|
||||
}: Props): Promise<Subscription> {
|
||||
await subscription.destroy({ transaction });
|
||||
|
||||
await Event.create(
|
||||
{
|
||||
name: "subscriptions.delete",
|
||||
modelId: subscription.id,
|
||||
actorId: user.id,
|
||||
userId: user.id,
|
||||
documentId: subscription.documentId,
|
||||
ip,
|
||||
},
|
||||
{ transaction }
|
||||
);
|
||||
|
||||
return subscription;
|
||||
}
|
||||
Reference in New Issue
Block a user