From 8a19bee1a1ee39f973bb37429e4f73c3f2873cdb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 17 Aug 2018 15:45:42 +0200 Subject: Add ability to set a name to a channel --- server/helpers/custom-validators/accounts.ts | 2 +- .../helpers/custom-validators/activitypub/actor.ts | 2 +- server/helpers/custom-validators/video-channels.ts | 22 ++++++++++++++++------ 3 files changed, 18 insertions(+), 8 deletions(-) (limited to 'server/helpers') diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts index 0607d661c..191de1496 100644 --- a/server/helpers/custom-validators/accounts.ts +++ b/server/helpers/custom-validators/accounts.ts @@ -42,7 +42,7 @@ function isAccountNameWithHostExist (nameWithDomain: string, res: Response, send let promise: Bluebird if (!host || host === CONFIG.WEBSERVER.HOST) promise = AccountModel.loadLocalByName(accountName) - else promise = AccountModel.loadLocalByNameAndHost(accountName, host) + else promise = AccountModel.loadByNameAndHost(accountName, host) return isAccountExist(promise, res, sendNotFound) } diff --git a/server/helpers/custom-validators/activitypub/actor.ts b/server/helpers/custom-validators/activitypub/actor.ts index ae5014f8f..c3a62c12d 100644 --- a/server/helpers/custom-validators/activitypub/actor.ts +++ b/server/helpers/custom-validators/activitypub/actor.ts @@ -27,7 +27,7 @@ function isActorPublicKeyValid (publicKey: string) { validator.isLength(publicKey, CONSTRAINTS_FIELDS.ACTORS.PUBLIC_KEY) } -const actorNameRegExp = new RegExp('[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_]+') +const actorNameRegExp = new RegExp('^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789\\-_]+$') function isActorPreferredUsernameValid (preferredUsername: string) { return exists(preferredUsername) && validator.matches(preferredUsername, actorNameRegExp) } diff --git a/server/helpers/custom-validators/video-channels.ts b/server/helpers/custom-validators/video-channels.ts index 32faf36f7..f13519c1d 100644 --- a/server/helpers/custom-validators/video-channels.ts +++ b/server/helpers/custom-validators/video-channels.ts @@ -2,10 +2,9 @@ import * as express from 'express' import 'express-validator' import 'multer' import * as validator from 'validator' -import { CONSTRAINTS_FIELDS } from '../../initializers' +import { CONFIG, 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 @@ -21,13 +20,13 @@ 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) +async function isLocalVideoChannelNameExist (name: string, res: express.Response) { + const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) return processVideoChannelExist(videoChannel, res) } -async function isVideoChannelExist (id: string, res: express.Response) { +async function isVideoChannelIdExist (id: string, res: express.Response) { let videoChannel: VideoChannelModel if (validator.isInt(id)) { videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) @@ -38,14 +37,25 @@ async function isVideoChannelExist (id: string, res: express.Response) { return processVideoChannelExist(videoChannel, res) } +async function isVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { + const [ name, host ] = nameWithDomain.split('@') + let videoChannel: VideoChannelModel + + if (!host || host === CONFIG.WEBSERVER.HOST) videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) + else videoChannel = await VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host) + + return processVideoChannelExist(videoChannel, res) +} + // --------------------------------------------------------------------------- export { + isVideoChannelNameWithHostExist, isLocalVideoChannelNameExist, isVideoChannelDescriptionValid, isVideoChannelNameValid, isVideoChannelSupportValid, - isVideoChannelExist + isVideoChannelIdExist } function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { -- cgit v1.2.3