mirror of
https://github.com/outline/outline.git
synced 2026-11-21 13:34:02 +03:00
feat: notifications.list
This commit is contained in:
1 parent
fb8d8f8159
commit
007cbeaab6
5 files changed
+246
-19
No files matched your search
@@ -0,0 +1,20 @@
|
||||
import { Notification } from "@server/models";
|
||||
import presentUser from "./user";
|
||||
|
||||
export default function presentNotification(notification: Notification) {
|
||||
return {
|
||||
id: notification.id,
|
||||
viewedAt: notification.viewedAt,
|
||||
createdAt: notification.createdAt,
|
||||
event: notification.event,
|
||||
userId: notification.userId,
|
||||
user: presentUser(notification.user),
|
||||
actorId: notification.actorId,
|
||||
actor: notification.actor ? presentUser(notification.actor) : undefined,
|
||||
commentId: notification.commentId,
|
||||
documentId: notification.documentId,
|
||||
revisionId: notification.revisionId,
|
||||
collectionId: notification.collectionId,
|
||||
teamId: notification.teamId,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
import { randomElement } from "@shared/random";
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import {
|
||||
buildCollection,
|
||||
buildDocument,
|
||||
buildNotification,
|
||||
buildUser,
|
||||
} from "@server/test/factories";
|
||||
import { getTestServer } from "@server/test/support";
|
||||
|
||||
const server = getTestServer();
|
||||
|
||||
describe("#notifications.list", () => {
|
||||
it("should return notifications in reverse chronological order", async () => {
|
||||
const actor = await buildUser();
|
||||
const user = await buildUser({
|
||||
teamId: actor.teamId,
|
||||
});
|
||||
const collection = await buildCollection({
|
||||
teamId: actor.teamId,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: actor.teamId,
|
||||
createdById: actor.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
await Promise.all([
|
||||
buildNotification({
|
||||
actorId: actor.id,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
event: NotificationEventType.UpdateDocument,
|
||||
userId: user.id,
|
||||
viewedAt: new Date(),
|
||||
}),
|
||||
buildNotification({
|
||||
actorId: actor.id,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
event: NotificationEventType.CreateComment,
|
||||
userId: user.id,
|
||||
}),
|
||||
buildNotification({
|
||||
actorId: actor.id,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
event: NotificationEventType.MentionedInComment,
|
||||
userId: user.id,
|
||||
}),
|
||||
]);
|
||||
|
||||
const res = await server.post("/api/notifications.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(body.data.length).toBe(3);
|
||||
expect(body.unseenCount).toBe(2);
|
||||
expect((randomElement(body.data) as any).actor.id).toBe(actor.id);
|
||||
expect((randomElement(body.data) as any).user.id).toBe(user.id);
|
||||
const events = body.data.map((n: any) => n.event);
|
||||
expect(events).toContain(NotificationEventType.UpdateDocument);
|
||||
expect(events).toContain(NotificationEventType.CreateComment);
|
||||
expect(events).toContain(NotificationEventType.MentionedInComment);
|
||||
});
|
||||
|
||||
it("should return notifications filtered by event type", async () => {
|
||||
const actor = await buildUser();
|
||||
const user = await buildUser({
|
||||
teamId: actor.teamId,
|
||||
});
|
||||
const collection = await buildCollection({
|
||||
teamId: actor.teamId,
|
||||
createdById: actor.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: actor.teamId,
|
||||
createdById: actor.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
await Promise.all([
|
||||
buildNotification({
|
||||
actorId: actor.id,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
event: NotificationEventType.UpdateDocument,
|
||||
userId: user.id,
|
||||
}),
|
||||
buildNotification({
|
||||
actorId: actor.id,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
event: NotificationEventType.CreateComment,
|
||||
userId: user.id,
|
||||
}),
|
||||
buildNotification({
|
||||
actorId: actor.id,
|
||||
documentId: document.id,
|
||||
collectionId: collection.id,
|
||||
event: NotificationEventType.MentionedInComment,
|
||||
userId: user.id,
|
||||
}),
|
||||
]);
|
||||
|
||||
const res = await server.post("/api/notifications.list", {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
eventType: NotificationEventType.MentionedInComment,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toBe(200);
|
||||
expect(body.data.length).toBe(1);
|
||||
expect(body.unseenCount).toBe(1);
|
||||
expect((randomElement(body.data) as any).actor.id).toBe(actor.id);
|
||||
expect((randomElement(body.data) as any).user.id).toBe(user.id);
|
||||
const events = body.data.map((n: any) => n.event);
|
||||
expect(events).toContain(NotificationEventType.MentionedInComment);
|
||||
});
|
||||
});
|
||||
@@ -1,11 +1,15 @@
|
||||
import Router from "koa-router";
|
||||
import { WhereOptions, Op } from "sequelize";
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import env from "@server/env";
|
||||
import auth from "@server/middlewares/authentication";
|
||||
import { transaction } from "@server/middlewares/transaction";
|
||||
import validate from "@server/middlewares/validate";
|
||||
import { User } from "@server/models";
|
||||
import { Notification, User } from "@server/models";
|
||||
import NotificationSettingsHelper from "@server/models/helpers/NotificationSettingsHelper";
|
||||
import presentNotification from "@server/presenters/notification";
|
||||
import { APIContext } from "@server/types";
|
||||
import pagination from "../middlewares/pagination";
|
||||
import * as T from "./schema";
|
||||
|
||||
const router = new Router();
|
||||
@@ -49,4 +53,48 @@ router.post(
|
||||
handleUnsubscribe
|
||||
);
|
||||
|
||||
router.post(
|
||||
"notifications.list",
|
||||
auth(),
|
||||
pagination(),
|
||||
validate(T.NotificationsListSchema),
|
||||
transaction(),
|
||||
async (ctx: APIContext<T.NotificationsListReq>) => {
|
||||
const eventType = ctx.input.body.eventType ?? ctx.input.query.eventType;
|
||||
const user = ctx.state.auth.user;
|
||||
let where: WhereOptions<Notification> = {
|
||||
userId: user.id,
|
||||
};
|
||||
if (eventType) {
|
||||
where = { ...where, event: eventType };
|
||||
}
|
||||
const [notifications, unseenCount] = await Promise.all([
|
||||
Notification.scope(["withUser", "withActor"]).findAll({
|
||||
where,
|
||||
order: [["createdAt", "DESC"]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
}),
|
||||
Notification.count({
|
||||
where: {
|
||||
...where,
|
||||
viewedAt: {
|
||||
[Op.is]: null,
|
||||
},
|
||||
},
|
||||
}),
|
||||
]);
|
||||
|
||||
const data = notifications.map((notification) =>
|
||||
presentNotification(notification)
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data,
|
||||
unseenCount,
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
export default router;
|
||||
@@ -1,8 +1,9 @@
|
||||
import { isEmpty } from "lodash";
|
||||
import { z } from "zod";
|
||||
import { NotificationEventType } from "@shared/types";
|
||||
import BaseSchema from "../BaseSchema";
|
||||
|
||||
export const NotificationSettingsCreateSchema = z.object({
|
||||
export const NotificationSettingsCreateSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
eventType: z.nativeEnum(NotificationEventType),
|
||||
}),
|
||||
@@ -12,7 +13,7 @@ export type NotificationSettingsCreateReq = z.infer<
|
||||
typeof NotificationSettingsCreateSchema
|
||||
>;
|
||||
|
||||
export const NotificationSettingsDeleteSchema = z.object({
|
||||
export const NotificationSettingsDeleteSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
eventType: z.nativeEnum(NotificationEventType),
|
||||
}),
|
||||
@@ -22,23 +23,32 @@ export type NotificationSettingsDeleteReq = z.infer<
|
||||
typeof NotificationSettingsDeleteSchema
|
||||
>;
|
||||
|
||||
export const NotificationsUnsubscribeSchema = z
|
||||
.object({
|
||||
body: z.object({
|
||||
userId: z.string().uuid().optional(),
|
||||
token: z.string().optional(),
|
||||
eventType: z.nativeEnum(NotificationEventType).optional(),
|
||||
}),
|
||||
query: z.object({
|
||||
userId: z.string().uuid().optional(),
|
||||
token: z.string().optional(),
|
||||
eventType: z.nativeEnum(NotificationEventType).optional(),
|
||||
}),
|
||||
})
|
||||
.refine((req) => !(isEmpty(req.body.userId) && isEmpty(req.query.userId)), {
|
||||
message: "userId is required",
|
||||
});
|
||||
export const NotificationsUnsubscribeSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
userId: z.string().uuid().optional(),
|
||||
token: z.string().optional(),
|
||||
eventType: z.nativeEnum(NotificationEventType).optional(),
|
||||
}),
|
||||
query: z.object({
|
||||
userId: z.string().uuid().optional(),
|
||||
token: z.string().optional(),
|
||||
eventType: z.nativeEnum(NotificationEventType).optional(),
|
||||
}),
|
||||
}).refine((req) => !(isEmpty(req.body.userId) && isEmpty(req.query.userId)), {
|
||||
message: "userId is required",
|
||||
});
|
||||
|
||||
export type NotificationsUnsubscribeReq = z.infer<
|
||||
typeof NotificationsUnsubscribeSchema
|
||||
>;
|
||||
|
||||
export const NotificationsListSchema = BaseSchema.extend({
|
||||
body: z.object({
|
||||
eventType: z.nativeEnum(NotificationEventType).optional(),
|
||||
}),
|
||||
query: z.object({
|
||||
eventType: z.nativeEnum(NotificationEventType).optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
export type NotificationsListReq = z.infer<typeof NotificationsListSchema>;
|
||||
@@ -6,6 +6,7 @@ import {
|
||||
FileOperationType,
|
||||
IntegrationService,
|
||||
IntegrationType,
|
||||
NotificationEventType,
|
||||
} from "@shared/types";
|
||||
import {
|
||||
Share,
|
||||
@@ -26,6 +27,7 @@ import {
|
||||
WebhookDelivery,
|
||||
ApiKey,
|
||||
Subscription,
|
||||
Notification,
|
||||
} from "@server/models";
|
||||
|
||||
let count = 1;
|
||||
@@ -493,3 +495,25 @@ export async function buildWebhookDelivery(
|
||||
|
||||
return WebhookDelivery.create(overrides);
|
||||
}
|
||||
|
||||
export async function buildNotification(
|
||||
overrides: Partial<Notification> = {}
|
||||
): Promise<Notification> {
|
||||
if (!overrides.event) {
|
||||
overrides.event = NotificationEventType.UpdateDocument;
|
||||
}
|
||||
|
||||
if (!overrides.teamId) {
|
||||
const team = await buildTeam();
|
||||
overrides.teamId = team.id;
|
||||
}
|
||||
|
||||
if (!overrides.userId) {
|
||||
const user = await buildUser({
|
||||
teamId: overrides.teamId,
|
||||
});
|
||||
overrides.userId = user.id;
|
||||
}
|
||||
|
||||
return Notification.create(overrides);
|
||||
}
|
||||
Reference in new issue
Block a user