From b033851fb54241bb703f86add025229e68cc6f59 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 29 Jul 2021 10:27:24 +0200 Subject: Search channels against handles and not names --- .../api/search/search-video-channels.ts | 2 +- server/helpers/custom-validators/misc.ts | 5 +++ server/middlewares/validators/search.ts | 7 +++-- server/models/video/video-channel.ts | 36 ++++++++++++++++------ server/tests/api/check-params/search.ts | 4 +++ server/tests/api/search/search-channels.ts | 17 +++++++--- 6 files changed, 53 insertions(+), 18 deletions(-) (limited to 'server') diff --git a/server/controllers/api/search/search-video-channels.ts b/server/controllers/api/search/search-video-channels.ts index 9fc2d53a5..ae32a6726 100644 --- a/server/controllers/api/search/search-video-channels.ts +++ b/server/controllers/api/search/search-video-channels.ts @@ -100,7 +100,7 @@ async function searchVideoChannelsDB (query: VideoChannelsSearchQuery, res: expr count: query.count, sort: query.sort, host: query.host, - names: query.names + handles: query.handles }, 'filter:api.search.video-channels.local.list.params') const resultList = await Hooks.wrapPromiseFun( diff --git a/server/helpers/custom-validators/misc.ts b/server/helpers/custom-validators/misc.ts index f8f168149..c19a3e5eb 100644 --- a/server/helpers/custom-validators/misc.ts +++ b/server/helpers/custom-validators/misc.ts @@ -23,6 +23,10 @@ function isNotEmptyIntArray (value: any) { return Array.isArray(value) && value.every(v => validator.isInt('' + v)) && value.length !== 0 } +function isNotEmptyStringArray (value: any) { + return Array.isArray(value) && value.every(v => typeof v === 'string' && v.length !== 0) && value.length !== 0 +} + function isArrayOf (value: any, validator: (value: any) => boolean) { return isArray(value) && value.every(v => validator(v)) } @@ -187,6 +191,7 @@ export { isIntOrNull, isIdValid, isSafePath, + isNotEmptyStringArray, isUUIDValid, toCompleteUUIDs, toCompleteUUID, diff --git a/server/middlewares/validators/search.ts b/server/middlewares/validators/search.ts index cde300968..27d0e541d 100644 --- a/server/middlewares/validators/search.ts +++ b/server/middlewares/validators/search.ts @@ -2,7 +2,7 @@ import * as express from 'express' import { query } from 'express-validator' import { isSearchTargetValid } from '@server/helpers/custom-validators/search' import { isHostValid } from '@server/helpers/custom-validators/servers' -import { areUUIDsValid, isDateValid, toCompleteUUIDs } from '../../helpers/custom-validators/misc' +import { areUUIDsValid, isDateValid, isNotEmptyStringArray, toCompleteUUIDs } from '../../helpers/custom-validators/misc' import { logger } from '../../helpers/logger' import { areValidationErrors } from './shared' @@ -64,9 +64,10 @@ const videoChannelsListSearchValidator = [ .optional() .custom(isSearchTargetValid).withMessage('Should have a valid search target'), - query('names') + query('handles') .optional() - .toArray(), + .toArray() + .custom(isNotEmptyStringArray).withMessage('Should have valid handles'), (req: express.Request, res: express.Response, next: express.NextFunction) => { logger.debug('Checking video channels search query', { parameters: req.query }) diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index 327f49304..e4b12c517 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -18,7 +18,7 @@ import { UpdatedAt } from 'sequelize-typescript' import { MAccountActor } from '@server/types/models' -import { AttributesOnly } from '@shared/core-utils' +import { AttributesOnly, pick } from '@shared/core-utils' import { ActivityPubActor } from '../../../shared/models/activitypub' import { VideoChannel, VideoChannelSummary } from '../../../shared/models/videos' import { @@ -59,7 +59,7 @@ type AvailableForListOptions = { actorId: number search?: string host?: string - names?: string[] + handles?: string[] } type AvailableWithStatsOptions = { @@ -114,15 +114,33 @@ export type SummaryOptions = { }) } - if (options.names) { - whereActorAnd.push({ - preferredUsername: { - [Op.in]: options.names + let rootWhere: WhereOptions + if (options.handles) { + const or: WhereOptions[] = [] + + for (const handle of options.handles || []) { + const [ preferredUsername, host ] = handle.split('@') + + if (!host) { + or.push({ + '$Actor.preferredUsername$': preferredUsername, + '$Actor.serverId$': null + }) + } else { + or.push({ + '$Actor.preferredUsername$': preferredUsername, + '$Actor.Server.host$': host + }) } - }) + } + + rootWhere = { + [Op.or]: or + } } return { + where: rootWhere, include: [ { attributes: { @@ -473,7 +491,7 @@ ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"` sort: string host?: string - names?: string[] + handles?: string[] }) { let attributesInclude: any[] = [ literal('0 as similarity') ] let where: WhereOptions @@ -507,7 +525,7 @@ ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"` return VideoChannelModel .scope({ - method: [ ScopeNames.FOR_API, { actorId: options.actorId, host: options.host, names: options.names } as AvailableForListOptions ] + method: [ ScopeNames.FOR_API, pick(options, [ 'actorId', 'host', 'handles' ]) as AvailableForListOptions ] }) .findAndCountAll(query) .then(({ rows, count }) => { diff --git a/server/tests/api/check-params/search.ts b/server/tests/api/check-params/search.ts index 789ea7754..cc15d2593 100644 --- a/server/tests/api/check-params/search.ts +++ b/server/tests/api/check-params/search.ts @@ -216,6 +216,10 @@ describe('Test videos API validator', function () { await makeGetRequest({ url: server.url, path, query: { ...query, host: '6565' }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) }) + it('Should fail with invalid handles', async function () { + await makeGetRequest({ url: server.url, path, query: { ...query, handles: [ '' ] }, expectedStatus: HttpStatusCode.BAD_REQUEST_400 }) + }) + it('Should succeed with the correct parameters', async function () { await makeGetRequest({ url: server.url, path, query, expectedStatus: HttpStatusCode.OK_200 }) }) diff --git a/server/tests/api/search/search-channels.ts b/server/tests/api/search/search-channels.ts index ef78c0f67..4485c424e 100644 --- a/server/tests/api/search/search-channels.ts +++ b/server/tests/api/search/search-channels.ts @@ -122,18 +122,25 @@ describe('Test channels search', function () { it('Should filter by names', async function () { { - const body = await command.advancedChannelSearch({ search: { names: [ 'squall_channel', 'zell_channel' ] } }) - expect(body.total).to.equal(2) - expect(body.data).to.have.lengthOf(2) + const body = await command.advancedChannelSearch({ search: { handles: [ 'squall_channel', 'zell_channel' ] } }) + expect(body.total).to.equal(1) + expect(body.data).to.have.lengthOf(1) expect(body.data[0].displayName).to.equal('Squall channel') - expect(body.data[1].displayName).to.equal('Zell channel') } { - const body = await command.advancedChannelSearch({ search: { names: [ 'chocobozzz_channel' ] } }) + const body = await command.advancedChannelSearch({ search: { handles: [ 'chocobozzz_channel' ] } }) expect(body.total).to.equal(0) expect(body.data).to.have.lengthOf(0) } + + { + const body = await command.advancedChannelSearch({ search: { handles: [ 'squall_channel', 'zell_channel@' + remoteServer.host ] } }) + expect(body.total).to.equal(2) + expect(body.data).to.have.lengthOf(2) + expect(body.data[0].displayName).to.equal('Squall channel') + expect(body.data[1].displayName).to.equal('Zell channel') + } }) after(async function () { -- cgit v1.2.3