1 import * as express from 'express'
5 asyncRetryTransactionMiddleware,
10 userNotificationsSortValidator
11 } from '../../../middlewares'
12 import { getFormattedObjects } from '../../../helpers/utils'
13 import { UserNotificationModel } from '../../../models/account/user-notification'
14 import { meRouter } from './me'
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'
23 const myNotificationsRouter = express.Router()
25 meRouter.put('/me/notification-settings',
27 updateNotificationSettingsValidator,
28 asyncRetryTransactionMiddleware(updateNotificationSettings)
31 myNotificationsRouter.get('/me/notifications',
34 userNotificationsSortValidator,
37 listUserNotificationsValidator,
38 asyncMiddleware(listUserNotifications)
41 myNotificationsRouter.post('/me/notifications/read',
43 markAsReadUserNotificationsValidator,
44 asyncMiddleware(markAsReadUserNotifications)
47 myNotificationsRouter.post('/me/notifications/read-all',
49 asyncMiddleware(markAsReadAllUserNotifications)
56 // ---------------------------------------------------------------------------
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
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
81 await UserNotificationSettingModel.update(values, query)
83 return res.status(204).end()
86 async function listUserNotifications (req: express.Request, res: express.Response) {
87 const user = res.locals.oauth.token.User
89 const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
91 return res.json(getFormattedObjects(resultList.data, resultList.total))
94 async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
95 const user = res.locals.oauth.token.User
97 await UserNotificationModel.markAsRead(user.id, req.body.ids)
99 return res.status(204).end()
102 async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
103 const user = res.locals.oauth.token.User
105 await UserNotificationModel.markAllAsRead(user.id)
107 return res.status(204).end()