From 4f5d045960b042eb27e10bac1bdaf1c074c9fa2a Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Thu, 23 Jul 2020 21:30:04 +0200 Subject: harmonize search for libraries --- server/models/activitypub/actor-follow.ts | 31 +++++++++++++++++++++++++------ server/models/video/video-channel.ts | 24 ++++++++++++++++++++---- 2 files changed, 45 insertions(+), 10 deletions(-) (limited to 'server/models') diff --git a/server/models/activitypub/actor-follow.ts b/server/models/activitypub/actor-follow.ts index 3e85cc329..529cb35cc 100644 --- a/server/models/activitypub/actor-follow.ts +++ b/server/models/activitypub/actor-follow.ts @@ -15,14 +15,15 @@ import { Max, Model, Table, - UpdatedAt + UpdatedAt, + Sequelize } from 'sequelize-typescript' import { FollowState } from '../../../shared/models/actors' import { ActorFollow } from '../../../shared/models/actors/follow.model' import { logger } from '../../helpers/logger' import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants' import { ServerModel } from '../server/server' -import { createSafeIn, getFollowsSort, getSort } from '../utils' +import { createSafeIn, getFollowsSort, getSort, searchAttribute } from '../utils' import { ActorModel, unusedActorAttributesForAPI } from './actor' import { VideoChannelModel } from '../video/video-channel' import { AccountModel } from '../account/account' @@ -440,16 +441,34 @@ export class ActorFollowModel extends Model { }) } - static listSubscriptionsForApi (actorId: number, start: number, count: number, sort: string) { + static listSubscriptionsForApi (options: { + actorId: number + start: number + count: number + sort: string + search?: string + }) { + const { actorId, start, count, sort } = options + const where = { + actorId: actorId + } + + if (options.search) { + Object.assign(where, { + [Op.or]: [ + searchAttribute(options.search, '$ActorFollowing.preferredUsername$'), + searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$') + ] + }) + } + const query = { attributes: [], distinct: true, offset: start, limit: count, order: getSort(sort), - where: { - actorId: actorId - }, + where, include: [ { attributes: [ 'id' ], diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index f3401fb9c..9da965bbc 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -315,9 +315,8 @@ export class VideoChannelModel extends Model { start: number count: number sort: string - search?: string }) { - const { actorId, search } = parameters + const { actorId } = parameters const query = { offset: parameters.start, @@ -326,7 +325,7 @@ export class VideoChannelModel extends Model { } const scopes = { - method: [ ScopeNames.FOR_API, { actorId, search } as AvailableForListOptions ] + method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ] } return VideoChannelModel .scope(scopes) @@ -405,7 +404,23 @@ export class VideoChannelModel extends Model { count: number sort: string withStats?: boolean + search?: string }) { + const escapedSearch = VideoModel.sequelize.escape(options.search) + const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%') + const where = options.search + ? { + [Op.or]: [ + Sequelize.literal( + 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))' + ), + Sequelize.literal( + 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))' + ) + ] + } + : null + const query = { offset: options.start, limit: options.count, @@ -418,7 +433,8 @@ export class VideoChannelModel extends Model { }, required: true } - ] + ], + where } const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR ] -- cgit v1.2.3