aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/video
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2020-03-19 10:54:02 +0100
committerChocobozzz <me@florianbigard.com>2020-03-19 11:04:05 +0100
commit891a81966148b8c973e9804b8b7952a95f3a6fd6 (patch)
tree40067e884eb4b00405d44f61dc58beeb7017afc8 /server/models/video
parent34450e1e559d8a6e747dc98d9a203d42db6504ec (diff)
downloadPeerTube-891a81966148b8c973e9804b8b7952a95f3a6fd6.tar.gz
PeerTube-891a81966148b8c973e9804b8b7952a95f3a6fd6.tar.zst
PeerTube-891a81966148b8c973e9804b8b7952a95f3a6fd6.zip
Optimize sql requests on broadcast
Diffstat (limited to 'server/models/video')
-rw-r--r--server/models/video/video-share.ts89
1 files changed, 36 insertions, 53 deletions
diff --git a/server/models/video/video-share.ts b/server/models/video/video-share.ts
index 50525b4c2..4bbef75e6 100644
--- a/server/models/video/video-share.ts
+++ b/server/models/video/video-share.ts
@@ -2,12 +2,10 @@ import * as Bluebird from 'bluebird'
2import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript' 2import { AllowNull, BelongsTo, Column, CreatedAt, DataType, ForeignKey, Is, Model, Scopes, Table, UpdatedAt } from 'sequelize-typescript'
3import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc' 3import { isActivityPubUrlValid } from '../../helpers/custom-validators/activitypub/misc'
4import { CONSTRAINTS_FIELDS } from '../../initializers/constants' 4import { CONSTRAINTS_FIELDS } from '../../initializers/constants'
5import { AccountModel } from '../account/account'
6import { ActorModel } from '../activitypub/actor' 5import { ActorModel } from '../activitypub/actor'
7import { buildLocalActorIdsIn, throwIfNotValid } from '../utils' 6import { buildLocalActorIdsIn, throwIfNotValid } from '../utils'
8import { VideoModel } from './video' 7import { VideoModel } from './video'
9import { VideoChannelModel } from './video-channel' 8import { literal, Op, Transaction } from 'sequelize'
10import { Op, Transaction } from 'sequelize'
11import { MVideoShareActor, MVideoShareFull } from '../../typings/models/video' 9import { MVideoShareActor, MVideoShareFull } from '../../typings/models/video'
12import { MActorDefault } from '../../typings/models' 10import { MActorDefault } from '../../typings/models'
13 11
@@ -124,70 +122,55 @@ export class VideoShareModel extends Model<VideoShareModel> {
124 } 122 }
125 123
126 return VideoShareModel.scope(ScopeNames.FULL).findAll(query) 124 return VideoShareModel.scope(ScopeNames.FULL).findAll(query)
127 .then((res: MVideoShareFull[]) => res.map(r => r.Actor)) 125 .then((res: MVideoShareFull[]) => res.map(r => r.Actor))
128 } 126 }
129 127
130 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird<MActorDefault[]> { 128 static loadActorsWhoSharedVideosOf (actorOwnerId: number, t: Transaction): Bluebird<MActorDefault[]> {
129 const safeOwnerId = parseInt(actorOwnerId + '', 10)
130
131 // /!\ On actor model
131 const query = { 132 const query = {
132 attributes: [], 133 where: {
133 include: [ 134 [Op.and]: [
134 { 135 literal(
135 model: ActorModel, 136 `EXISTS (` +
136 required: true 137 ` SELECT 1 FROM "videoShare" ` +
137 }, 138 ` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
138 { 139 ` INNER JOIN "videoChannel" ON "videoChannel"."id" = "video"."channelId" ` +
139 attributes: [], 140 ` INNER JOIN "account" ON "account"."id" = "videoChannel"."accountId" ` +
140 model: VideoModel, 141 ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "account"."actorId" = ${safeOwnerId} ` +
141 required: true, 142 ` LIMIT 1` +
142 include: [ 143 `)`
143 { 144 )
144 attributes: [], 145 ]
145 model: VideoChannelModel.unscoped(), 146 },
146 required: true,
147 include: [
148 {
149 attributes: [],
150 model: AccountModel.unscoped(),
151 required: true,
152 where: {
153 actorId: actorOwnerId
154 }
155 }
156 ]
157 }
158 ]
159 }
160 ],
161 transaction: t 147 transaction: t
162 } 148 }
163 149
164 return VideoShareModel.scope(ScopeNames.FULL).findAll(query) 150 return ActorModel.findAll(query)
165 .then(res => res.map(r => r.Actor))
166 } 151 }
167 152
168 static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird<MActorDefault[]> { 153 static loadActorsByVideoChannel (videoChannelId: number, t: Transaction): Bluebird<MActorDefault[]> {
154 const safeChannelId = parseInt(videoChannelId + '', 10)
155
156 // /!\ On actor model
169 const query = { 157 const query = {
170 attributes: [], 158 where: {
171 include: [ 159 [Op.and]: [
172 { 160 literal(
173 model: ActorModel, 161 `EXISTS (` +
174 required: true 162 ` SELECT 1 FROM "videoShare" ` +
175 }, 163 ` INNER JOIN "video" ON "videoShare"."videoId" = "video"."id" ` +
176 { 164 ` WHERE "videoShare"."actorId" = "ActorModel"."id" AND "video"."channelId" = ${safeChannelId} ` +
177 attributes: [], 165 ` LIMIT 1` +
178 model: VideoModel, 166 `)`
179 required: true, 167 )
180 where: { 168 ]
181 channelId: videoChannelId 169 },
182 }
183 }
184 ],
185 transaction: t 170 transaction: t
186 } 171 }
187 172
188 return VideoShareModel.scope(ScopeNames.FULL) 173 return ActorModel.findAll(query)
189 .findAll(query)
190 .then(res => res.map(r => r.Actor))
191 } 174 }
192 175
193 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) { 176 static listAndCountByVideoId (videoId: number, start: number, count: number, t?: Transaction) {