aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-01-18 15:22:36 +0100
committerChocobozzz <me@florianbigard.com>2018-01-18 16:43:26 +0100
commit2c897999fe877c52c8f7458d8bbff6c9367b6587 (patch)
tree3f66304fcea68a608bfd0f800e991731d910cc3d
parent54e740594bc2eacd8026b5d2d6cfdfc06416a65b (diff)
downloadPeerTube-2c897999fe877c52c8f7458d8bbff6c9367b6587.tar.gz
PeerTube-2c897999fe877c52c8f7458d8bbff6c9367b6587.tar.zst
PeerTube-2c897999fe877c52c8f7458d8bbff6c9367b6587.zip
Optimize SQL query that fetch actor outbox
-rw-r--r--server/lib/activitypub/actor.ts42
-rw-r--r--server/models/video/video.ts54
-rw-r--r--server/tests/api/server/follows.ts4
-rw-r--r--server/tests/api/server/handle-down.ts4
4 files changed, 77 insertions, 27 deletions
diff --git a/server/lib/activitypub/actor.ts b/server/lib/activitypub/actor.ts
index a39b4e137..7494aadbb 100644
--- a/server/lib/activitypub/actor.ts
+++ b/server/lib/activitypub/actor.ts
@@ -212,7 +212,13 @@ function saveActorAndServerAndModelIfNotExist (
212 212
213 // Force the actor creation, sometimes Sequelize skips the save() when it thinks the instance already exists 213 // Force the actor creation, sometimes Sequelize skips the save() when it thinks the instance already exists
214 // (which could be false in a retried query) 214 // (which could be false in a retried query)
215 const actorCreated = await ActorModel.create(actor.toJSON(), { transaction: t }) 215 const [ actorCreated ] = await ActorModel.findOrCreate({
216 defaults: actor.toJSON(),
217 where: {
218 url: actor.url
219 },
220 transaction: t
221 })
216 222
217 if (actorCreated.type === 'Person' || actorCreated.type === 'Application') { 223 if (actorCreated.type === 'Person' || actorCreated.type === 'Application') {
218 const account = await saveAccount(actorCreated, result, t) 224 const account = await saveAccount(actorCreated, result, t)
@@ -284,24 +290,36 @@ async function fetchRemoteActor (actorUrl: string): Promise<FetchRemoteActorResu
284 } 290 }
285} 291}
286 292
287function saveAccount (actor: ActorModel, result: FetchRemoteActorResult, t: Transaction) { 293async function saveAccount (actor: ActorModel, result: FetchRemoteActorResult, t: Transaction) {
288 const account = new AccountModel({ 294 const [ accountCreated ] = await AccountModel.findOrCreate({
289 name: result.name, 295 defaults: {
290 actorId: actor.id 296 name: result.name,
297 actorId: actor.id
298 },
299 where: {
300 actorId: actor.id
301 },
302 transaction: t
291 }) 303 })
292 304
293 return account.save({ transaction: t }) 305 return accountCreated
294} 306}
295 307
296async function saveVideoChannel (actor: ActorModel, result: FetchRemoteActorResult, ownerActor: ActorModel, t: Transaction) { 308async function saveVideoChannel (actor: ActorModel, result: FetchRemoteActorResult, ownerActor: ActorModel, t: Transaction) {
297 const videoChannel = new VideoChannelModel({ 309 const [ videoChannelCreated ] = await VideoChannelModel.findOrCreate({
298 name: result.name, 310 defaults: {
299 description: result.summary, 311 name: result.name,
300 actorId: actor.id, 312 description: result.summary,
301 accountId: ownerActor.Account.id 313 actorId: actor.id,
314 accountId: ownerActor.Account.id
315 },
316 where: {
317 actorId: actor.id
318 },
319 transaction: t
302 }) 320 })
303 321
304 return videoChannel.save({ transaction: t }) 322 return videoChannelCreated
305} 323}
306 324
307async function refreshActorIfNeeded (actor: ActorModel) { 325async function refreshActorIfNeeded (actor: ActorModel) {
diff --git a/server/models/video/video.ts b/server/models/video/video.ts
index 0d115367f..7af68b20b 100644
--- a/server/models/video/video.ts
+++ b/server/models/video/video.ts
@@ -459,7 +459,8 @@ export class VideoModel extends Model<VideoModel> {
459 }, 459 },
460 include: [ 460 include: [
461 { 461 {
462 model: VideoShareModel, 462 attributes: [ 'id' ],
463 model: VideoShareModel.unscoped(),
463 required: false, 464 required: false,
464 where: { 465 where: {
465 [Sequelize.Op.and]: [ 466 [Sequelize.Op.and]: [
@@ -475,28 +476,65 @@ export class VideoModel extends Model<VideoModel> {
475 }, 476 },
476 include: [ 477 include: [
477 { 478 {
478 model: ActorModel, 479 attributes: [ 'id', 'url' ],
479 required: true 480 model: ActorModel.unscoped()
480 } 481 }
481 ] 482 ]
482 }, 483 },
483 { 484 {
484 model: VideoChannelModel, 485 model: VideoChannelModel.unscoped(),
485 required: true, 486 required: true,
486 include: [ 487 include: [
487 { 488 {
488 model: AccountModel, 489 attributes: [ 'name' ],
490 model: AccountModel.unscoped(),
491 required: true,
492 include: [
493 {
494 attributes: [ 'id', 'url' ],
495 model: ActorModel.unscoped(),
496 required: true
497 }
498 ]
499 },
500 {
501 attributes: [ 'id', 'url' ],
502 model: ActorModel.unscoped(),
489 required: true 503 required: true
490 } 504 }
491 ] 505 ]
492 }, 506 },
493 { 507 {
508 attributes: [ 'type' ],
494 model: AccountVideoRateModel, 509 model: AccountVideoRateModel,
495 include: [ AccountModel ] 510 required: false,
511 include: [
512 {
513 attributes: [ 'id' ],
514 model: AccountModel.unscoped(),
515 include: [
516 {
517 attributes: [ 'url' ],
518 model: ActorModel.unscoped(),
519 include: [
520 {
521 attributes: [ 'host' ],
522 model: ServerModel,
523 required: false
524 }
525 ]
526 }
527 ]
528 }
529 ]
530 },
531 {
532 attributes: [ 'url' ],
533 model: VideoCommentModel,
534 required: false
496 }, 535 },
497 VideoFileModel, 536 VideoFileModel,
498 TagModel, 537 TagModel
499 VideoCommentModel
500 ] 538 ]
501 } 539 }
502 540
diff --git a/server/tests/api/server/follows.ts b/server/tests/api/server/follows.ts
index c0115e534..b26af1a16 100644
--- a/server/tests/api/server/follows.ts
+++ b/server/tests/api/server/follows.ts
@@ -4,7 +4,7 @@ import * as chai from 'chai'
4import 'mocha' 4import 'mocha'
5import { Video, VideoPrivacy } from '../../../../shared/models/videos' 5import { Video, VideoPrivacy } from '../../../../shared/models/videos'
6import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model' 6import { VideoComment, VideoCommentThreadTree } from '../../../../shared/models/videos/video-comment.model'
7import { checkVideoFilesWereRemoved, completeVideoCheck } from '../../utils' 7import { completeVideoCheck } from '../../utils'
8 8
9import { 9import {
10 flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo, 10 flushAndRunMultipleServers, flushTests, getVideosList, killallServers, ServerInfo, setAccessTokensToServers, uploadVideo,
@@ -353,8 +353,6 @@ describe('Test follows', function () {
353 353
354 let res = await getVideosList(servers[ 0 ].url) 354 let res = await getVideosList(servers[ 0 ].url)
355 expect(res.body.total).to.equal(1) 355 expect(res.body.total).to.equal(1)
356
357 await checkVideoFilesWereRemoved(video4.uuid, servers[0].serverNumber)
358 }) 356 })
359 357
360 }) 358 })
diff --git a/server/tests/api/server/handle-down.ts b/server/tests/api/server/handle-down.ts
index 6ca8cfb64..e99e517e4 100644
--- a/server/tests/api/server/handle-down.ts
+++ b/server/tests/api/server/handle-down.ts
@@ -187,10 +187,6 @@ describe('Test handle downs', function () {
187 187
188 await wait(5000) 188 await wait(5000)
189 189
190 const res = await getVideosList(servers[1].url)
191 expect(res.body.data).to.be.an('array')
192 expect(res.body.data).to.have.lengthOf(2)
193
194 const resVideo = await getVideo(servers[1].url, videos[0].uuid) 190 const resVideo = await getVideo(servers[1].url, videos[0].uuid)
195 expect(resVideo.body).not.to.be.undefined 191 expect(resVideo.body).not.to.be.undefined
196 192