]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-channel.ts
Refactor options in models
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
index 9aa27171113beb33a0de49d100a99924694c2bde..9f04a57c64025f11baa6e9d02fee6f4ef1208141 100644 (file)
@@ -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,6 +59,7 @@ type AvailableForListOptions = {
   actorId: number
   search?: string
   host?: string
+  handles?: string[]
 }
 
 type AvailableWithStatsOptions = {
@@ -84,18 +85,20 @@ export type SummaryOptions = {
     // Only list local channels OR channels that are on an instance followed by actorId
     const inQueryInstanceFollow = buildServerIdsFollowedBy(options.actorId)
 
-    const whereActor = {
-      [Op.or]: [
-        {
-          serverId: null
-        },
-        {
-          serverId: {
-            [Op.in]: Sequelize.literal(inQueryInstanceFollow)
+    const whereActorAnd: WhereOptions[] = [
+      {
+        [Op.or]: [
+          {
+            serverId: null
+          },
+          {
+            serverId: {
+              [Op.in]: Sequelize.literal(inQueryInstanceFollow)
+            }
           }
-        }
-      ]
-    }
+        ]
+      }
+    ]
 
     let serverRequired = false
     let whereServer: WhereOptions
@@ -106,19 +109,47 @@ export type SummaryOptions = {
     }
 
     if (options.host === WEBSERVER.HOST) {
-      Object.assign(whereActor, {
-        [Op.and]: [ { serverId: null } ]
+      whereActorAnd.push({
+        serverId: null
       })
     }
 
+    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: {
             exclude: unusedActorAttributesForAPI
           },
           model: ActorModel,
-          where: whereActor,
+          where: {
+            [Op.and]: whereActorAnd
+          },
           include: [
             {
               model: ServerModel,
@@ -407,30 +438,6 @@ ON              "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
     }
   }
 
-  static listForApi (parameters: {
-    actorId: number
-    start: number
-    count: number
-    sort: string
-  }) {
-    const { actorId } = parameters
-
-    const query = {
-      offset: parameters.start,
-      limit: parameters.count,
-      order: getSort(parameters.sort)
-    }
-
-    return VideoChannelModel
-      .scope({
-        method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
-      })
-      .findAndCountAll(query)
-      .then(({ rows, count }) => {
-        return { total: count, data: rows }
-      })
-  }
-
   static listLocalsForSitemap (sort: string): Promise<MChannelActor[]> {
     const query = {
       attributes: [ ],
@@ -452,28 +459,43 @@ ON              "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
       .findAll(query)
   }
 
-  static searchForApi (options: {
-    actorId: number
-    search: string
+  static listForApi (parameters: Pick<AvailableForListOptions, 'actorId'> & {
     start: number
     count: number
     sort: string
-
-    host?: string
   }) {
-    const attributesInclude = []
-    const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
-    const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
-    attributesInclude.push(createSimilarityAttribute('VideoChannelModel.name', options.search))
+    const { actorId } = parameters
 
     const query = {
-      attributes: {
-        include: attributesInclude
-      },
-      offset: options.start,
-      limit: options.count,
-      order: getSort(options.sort),
-      where: {
+      offset: parameters.start,
+      limit: parameters.count,
+      order: getSort(parameters.sort)
+    }
+
+    return VideoChannelModel
+      .scope({
+        method: [ ScopeNames.FOR_API, { actorId } as AvailableForListOptions ]
+      })
+      .findAndCountAll(query)
+      .then(({ rows, count }) => {
+        return { total: count, data: rows }
+      })
+  }
+
+  static searchForApi (options: Pick<AvailableForListOptions, 'actorId' | 'search' | 'host' | 'handles'> & {
+    start: number
+    count: number
+    sort: string
+  }) {
+    let attributesInclude: any[] = [ literal('0 as similarity') ]
+    let where: WhereOptions
+
+    if (options.search) {
+      const escapedSearch = VideoChannelModel.sequelize.escape(options.search)
+      const escapedLikeSearch = VideoChannelModel.sequelize.escape('%' + options.search + '%')
+      attributesInclude = [ createSimilarityAttribute('VideoChannelModel.name', options.search) ]
+
+      where = {
         [Op.or]: [
           Sequelize.literal(
             'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
@@ -485,9 +507,19 @@ ON              "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
       }
     }
 
+    const query = {
+      attributes: {
+        include: attributesInclude
+      },
+      offset: options.start,
+      limit: options.count,
+      order: getSort(options.sort),
+      where
+    }
+
     return VideoChannelModel
       .scope({
-        method: [ ScopeNames.FOR_API, { actorId: options.actorId, host: options.host } as AvailableForListOptions ]
+        method: [ ScopeNames.FOR_API, pick(options, [ 'actorId', 'host', 'handles' ]) as AvailableForListOptions ]
       })
       .findAndCountAll(query)
       .then(({ rows, count }) => {