From c6f8ca4d6596572de981162983bd02eb2613791d Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Tue, 31 May 2022 16:01:11 +0200 Subject: Added "total views" in the my channels list (#5007) * Added "total views" in the my channels list Implements https://github.com/Chocobozzz/PeerTube/issues/4331 * Fix lint * applied suggested change * updated openAPI spec for the use "withStats" when getting video channels * applied code change * removed GROUP BY in query * Fixed test --- server/models/video/video-channel.ts | 13 +++++++++++++ server/tests/api/videos/video-channels.ts | 19 +++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'server') diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index d6dd1b8bb..91dafbcf1 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -311,6 +311,16 @@ export type SummaryOptions = { ')' ), 'viewsPerDay' + ], + [ + literal( + '(' + + 'SELECT COALESCE(SUM("video".views), 0) AS totalViews ' + + 'FROM "video" ' + + 'WHERE "video"."channelId" = "VideoChannelModel"."id"' + + ')' + ), + 'totalViews' ] ] } @@ -766,6 +776,8 @@ ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"` }) } + const totalViews = this.get('totalViews') as number + const actor = this.Actor.toFormattedJSON() const videoChannel = { id: this.id, @@ -779,6 +791,7 @@ ON "Account->Actor"."serverId" = "Account->Actor->Server"."id"` videosCount, viewsPerDay, + totalViews, avatars: actor.avatars, diff --git a/server/tests/api/videos/video-channels.ts b/server/tests/api/videos/video-channels.ts index 6f495c42d..42e0cf431 100644 --- a/server/tests/api/videos/video-channels.ts +++ b/server/tests/api/videos/video-channels.ts @@ -478,6 +478,25 @@ describe('Test video channels', function () { } }) + it('Should report correct total views count', async function () { + // check if there's the property + { + const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true }) + + for (const channel of data) { + expect(channel).to.haveOwnProperty('totalViews') + expect(channel.totalViews).to.be.a('number') + } + } + + // Check if the totalViews count can be updated + { + const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true }) + const channelWithView = data.find(channel => channel.id === servers[0].store.channel.id) + expect(channelWithView.totalViews).to.equal(2) + } + }) + it('Should report correct videos count', async function () { const { data } = await servers[0].channels.listByAccount({ accountName, withStats: true }) -- cgit v1.2.3