aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models
diff options
context:
space:
mode:
authorRigel Kent <sendmemail@rigelk.eu>2020-07-23 21:30:04 +0200
committerRigel Kent <par@rigelk.eu>2020-07-29 18:15:53 +0200
commit4f5d045960b042eb27e10bac1bdaf1c074c9fa2a (patch)
tree09e1e8cce0a2e64146ede51941cfa2f1bdcf3c2f /server/models
parentbc99dfe54e093e69ba8fd06d36b36fbbda3f45de (diff)
downloadPeerTube-4f5d045960b042eb27e10bac1bdaf1c074c9fa2a.tar.gz
PeerTube-4f5d045960b042eb27e10bac1bdaf1c074c9fa2a.tar.zst
PeerTube-4f5d045960b042eb27e10bac1bdaf1c074c9fa2a.zip
harmonize search for libraries
Diffstat (limited to 'server/models')
-rw-r--r--server/models/activitypub/actor-follow.ts31
-rw-r--r--server/models/video/video-channel.ts24
2 files changed, 45 insertions, 10 deletions
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 {
15 Max, 15 Max,
16 Model, 16 Model,
17 Table, 17 Table,
18 UpdatedAt 18 UpdatedAt,
19 Sequelize
19} from 'sequelize-typescript' 20} from 'sequelize-typescript'
20import { FollowState } from '../../../shared/models/actors' 21import { FollowState } from '../../../shared/models/actors'
21import { ActorFollow } from '../../../shared/models/actors/follow.model' 22import { ActorFollow } from '../../../shared/models/actors/follow.model'
22import { logger } from '../../helpers/logger' 23import { logger } from '../../helpers/logger'
23import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants' 24import { ACTOR_FOLLOW_SCORE, FOLLOW_STATES, SERVER_ACTOR_NAME } from '../../initializers/constants'
24import { ServerModel } from '../server/server' 25import { ServerModel } from '../server/server'
25import { createSafeIn, getFollowsSort, getSort } from '../utils' 26import { createSafeIn, getFollowsSort, getSort, searchAttribute } from '../utils'
26import { ActorModel, unusedActorAttributesForAPI } from './actor' 27import { ActorModel, unusedActorAttributesForAPI } from './actor'
27import { VideoChannelModel } from '../video/video-channel' 28import { VideoChannelModel } from '../video/video-channel'
28import { AccountModel } from '../account/account' 29import { AccountModel } from '../account/account'
@@ -440,16 +441,34 @@ export class ActorFollowModel extends Model<ActorFollowModel> {
440 }) 441 })
441 } 442 }
442 443
443 static listSubscriptionsForApi (actorId: number, start: number, count: number, sort: string) { 444 static listSubscriptionsForApi (options: {
445 actorId: number
446 start: number
447 count: number
448 sort: string
449 search?: string
450 }) {
451 const { actorId, start, count, sort } = options
452 const where = {
453 actorId: actorId
454 }
455
456 if (options.search) {
457 Object.assign(where, {
458 [Op.or]: [
459 searchAttribute(options.search, '$ActorFollowing.preferredUsername$'),
460 searchAttribute(options.search, '$ActorFollowing.VideoChannel.name$')
461 ]
462 })
463 }
464
444 const query = { 465 const query = {
445 attributes: [], 466 attributes: [],
446 distinct: true, 467 distinct: true,
447 offset: start, 468 offset: start,
448 limit: count, 469 limit: count,
449 order: getSort(sort), 470 order: getSort(sort),
450 where: { 471 where,
451 actorId: actorId
452 },
453 include: [ 472 include: [
454 { 473 {
455 attributes: [ 'id' ], 474 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<VideoChannelModel> {
315 start: number 315 start: number
316 count: number 316 count: number
317 sort: string 317 sort: string
318 search?: string
319 }) { 318 }) {
320 const { actorId, search } = parameters 319 const { actorId } = parameters
321 320
322 const query = { 321 const query = {
323 offset: parameters.start, 322 offset: parameters.start,
@@ -326,7 +325,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
326 } 325 }
327 326
328 const scopes = { 327 const scopes = {
329 method: [ ScopeNames.FOR_API, { actorId, search } as AvailableForListOptions ] 328 method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
330 } 329 }
331 return VideoChannelModel 330 return VideoChannelModel
332 .scope(scopes) 331 .scope(scopes)
@@ -405,7 +404,23 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
405 count: number 404 count: number
406 sort: string 405 sort: string
407 withStats?: boolean 406 withStats?: boolean
407 search?: string
408 }) { 408 }) {
409 const escapedSearch = VideoModel.sequelize.escape(options.search)
410 const escapedLikeSearch = VideoModel.sequelize.escape('%' + options.search + '%')
411 const where = options.search
412 ? {
413 [Op.or]: [
414 Sequelize.literal(
415 'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
416 ),
417 Sequelize.literal(
418 'lower(immutable_unaccent("VideoChannelModel"."name")) LIKE lower(immutable_unaccent(' + escapedLikeSearch + '))'
419 )
420 ]
421 }
422 : null
423
409 const query = { 424 const query = {
410 offset: options.start, 425 offset: options.start,
411 limit: options.count, 426 limit: options.count,
@@ -418,7 +433,8 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
418 }, 433 },
419 required: true 434 required: true
420 } 435 }
421 ] 436 ],
437 where
422 } 438 }
423 439
424 const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR ] 440 const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR ]