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 --- .../helpers/custom-validators/activitypub/actor.ts | 13 ++++++++- server/helpers/custom-validators/video-channels.ts | 32 +++++++++++++++------- server/helpers/custom-validators/webfinger.ts | 4 +-- server/helpers/webfinger.ts | 10 ++++--- 4 files changed, 42 insertions(+), 17 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index c7a64e24d..ae5014f8f 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts @@ -3,6 +3,7 @@ import { CONSTRAINTS_FIELDS } from '../../../initializers' import { exists } from '../misc' import { truncate } from 'lodash' import { isActivityPubUrlValid, isBaseActivityValid, setValidAttributedTo } from './misc' +import { isHostValid } from '../servers' function isActorEndpointsObjectValid (endpointObject: any) { return isActivityPubUrlValid(endpointObject.sharedInbox) @@ -109,6 +110,15 @@ function normalizeActor (actor: any) { return } +function isValidActorHandle (handle: string) { + if (!exists(handle)) return false + + const parts = handle.split('@') + if (parts.length !== 2) return false + + return isHostValid(parts[1]) +} + // --------------------------------------------------------------------------- export { @@ -126,5 +136,6 @@ export { isActorAcceptActivityValid, isActorRejectActivityValid, isActorDeleteActivityValid, - isActorUpdateActivityValid + isActorUpdateActivityValid, + isValidActorHandle } diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 2a6f56840..32faf36f7 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts @@ -5,6 +5,7 @@ import * as validator from 'validator' import { CONSTRAINTS_FIELDS } from '../../initializers' import { VideoChannelModel } from '../../models/video/video-channel' import { exists } from './misc' +import { Response } from 'express' const VIDEO_CHANNELS_CONSTRAINTS_FIELDS = CONSTRAINTS_FIELDS.VIDEO_CHANNELS @@ -20,6 +21,12 @@ function isVideoChannelSupportValid (value: string) { return value === null || (exists(value) && validator.isLength(value, VIDEO_CHANNELS_CONSTRAINTS_FIELDS.SUPPORT)) } +async function isLocalVideoChannelNameExist (name: string, res: Response) { + const videoChannel = await VideoChannelModel.loadLocalByName(name) + + return processVideoChannelExist(videoChannel, res) +} + async function isVideoChannelExist (id: string, res: express.Response) { let videoChannel: VideoChannelModel if (validator.isInt(id)) { @@ -28,23 +35,28 @@ async function isVideoChannelExist (id: string, res: express.Response) { videoChannel = await VideoChannelModel.loadByUUIDAndPopulateAccount(id) } - if (!videoChannel) { - res.status(404) - .json({ error: 'Video channel not found' }) - .end() - - return false - } - - res.locals.videoChannel = videoChannel - return true + return processVideoChannelExist(videoChannel, res) } // --------------------------------------------------------------------------- export { + isLocalVideoChannelNameExist, isVideoChannelDescriptionValid, isVideoChannelNameValid, isVideoChannelSupportValid, isVideoChannelExist } + +function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { + if (!videoChannel) { + res.status(404) + .json({ error: 'Video channel not found' }) + .end() + + return false + } + + res.locals.videoChannel = videoChannel + return true +} diff --git a/server/helpers/custom-validators/webfinger.ts b/server/helpers/custom-validators/webfinger.ts index d8c1232ce..80a7e4a9d 100644 --- a/server/helpers/custom-validators/webfinger.ts +++ b/server/helpers/custom-validators/webfinger.ts @@ -2,7 +2,7 @@ import { CONFIG, REMOTE_SCHEME } from '../../initializers' import { sanitizeHost } from '../core-utils' import { exists } from './misc' -function isWebfingerResourceValid (value: string) { +function isWebfingerLocalResourceValid (value: string) { if (!exists(value)) return false if (value.startsWith('acct:') === false) return false @@ -17,5 +17,5 @@ function isWebfingerResourceValid (value: string) { // --------------------------------------------------------------------------- export { - isWebfingerResourceValid + isWebfingerLocalResourceValid } diff --git a/server/helpers/webfinger.ts b/server/helpers/webfinger.ts index 688bf2bab..5c60de10c 100644 --- a/server/helpers/webfinger.ts +++ b/server/helpers/webfinger.ts @@ -11,15 +11,17 @@ const webfinger = new WebFinger({ request_timeout: 3000 }) -async function loadActorUrlOrGetFromWebfinger (name: string, host: string) { +async function loadActorUrlOrGetFromWebfinger (uri: string) { + const [ name, host ] = uri.split('@') + const actor = await ActorModel.loadByNameAndHost(name, host) if (actor) return actor.url - return getUrlFromWebfinger(name, host) + return getUrlFromWebfinger(uri) } -async function getUrlFromWebfinger (name: string, host: string) { - const webfingerData: WebFingerData = await webfingerLookup(name + '@' + host) +async function getUrlFromWebfinger (uri: string) { + const webfingerData: WebFingerData = await webfingerLookup(uri) return getLinkOrThrow(webfingerData) } -- cgit v1.2.3