diff options
Diffstat (limited to 'shared/server-commands/users/notifications-command.ts')
-rw-r--r-- | shared/server-commands/users/notifications-command.ts | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/shared/server-commands/users/notifications-command.ts b/shared/server-commands/users/notifications-command.ts new file mode 100644 index 000000000..692420b8b --- /dev/null +++ b/shared/server-commands/users/notifications-command.ts | |||
@@ -0,0 +1,86 @@ | |||
1 | import { HttpStatusCode, ResultList } from '@shared/models' | ||
2 | import { UserNotification, UserNotificationSetting } from '../../models/users' | ||
3 | import { AbstractCommand, OverrideCommandOptions } from '../shared' | ||
4 | |||
5 | export class NotificationsCommand extends AbstractCommand { | ||
6 | |||
7 | updateMySettings (options: OverrideCommandOptions & { | ||
8 | settings: UserNotificationSetting | ||
9 | }) { | ||
10 | const path = '/api/v1/users/me/notification-settings' | ||
11 | |||
12 | return this.putBodyRequest({ | ||
13 | ...options, | ||
14 | |||
15 | path, | ||
16 | fields: options.settings, | ||
17 | implicitToken: true, | ||
18 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
19 | }) | ||
20 | } | ||
21 | |||
22 | list (options: OverrideCommandOptions & { | ||
23 | start?: number | ||
24 | count?: number | ||
25 | unread?: boolean | ||
26 | sort?: string | ||
27 | }) { | ||
28 | const { start, count, unread, sort = '-createdAt' } = options | ||
29 | const path = '/api/v1/users/me/notifications' | ||
30 | |||
31 | return this.getRequestBody<ResultList<UserNotification>>({ | ||
32 | ...options, | ||
33 | |||
34 | path, | ||
35 | query: { | ||
36 | start, | ||
37 | count, | ||
38 | sort, | ||
39 | unread | ||
40 | }, | ||
41 | implicitToken: true, | ||
42 | defaultExpectedStatus: HttpStatusCode.OK_200 | ||
43 | }) | ||
44 | } | ||
45 | |||
46 | markAsRead (options: OverrideCommandOptions & { | ||
47 | ids: number[] | ||
48 | }) { | ||
49 | const { ids } = options | ||
50 | const path = '/api/v1/users/me/notifications/read' | ||
51 | |||
52 | return this.postBodyRequest({ | ||
53 | ...options, | ||
54 | |||
55 | path, | ||
56 | fields: { ids }, | ||
57 | implicitToken: true, | ||
58 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
59 | }) | ||
60 | } | ||
61 | |||
62 | markAsReadAll (options: OverrideCommandOptions) { | ||
63 | const path = '/api/v1/users/me/notifications/read-all' | ||
64 | |||
65 | return this.postBodyRequest({ | ||
66 | ...options, | ||
67 | |||
68 | path, | ||
69 | implicitToken: true, | ||
70 | defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204 | ||
71 | }) | ||
72 | } | ||
73 | |||
74 | async getLatest (options: OverrideCommandOptions = {}) { | ||
75 | const { total, data } = await this.list({ | ||
76 | ...options, | ||
77 | start: 0, | ||
78 | count: 1, | ||
79 | sort: '-createdAt' | ||
80 | }) | ||
81 | |||
82 | if (total === 0) return undefined | ||
83 | |||
84 | return data[0] | ||
85 | } | ||
86 | } | ||