From cef534ed53e4518fe0acf581bfe880788d42fc36 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 26 Dec 2018 10:36:24 +0100 Subject: Add user notification base code --- server/middlewares/oauth.ts | 22 +++++++++++ server/middlewares/validators/sort.ts | 5 ++- server/middlewares/validators/user-history.ts | 8 +--- .../middlewares/validators/user-notifications.ts | 46 ++++++++++++++++++++++ 4 files changed, 74 insertions(+), 7 deletions(-) create mode 100644 server/middlewares/validators/user-notifications.ts (limited to 'server/middlewares') diff --git a/server/middlewares/oauth.ts b/server/middlewares/oauth.ts index 8c1df2c3e..1d193d467 100644 --- a/server/middlewares/oauth.ts +++ b/server/middlewares/oauth.ts @@ -3,6 +3,8 @@ import * as OAuthServer from 'express-oauth-server' import 'express-validator' import { OAUTH_LIFETIME } from '../initializers' import { logger } from '../helpers/logger' +import { Socket } from 'socket.io' +import { getAccessToken } from '../lib/oauth-model' const oAuthServer = new OAuthServer({ useErrorHandler: true, @@ -28,6 +30,25 @@ function authenticate (req: express.Request, res: express.Response, next: expres }) } +function authenticateSocket (socket: Socket, next: (err?: any) => void) { + const accessToken = socket.handshake.query.accessToken + + logger.debug('Checking socket access token %s.', accessToken) + + getAccessToken(accessToken) + .then(tokenDB => { + const now = new Date() + + if (!tokenDB || tokenDB.accessTokenExpiresAt < now || tokenDB.refreshTokenExpiresAt < now) { + return next(new Error('Invalid access token.')) + } + + socket.handshake.query.user = tokenDB.User + + return next() + }) +} + function authenticatePromiseIfNeeded (req: express.Request, res: express.Response) { return new Promise(resolve => { // Already authenticated? (or tried to) @@ -68,6 +89,7 @@ function token (req: express.Request, res: express.Response, next: express.NextF export { authenticate, + authenticateSocket, authenticatePromiseIfNeeded, optionalAuthenticate, token diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index 4c0577d8f..5ceda845f 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts @@ -18,6 +18,7 @@ const SORTABLE_FOLLOWING_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOW const SORTABLE_USER_SUBSCRIPTIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS) const SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.ACCOUNTS_BLOCKLIST) const SORTABLE_SERVERS_BLOCKLIST_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.SERVERS_BLOCKLIST) +const SORTABLE_USER_NOTIFICATIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_NOTIFICATIONS) const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS) @@ -35,6 +36,7 @@ const followingSortValidator = checkSort(SORTABLE_FOLLOWING_COLUMNS) const userSubscriptionsSortValidator = checkSort(SORTABLE_USER_SUBSCRIPTIONS_COLUMNS) const accountsBlocklistSortValidator = checkSort(SORTABLE_ACCOUNTS_BLOCKLIST_COLUMNS) const serversBlocklistSortValidator = checkSort(SORTABLE_SERVERS_BLOCKLIST_COLUMNS) +const userNotificationsSortValidator = checkSort(SORTABLE_USER_NOTIFICATIONS_COLUMNS) // --------------------------------------------------------------------------- @@ -54,5 +56,6 @@ export { userSubscriptionsSortValidator, videoChannelsSearchSortValidator, accountsBlocklistSortValidator, - serversBlocklistSortValidator + serversBlocklistSortValidator, + userNotificationsSortValidator } diff --git a/server/middlewares/validators/user-history.ts b/server/middlewares/validators/user-history.ts index 3c8971ea1..418313d09 100644 --- a/server/middlewares/validators/user-history.ts +++ b/server/middlewares/validators/user-history.ts @@ -1,13 +1,9 @@ import * as express from 'express' import 'express-validator' -import { body, param, query } from 'express-validator/check' +import { body } from 'express-validator/check' import { logger } from '../../helpers/logger' import { areValidationErrors } from './utils' -import { ActorFollowModel } from '../../models/activitypub/actor-follow' -import { areValidActorHandles, isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' -import { UserModel } from '../../models/account/user' -import { CONFIG } from '../../initializers' -import { isDateValid, toArray } from '../../helpers/custom-validators/misc' +import { isDateValid } from '../../helpers/custom-validators/misc' const userHistoryRemoveValidator = [ body('beforeDate') diff --git a/server/middlewares/validators/user-notifications.ts b/server/middlewares/validators/user-notifications.ts new file mode 100644 index 000000000..8202f307e --- /dev/null +++ b/server/middlewares/validators/user-notifications.ts @@ -0,0 +1,46 @@ +import * as express from 'express' +import 'express-validator' +import { body } from 'express-validator/check' +import { logger } from '../../helpers/logger' +import { areValidationErrors } from './utils' +import { isUserNotificationSettingValid } from '../../helpers/custom-validators/user-notifications' +import { isIntArray } from '../../helpers/custom-validators/misc' + +const updateNotificationSettingsValidator = [ + body('newVideoFromSubscription') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid new video from subscription notification setting'), + body('newCommentOnMyVideo') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid new comment on my video notification setting'), + body('videoAbuseAsModerator') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid new video abuse as moderator notification setting'), + body('blacklistOnMyVideo') + .custom(isUserNotificationSettingValid).withMessage('Should have a valid new blacklist on my video notification setting'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking updateNotificationSettingsValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + +const markAsReadUserNotificationsValidator = [ + body('ids') + .custom(isIntArray).withMessage('Should have a valid notification ids to mark as read'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking markAsReadUserNotificationsValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + updateNotificationSettingsValidator, + markAsReadUserNotificationsValidator +} -- cgit v1.2.3