]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/models/video/video-channel.ts
Fix multiple server tests
[github/Chocobozzz/PeerTube.git] / server / models / video / video-channel.ts
index ca06048d1c47e65c73231be9d30209f352db8ee9..6241a75a30bfffd499cede0102bd4734781cc838 100644 (file)
@@ -8,7 +8,7 @@ import {
   Default,
   DefaultScope,
   ForeignKey,
-  HasMany, IFindOptions,
+  HasMany,
   Is,
   Model,
   Scopes,
@@ -24,18 +24,18 @@ import {
   isVideoChannelSupportValid
 } from '../../helpers/custom-validators/video-channels'
 import { sendDeleteActor } from '../../lib/activitypub/send'
-import { AccountModel, ScopeNames as AccountModelScopeNames } from '../account/account'
+import { AccountModel, ScopeNames as AccountModelScopeNames, SummaryOptions as AccountSummaryOptions } from '../account/account'
 import { ActorModel, unusedActorAttributesForAPI } from '../activitypub/actor'
 import { buildServerIdsFollowedBy, buildTrigramSearchIndex, createSimilarityAttribute, getSort, throwIfNotValid } from '../utils'
 import { VideoModel } from './video'
-import { CONFIG, CONSTRAINTS_FIELDS } from '../../initializers'
+import { CONSTRAINTS_FIELDS, WEBSERVER } from '../../initializers/constants'
 import { ServerModel } from '../server/server'
-import { DefineIndexesOptions } from 'sequelize'
+import { FindOptions, ModelIndexesOptions, Op } from 'sequelize'
 import { AvatarModel } from '../avatar/avatar'
 import { VideoPlaylistModel } from './video-playlist'
 
 // FIXME: Define indexes here because there is an issue with TS and Sequelize.literal when called directly in the annotation
-const indexes: DefineIndexesOptions[] = [
+const indexes: ModelIndexesOptions[] = [
   buildTrigramSearchIndex('video_channel_name_trigram', 'name'),
 
   {
@@ -58,21 +58,26 @@ type AvailableForListOptions = {
   actorId: number
 }
 
-@DefaultScope({
+export type SummaryOptions = {
+  withAccount?: boolean // Default: false
+  withAccountBlockerIds?: number[]
+}
+
+@DefaultScope(() => ({
   include: [
     {
-      model: () => ActorModel,
+      model: ActorModel,
       required: true
     }
   ]
-})
-@Scopes({
-  [ScopeNames.SUMMARY]: (withAccount = false) => {
-    const base: IFindOptions<VideoChannelModel> = {
+}))
+@Scopes(() => ({
+  [ScopeNames.SUMMARY]: (options: SummaryOptions = {}) => {
+    const base: FindOptions = {
       attributes: [ 'name', 'description', 'id', 'actorId' ],
       include: [
         {
-          attributes: [ 'uuid', 'preferredUsername', 'url', 'serverId', 'avatarId' ],
+          attributes: [ 'preferredUsername', 'url', 'serverId', 'avatarId' ],
           model: ActorModel.unscoped(),
           required: true,
           include: [
@@ -90,9 +95,11 @@ type AvailableForListOptions = {
       ]
     }
 
-    if (withAccount === true) {
+    if (options.withAccount === true) {
       base.include.push({
-        model: AccountModel.scope(AccountModelScopeNames.SUMMARY),
+        model: AccountModel.scope({
+          method: [ AccountModelScopeNames.SUMMARY, { withAccountBlockerIds: options.withAccountBlockerIds } as AccountSummaryOptions ]
+        }),
         required: true
       })
     }
@@ -111,13 +118,13 @@ type AvailableForListOptions = {
           },
           model: ActorModel,
           where: {
-            [Sequelize.Op.or]: [
+            [Op.or]: [
               {
                 serverId: null
               },
               {
                 serverId: {
-                  [ Sequelize.Op.in ]: Sequelize.literal(inQueryInstanceFollow)
+                  [ Op.in ]: Sequelize.literal(inQueryInstanceFollow)
                 }
               }
             ]
@@ -142,22 +149,22 @@ type AvailableForListOptions = {
   [ScopeNames.WITH_ACCOUNT]: {
     include: [
       {
-        model: () => AccountModel,
+        model: AccountModel,
         required: true
       }
     ]
   },
   [ScopeNames.WITH_VIDEOS]: {
     include: [
-      () => VideoModel
+      VideoModel
     ]
   },
   [ScopeNames.WITH_ACTOR]: {
     include: [
-      () => ActorModel
+      ActorModel
     ]
   }
-})
+}))
 @Table({
   tableName: 'videoChannel',
   indexes
@@ -171,13 +178,13 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
 
   @AllowNull(true)
   @Default(null)
-  @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description'))
+  @Is('VideoChannelDescription', value => throwIfNotValid(value, isVideoChannelDescriptionValid, 'description', true))
   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.DESCRIPTION.max))
   description: string
 
   @AllowNull(true)
   @Default(null)
-  @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support'))
+  @Is('VideoChannelSupport', value => throwIfNotValid(value, isVideoChannelSupportValid, 'support', true))
   @Column(DataType.STRING(CONSTRAINTS_FIELDS.VIDEO_CHANNELS.SUPPORT.max))
   support: string
 
@@ -312,7 +319,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
       limit: options.count,
       order: getSort(options.sort),
       where: {
-        [Sequelize.Op.or]: [
+        [Op.or]: [
           Sequelize.literal(
             'lower(immutable_unaccent("VideoChannelModel"."name")) % lower(immutable_unaccent(' + escapedSearch + '))'
           ),
@@ -334,14 +341,21 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
       })
   }
 
-  static listByAccount (accountId: number) {
+  static listByAccount (options: {
+    accountId: number,
+    start: number,
+    count: number,
+    sort: string
+  }) {
     const query = {
-      order: getSort('createdAt'),
+      offset: options.start,
+      limit: options.count,
+      order: getSort(options.sort),
       include: [
         {
           model: AccountModel,
           where: {
-            id: accountId
+            id: options.accountId
           },
           required: true
         }
@@ -380,24 +394,6 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
       .findByPk(id)
   }
 
-  static loadByUUIDAndPopulateAccount (uuid: string) {
-    const query = {
-      include: [
-        {
-          model: ActorModel,
-          required: true,
-          where: {
-            uuid
-          }
-        }
-      ]
-    }
-
-    return VideoChannelModel
-      .scope([ ScopeNames.WITH_ACCOUNT ])
-      .findOne(query)
-  }
-
   static loadByUrlAndPopulateAccount (url: string) {
     const query = {
       include: [
@@ -419,7 +415,7 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
   static loadByNameWithHostAndPopulateAccount (nameWithHost: string) {
     const [ name, host ] = nameWithHost.split('@')
 
-    if (!host || host === CONFIG.WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
+    if (!host || host === WEBSERVER.HOST) return VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
 
     return VideoChannelModel.loadByNameAndHostAndPopulateAccount(name, host)
   }
@@ -503,7 +499,6 @@ export class VideoChannelModel extends Model<VideoChannelModel> {
 
     return {
       id: this.id,
-      uuid: actor.uuid,
       name: actor.name,
       displayName: this.getDisplayName(),
       url: actor.url,