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