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)
48 myNotificationsRouter.post('/me/notifications/read-all',
50 asyncMiddleware(markAsReadAllUserNotifications)
57 // ---------------------------------------------------------------------------
59 async function updateNotificationSettings (req: express.Request, res: express.Response) {
60 const user: UserModel = res.locals.oauth.token.User
69 const values: UserNotificationSetting = {
70 newVideoFromSubscription: body.newVideoFromSubscription,
71 newCommentOnMyVideo: body.newCommentOnMyVideo,
72 videoAbuseAsModerator: body.videoAbuseAsModerator,
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: UserModel = 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: UserModel = 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: UserModel = res.locals.oauth.token.User
105 await UserNotificationModel.markAllAsRead(user.id)
107 return res.status(204).end()