]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/videos/get.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / get.ts
index 38ba4978c21dd8c2dce46aa829821085da60444d..f3e2f0625d46b04b265fac18da2ac1321316fe67 100644 (file)
@@ -1,8 +1,9 @@
 import { getAPId } from '@server/helpers/activitypub'
 import { retryTransactionWrapper } from '@server/helpers/database-utils'
 import { JobQueue } from '@server/lib/job-queue'
-import { fetchVideoByUrl, VideoFetchByUrlType } from '@server/lib/model-loaders'
+import { loadVideoByUrl, VideoLoadByUrlType } from '@server/lib/model-loaders'
 import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models'
+import { APObject } from '@shared/models'
 import { refreshVideoIfNeeded } from './refresh'
 import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
 
@@ -13,21 +14,21 @@ type GetVideoResult <T> = Promise<{
 }>
 
 type GetVideoParamAll = {
-  videoObject: { id: string } | string
+  videoObject: APObject
   syncParam?: SyncParam
   fetchType?: 'all'
   allowRefresh?: boolean
 }
 
 type GetVideoParamImmutable = {
-  videoObject: { id: string } | string
+  videoObject: APObject
   syncParam?: SyncParam
   fetchType: 'only-immutable-attributes'
   allowRefresh: false
 }
 
 type GetVideoParamOther = {
-  videoObject: { id: string } | string
+  videoObject: APObject
   syncParam?: SyncParam
   fetchType?: 'all' | 'only-video'
   allowRefresh?: boolean
@@ -47,7 +48,7 @@ async function getOrCreateAPVideo (
 
   // Get video url
   const videoUrl = getAPId(options.videoObject)
-  let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType)
+  let videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
 
   if (videoFromDatabase) {
     if (allowRefresh === true) {
@@ -61,6 +62,9 @@ async function getOrCreateAPVideo (
   const { videoObject } = await fetchRemoteVideo(videoUrl)
   if (!videoObject) throw new Error('Cannot fetch remote video with url: ' + videoUrl)
 
+  // videoUrl is just an alias/rediraction, so process object id instead
+  if (videoObject.id !== videoUrl) return getOrCreateAPVideo({ ...options, fetchType: 'all', videoObject })
+
   try {
     const creator = new APVideoCreator(videoObject)
     const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(creator.create.bind(creator), syncParam.thumbnail)
@@ -71,7 +75,7 @@ async function getOrCreateAPVideo (
   } catch (err) {
     // Maybe a concurrent getOrCreateAPVideo call created this video
     if (err.name === 'SequelizeUniqueConstraintError') {
-      const alreadyCreatedVideo = await fetchVideoByUrl(videoUrl, fetchType)
+      const alreadyCreatedVideo = await loadVideoByUrl(videoUrl, fetchType)
       if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false }
     }
 
@@ -87,7 +91,7 @@ export {
 
 // ---------------------------------------------------------------------------
 
-async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoFetchByUrlType, syncParam: SyncParam) {
+async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoLoadByUrlType, syncParam: SyncParam) {
   if (!video.isOutdated()) return video
 
   const refreshOptions = {