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 { refreshVideoIfNeeded } from './refresh'
7 import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
9 type GetVideoResult <T> = Promise<{
12 autoBlacklisted?: boolean
15 type GetVideoParamAll = {
16 videoObject: { id: string } | string
19 allowRefresh?: boolean
22 type GetVideoParamImmutable = {
23 videoObject: { id: string } | string
25 fetchType: 'only-immutable-attributes'
29 type GetVideoParamOther = {
30 videoObject: { id: string } | string
32 fetchType?: 'all' | 'only-video'
33 allowRefresh?: boolean
36 function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
37 function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
38 function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
40 async function getOrCreateAPVideo (
41 options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther
42 ): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
44 const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false }
45 const fetchType = options.fetchType || 'all'
46 const allowRefresh = options.allowRefresh !== false
49 const videoUrl = getAPId(options.videoObject)
50 let videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
52 if (videoFromDatabase) {
53 if (allowRefresh === true) {
54 // Typings ensure allowRefresh === false in only-immutable-attributes fetch type
55 videoFromDatabase = await scheduleRefresh(videoFromDatabase as MVideoThumbnail, fetchType, syncParam)
58 return { video: videoFromDatabase, created: false }
61 const { videoObject } = await fetchRemoteVideo(videoUrl)
62 if (!videoObject) throw new Error('Cannot fetch remote video with url: ' + videoUrl)
64 // videoUrl is just an alias/rediraction, so process object id instead
65 if (videoObject.id !== videoUrl) return getOrCreateAPVideo({ ...options, fetchType: 'all', videoObject })
68 const creator = new APVideoCreator(videoObject)
69 const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(creator.create.bind(creator), syncParam.thumbnail)
71 await syncVideoExternalAttributes(videoCreated, videoObject, syncParam)
73 return { video: videoCreated, created: true, autoBlacklisted }
75 // Maybe a concurrent getOrCreateAPVideo call created this video
76 if (err.name === 'SequelizeUniqueConstraintError') {
77 const alreadyCreatedVideo = await loadVideoByUrl(videoUrl, fetchType)
78 if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false }
85 // ---------------------------------------------------------------------------
91 // ---------------------------------------------------------------------------
93 async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoLoadByUrlType, syncParam: SyncParam) {
94 if (!video.isOutdated()) return video
96 const refreshOptions = {
98 fetchedType: fetchType,
102 if (syncParam.refreshVideo === true) {
103 return refreshVideoIfNeeded(refreshOptions)
106 await JobQueue.Instance.createJobWithPromise({
107 type: 'activitypub-refresher',
108 payload: { type: 'video', url: video.url }