]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/videos/shared/creator.ts
Fix reset sequelize instance
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / shared / creator.ts
CommitLineData
08a47c75 1
908e6ead 2import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger'
08a47c75 3import { sequelizeTypescript } from '@server/initializers/database'
c3441b03 4import { Hooks } from '@server/lib/plugins/hooks'
08a47c75
C
5import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist'
6import { VideoModel } from '@server/models/video/video'
c56faf0d 7import { MThumbnail, MVideoFullLight, MVideoThumbnail } from '@server/types/models'
08a47c75
C
8import { VideoObject } from '@shared/models'
9import { APVideoAbstractBuilder } from './abstract-builder'
10import { getVideoAttributesFromObject } from './object-to-model-attributes'
11
12export class APVideoCreator extends APVideoAbstractBuilder {
908e6ead 13 protected lTags: LoggerTagsFn
08a47c75 14
c56faf0d 15 constructor (protected readonly videoObject: VideoObject) {
08a47c75 16 super()
908e6ead
C
17
18 this.lTags = loggerTagsFactory('ap', 'video', 'create', this.videoObject.uuid, this.videoObject.id)
08a47c75
C
19 }
20
21 async create (waitThumbnail = false) {
908e6ead 22 logger.debug('Adding remote video %s.', this.videoObject.id, this.lTags())
08a47c75 23
c56faf0d
C
24 const channelActor = await this.getOrCreateVideoChannelFromVideoObject()
25 const channel = channelActor.VideoChannel
26
e54bd458 27 const videoData = getVideoAttributesFromObject(channel, this.videoObject, this.videoObject.to)
b89b0bfc 28 const video = VideoModel.build({ ...videoData, likes: 0, dislikes: 0 }) as MVideoThumbnail
08a47c75
C
29
30 const promiseThumbnail = this.tryToGenerateThumbnail(video)
31
32 let thumbnailModel: MThumbnail
33 if (waitThumbnail === true) {
34 thumbnailModel = await promiseThumbnail
35 }
36
37 const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => {
38 try {
39 const videoCreated = await video.save({ transaction: t }) as MVideoFullLight
c56faf0d 40 videoCreated.VideoChannel = channel
08a47c75
C
41
42 if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t)
43
44 await this.setPreview(videoCreated, t)
45 await this.setWebTorrentFiles(videoCreated, t)
46 await this.setStreamingPlaylists(videoCreated, t)
47 await this.setTags(videoCreated, t)
48 await this.setTrackers(videoCreated, t)
49 await this.insertOrReplaceCaptions(videoCreated, t)
50 await this.insertOrReplaceLive(videoCreated, t)
51
52 // We added a video in this channel, set it as updated
c56faf0d 53 await channel.setAsUpdated(t)
08a47c75
C
54
55 const autoBlacklisted = await autoBlacklistVideoIfNeeded({
56 video: videoCreated,
57 user: undefined,
58 isRemote: true,
59 isNew: true,
60 transaction: t
61 })
62
908e6ead 63 logger.info('Remote video with uuid %s inserted.', this.videoObject.uuid, this.lTags())
08a47c75 64
c3441b03
C
65 Hooks.runAction('action:activity-pub.remote-video.created', { video: videoCreated, videoAPObject: this.videoObject })
66
08a47c75
C
67 return { autoBlacklisted, videoCreated }
68 } catch (err) {
69 // FIXME: Use rollback hook when https://github.com/sequelize/sequelize/pull/13038 is released
08a47c75
C
70 if (thumbnailModel) await thumbnailModel.removeThumbnail()
71
72 throw err
73 }
74 })
75
76 if (waitThumbnail === false) {
77 // Error is already caught above
78 // eslint-disable-next-line @typescript-eslint/no-floating-promises
79 promiseThumbnail.then(thumbnailModel => {
80 if (!thumbnailModel) return
81
82 thumbnailModel = videoCreated.id
83
84 return thumbnailModel.save()
85 })
86 }
87
88 return { autoBlacklisted, videoCreated }
89 }
90}