]>
Commit | Line | Data |
---|---|---|
cef534ed C |
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 { UserModel } from '../../../models/account/user' | |
13 | import { getFormattedObjects } from '../../../helpers/utils' | |
14 | import { UserNotificationModel } from '../../../models/account/user-notification' | |
15 | import { meRouter } from './me' | |
16 | import { | |
dc133480 | 17 | listUserNotificationsValidator, |
cef534ed C |
18 | markAsReadUserNotificationsValidator, |
19 | updateNotificationSettingsValidator | |
20 | } from '../../../middlewares/validators/user-notifications' | |
f7cc67b4 | 21 | import { UserNotificationSetting } from '../../../../shared/models/users' |
cef534ed C |
22 | import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting' |
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) { | |
60 | const user: UserModel = res.locals.oauth.token.User | |
f7cc67b4 | 61 | const body = req.body |
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 C |
71 | newCommentOnMyVideo: body.newCommentOnMyVideo, |
72 | videoAbuseAsModerator: body.videoAbuseAsModerator, | |
73 | blacklistOnMyVideo: body.blacklistOnMyVideo, | |
74 | myVideoPublished: body.myVideoPublished, | |
f7cc67b4 C |
75 | myVideoImportFinished: body.myVideoImportFinished, |
76 | newFollow: body.newFollow, | |
77 | newUserRegistration: body.newUserRegistration, | |
2f1548fd | 78 | commentMention: body.commentMention |
f7cc67b4 C |
79 | } |
80 | ||
81 | await UserNotificationSettingModel.update(values, query) | |
cef534ed C |
82 | |
83 | return res.status(204).end() | |
84 | } | |
85 | ||
86 | async function listUserNotifications (req: express.Request, res: express.Response) { | |
87 | const user: UserModel = res.locals.oauth.token.User | |
88 | ||
dc133480 | 89 | const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread) |
cef534ed C |
90 | |
91 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
92 | } | |
93 | ||
94 | async function markAsReadUserNotifications (req: express.Request, res: express.Response) { | |
95 | const user: UserModel = res.locals.oauth.token.User | |
96 | ||
97 | await UserNotificationModel.markAsRead(user.id, req.body.ids) | |
98 | ||
99 | return res.status(204).end() | |
100 | } | |
2f1548fd C |
101 | |
102 | async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) { | |
103 | const user: UserModel = res.locals.oauth.token.User | |
104 | ||
105 | await UserNotificationModel.markAllAsRead(user.id) | |
106 | ||
107 | return res.status(204).end() | |
108 | } |