]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/videos/get.ts
Add ability to search playlists
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos / get.ts
CommitLineData
304a84d5
C
1import { getAPId } from '@server/helpers/activitypub'
2import { retryTransactionWrapper } from '@server/helpers/database-utils'
304a84d5 3import { JobQueue } from '@server/lib/job-queue'
868fce62 4import { loadVideoByUrl, VideoLoadByUrlType } from '@server/lib/model-loaders'
304a84d5 5import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models'
37a44fc9 6import { APObject } from '@shared/models'
304a84d5
C
7import { refreshVideoIfNeeded } from './refresh'
8import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
9
10type GetVideoResult <T> = Promise<{
11 video: T
12 created: boolean
13 autoBlacklisted?: boolean
14}>
15
16type GetVideoParamAll = {
37a44fc9 17 videoObject: APObject
304a84d5
C
18 syncParam?: SyncParam
19 fetchType?: 'all'
20 allowRefresh?: boolean
21}
22
23type GetVideoParamImmutable = {
37a44fc9 24 videoObject: APObject
304a84d5
C
25 syncParam?: SyncParam
26 fetchType: 'only-immutable-attributes'
27 allowRefresh: false
28}
29
30type GetVideoParamOther = {
37a44fc9 31 videoObject: APObject
304a84d5
C
32 syncParam?: SyncParam
33 fetchType?: 'all' | 'only-video'
34 allowRefresh?: boolean
35}
36
37function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
38function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
39function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
40
41async function getOrCreateAPVideo (
42 options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther
43): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> {
44 // Default params
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
48
49 // Get video url
50 const videoUrl = getAPId(options.videoObject)
868fce62 51 let videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
304a84d5
C
52
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)
57 }
58
59 return { video: videoFromDatabase, created: false }
60 }
61
62 const { videoObject } = await fetchRemoteVideo(videoUrl)
63 if (!videoObject) throw new Error('Cannot fetch remote video with url: ' + videoUrl)
64
fd658484 65 // videoUrl is just an alias/rediraction, so process object id instead
a9fbc2aa
C
66 if (videoObject.id !== videoUrl) return getOrCreateAPVideo({ ...options, fetchType: 'all', videoObject })
67
304a84d5
C
68 try {
69 const creator = new APVideoCreator(videoObject)
70 const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(creator.create.bind(creator), syncParam.thumbnail)
71
72 await syncVideoExternalAttributes(videoCreated, videoObject, syncParam)
73
74 return { video: videoCreated, created: true, autoBlacklisted }
75 } catch (err) {
76 // Maybe a concurrent getOrCreateAPVideo call created this video
77 if (err.name === 'SequelizeUniqueConstraintError') {
868fce62 78 const alreadyCreatedVideo = await loadVideoByUrl(videoUrl, fetchType)
304a84d5
C
79 if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false }
80 }
81
82 throw err
83 }
84}
85
86// ---------------------------------------------------------------------------
87
88export {
89 getOrCreateAPVideo
90}
91
92// ---------------------------------------------------------------------------
93
868fce62 94async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoLoadByUrlType, syncParam: SyncParam) {
304a84d5
C
95 if (!video.isOutdated()) return video
96
97 const refreshOptions = {
98 video,
99 fetchedType: fetchType,
100 syncParam
101 }
102
103 if (syncParam.refreshVideo === true) {
104 return refreshVideoIfNeeded(refreshOptions)
105 }
106
107 await JobQueue.Instance.createJobWithPromise({
108 type: 'activitypub-refresher',
109 payload: { type: 'video', url: video.url }
110 })
111
112 return video
113}