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