]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/videos/updater.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / updater.ts
index 6745e2efd031811165121a30e2edd4c1945347ae..f786bb196efc73573e8b263c2493d5455b9e9d74 100644 (file)
@@ -1,11 +1,9 @@
 import { Transaction } from 'sequelize/types'
-import { resetSequelizeInstance } from '@server/helpers/database-utils'
-import { logger, loggerTagsFactory } from '@server/helpers/logger'
-import { sequelizeTypescript } from '@server/initializers/database'
+import { resetSequelizeInstance, runInReadCommittedTransaction } from '@server/helpers/database-utils'
+import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger'
 import { Notifier } from '@server/lib/notifier'
 import { PeerTubeSocket } from '@server/lib/peertube-socket'
 import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist'
-import { VideoCaptionModel } from '@server/models/video/video-caption'
 import { VideoLiveModel } from '@server/models/video/video-live'
 import { MActor, MChannelAccountLight, MChannelId, MVideoAccountLightBlacklistAllFiles, MVideoFullLight } from '@server/types/models'
 import { VideoObject, VideoPrivacy } from '@shared/models'
@@ -19,7 +17,7 @@ export class APVideoUpdater extends APVideoAbstractBuilder {
 
   private readonly oldVideoChannel: MChannelAccountLight
 
-  protected lTags = loggerTagsFactory('ap', 'video', 'update')
+  protected lTags: LoggerTagsFn
 
   constructor (
     protected readonly videoObject: VideoObject,
@@ -33,12 +31,14 @@ export class APVideoUpdater extends APVideoAbstractBuilder {
     this.oldVideoChannel = this.video.VideoChannel
 
     this.videoFieldsSave = this.video.toJSON()
+
+    this.lTags = loggerTagsFactory('ap', 'video', 'update', video.uuid, video.url)
   }
 
   async update (overrideTo?: string[]) {
     logger.debug(
       'Updating remote video "%s".', this.videoObject.uuid,
-      { videoObject: this.videoObject, ...this.lTags(this.videoObject.uuid) }
+      { videoObject: this.videoObject, ...this.lTags() }
     )
 
     try {
@@ -46,24 +46,26 @@ export class APVideoUpdater extends APVideoAbstractBuilder {
 
       const thumbnailModel = await this.tryToGenerateThumbnail(this.video)
 
-      const videoUpdated = await sequelizeTypescript.transaction(async t => {
-        this.checkChannelUpdateOrThrow(channelActor)
+      this.checkChannelUpdateOrThrow(channelActor)
 
-        const videoUpdated = await this.updateVideo(channelActor.VideoChannel, t, overrideTo)
+      const videoUpdated = await this.updateVideo(channelActor.VideoChannel, undefined, overrideTo)
 
-        if (thumbnailModel) await videoUpdated.addAndSaveThumbnail(thumbnailModel, t)
+      if (thumbnailModel) await videoUpdated.addAndSaveThumbnail(thumbnailModel)
 
-        await this.setPreview(videoUpdated, t)
+      await runInReadCommittedTransaction(async t => {
         await this.setWebTorrentFiles(videoUpdated, t)
         await this.setStreamingPlaylists(videoUpdated, t)
-        await this.setTags(videoUpdated, t)
-        await this.setTrackers(videoUpdated, t)
-        await this.setCaptions(videoUpdated, t)
-        await this.setOrDeleteLive(videoUpdated, t)
-
-        return videoUpdated
       })
 
+      await Promise.all([
+        runInReadCommittedTransaction(t => this.setTags(videoUpdated, t)),
+        runInReadCommittedTransaction(t => this.setTrackers(videoUpdated, t)),
+        this.setOrDeleteLive(videoUpdated),
+        this.setPreview(videoUpdated)
+      ])
+
+      await runInReadCommittedTransaction(t => this.setCaptions(videoUpdated, t))
+
       await autoBlacklistVideoIfNeeded({
         video: videoUpdated,
         user: undefined,
@@ -79,10 +81,9 @@ export class APVideoUpdater extends APVideoAbstractBuilder {
 
       if (videoUpdated.isLive) {
         PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated)
-        PeerTubeSocket.Instance.sendVideoViewsUpdate(videoUpdated)
       }
 
-      logger.info('Remote video with uuid %s updated', this.videoObject.uuid, this.lTags(this.videoObject.uuid))
+      logger.info('Remote video with uuid %s updated', this.videoObject.uuid, this.lTags())
 
       return videoUpdated
     } catch (err) {
@@ -101,7 +102,7 @@ export class APVideoUpdater extends APVideoAbstractBuilder {
     }
   }
 
-  private updateVideo (channel: MChannelId, transaction: Transaction, overrideTo?: string[]) {
+  private updateVideo (channel: MChannelId, transaction?: Transaction, overrideTo?: string[]) {
     const to = overrideTo || this.videoObject.to
     const videoData = getVideoAttributesFromObject(channel, this.videoObject, to)
     this.video.name = videoData.name
@@ -133,12 +134,12 @@ export class APVideoUpdater extends APVideoAbstractBuilder {
   }
 
   private async setCaptions (videoUpdated: MVideoFullLight, t: Transaction) {
-    await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(videoUpdated.id, t)
-
     await this.insertOrReplaceCaptions(videoUpdated, t)
   }
 
-  private async setOrDeleteLive (videoUpdated: MVideoFullLight, transaction: Transaction) {
+  private async setOrDeleteLive (videoUpdated: MVideoFullLight, transaction?: Transaction) {
+    if (!this.video.isLive) return
+
     if (this.video.isLive) return this.insertOrReplaceLive(videoUpdated, transaction)
 
     // Delete existing live if it exists
@@ -158,7 +159,7 @@ export class APVideoUpdater extends APVideoAbstractBuilder {
     }
 
     // This is just a debug because we will retry the insert
-    logger.debug('Cannot update the remote video.', { err, ...this.lTags(this.videoObject.uuid) })
+    logger.debug('Cannot update the remote video.', { err, ...this.lTags() })
     throw err
   }
 }