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