aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video/video.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/video/video.ts')
-rw-r--r--server/models/video/video.ts38
1 files changed, 28 insertions, 10 deletions
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'
46import { VideoFileInstance, VideoFileModel } from './video-file-interface' 46import { VideoFileInstance, VideoFileModel } from './video-file-interface'
47import { VideoAttributes, VideoInstance, VideoMethods } from './video-interface' 47import { VideoAttributes, VideoInstance, VideoMethods } from './video-interface'
48import { sendDeleteVideo } from '../../lib/index' 48import { sendDeleteVideo } from '../../lib/index'
49import * as Bluebird from 'bluebird'
49 50
50const Buffer = safeBuffer.Buffer 51const Buffer = safeBuffer.Buffer
51 52
@@ -786,14 +787,21 @@ list = function () {
786} 787}
787 788
788listAllAndSharedByAccountForOutbox = function (accountId: number, start: number, count: number) { 789listAllAndSharedByAccountForOutbox = function (accountId: number, start: number, count: number) {
789 const queryVideo = 'SELECT "Video"."id" FROM "Videos" AS "Video" ' + 790 function getRawQuery (select: string) {
790 'INNER JOIN "VideoChannels" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + 791 const queryVideo = 'SELECT ' + select + ' FROM "Videos" AS "Video" ' +
791 'WHERE "VideoChannel"."accountId" = ' + accountId 792 'INNER JOIN "VideoChannels" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' +
792 const queryVideoShare = 'SELECT "Video"."id" FROM "VideoShares" AS "VideoShare" ' + 793 'WHERE "VideoChannel"."accountId" = ' + accountId
793 'INNER JOIN "Videos" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' + 794 const queryVideoShare = 'SELECT ' + select + ' FROM "VideoShares" AS "VideoShare" ' +
794 'INNER JOIN "VideoChannels" AS "VideoChannel" ON "VideoChannel"."id" = "Video"."channelId" ' + 795 'INNER JOIN "Videos" AS "Video" ON "Video"."id" = "VideoShare"."videoId" ' +
795 'WHERE "VideoShare"."accountId" = ' + accountId 796 'WHERE "VideoShare"."accountId" = ' + accountId
796 const rawQuery = `(${queryVideo}) UNION (${queryVideoShare}) LIMIT ${count} OFFSET ${start}` 797
798 let rawQuery = `(${queryVideo}) UNION (${queryVideoShare})`
799
800 return rawQuery
801 }
802
803 const rawQuery = getRawQuery('"Video"."id"')
804 const rawCountQuery = getRawQuery('COUNT("Video"."id") as "total"')
797 805
798 const query = { 806 const query = {
799 distinct: true, 807 distinct: true,
@@ -825,10 +833,20 @@ listAllAndSharedByAccountForOutbox = function (accountId: number, start: number,
825 ] 833 ]
826 } 834 }
827 835
828 return Video.findAndCountAll(query).then(({ rows, count }) => { 836 return Bluebird.all([
837 Video.findAll(query),
838 Video['sequelize'].query(rawCountQuery, { type: Sequelize.QueryTypes.SELECT })
839 ]).then(([ rows, totals ]) => {
840 // totals: totalVideos + totalVideoShares
841 let totalVideos = 0
842 let totalVideoShares = 0
843 if (totals[0]) totalVideos = parseInt(totals[0].total, 10)
844 if (totals[1]) totalVideoShares = parseInt(totals[1].total, 10)
845
846 const total = totalVideos + totalVideoShares
829 return { 847 return {
830 data: rows, 848 data: rows,
831 total: count 849 total: total
832 } 850 }
833 }) 851 })
834} 852}