From c46edbc2f6ca310b2f0331f979ac6caf27f6eb92 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 22 Nov 2017 11:27:40 +0100 Subject: Fetch outbox to grab old activities tests --- server/models/video/video.ts | 38 ++++++++++++++++++++++++++++---------- 1 file changed, 28 insertions(+), 10 deletions(-) (limited to 'server/models/video') diff --git a/server/models/video/video.ts b/server/models/video/video.ts index 3b7e83779..9b411a92e 100644 --- a/server/models/video/video.ts +++ b/server/models/video/video.ts @@ -46,6 +46,7 @@ import { TagInstance } from './tag-interface' import { VideoFileInstance, VideoFileModel } from './video-file-interface' import { VideoAttributes, VideoInstance, VideoMethods } from './video-interface' import { sendDeleteVideo } from '../../lib/index' +import * as Bluebird from 'bluebird' const Buffer = safeBuffer.Buffer @@ -786,14 +787,21 @@ list = function () { } listAllAndSharedByAccountForOutbox = function (accountId: number, start: number, count: number) { - const queryVideo = 'SELECT "Video"."id" FROM "Videos" AS "Video" ' + - 'INNER JOIN "VideoChannels" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + - 'WHERE "VideoChannel"."accountId" = ' + accountId - const queryVideoShare = 'SELECT "Video"."id" FROM "VideoShares" AS "VideoShare" ' + - 'INNER JOIN "Videos" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' + - 'INNER JOIN "VideoChannels" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + - 'WHERE "VideoShare"."accountId" = ' + accountId - const rawQuery = `(${queryVideo}) UNION (${queryVideoShare}) LIMIT ${count} OFFSET ${start}` + function getRawQuery (select: string) { + const queryVideo = 'SELECT ' + select + ' FROM "Videos" AS "Video" ' + + 'INNER JOIN "VideoChannels" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + + 'WHERE "VideoChannel"."accountId" = ' + accountId + const queryVideoShare = 'SELECT ' + select + ' FROM "VideoShares" AS "VideoShare" ' + + 'INNER JOIN "Videos" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' + + 'WHERE "VideoShare"."accountId" = ' + accountId + + let rawQuery = `(${queryVideo}) UNION (${queryVideoShare})` + + return rawQuery + } + + const rawQuery = getRawQuery('"Video"."id"') + const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"') const query = { distinct: true, @@ -825,10 +833,20 @@ listAllAndSharedByAccountForOutbox = function (accountId: number, start: number, ] } - return Video.findAndCountAll(query).then(({ rows, count }) => { + return Bluebird.all([ + Video.findAll(query), + Video['sequelize'].query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT }) + ]).then(([ rows, totals ]) => { + // totals: totalVideos + totalVideoShares + let totalVideos = 0 + let totalVideoShares = 0 + if (totals[0]) totalVideos = parseInt(totals[0].total, 10) + if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10) + + const total = totalVideos + totalVideoShares return { data: rows, - total: count + total: total } }) } -- cgit v1.2.3