From 50d6de9c286abcb34ff4234d56d9cbb803db7665 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 14 Dec 2017 17:38:41 +0100 Subject: Begin moving video channel to actor --- server/middlewares/activitypub.ts | 28 +++++++++------------- server/middlewares/validators/follows.ts | 13 +++++------ server/middlewares/validators/video-channels.ts | 31 +++---------------------- server/middlewares/validators/webfinger.ts | 10 ++++---- 4 files changed, 25 insertions(+), 57 deletions(-) (limited to 'server/middlewares') diff --git a/server/middlewares/activitypub.ts b/server/middlewares/activitypub.ts index 489396447..37b7c42ec 100644 --- a/server/middlewares/activitypub.ts +++ b/server/middlewares/activitypub.ts @@ -3,33 +3,27 @@ import { NextFunction, Request, RequestHandler, Response } from 'express' import { ActivityPubSignature } from '../../shared' import { isSignatureVerified, logger } from '../helpers' import { ACCEPT_HEADERS, ACTIVITY_PUB } from '../initializers' -import { fetchRemoteAccount, saveAccountAndServerIfNotExist } from '../lib/activitypub' -import { AccountModel } from '../models/account/account' +import { getOrCreateActorAndServerAndModel } from '../lib/activitypub' +import { ActorModel } from '../models/activitypub/actor' async function checkSignature (req: Request, res: Response, next: NextFunction) { const signatureObject: ActivityPubSignature = req.body.signature - logger.debug('Checking signature of account %s...', signatureObject.creator) + logger.debug('Checking signature of actor %s...', signatureObject.creator) - let account = await AccountModel.loadByUrl(signatureObject.creator) - - // We don't have this account in our database, fetch it on remote - if (!account) { - account = await fetchRemoteAccount(signatureObject.creator) - - if (!account) { - return res.sendStatus(403) - } - - // Save our new account and its server in database - await saveAccountAndServerIfNotExist(account) + let actor: ActorModel + try { + actor = await getOrCreateActorAndServerAndModel(signatureObject.creator) + } catch (err) { + logger.error('Cannot create remote actor and check signature.', err) + return res.sendStatus(403) } - const verified = await isSignatureVerified(account, req.body) + const verified = await isSignatureVerified(actor, req.body) if (verified === false) return res.sendStatus(403) res.locals.signature = { - account + actor } return next() diff --git a/server/middlewares/validators/follows.ts b/server/middlewares/validators/follows.ts index 10482e5d0..2240d30db 100644 --- a/server/middlewares/validators/follows.ts +++ b/server/middlewares/validators/follows.ts @@ -1,10 +1,9 @@ import * as express from 'express' import { body, param } from 'express-validator/check' -import { getServerAccount, isTestInstance, logger } from '../../helpers' -import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' -import { isEachUniqueHostValid } from '../../helpers/custom-validators/servers' +import { getServerActor, isTestInstance, logger } from '../../helpers' +import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' import { CONFIG } from '../../initializers' -import { AccountFollowModel } from '../../models/account/account-follow' +import { ActorFollowModel } from '../../models/activitypub/actor-follow' import { areValidationErrors } from './utils' const followValidator = [ @@ -29,15 +28,15 @@ const followValidator = [ ] const removeFollowingValidator = [ - param('accountId').custom(isIdOrUUIDValid).withMessage('Should have a valid account id'), + param('host').custom(isHostValid).withMessage('Should have a valid host'), async (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking unfollow parameters', { parameters: req.params }) if (areValidationErrors(req, res)) return - const serverAccount = await getServerAccount() - const follow = await AccountFollowModel.loadByAccountAndTarget(serverAccount.id, req.params.accountId) + const serverActor = await getServerActor() + const follow = await ActorFollowModel.loadByActorAndTargetHost(serverActor.id, req.params.host) if (!follow) { return res.status(404) diff --git a/server/middlewares/validators/video-channels.ts b/server/middlewares/validators/video-channels.ts index 068fd210f..cc7d54c06 100644 --- a/server/middlewares/validators/video-channels.ts +++ b/server/middlewares/validators/video-channels.ts @@ -3,7 +3,7 @@ import { body, param } from 'express-validator/check' import { UserRight } from '../../../shared' import { logger } from '../../helpers' import { isAccountIdExist } from '../../helpers/custom-validators/accounts' -import { isIdOrUUIDValid, isIdValid } from '../../helpers/custom-validators/misc' +import { isIdOrUUIDValid } from '../../helpers/custom-validators/misc' import { isVideoChannelDescriptionValid, isVideoChannelExist, @@ -11,7 +11,6 @@ import { } from '../../helpers/custom-validators/video-channels' import { UserModel } from '../../models/account/user' import { VideoChannelModel } from '../../models/video/video-channel' -import { VideoChannelShareModel } from '../../models/video/video-channel-share' import { areValidationErrors } from './utils' const listVideoAccountChannelsValidator = [ @@ -98,28 +97,6 @@ const videoChannelsGetValidator = [ } ] -const videoChannelsShareValidator = [ - param('id').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid id'), - param('accountId').custom(isIdValid).not().isEmpty().withMessage('Should have a valid account id'), - - async (req: express.Request, res: express.Response, next: express.NextFunction) => { - logger.debug('Checking videoChannelShare parameters', { parameters: req.params }) - - if (areValidationErrors(req, res)) return - if (!await isVideoChannelExist(req.params.id, res)) return - - const share = await VideoChannelShareModel.load(res.locals.video.id, req.params.accountId, undefined) - if (!share) { - return res.status(404) - .end() - } - - res.locals.videoChannelShare = share - - return next() - } -] - // --------------------------------------------------------------------------- export { @@ -127,15 +104,13 @@ export { videoChannelsAddValidator, videoChannelsUpdateValidator, videoChannelsRemoveValidator, - videoChannelsGetValidator, - videoChannelsShareValidator + videoChannelsGetValidator } // --------------------------------------------------------------------------- function checkUserCanDeleteVideoChannel (user: UserModel, videoChannel: VideoChannelModel, res: express.Response) { - // Retrieve the user who did the request - if (videoChannel.isOwned() === false) { + if (videoChannel.Actor.isOwned() === false) { res.status(403) .json({ error: 'Cannot remove video channel of another server.' }) .end() diff --git a/server/middlewares/validators/webfinger.ts b/server/middlewares/validators/webfinger.ts index 7903c7400..2c8351799 100644 --- a/server/middlewares/validators/webfinger.ts +++ b/server/middlewares/validators/webfinger.ts @@ -2,7 +2,7 @@ import * as express from 'express' import { query } from 'express-validator/check' import { logger } from '../../helpers' import { isWebfingerResourceValid } from '../../helpers/custom-validators/webfinger' -import { AccountModel } from '../../models/account/account' +import { ActorModel } from '../../models/activitypub/actor' import { areValidationErrors } from './utils' const webfingerValidator = [ @@ -17,14 +17,14 @@ const webfingerValidator = [ const nameWithHost = req.query.resource.substr(5) const [ name ] = nameWithHost.split('@') - const account = await AccountModel.loadLocalByName(name) - if (!account) { + const actor = await ActorModel.loadLocalByName(name) + if (!actor) { return res.status(404) - .send({ error: 'Account not found' }) + .send({ error: 'Actor not found' }) .end() } - res.locals.account = account + res.locals.actor = actor return next() } ] -- cgit v1.2.3