mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-04 02:09:37 +02:00
Add user notification base code
This commit is contained in:
parent
1de1d05f4c
commit
cef534ed53
52 changed files with 2479 additions and 141 deletions
84
server/controllers/api/users/my-notifications.ts
Normal file
84
server/controllers/api/users/my-notifications.ts
Normal file
|
@ -0,0 +1,84 @@
|
|||
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 {
|
||||
markAsReadUserNotificationsValidator,
|
||||
updateNotificationSettingsValidator
|
||||
} from '../../../middlewares/validators/user-notifications'
|
||||
import { UserNotificationSetting } from '../../../../shared/models/users'
|
||||
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,
|
||||
asyncMiddleware(listUserNotifications)
|
||||
)
|
||||
|
||||
myNotificationsRouter.post('/me/notifications/read',
|
||||
authenticate,
|
||||
markAsReadUserNotificationsValidator,
|
||||
asyncMiddleware(markAsReadUserNotifications)
|
||||
)
|
||||
|
||||
export {
|
||||
myNotificationsRouter
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
async function updateNotificationSettings (req: express.Request, res: express.Response) {
|
||||
const user: UserModel = res.locals.oauth.token.User
|
||||
const body: UserNotificationSetting = req.body
|
||||
|
||||
const query = {
|
||||
where: {
|
||||
userId: user.id
|
||||
}
|
||||
}
|
||||
|
||||
await UserNotificationSettingModel.update({
|
||||
newVideoFromSubscription: body.newVideoFromSubscription,
|
||||
newCommentOnMyVideo: body.newCommentOnMyVideo
|
||||
}, query)
|
||||
|
||||
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)
|
||||
|
||||
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()
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue