]>
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' | |
cef534ed C |
12 | import { getFormattedObjects } from '../../../helpers/utils' |
13 | import { UserNotificationModel } from '../../../models/account/user-notification' | |
14 | import { meRouter } from './me' | |
15 | import { | |
dc133480 | 16 | listUserNotificationsValidator, |
cef534ed C |
17 | markAsReadUserNotificationsValidator, |
18 | updateNotificationSettingsValidator | |
19 | } from '../../../middlewares/validators/user-notifications' | |
f7cc67b4 | 20 | import { UserNotificationSetting } from '../../../../shared/models/users' |
cef534ed C |
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, | |
dc133480 | 37 | listUserNotificationsValidator, |
cef534ed C |
38 | asyncMiddleware(listUserNotifications) |
39 | ) | |
40 | ||
41 | myNotificationsRouter.post('/me/notifications/read', | |
42 | authenticate, | |
43 | markAsReadUserNotificationsValidator, | |
44 | asyncMiddleware(markAsReadUserNotifications) | |
45 | ) | |
46 | ||
2f1548fd C |
47 | myNotificationsRouter.post('/me/notifications/read-all', |
48 | authenticate, | |
49 | asyncMiddleware(markAsReadAllUserNotifications) | |
50 | ) | |
51 | ||
cef534ed C |
52 | export { |
53 | myNotificationsRouter | |
54 | } | |
55 | ||
56 | // --------------------------------------------------------------------------- | |
57 | ||
58 | async function updateNotificationSettings (req: express.Request, res: express.Response) { | |
dae86118 C |
59 | const user = res.locals.oauth.token.User |
60 | const body = req.body as UserNotificationSetting | |
cef534ed C |
61 | |
62 | const query = { | |
63 | where: { | |
64 | userId: user.id | |
65 | } | |
66 | } | |
67 | ||
f7cc67b4 | 68 | const values: UserNotificationSetting = { |
cef534ed | 69 | newVideoFromSubscription: body.newVideoFromSubscription, |
dc133480 | 70 | newCommentOnMyVideo: body.newCommentOnMyVideo, |
4f32032f | 71 | abuseAsModerator: body.abuseAsModerator, |
7ccddd7b | 72 | videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator, |
dc133480 C |
73 | blacklistOnMyVideo: body.blacklistOnMyVideo, |
74 | myVideoPublished: body.myVideoPublished, | |
f7cc67b4 C |
75 | myVideoImportFinished: body.myVideoImportFinished, |
76 | newFollow: body.newFollow, | |
77 | newUserRegistration: body.newUserRegistration, | |
883993c8 | 78 | commentMention: body.commentMention, |
8424c402 C |
79 | newInstanceFollower: body.newInstanceFollower, |
80 | autoInstanceFollowing: body.autoInstanceFollowing | |
f7cc67b4 C |
81 | } |
82 | ||
83 | await UserNotificationSettingModel.update(values, query) | |
cef534ed C |
84 | |
85 | return res.status(204).end() | |
86 | } | |
87 | ||
88 | async function listUserNotifications (req: express.Request, res: express.Response) { | |
dae86118 | 89 | const user = res.locals.oauth.token.User |
cef534ed | 90 | |
dc133480 | 91 | const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread) |
cef534ed C |
92 | |
93 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
94 | } | |
95 | ||
96 | async function markAsReadUserNotifications (req: express.Request, res: express.Response) { | |
dae86118 | 97 | const user = res.locals.oauth.token.User |
cef534ed C |
98 | |
99 | await UserNotificationModel.markAsRead(user.id, req.body.ids) | |
100 | ||
101 | return res.status(204).end() | |
102 | } | |
2f1548fd C |
103 | |
104 | async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) { | |
dae86118 | 105 | const user = res.locals.oauth.token.User |
2f1548fd C |
106 | |
107 | await UserNotificationModel.markAllAsRead(user.id) | |
108 | ||
109 | return res.status(204).end() | |
110 | } |