PeerTube/server/controllers/api/users/my-notifications.ts

109 lines
3.3 KiB
TypeScript
Raw Normal View History

2018-12-26 03:36:24 -06:00
import * as express from 'express'
import 'multer'
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
authenticate,
paginationValidator,
setDefaultPagination,
setDefaultSort,
userNotificationsSortValidator
} from '../../../middlewares'
import { UserModel } from '../../../models/account/user'
import { getFormattedObjects } from '../../../helpers/utils'
import { UserNotificationModel } from '../../../models/account/user-notification'
import { meRouter } from './me'
import {
listUserNotificationsValidator,
2018-12-26 03:36:24 -06:00
markAsReadUserNotificationsValidator,
updateNotificationSettingsValidator
} from '../../../middlewares/validators/user-notifications'
import { UserNotificationSetting } from '../../../../shared/models/users'
2018-12-26 03:36:24 -06:00
import { UserNotificationSettingModel } from '../../../models/account/user-notification-setting'
const myNotificationsRouter = express.Router()
meRouter.put('/me/notification-settings',
authenticate,
updateNotificationSettingsValidator,
asyncRetryTransactionMiddleware(updateNotificationSettings)
)
myNotificationsRouter.get('/me/notifications',
authenticate,
paginationValidator,
userNotificationsSortValidator,
setDefaultSort,
setDefaultPagination,
listUserNotificationsValidator,
2018-12-26 03:36:24 -06:00
asyncMiddleware(listUserNotifications)
)
myNotificationsRouter.post('/me/notifications/read',
authenticate,
markAsReadUserNotificationsValidator,
asyncMiddleware(markAsReadUserNotifications)
)
2019-01-08 04:26:41 -06:00
myNotificationsRouter.post('/me/notifications/read-all',
authenticate,
asyncMiddleware(markAsReadAllUserNotifications)
)
2018-12-26 03:36:24 -06:00
export {
myNotificationsRouter
}
// ---------------------------------------------------------------------------
async function updateNotificationSettings (req: express.Request, res: express.Response) {
const user: UserModel = res.locals.oauth.token.User
const body = req.body
2018-12-26 03:36:24 -06:00
const query = {
where: {
userId: user.id
}
}
const values: UserNotificationSetting = {
2018-12-26 03:36:24 -06:00
newVideoFromSubscription: body.newVideoFromSubscription,
newCommentOnMyVideo: body.newCommentOnMyVideo,
videoAbuseAsModerator: body.videoAbuseAsModerator,
blacklistOnMyVideo: body.blacklistOnMyVideo,
myVideoPublished: body.myVideoPublished,
myVideoImportFinished: body.myVideoImportFinished,
newFollow: body.newFollow,
newUserRegistration: body.newUserRegistration,
2019-01-08 04:26:41 -06:00
commentMention: body.commentMention
}
await UserNotificationSettingModel.update(values, query)
2018-12-26 03:36:24 -06:00
return res.status(204).end()
}
async function listUserNotifications (req: express.Request, res: express.Response) {
const user: UserModel = res.locals.oauth.token.User
const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
2018-12-26 03:36:24 -06:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
const user: UserModel = res.locals.oauth.token.User
await UserNotificationModel.markAsRead(user.id, req.body.ids)
return res.status(204).end()
}
2019-01-08 04:26:41 -06:00
async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
const user: UserModel = res.locals.oauth.token.User
await UserNotificationModel.markAllAsRead(user.id)
return res.status(204).end()
}