aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/videos/get.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-02 15:47:05 +0200
committerChocobozzz <me@florianbigard.com>2021-06-02 16:57:53 +0200
commit304a84d59c3a800b7f7aef48cf55f307534c0926 (patch)
treee099aefd76aa8ee5aacef7ddfc59d79111fe474b /server/lib/activitypub/videos/get.ts
parentc56faf0d9453490737f283b29a203bb1ca632b95 (diff)
downloadPeerTube-304a84d59c3a800b7f7aef48cf55f307534c0926.tar.gz
PeerTube-304a84d59c3a800b7f7aef48cf55f307534c0926.tar.zst
PeerTube-304a84d59c3a800b7f7aef48cf55f307534c0926.zip
Refactor getOrCreateAPVideo
Diffstat (limited to 'server/lib/activitypub/videos/get.ts')
-rw-r--r--server/lib/activitypub/videos/get.ts109
1 files changed, 109 insertions, 0 deletions
diff --git a/server/lib/activitypub/videos/get.ts b/server/lib/activitypub/videos/get.ts
new file mode 100644
index 000000000..a8c41e178
--- /dev/null
+++ b/server/lib/activitypub/videos/get.ts
@@ -0,0 +1,109 @@
1import { getAPId } from '@server/helpers/activitypub'
2import { retryTransactionWrapper } from '@server/helpers/database-utils'
3import { fetchVideoByUrl, VideoFetchByUrlType } from '@server/helpers/video'
4import { JobQueue } from '@server/lib/job-queue'
5import { MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models'
6import { refreshVideoIfNeeded } from './refresh'
7import { APVideoCreator, fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared'
8
9type GetVideoResult <T> = Promise<{
10 video: T
11 created: boolean
12 autoBlacklisted?: boolean
13}>
14
15type GetVideoParamAll = {
16 videoObject: { id: string } | string
17 syncParam?: SyncParam
18 fetchType?: 'all'
19 allowRefresh?: boolean
20}
21
22type GetVideoParamImmutable = {
23 videoObject: { id: string } | string
24 syncParam?: SyncParam
25 fetchType: 'only-immutable-attributes'
26 allowRefresh: false
27}
28
29type GetVideoParamOther = {
30 videoObject: { id: string } | string
31 syncParam?: SyncParam
32 fetchType?: 'all' | 'only-video'
33 allowRefresh?: boolean
34}
35
36function getOrCreateAPVideo (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles>
37function getOrCreateAPVideo (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable>
38function getOrCreateAPVideo (options: GetVideoParamOther): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail>
39
40async 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)
50 let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType)
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
64 try {
65 const creator = new APVideoCreator(videoObject)
66 const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(creator.create.bind(creator), syncParam.thumbnail)
67
68 await syncVideoExternalAttributes(videoCreated, videoObject, syncParam)
69
70 return { video: videoCreated, created: true, autoBlacklisted }
71 } catch (err) {
72 // Maybe a concurrent getOrCreateAPVideo call created this video
73 if (err.name === 'SequelizeUniqueConstraintError') {
74 const alreadyCreatedVideo = await fetchVideoByUrl(videoUrl, fetchType)
75 if (alreadyCreatedVideo) return { video: alreadyCreatedVideo, created: false }
76 }
77
78 throw err
79 }
80}
81
82// ---------------------------------------------------------------------------
83
84export {
85 getOrCreateAPVideo
86}
87
88// ---------------------------------------------------------------------------
89
90async function scheduleRefresh (video: MVideoThumbnail, fetchType: VideoFetchByUrlType, syncParam: SyncParam) {
91 if (!video.isOutdated()) return video
92
93 const refreshOptions = {
94 video,
95 fetchedType: fetchType,
96 syncParam
97 }
98
99 if (syncParam.refreshVideo === true) {
100 return refreshVideoIfNeeded(refreshOptions)
101 }
102
103 await JobQueue.Instance.createJobWithPromise({
104 type: 'activitypub-refresher',
105 payload: { type: 'video', url: video.url }
106 })
107
108 return video
109}