]>
Commit | Line | Data |
---|---|---|
cef534ed | 1 | import 'multer' |
41fb13c3 | 2 | import express from 'express' |
7d9ba5c0 | 3 | import { UserNotificationModel } from '@server/models/user/user-notification' |
c0e8b12e | 4 | import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes' |
7d9ba5c0 | 5 | import { UserNotificationSetting } from '../../../../shared/models/users' |
cef534ed C |
6 | import { |
7 | asyncMiddleware, | |
8 | asyncRetryTransactionMiddleware, | |
9 | authenticate, | |
10 | paginationValidator, | |
11 | setDefaultPagination, | |
12 | setDefaultSort, | |
13 | userNotificationsSortValidator | |
14 | } from '../../../middlewares' | |
cef534ed | 15 | import { |
dc133480 | 16 | listUserNotificationsValidator, |
cef534ed C |
17 | markAsReadUserNotificationsValidator, |
18 | updateNotificationSettingsValidator | |
19 | } from '../../../middlewares/validators/user-notifications' | |
7d9ba5c0 C |
20 | import { UserNotificationSettingModel } from '../../../models/user/user-notification-setting' |
21 | import { meRouter } from './me' | |
d0800f76 | 22 | import { getFormattedObjects } from '@server/helpers/utils' |
cef534ed C |
23 | |
24 | const myNotificationsRouter = express.Router() | |
25 | ||
26 | meRouter.put('/me/notification-settings', | |
27 | authenticate, | |
28 | updateNotificationSettingsValidator, | |
29 | asyncRetryTransactionMiddleware(updateNotificationSettings) | |
30 | ) | |
31 | ||
32 | myNotificationsRouter.get('/me/notifications', | |
33 | authenticate, | |
34 | paginationValidator, | |
35 | userNotificationsSortValidator, | |
36 | setDefaultSort, | |
37 | setDefaultPagination, | |
dc133480 | 38 | listUserNotificationsValidator, |
cef534ed C |
39 | asyncMiddleware(listUserNotifications) |
40 | ) | |
41 | ||
42 | myNotificationsRouter.post('/me/notifications/read', | |
43 | authenticate, | |
44 | markAsReadUserNotificationsValidator, | |
45 | asyncMiddleware(markAsReadUserNotifications) | |
46 | ) | |
47 | ||
2f1548fd C |
48 | myNotificationsRouter.post('/me/notifications/read-all', |
49 | authenticate, | |
50 | asyncMiddleware(markAsReadAllUserNotifications) | |
51 | ) | |
52 | ||
cef534ed C |
53 | export { |
54 | myNotificationsRouter | |
55 | } | |
56 | ||
57 | // --------------------------------------------------------------------------- | |
58 | ||
59 | async function updateNotificationSettings (req: express.Request, res: express.Response) { | |
dae86118 C |
60 | const user = res.locals.oauth.token.User |
61 | const body = req.body as UserNotificationSetting | |
cef534ed C |
62 | |
63 | const query = { | |
64 | where: { | |
65 | userId: user.id | |
66 | } | |
67 | } | |
68 | ||
f7cc67b4 | 69 | const values: UserNotificationSetting = { |
cef534ed | 70 | newVideoFromSubscription: body.newVideoFromSubscription, |
dc133480 | 71 | newCommentOnMyVideo: body.newCommentOnMyVideo, |
4f32032f | 72 | abuseAsModerator: body.abuseAsModerator, |
7ccddd7b | 73 | videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator, |
dc133480 C |
74 | blacklistOnMyVideo: body.blacklistOnMyVideo, |
75 | myVideoPublished: body.myVideoPublished, | |
f7cc67b4 C |
76 | myVideoImportFinished: body.myVideoImportFinished, |
77 | newFollow: body.newFollow, | |
78 | newUserRegistration: body.newUserRegistration, | |
883993c8 | 79 | commentMention: body.commentMention, |
8424c402 | 80 | newInstanceFollower: body.newInstanceFollower, |
594d3e48 C |
81 | autoInstanceFollowing: body.autoInstanceFollowing, |
82 | abuseNewMessage: body.abuseNewMessage, | |
32a18cbf C |
83 | abuseStateChange: body.abuseStateChange, |
84 | newPeerTubeVersion: body.newPeerTubeVersion, | |
1808a1f8 | 85 | newPluginVersion: body.newPluginVersion, |
92e66e04 | 86 | myVideoStudioEditionFinished: body.myVideoStudioEditionFinished |
f7cc67b4 C |
87 | } |
88 | ||
89 | await UserNotificationSettingModel.update(values, query) | |
cef534ed | 90 | |
2d53be02 | 91 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
cef534ed C |
92 | } |
93 | ||
94 | async function listUserNotifications (req: express.Request, res: express.Response) { | |
dae86118 | 95 | const user = res.locals.oauth.token.User |
cef534ed | 96 | |
dc133480 | 97 | const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread) |
cef534ed C |
98 | |
99 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
100 | } | |
101 | ||
102 | async function markAsReadUserNotifications (req: express.Request, res: express.Response) { | |
dae86118 | 103 | const user = res.locals.oauth.token.User |
cef534ed C |
104 | |
105 | await UserNotificationModel.markAsRead(user.id, req.body.ids) | |
106 | ||
2d53be02 | 107 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
cef534ed | 108 | } |
2f1548fd C |
109 | |
110 | async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) { | |
dae86118 | 111 | const user = res.locals.oauth.token.User |
2f1548fd C |
112 | |
113 | await UserNotificationModel.markAllAsRead(user.id) | |
114 | ||
2d53be02 | 115 | return res.status(HttpStatusCode.NO_CONTENT_204).end() |
2f1548fd | 116 | } |