]>
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' | |
dc133480 | 21 | import { UserNotificationSetting, UserNotificationSettingValue } 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 | ||
48 | export { | |
49 | myNotificationsRouter | |
50 | } | |
51 | ||
52 | // --------------------------------------------------------------------------- | |
53 | ||
54 | async function updateNotificationSettings (req: express.Request, res: express.Response) { | |
55 | const user: UserModel = res.locals.oauth.token.User | |
56 | const body: UserNotificationSetting = req.body | |
57 | ||
58 | const query = { | |
59 | where: { | |
60 | userId: user.id | |
61 | } | |
62 | } | |
63 | ||
64 | await UserNotificationSettingModel.update({ | |
65 | newVideoFromSubscription: body.newVideoFromSubscription, | |
dc133480 C |
66 | newCommentOnMyVideo: body.newCommentOnMyVideo, |
67 | videoAbuseAsModerator: body.videoAbuseAsModerator, | |
68 | blacklistOnMyVideo: body.blacklistOnMyVideo, | |
69 | myVideoPublished: body.myVideoPublished, | |
70 | myVideoImportFinished: body.myVideoImportFinished | |
cef534ed C |
71 | }, query) |
72 | ||
73 | return res.status(204).end() | |
74 | } | |
75 | ||
76 | async function listUserNotifications (req: express.Request, res: express.Response) { | |
77 | const user: UserModel = res.locals.oauth.token.User | |
78 | ||
dc133480 | 79 | const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread) |
cef534ed C |
80 | |
81 | return res.json(getFormattedObjects(resultList.data, resultList.total)) | |
82 | } | |
83 | ||
84 | async function markAsReadUserNotifications (req: express.Request, res: express.Response) { | |
85 | const user: UserModel = res.locals.oauth.token.User | |
86 | ||
87 | await UserNotificationModel.markAsRead(user.id, req.body.ids) | |
88 | ||
89 | return res.status(204).end() | |
90 | } |