1 import { retryTransactionWrapper } from '@server/helpers/database-utils'
2 import { logger } from '@server/helpers/logger'
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 { getAPId } from '../activity'
8 import { refreshVideoIfNeeded } from './refresh'
9 import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
11 type GetVideoResult <T> = Promise<{
14 autoBlacklisted?: boolean
17 type GetVideoParamAll = {
21 allowRefresh?: boolean
24 type GetVideoParamImmutable = {
27 fetchType: 'only-immutable-attributes'
31 type GetVideoParamOther = {
34 fetchType?: 'all' | 'only-video'
35 allowRefresh?: boolean
38 function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
39 function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
40 function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
42 async function getOrCreateAPVideo (
43 options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther
44 ): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
46 const syncParam = options.syncParam || { rates: true, shares: true, comments: true, thumbnail: true, refreshVideo: false }
47 const fetchType = options.fetchType || 'all'
48 const allowRefresh = options.allowRefresh !== false
51 const videoUrl = getAPId(options.videoObject)
52 let videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
54 if (videoFromDatabase) {
55 if (allowRefresh === true) {
56 // Typings ensure allowRefresh === false in only-immutable-attributes fetch type
57 videoFromDatabase = await scheduleRefresh(videoFromDatabase as MVideoThumbnail, fetchType, syncParam)
60 return { video: videoFromDatabase, created: false }
63 const { videoObject } = await fetchRemoteVideo(videoUrl)
64 if (!videoObject) throw new Error('Cannot fetch remote video with url: ' + videoUrl)
66 // videoUrl is just an alias/rediraction, so process object id instead
67 if (videoObject.id !== videoUrl) return getOrCreateAPVideo({ ...options, fetchType: 'all', videoObject })
70 const creator = new APVideoCreator(videoObject)
71 const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(creator.create.bind(creator), syncParam.thumbnail)
73 await syncVideoExternalAttributes(videoCreated, videoObject, syncParam)
75 return { video: videoCreated, created: true, autoBlacklisted }
77 // Maybe a concurrent getOrCreateAPVideo call created this video
78 if (err.name === 'SequelizeUniqueConstraintError') {
79 const alreadyCreatedVideo = await loadVideoByUrl(videoUrl, fetchType)
80 if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false }
82 logger.error('Cannot create video %s because of SequelizeUniqueConstraintError error, but cannot find it in database.', videoUrl)
89 // ---------------------------------------------------------------------------
95 // ---------------------------------------------------------------------------
97 async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoLoadByUrlType, syncParam: SyncParam) {
98 if (!video.isOutdated()) return video
100 const refreshOptions = {
102 fetchedType: fetchType,
106 if (syncParam.refreshVideo === true) {
107 return refreshVideoIfNeeded(refreshOptions)
110 await JobQueue.Instance.createJob({
111 type: 'activitypub-refresher',
112 payload: { type: 'video', url: video.url }