From 06a05d5f4784a7cbb27aa1188385b5679845dad8 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 16 Aug 2018 15:25:20 +0200 Subject: Add subscriptions endpoints to REST API --- server/middlewares/validators/follows.ts | 4 +- server/middlewares/validators/index.ts | 1 + server/middlewares/validators/sort.ts | 5 +- .../middlewares/validators/user-subscriptions.ts | 58 ++++++++++++++++++++++ server/middlewares/validators/video-channels.ts | 17 ++++++- server/middlewares/validators/webfinger.ts | 4 +- 6 files changed, 83 insertions(+), 6 deletions(-) create mode 100644 server/middlewares/validators/user-subscriptions.ts (limited to 'server/middlewares') diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts index 040ee1f21..faefc1179 100644 --- a/server/middlewares/validators/follows.ts +++ b/server/middlewares/validators/follows.ts @@ -4,7 +4,7 @@ import { isTestInstance } from '../../helpers/core-utils' import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' import { logger } from '../../helpers/logger' import { getServerActor } from '../../helpers/utils' -import { CONFIG } from '../../initializers' +import { CONFIG, SERVER_ACTOR_NAME } from '../../initializers' import { ActorFollowModel } from '../../models/activitypub/actor-follow' import { areValidationErrors } from './utils' @@ -38,7 +38,7 @@ const removeFollowingValidator = [ if (areValidationErrors(req, res)) return const serverActor = await getServerActor() - const follow = await ActorFollowModel.loadByActorAndTargetHost(serverActor.id, req.params.host) + const follow = await ActorFollowModel.loadByActorAndTargetNameAndHost(serverActor.id, SERVER_ACTOR_NAME, req.params.host) if (!follow) { return res diff --git a/server/middlewares/validators/index.ts b/server/middlewares/validators/index.ts index ccbedd57d..940547a3e 100644 --- a/server/middlewares/validators/index.ts +++ b/server/middlewares/validators/index.ts @@ -6,6 +6,7 @@ export * from './follows' export * from './feeds' export * from './sort' export * from './users' +export * from './user-subscriptions' export * from './videos' export * from './video-abuses' export * from './video-blacklist' diff --git a/server/middlewares/validators/sort.ts b/server/middlewares/validators/sort.ts index d85611773..b30e97e61 100644 --- a/server/middlewares/validators/sort.ts +++ b/server/middlewares/validators/sort.ts @@ -14,6 +14,7 @@ const SORTABLE_BLACKLISTS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.BLACK const SORTABLE_VIDEO_CHANNELS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.VIDEO_CHANNELS) const SORTABLE_FOLLOWERS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWERS) const SORTABLE_FOLLOWING_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.FOLLOWING) +const SORTABLE_USER_SUBSCRIPTIONS_COLUMNS = createSortableColumns(SORTABLE_COLUMNS.USER_SUBSCRIPTIONS) const usersSortValidator = checkSort(SORTABLE_USERS_COLUMNS) const accountsSortValidator = checkSort(SORTABLE_ACCOUNTS_COLUMNS) @@ -27,6 +28,7 @@ const blacklistSortValidator = checkSort(SORTABLE_BLACKLISTS_COLUMNS) const videoChannelsSortValidator = checkSort(SORTABLE_VIDEO_CHANNELS_COLUMNS) const followersSortValidator = checkSort(SORTABLE_FOLLOWERS_COLUMNS) const followingSortValidator = checkSort(SORTABLE_FOLLOWING_COLUMNS) +const userSubscriptionsSortValidator = checkSort(SORTABLE_USER_SUBSCRIPTIONS_COLUMNS) // --------------------------------------------------------------------------- @@ -42,5 +44,6 @@ export { followersSortValidator, followingSortValidator, jobsSortValidator, - videoCommentThreadsSortValidator + videoCommentThreadsSortValidator, + userSubscriptionsSortValidator } diff --git a/server/middlewares/validators/user-subscriptions.ts b/server/middlewares/validators/user-subscriptions.ts new file mode 100644 index 000000000..f331b6c34 --- /dev/null +++ b/server/middlewares/validators/user-subscriptions.ts @@ -0,0 +1,58 @@ +import * as express from 'express' +import 'express-validator' +import { body, param } from 'express-validator/check' +import { logger } from '../../helpers/logger' +import { areValidationErrors } from './utils' +import { ActorFollowModel } from '../../models/activitypub/actor-follow' +import { isValidActorHandle } from '../../helpers/custom-validators/activitypub/actor' +import { UserModel } from '../../models/account/user' +import { CONFIG } from '../../initializers' + +const userSubscriptionAddValidator = [ + body('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to follow (username@domain)'), + + (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking userSubscriptionAddValidator parameters', { parameters: req.body }) + + if (areValidationErrors(req, res)) return + + return next() + } +] + +const userSubscriptionRemoveValidator = [ + param('uri').custom(isValidActorHandle).withMessage('Should have a valid URI to unfollow'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking unfollow parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + + let [ name, host ] = req.params.uri.split('@') + if (host === CONFIG.WEBSERVER.HOST) host = null + + const user: UserModel = res.locals.oauth.token.User + const subscription = await ActorFollowModel.loadByActorAndTargetNameAndHost(user.Account.Actor.id, name, host) + + if (!subscription) { + return res + .status(404) + .json({ + error: `Subscription ${req.params.uri} not found.` + }) + .end() + } + + res.locals.subscription = subscription + return next() + } +] + +// --------------------------------------------------------------------------- + +export { + userSubscriptionAddValidator, + userSubscriptionRemoveValidator +} + +// --------------------------------------------------------------------------- diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts index 143ce9582..d354c7e05 100644 --- a/server/middlewares/validators/video-channels.ts +++ b/server/middlewares/validators/video-channels.ts @@ -4,6 +4,7 @@ import { UserRight } from '../../../shared' import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts' import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { + isLocalVideoChannelNameExist, isVideoChannelDescriptionValid, isVideoChannelExist, isVideoChannelNameValid, @@ -100,6 +101,19 @@ const videoChannelsGetValidator = [ } ] +const localVideoChannelValidator = [ + param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'), + + async (req: express.Request, res: express.Response, next: express.NextFunction) => { + logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params }) + + if (areValidationErrors(req, res)) return + if (!await isLocalVideoChannelNameExist(req.params.name, res)) return + + return next() + } +] + // --------------------------------------------------------------------------- export { @@ -107,7 +121,8 @@ export { videoChannelsAddValidator, videoChannelsUpdateValidator, videoChannelsRemoveValidator, - videoChannelsGetValidator + videoChannelsGetValidator, + localVideoChannelValidator } // --------------------------------------------------------------------------- diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts index 3b9645048..63a1678ec 100644 --- a/server/middlewares/validators/webfinger.ts +++ b/server/middlewares/validators/webfinger.ts @@ -1,13 +1,13 @@ import * as express from 'express' import { query } from 'express-validator/check' -import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger' +import { isWebfingerLocalResourceValid } from '../../helpers/custom-validators/webfinger' import { logger } from '../../helpers/logger' import { ActorModel } from '../../models/activitypub/actor' import { areValidationErrors } from './utils' import { getHostWithPort } from '../../helpers/express-utils' const webfingerValidator = [ - query('resource').custom(isWebfingerResourceValid).withMessage('Should have a valid webfinger resource'), + query('resource').custom(isWebfingerLocalResourceValid).withMessage('Should have a valid webfinger resource'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking webfinger parameters', { parameters: req.query }) -- cgit v1.2.3