From 1ba471c55fdbf05ef2c10152b3e4b0c5d8da3213 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 16 Jun 2020 14:13:01 +0200 Subject: Add videos count in channels list --- server/models/video/video-channel.ts | 32 ++++++++++++++++++++++---------- server/models/video/video.ts | 2 +- 2 files changed, 23 insertions(+), 11 deletions(-) (limited to 'server/models') diff --git a/server/models/video/video-channel.ts b/server/models/video/video-channel.ts index b5bcbdc65..a4231b6b3 100644 --- a/server/models/video/video-channel.ts +++ b/server/models/video/video-channel.ts @@ -172,6 +172,10 @@ export type SummaryOptions = { return { attributes: { include: [ + [ + literal('(SELECT COUNT(*) FROM "video" WHERE "channelId" = "VideoChannelModel"."id")'), + 'videosCount' + ], [ literal( '(' + @@ -544,7 +548,22 @@ export class VideoChannelModel extends Model { } toFormattedJSON (this: MChannelFormattable): VideoChannel { - const viewsPerDay = this.get('viewsPerDay') as string + const viewsPerDayString = this.get('viewsPerDay') as string + const videosCount = this.get('videosCount') as number + + let viewsPerDay: { date: Date, views: number }[] + + if (viewsPerDayString) { + viewsPerDay = viewsPerDayString.split(',') + .map(v => { + const [ dateString, amount ] = v.split('|') + + return { + date: new Date(dateString), + views: +amount + } + }) + } const actor = this.Actor.toFormattedJSON() const videoChannel = { @@ -556,15 +575,8 @@ export class VideoChannelModel extends Model { createdAt: this.createdAt, updatedAt: this.updatedAt, ownerAccount: undefined, - viewsPerDay: viewsPerDay - ? viewsPerDay.split(',').map(v => { - const o = v.split('|') - return { - date: new Date(o[0]), - views: +o[1] - } - }) - : undefined + videosCount, + viewsPerDay } if (this.Account) videoChannel.ownerAccount = this.Account.toFormattedJSON() diff --git a/server/models/video/video.ts b/server/models/video/video.ts index ae2483b2f..1f590c02d 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -500,7 +500,7 @@ export class VideoModel extends Model { @AllowNull(false) @Is('VideoPrivacy', value => throwIfNotValid(value, isVideoPrivacyValid, 'privacy')) @Column - privacy: number + privacy: VideoPrivacy @AllowNull(false) @Is('VideoNSFW', value => throwIfNotValid(value, isBooleanValid, 'NSFW boolean')) -- cgit v1.2.3