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