From 43fc899a101c0fd266cc294dc573b256168bef18 Mon Sep 17 00:00:00 2001
From: Chocobozzz <me@florianbigard.com>
Date: Mon, 21 Mar 2022 09:26:48 +0100
Subject: [PATCH] Fix channels list count

---
 server/models/video/video-channel.ts      | 26 +++++++++++------------
 server/tests/api/videos/video-channels.ts | 18 ++++++++++++++++
 2 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts
index 410fd6d3f..00ae58c68 100644
--- a/server/models/video/video-channel.ts
+++ b/server/models/video/video-channel.ts
@@ -143,28 +143,28 @@ export type SummaryOptions = {
       })
     }
 
-    const channelInclude: Includeable[] = []
-    const accountInclude: Includeable[] = []
+    const channelActorInclude: Includeable[] = []
+    const accountActorInclude: Includeable[] = []
 
     if (options.forCount !== true) {
-      accountInclude.push({
+      accountActorInclude.push({
         model: ServerModel,
         required: false
       })
 
-      accountInclude.push({
+      accountActorInclude.push({
         model: ActorImageModel,
         as: 'Avatars',
         required: false
       })
 
-      channelInclude.push({
+      channelActorInclude.push({
         model: ActorImageModel,
         as: 'Avatars',
         required: false
       })
 
-      channelInclude.push({
+      channelActorInclude.push({
         model: ActorImageModel,
         as: 'Banners',
         required: false
@@ -172,7 +172,7 @@ export type SummaryOptions = {
     }
 
     if (options.forCount !== true || serverRequired) {
-      channelInclude.push({
+      channelActorInclude.push({
         model: ServerModel,
         duplicating: false,
         required: serverRequired,
@@ -190,7 +190,7 @@ export type SummaryOptions = {
           where: {
             [Op.and]: whereActorAnd
           },
-          include: channelInclude
+          include: channelActorInclude
         },
         {
           model: AccountModel.unscoped(),
@@ -202,7 +202,7 @@ export type SummaryOptions = {
               },
               model: ActorModel.unscoped(),
               required: true,
-              include: accountInclude
+              include: accountActorInclude
             }
           ]
         }
@@ -605,17 +605,17 @@ ON              "Account->Actor"."serverId" = "Account->Actor->Server"."id"`
       }
     }
 
-    const scopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
+    const findScopes: string | ScopeOptions | (string | ScopeOptions)[] = [ ScopeNames.WITH_ACTOR_BANNER ]
 
     if (options.withStats === true) {
-      scopes.push({
+      findScopes.push({
         method: [ ScopeNames.WITH_STATS, { daysPrior: 30 } as AvailableWithStatsOptions ]
       })
     }
 
     return Promise.all([
-      VideoChannelModel.scope(scopes).count(getQuery(true)),
-      VideoChannelModel.scope(scopes).findAll(getQuery(false))
+      VideoChannelModel.unscoped().count(getQuery(true)),
+      VideoChannelModel.scope(findScopes).findAll(getQuery(false))
     ]).then(([ total, data ]) => ({ total, data }))
   }
 
diff --git a/server/tests/api/videos/video-channels.ts b/server/tests/api/videos/video-channels.ts
index 0f8227fd3..09a4bfa70 100644
--- a/server/tests/api/videos/video-channels.ts
+++ b/server/tests/api/videos/video-channels.ts
@@ -325,6 +325,24 @@ describe('Test video channels', function () {
     }
   })
 
+  it('Should still correctly list channels', async function () {
+    {
+      const body = await servers[0].channels.list({ start: 1, count: 1, sort: 'createdAt' })
+
+      expect(body.total).to.equal(3)
+      expect(body.data).to.have.lengthOf(1)
+      expect(body.data[0].name).to.equal('second_video_channel')
+    }
+
+    {
+      const body = await servers[0].channels.listByAccount({ accountName, start: 1, count: 1, sort: 'createdAt' })
+
+      expect(body.total).to.equal(2)
+      expect(body.data).to.have.lengthOf(1)
+      expect(body.data[0].name).to.equal('second_video_channel')
+    }
+  })
+
   it('Should delete the video channel avatar', async function () {
     this.timeout(15000)
     await servers[0].channels.deleteImage({ channelName: 'second_video_channel', type: 'avatar' })