1
0
Fork 0
peertube/server/lib/activitypub/videos/get.ts

114 lines
3.9 KiB
TypeScript
Raw Normal View History

2021-06-02 09:47:05 -04:00
import { getAPId } from '@server/helpers/activitypub'
import { retryTransactionWrapper } from '@server/helpers/database-utils'
import { JobQueue } from '@server/lib/job-queue'
import { loadVideoByUrl, VideoLoadByUrlType } from '@server/lib/model-loaders'
2021-06-02 09:47:05 -04:00
import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models'
2021-06-17 10:02:38 -04:00
import { APObject } from '@shared/models'
2021-06-02 09:47:05 -04:00
import { refreshVideoIfNeeded } from './refresh'
import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
type GetVideoResult <T> = Promise<{
video: T
created: boolean
autoBlacklisted?: boolean
}>
type GetVideoParamAll = {
2021-06-17 10:02:38 -04:00
videoObject: APObject
2021-06-02 09:47:05 -04:00
syncParam?: SyncParam
fetchType?: 'all'
allowRefresh?: boolean
}
type GetVideoParamImmutable = {
2021-06-17 10:02:38 -04:00
videoObject: APObject
2021-06-02 09:47:05 -04:00
syncParam?: SyncParam
fetchType: 'only-immutable-attributes'
allowRefresh: false
}
type GetVideoParamOther = {
2021-06-17 10:02:38 -04:00
videoObject: APObject
2021-06-02 09:47:05 -04:00
syncParam?: SyncParam
fetchType?: 'all' | 'only-video'
allowRefresh?: boolean
}
function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
async function getOrCreateAPVideo (
options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther
): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
// Default params
const syncParam = options.syncParam || { rates: true, shares: true, comments: true, thumbnail: true, refreshVideo: false }
2021-06-02 09:47:05 -04:00
const fetchType = options.fetchType || 'all'
const allowRefresh = options.allowRefresh !== false
// Get video url
const videoUrl = getAPId(options.videoObject)
let videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
2021-06-02 09:47:05 -04:00
if (videoFromDatabase) {
if (allowRefresh === true) {
// Typings ensure allowRefresh === false in only-immutable-attributes fetch type
videoFromDatabase = await scheduleRefresh(videoFromDatabase as MVideoThumbnail, fetchType, syncParam)
}
return { video: videoFromDatabase, created: false }
}
const { videoObject } = await fetchRemoteVideo(videoUrl)
if (!videoObject) throw new Error('Cannot fetch remote video with url: ' + videoUrl)
2021-06-16 04:01:31 -04:00
// videoUrl is just an alias/rediraction, so process object id instead
2021-06-14 10:05:25 -04:00
if (videoObject.id !== videoUrl) return getOrCreateAPVideo({ ...options, fetchType: 'all', videoObject })
2021-06-02 09:47:05 -04:00
try {
const creator = new APVideoCreator(videoObject)
const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(creator.create.bind(creator), syncParam.thumbnail)
await syncVideoExternalAttributes(videoCreated, videoObject, syncParam)
return { video: videoCreated, created: true, autoBlacklisted }
} catch (err) {
// Maybe a concurrent getOrCreateAPVideo call created this video
if (err.name === 'SequelizeUniqueConstraintError') {
const alreadyCreatedVideo = await loadVideoByUrl(videoUrl, fetchType)
2021-06-02 09:47:05 -04:00
if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false }
}
throw err
}
}
// ---------------------------------------------------------------------------
export {
getOrCreateAPVideo
}
// ---------------------------------------------------------------------------
async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoLoadByUrlType, syncParam: SyncParam) {
2021-06-02 09:47:05 -04:00
if (!video.isOutdated()) return video
const refreshOptions = {
video,
fetchedType: fetchType,
syncParam
}
if (syncParam.refreshVideo === true) {
return refreshVideoIfNeeded(refreshOptions)
}
await JobQueue.Instance.createJobWithPromise({
type: 'activitypub-refresher',
payload: { type: 'video', url: video.url }
})
return video
}