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 --- .../custom-validators/activitypub/activity.ts | 19 +++---- .../helpers/custom-validators/activitypub/actor.ts | 61 ++++++++++++++++++++-- .../custom-validators/activitypub/announce.ts | 6 +-- .../helpers/custom-validators/activitypub/misc.ts | 20 ++++++- .../helpers/custom-validators/activitypub/undo.ts | 4 +- .../activitypub/video-channels.ts | 6 --- .../custom-validators/activitypub/videos.ts | 14 +++-- server/helpers/custom-validators/webfinger.ts | 8 +-- 8 files changed, 99 insertions(+), 39 deletions(-) (limited to 'server/helpers/custom-validators') diff --git a/server/helpers/custom-validators/activitypub/activity.ts b/server/helpers/custom-validators/activitypub/activity.ts index ae7732194..c402800a4 100644 --- a/server/helpers/custom-validators/activitypub/activity.ts +++ b/server/helpers/custom-validators/activitypub/activity.ts @@ -1,14 +1,14 @@ import * as validator from 'validator' import { Activity, ActivityType } from '../../../../shared/models/activitypub' -import { isAccountAcceptActivityValid, isAccountDeleteActivityValid, isAccountFollowActivityValid } from './actor' +import { isActorAcceptActivityValid, isActorDeleteActivityValid, isActorFollowActivityValid } from './actor' import { isAnnounceActivityValid } from './announce' import { isActivityPubUrlValid } from './misc' import { isDislikeActivityValid, isLikeActivityValid } from './rate' import { isUndoActivityValid } from './undo' -import { isVideoChannelCreateActivityValid, isVideoChannelDeleteActivityValid, isVideoChannelUpdateActivityValid } from './video-channels' +import { isVideoChannelDeleteActivityValid, isVideoChannelUpdateActivityValid } from './video-channels' import { isVideoFlagValid, - isVideoTorrentAddActivityValid, + isVideoTorrentCreateActivityValid, isVideoTorrentDeleteActivityValid, isVideoTorrentUpdateActivityValid } from './videos' @@ -29,7 +29,6 @@ function isRootActivityValid (activity: any) { const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean } = { Create: checkCreateActivity, - Add: checkAddActivity, Update: checkUpdateActivity, Delete: checkDeleteActivity, Follow: checkFollowActivity, @@ -59,14 +58,10 @@ export { function checkCreateActivity (activity: any) { return isViewActivityValid(activity) || isDislikeActivityValid(activity) || - isVideoChannelCreateActivityValid(activity) || + isVideoTorrentCreateActivityValid(activity) || isVideoFlagValid(activity) } -function checkAddActivity (activity: any) { - return isVideoTorrentAddActivityValid(activity) -} - function checkUpdateActivity (activity: any) { return isVideoTorrentUpdateActivityValid(activity) || isVideoChannelUpdateActivityValid(activity) @@ -75,15 +70,15 @@ function checkUpdateActivity (activity: any) { function checkDeleteActivity (activity: any) { return isVideoTorrentDeleteActivityValid(activity) || isVideoChannelDeleteActivityValid(activity) || - isAccountDeleteActivityValid(activity) + isActorDeleteActivityValid(activity) } function checkFollowActivity (activity: any) { - return isAccountFollowActivityValid(activity) + return isActorFollowActivityValid(activity) } function checkAcceptActivity (activity: any) { - return isAccountAcceptActivityValid(activity) + return isActorAcceptActivityValid(activity) } function checkAnnounceActivity (activity: any) { diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index 28551c96c..bf42757c5 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts @@ -1,8 +1,12 @@ +import * as Bluebird from 'bluebird' +import { Response } from 'express' import * as validator from 'validator' import { CONSTRAINTS_FIELDS } from '../../../initializers' +import { ActorModel } from '../../../models/activitypub/actor' import { isAccountNameValid } from '../accounts' import { exists, isUUIDValid } from '../misc' -import { isActivityPubUrlValid, isBaseActivityValid } from './misc' +import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels' +import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' function isActorEndpointsObjectValid (endpointObject: any) { return isActivityPubUrlValid(endpointObject.sharedInbox) @@ -27,7 +31,12 @@ function isActorPublicKeyValid (publicKey: string) { } function isActorPreferredUsernameValid (preferredUsername: string) { - return isAccountNameValid(preferredUsername) + return isAccountNameValid(preferredUsername) || isVideoChannelNameValid(preferredUsername) +} + +const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+') +function isActorNameValid (name: string) { + return exists(name) && validator.matches(name, actorNameRegExp) } function isActorPrivateKeyValid (privateKey: string) { @@ -46,10 +55,16 @@ function isRemoteActorValid (remoteActor: any) { isActivityPubUrlValid(remoteActor.followers) && isActivityPubUrlValid(remoteActor.inbox) && isActivityPubUrlValid(remoteActor.outbox) && + isActorNameValid(remoteActor.name) && isActorPreferredUsernameValid(remoteActor.preferredUsername) && isActivityPubUrlValid(remoteActor.url) && isActorPublicKeyObjectValid(remoteActor.publicKey) && - isActorEndpointsObjectValid(remoteActor.endpoints) + isActorEndpointsObjectValid(remoteActor.endpoints) && + (!remoteActor.summary || isVideoChannelDescriptionValid(remoteActor.summary)) && + setValidAttributedTo(remoteActor) && + // If this is not an account, it should be attributed to an account + // In PeerTube we use this to attach a video channel to a specific account + (remoteActor.type === 'Person' || remoteActor.attributedTo.length !== 0) } function isActorFollowingCountValid (value: string) { @@ -73,6 +88,40 @@ function isActorAcceptActivityValid (activity: any) { return isBaseActivityValid(activity, 'Accept') } +function isActorIdExist (id: number | string, res: Response) { + let promise: Bluebird + + if (validator.isInt('' + id)) { + promise = ActorModel.load(+id) + } else { // UUID + promise = ActorModel.loadByUUID('' + id) + } + + return isActorExist(promise, res) +} + +function isLocalActorNameExist (name: string, res: Response) { + const promise = ActorModel.loadLocalByName(name) + + return isActorExist(promise, res) +} + +async function isActorExist (p: Bluebird, res: Response) { + const actor = await p + + if (!actor) { + res.status(404) + .send({ error: 'Actor not found' }) + .end() + + return false + } + + res.locals.actor = actor + + return true +} + // --------------------------------------------------------------------------- export { @@ -87,5 +136,9 @@ export { isActorFollowersCountValid, isActorFollowActivityValid, isActorAcceptActivityValid, - isActorDeleteActivityValid + isActorDeleteActivityValid, + isActorIdExist, + isLocalActorNameExist, + isActorNameValid, + isActorExist } diff --git a/server/helpers/custom-validators/activitypub/announce.ts b/server/helpers/custom-validators/activitypub/announce.ts index 45f6b05a0..80511129c 100644 --- a/server/helpers/custom-validators/activitypub/announce.ts +++ b/server/helpers/custom-validators/activitypub/announce.ts @@ -1,12 +1,10 @@ import { isBaseActivityValid } from './misc' -import { isVideoTorrentAddActivityValid } from './videos' -import { isVideoChannelCreateActivityValid } from './video-channels' +import { isVideoTorrentCreateActivityValid } from './videos' function isAnnounceActivityValid (activity: any) { return isBaseActivityValid(activity, 'Announce') && ( - isVideoChannelCreateActivityValid(activity.object) || - isVideoTorrentAddActivityValid(activity.object) + isVideoTorrentCreateActivityValid(activity.object) ) } diff --git a/server/helpers/custom-validators/activitypub/misc.ts b/server/helpers/custom-validators/activitypub/misc.ts index 65f5ca809..3ca4e4ff4 100644 --- a/server/helpers/custom-validators/activitypub/misc.ts +++ b/server/helpers/custom-validators/activitypub/misc.ts @@ -17,7 +17,7 @@ function isActivityPubUrlValid (url: string) { isURLOptions.require_tld = false } - return exists(url) && validator.isURL(url, isURLOptions) && validator.isLength(url, CONSTRAINTS_FIELDS.ACCOUNTS.URL) + return exists(url) && validator.isURL(url, isURLOptions) && validator.isLength(url, CONSTRAINTS_FIELDS.ACTOR.URL) } function isBaseActivityValid (activity: any, type: string) { @@ -35,7 +35,23 @@ function isBaseActivityValid (activity: any, type: string) { ) } +function setValidAttributedTo (obj: any) { + if (Array.isArray(obj.attributedTo) === false) { + obj.attributedTo = [] + return true + } + + const newAttributesTo = obj.attributedTo.filter(a => { + return (a.type === 'Group' || a.type === 'Person') && isActivityPubUrlValid(a.id) + }) + + obj.attributedTo = newAttributesTo + + return true +} + export { isActivityPubUrlValid, - isBaseActivityValid + isBaseActivityValid, + setValidAttributedTo } diff --git a/server/helpers/custom-validators/activitypub/undo.ts b/server/helpers/custom-validators/activitypub/undo.ts index d07bbf6b7..a2831b0bf 100644 --- a/server/helpers/custom-validators/activitypub/undo.ts +++ b/server/helpers/custom-validators/activitypub/undo.ts @@ -1,11 +1,11 @@ -import { isAccountFollowActivityValid } from './actor' +import { isActorFollowActivityValid } from './actor' import { isBaseActivityValid } from './misc' import { isDislikeActivityValid, isLikeActivityValid } from './rate' function isUndoActivityValid (activity: any) { return isBaseActivityValid(activity, 'Undo') && ( - isAccountFollowActivityValid(activity.object) || + isActorFollowActivityValid(activity.object) || isLikeActivityValid(activity.object) || isDislikeActivityValid(activity.object) ) diff --git a/server/helpers/custom-validators/activitypub/video-channels.ts b/server/helpers/custom-validators/activitypub/video-channels.ts index 9fd3bb149..eb45c6372 100644 --- a/server/helpers/custom-validators/activitypub/video-channels.ts +++ b/server/helpers/custom-validators/activitypub/video-channels.ts @@ -2,11 +2,6 @@ import { isDateValid, isUUIDValid } from '../misc' import { isVideoChannelDescriptionValid, isVideoChannelNameValid } from '../video-channels' import { isActivityPubUrlValid, isBaseActivityValid } from './misc' -function isVideoChannelCreateActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Create') && - isVideoChannelObjectValid(activity.object) -} - function isVideoChannelUpdateActivityValid (activity: any) { return isBaseActivityValid(activity, 'Update') && isVideoChannelObjectValid(activity.object) @@ -29,7 +24,6 @@ function isVideoChannelObjectValid (videoChannel: any) { // --------------------------------------------------------------------------- export { - isVideoChannelCreateActivityValid, isVideoChannelUpdateActivityValid, isVideoChannelDeleteActivityValid, isVideoChannelObjectValid diff --git a/server/helpers/custom-validators/activitypub/videos.ts b/server/helpers/custom-validators/activitypub/videos.ts index 2ed2988f5..b485e5fcf 100644 --- a/server/helpers/custom-validators/activitypub/videos.ts +++ b/server/helpers/custom-validators/activitypub/videos.ts @@ -10,10 +10,10 @@ import { isVideoTruncatedDescriptionValid, isVideoViewsValid } from '../videos' -import { isActivityPubUrlValid, isBaseActivityValid } from './misc' +import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' -function isVideoTorrentAddActivityValid (activity: any) { - return isBaseActivityValid(activity, 'Add') && +function isVideoTorrentCreateActivityValid (activity: any) { + return isBaseActivityValid(activity, 'Create') && isVideoTorrentObjectValid(activity.object) } @@ -43,6 +43,8 @@ function isActivityPubVideoDurationValid (value: string) { } function isVideoTorrentObjectValid (video: any) { + console.log(video) + return video.type === 'Video' && isActivityPubUrlValid(video.id) && isVideoNameValid(video.name) && @@ -59,13 +61,15 @@ function isVideoTorrentObjectValid (video: any) { (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) && isRemoteVideoIconValid(video.icon) && setValidRemoteVideoUrls(video) && - video.url.length !== 0 + video.url.length !== 0 && + setValidAttributedTo(video) && + video.attributedTo.length !== 0 } // --------------------------------------------------------------------------- export { - isVideoTorrentAddActivityValid, + isVideoTorrentCreateActivityValid, isVideoTorrentUpdateActivityValid, isVideoTorrentDeleteActivityValid, isVideoFlagValid diff --git a/server/helpers/custom-validators/webfinger.ts b/server/helpers/custom-validators/webfinger.ts index 38f6b938d..c53db4027 100644 --- a/server/helpers/custom-validators/webfinger.ts +++ b/server/helpers/custom-validators/webfinger.ts @@ -5,11 +5,11 @@ function isWebfingerResourceValid (value: string) { if (!exists(value)) return false if (value.startsWith('acct:') === false) return false - const accountWithHost = value.substr(5) - const accountParts = accountWithHost.split('@') - if (accountParts.length !== 2) return false + const actorWithHost = value.substr(5) + const actorParts = actorWithHost.split('@') + if (actorParts.length !== 2) return false - const host = accountParts[1] + const host = actorParts[1] return host === CONFIG.WEBSERVER.HOST } -- cgit v1.2.3