1 import { getAPId } from '@server/helpers/activitypub'
2 import { retryTransactionWrapper } from '@server/helpers/database-utils'
3 import { JobQueue } from '@server/lib/job-queue'
4 import { loadVideoByUrl, VideoLoadByUrlType } from '@server/lib/model-loaders'
5 import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models'
6 import { APObject } from '@shared/models'
7 import { refreshVideoIfNeeded } from './refresh'
8 import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
10 type GetVideoResult <T> = Promise<{
13 autoBlacklisted?: boolean
16 type GetVideoParamAll = {
20 allowRefresh?: boolean
23 type GetVideoParamImmutable = {
26 fetchType: 'only-immutable-attributes'
30 type GetVideoParamOther = {
33 fetchType?: 'all' | 'only-video'
34 allowRefresh?: boolean
37 function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
38 function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
39 function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
41 async function getOrCreateAPVideo (
42 options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther
43 ): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
45 const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false }
46 const fetchType = options.fetchType || 'all'
47 const allowRefresh = options.allowRefresh !== false
50 const videoUrl = getAPId(options.videoObject)
51 let videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
53 if (videoFromDatabase) {
54 if (allowRefresh === true) {
55 // Typings ensure allowRefresh === false in only-immutable-attributes fetch type
56 videoFromDatabase = await scheduleRefresh(videoFromDatabase as MVideoThumbnail, fetchType, syncParam)
59 return { video: videoFromDatabase, created: false }
62 const { videoObject } = await fetchRemoteVideo(videoUrl)
63 if (!videoObject) throw new Error('Cannot fetch remote video with url: ' + videoUrl)
65 // videoUrl is just an alias/rediraction, so process object id instead
66 if (videoObject.id !== videoUrl) return getOrCreateAPVideo({ ...options, fetchType: 'all', videoObject })
69 const creator = new APVideoCreator(videoObject)
70 const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(creator.create.bind(creator), syncParam.thumbnail)
72 await syncVideoExternalAttributes(videoCreated, videoObject, syncParam)
74 return { video: videoCreated, created: true, autoBlacklisted }
76 // Maybe a concurrent getOrCreateAPVideo call created this video
77 if (err.name === 'SequelizeUniqueConstraintError') {
78 const alreadyCreatedVideo = await loadVideoByUrl(videoUrl, fetchType)
79 if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false }
86 // ---------------------------------------------------------------------------
92 // ---------------------------------------------------------------------------
94 async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoLoadByUrlType, syncParam: SyncParam) {
95 if (!video.isOutdated()) return video
97 const refreshOptions = {
99 fetchedType: fetchType,
103 if (syncParam.refreshVideo === true) {
104 return refreshVideoIfNeeded(refreshOptions)
107 await JobQueue.Instance.createJobWithPromise({
108 type: 'activitypub-refresher',
109 payload: { type: 'video', url: video.url }