1 import * as express from 'express'
5 asyncRetryTransactionMiddleware,
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'
17 listUserNotificationsValidator,
18 markAsReadUserNotificationsValidator,
19 updateNotificationSettingsValidator
20 } from '../../../middlewares/validators/user-notifications'
21 import { UserNotificationSetting } from '../../../../shared/models/users'
22 import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting'
24 const myNotificationsRouter = express.Router()
26 meRouter.put('/me/notification-settings',
28 updateNotificationSettingsValidator,
29 asyncRetryTransactionMiddleware(updateNotificationSettings)
32 myNotificationsRouter.get('/me/notifications',
35 userNotificationsSortValidator,
38 listUserNotificationsValidator,
39 asyncMiddleware(listUserNotifications)
42 myNotificationsRouter.post('/me/notifications/read',
44 markAsReadUserNotificationsValidator,
45 asyncMiddleware(markAsReadUserNotifications)
52 // ---------------------------------------------------------------------------
54 async function updateNotificationSettings (req: express.Request, res: express.Response) {
55 const user: UserModel = res.locals.oauth.token.User
64 const values: UserNotificationSetting = {
65 newVideoFromSubscription: body.newVideoFromSubscription,
66 newCommentOnMyVideo: body.newCommentOnMyVideo,
67 videoAbuseAsModerator: body.videoAbuseAsModerator,
68 blacklistOnMyVideo: body.blacklistOnMyVideo,
69 myVideoPublished: body.myVideoPublished,
70 myVideoImportFinished: body.myVideoImportFinished,
71 newFollow: body.newFollow,
72 newUserRegistration: body.newUserRegistration,
73 commentMention: body.commentMention,
76 await UserNotificationSettingModel.update(values, query)
78 return res.status(204).end()
81 async function listUserNotifications (req: express.Request, res: express.Response) {
82 const user: UserModel = res.locals.oauth.token.User
84 const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
86 return res.json(getFormattedObjects(resultList.data, resultList.total))
89 async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
90 const user: UserModel = res.locals.oauth.token.User
92 await UserNotificationModel.markAsRead(user.id, req.body.ids)
94 return res.status(204).end()