1
0
Fork 0
peertube/server/controllers/api/users/my-notifications.ts

116 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-12-26 04:36:24 -05:00
import 'multer'
2021-08-27 08:32:44 -04:00
import express from 'express'
2021-05-11 05:15:29 -04:00
import { UserNotificationModel } from '@server/models/user/user-notification'
2021-07-16 04:42:24 -04:00
import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
2021-05-11 05:15:29 -04:00
import { UserNotificationSetting } from '../../../../shared/models/users'
2018-12-26 04:36:24 -05:00
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
authenticate,
paginationValidator,
setDefaultPagination,
setDefaultSort,
userNotificationsSortValidator
} from '../../../middlewares'
import {
listUserNotificationsValidator,
2018-12-26 04:36:24 -05:00
markAsReadUserNotificationsValidator,
updateNotificationSettingsValidator
} from '../../../middlewares/validators/user-notifications'
2021-05-11 05:15:29 -04:00
import { UserNotificationSettingModel } from '../../../models/user/user-notification-setting'
import { meRouter } from './me'
import { getFormattedObjects } from '@server/helpers/utils'
2018-12-26 04:36:24 -05:00
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 04:36:24 -05:00
asyncMiddleware(listUserNotifications)
)
myNotificationsRouter.post('/me/notifications/read',
authenticate,
markAsReadUserNotificationsValidator,
asyncMiddleware(markAsReadUserNotifications)
)
2019-01-08 05:26:41 -05:00
myNotificationsRouter.post('/me/notifications/read-all',
authenticate,
asyncMiddleware(markAsReadAllUserNotifications)
)
2018-12-26 04:36:24 -05:00
export {
myNotificationsRouter
}
// ---------------------------------------------------------------------------
async function updateNotificationSettings (req: express.Request, res: express.Response) {
2019-03-19 05:35:15 -04:00
const user = res.locals.oauth.token.User
const body = req.body as UserNotificationSetting
2018-12-26 04:36:24 -05:00
const query = {
where: {
userId: user.id
}
}
const values: UserNotificationSetting = {
2018-12-26 04:36:24 -05:00
newVideoFromSubscription: body.newVideoFromSubscription,
newCommentOnMyVideo: body.newCommentOnMyVideo,
2020-07-07 08:34:16 -04:00
abuseAsModerator: body.abuseAsModerator,
videoAutoBlacklistAsModerator: body.videoAutoBlacklistAsModerator,
blacklistOnMyVideo: body.blacklistOnMyVideo,
myVideoPublished: body.myVideoPublished,
myVideoImportFinished: body.myVideoImportFinished,
newFollow: body.newFollow,
newUserRegistration: body.newUserRegistration,
commentMention: body.commentMention,
newInstanceFollower: body.newInstanceFollower,
autoInstanceFollowing: body.autoInstanceFollowing,
abuseNewMessage: body.abuseNewMessage,
2021-03-11 10:54:52 -05:00
abuseStateChange: body.abuseStateChange,
newPeerTubeVersion: body.newPeerTubeVersion,
newPluginVersion: body.newPluginVersion
}
await UserNotificationSettingModel.update(values, query)
2018-12-26 04:36:24 -05:00
return res.status(HttpStatusCode.NO_CONTENT_204).end()
2018-12-26 04:36:24 -05:00
}
async function listUserNotifications (req: express.Request, res: express.Response) {
2019-03-19 05:35:15 -04:00
const user = res.locals.oauth.token.User
2018-12-26 04:36:24 -05:00
const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
2018-12-26 04:36:24 -05:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
2019-03-19 05:35:15 -04:00
const user = res.locals.oauth.token.User
2018-12-26 04:36:24 -05:00
await UserNotificationModel.markAsRead(user.id, req.body.ids)
return res.status(HttpStatusCode.NO_CONTENT_204).end()
2018-12-26 04:36:24 -05:00
}
2019-01-08 05:26:41 -05:00
async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
2019-03-19 05:35:15 -04:00
const user = res.locals.oauth.token.User
2019-01-08 05:26:41 -05:00
await UserNotificationModel.markAllAsRead(user.id)
return res.status(HttpStatusCode.NO_CONTENT_204).end()
2019-01-08 05:26:41 -05:00
}