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

111 lines
3.7 KiB
TypeScript
Raw Normal View History

2018-12-26 10:36:24 +01:00
import 'multer'
2021-08-27 14:32:44 +02:00
import express from 'express'
import { HttpStatusCode, UserNotificationSetting } from '@peertube/peertube-models'
import { getFormattedObjects } from '@server/helpers/utils.js'
import { UserNotificationModel } from '@server/models/user/user-notification.js'
2018-12-26 10:36:24 +01:00
import {
asyncMiddleware,
asyncRetryTransactionMiddleware,
authenticate,
paginationValidator,
setDefaultPagination,
setDefaultSort,
userNotificationsSortValidator
} from '../../../middlewares/index.js'
2018-12-26 10:36:24 +01:00
import {
listUserNotificationsValidator,
2018-12-26 10:36:24 +01:00
markAsReadUserNotificationsValidator,
updateNotificationSettingsValidator
2024-02-12 10:47:52 +01:00
} from '../../../middlewares/validators/users/user-notifications.js'
import { UserNotificationSettingModel } from '../../../models/user/user-notification-setting.js'
import { meRouter } from './me.js'
2018-12-26 10:36:24 +01: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 10:36:24 +01:00
asyncMiddleware(listUserNotifications)
)
myNotificationsRouter.post('/me/notifications/read',
authenticate,
markAsReadUserNotificationsValidator,
asyncMiddleware(markAsReadUserNotifications)
)
2019-01-08 11:26:41 +01:00
myNotificationsRouter.post('/me/notifications/read-all',
authenticate,
asyncMiddleware(markAsReadAllUserNotifications)
)
2018-12-26 10:36:24 +01:00
export {
myNotificationsRouter
}
// ---------------------------------------------------------------------------
async function updateNotificationSettings (req: express.Request, res: express.Response) {
2019-03-19 10:35:15 +01:00
const user = res.locals.oauth.token.User
const body = req.body as UserNotificationSetting
2018-12-26 10:36:24 +01:00
const values: UserNotificationSetting = {
2018-12-26 10:36:24 +01:00
newVideoFromSubscription: body.newVideoFromSubscription,
newCommentOnMyVideo: body.newCommentOnMyVideo,
2020-07-07 14:34:16 +02: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 16:54:52 +01:00
abuseStateChange: body.abuseStateChange,
newPeerTubeVersion: body.newPeerTubeVersion,
newPluginVersion: body.newPluginVersion,
2024-06-13 09:23:12 +02:00
myVideoTranscriptionGenerated: body.myVideoTranscriptionGenerated,
2022-03-22 16:58:49 +01:00
myVideoStudioEditionFinished: body.myVideoStudioEditionFinished
}
2024-02-12 10:47:52 +01:00
await UserNotificationSettingModel.updateUserSettings(values, user.id)
2018-12-26 10:36:24 +01:00
2024-02-12 10:47:52 +01:00
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
2018-12-26 10:36:24 +01:00
}
async function listUserNotifications (req: express.Request, res: express.Response) {
2019-03-19 10:35:15 +01:00
const user = res.locals.oauth.token.User
2018-12-26 10:36:24 +01:00
const resultList = await UserNotificationModel.listForApi(user.id, req.query.start, req.query.count, req.query.sort, req.query.unread)
2018-12-26 10:36:24 +01:00
return res.json(getFormattedObjects(resultList.data, resultList.total))
}
async function markAsReadUserNotifications (req: express.Request, res: express.Response) {
2019-03-19 10:35:15 +01:00
const user = res.locals.oauth.token.User
2018-12-26 10:36:24 +01:00
await UserNotificationModel.markAsRead(user.id, req.body.ids)
2024-02-12 10:47:52 +01:00
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
2018-12-26 10:36:24 +01:00
}
2019-01-08 11:26:41 +01:00
async function markAsReadAllUserNotifications (req: express.Request, res: express.Response) {
2019-03-19 10:35:15 +01:00
const user = res.locals.oauth.token.User
2019-01-08 11:26:41 +01:00
await UserNotificationModel.markAllAsRead(user.id)
2024-02-12 10:47:52 +01:00
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
2019-01-08 11:26:41 +01:00
}