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