PeerTube/server/lib/activitypub/videos/shared/creator.ts

66 lines
2.5 KiB
TypeScript
Raw Normal View History

2021-06-02 10:41:46 +02:00
2021-06-03 16:56:42 +02:00
import { logger, loggerTagsFactory, LoggerTagsFn } from '@server/helpers/logger'
2021-06-02 10:41:46 +02:00
import { sequelizeTypescript } from '@server/initializers/database'
2023-03-16 10:36:33 +01:00
import { Hooks } from '@server/lib/plugins/hooks'
2021-06-02 10:41:46 +02:00
import { autoBlacklistVideoIfNeeded } from '@server/lib/video-blacklist'
import { VideoModel } from '@server/models/video/video'
import { MVideoFullLight, MVideoThumbnail } from '@server/types/models'
2021-06-02 10:41:46 +02:00
import { VideoObject } from '@shared/models'
import { APVideoAbstractBuilder } from './abstract-builder'
import { getVideoAttributesFromObject } from './object-to-model-attributes'
export class APVideoCreator extends APVideoAbstractBuilder {
2021-06-03 16:56:42 +02:00
protected lTags: LoggerTagsFn
2021-06-02 10:41:46 +02:00
2021-06-02 11:54:29 +02:00
constructor (protected readonly videoObject: VideoObject) {
2021-06-02 10:41:46 +02:00
super()
2021-06-03 16:56:42 +02:00
this.lTags = loggerTagsFactory('ap', 'video', 'create', this.videoObject.uuid, this.videoObject.id)
2021-06-02 10:41:46 +02:00
}
2023-07-25 14:26:12 +02:00
async create () {
2021-06-03 16:56:42 +02:00
logger.debug('Adding remote video %s.', this.videoObject.id, this.lTags())
2021-06-02 10:41:46 +02:00
2021-06-02 11:54:29 +02:00
const channelActor = await this.getOrCreateVideoChannelFromVideoObject()
const channel = channelActor.VideoChannel
2021-06-15 08:36:39 +02:00
const videoData = getVideoAttributesFromObject(channel, this.videoObject, this.videoObject.to)
const video = VideoModel.build({ ...videoData, likes: 0, dislikes: 0 }) as MVideoThumbnail
2021-06-02 10:41:46 +02:00
const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => {
const videoCreated = await video.save({ transaction: t }) as MVideoFullLight
videoCreated.VideoChannel = channel
await this.setThumbnail(videoCreated, t)
await this.setPreview(videoCreated, t)
await this.setWebVideoFiles(videoCreated, t)
await this.setStreamingPlaylists(videoCreated, t)
await this.setTags(videoCreated, t)
await this.setTrackers(videoCreated, t)
await this.insertOrReplaceCaptions(videoCreated, t)
await this.insertOrReplaceLive(videoCreated, t)
await this.insertOrReplaceStoryboard(videoCreated, t)
// We added a video in this channel, set it as updated
await channel.setAsUpdated(t)
const autoBlacklisted = await autoBlacklistVideoIfNeeded({
video: videoCreated,
user: undefined,
isRemote: true,
isNew: true,
2023-07-19 16:02:49 +02:00
isNewFile: true,
transaction: t
})
2021-06-02 10:41:46 +02:00
logger.info('Remote video with uuid %s inserted.', this.videoObject.uuid, this.lTags())
2023-03-16 10:36:33 +01:00
Hooks.runAction('action:activity-pub.remote-video.created', { video: videoCreated, videoAPObject: this.videoObject })
2021-06-02 10:41:46 +02:00
return { autoBlacklisted, videoCreated }
2021-06-02 10:41:46 +02:00
})
return { autoBlacklisted, videoCreated }
}
}