diff options
Diffstat (limited to 'server/lib/activitypub/videos')
-rw-r--r-- | server/lib/activitypub/videos/fetch.ts | 180 | ||||
-rw-r--r-- | server/lib/activitypub/videos/get.ts | 109 | ||||
-rw-r--r-- | server/lib/activitypub/videos/index.ts | 3 | ||||
-rw-r--r-- | server/lib/activitypub/videos/refresh.ts | 64 | ||||
-rw-r--r-- | server/lib/activitypub/videos/shared/index.ts | 1 | ||||
-rw-r--r-- | server/lib/activitypub/videos/shared/url-to-object.ts | 22 |
6 files changed, 198 insertions, 181 deletions
diff --git a/server/lib/activitypub/videos/fetch.ts b/server/lib/activitypub/videos/fetch.ts deleted file mode 100644 index 5113c9d7e..000000000 --- a/server/lib/activitypub/videos/fetch.ts +++ /dev/null | |||
@@ -1,180 +0,0 @@ | |||
1 | import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub' | ||
2 | import { sanitizeAndCheckVideoTorrentObject } from '@server/helpers/custom-validators/activitypub/videos' | ||
3 | import { retryTransactionWrapper } from '@server/helpers/database-utils' | ||
4 | import { logger } from '@server/helpers/logger' | ||
5 | import { doJSONRequest, PeerTubeRequestError } from '@server/helpers/requests' | ||
6 | import { fetchVideoByUrl, VideoFetchByUrlType } from '@server/helpers/video' | ||
7 | import { REMOTE_SCHEME } from '@server/initializers/constants' | ||
8 | import { ActorFollowScoreCache } from '@server/lib/files-cache' | ||
9 | import { JobQueue } from '@server/lib/job-queue' | ||
10 | import { VideoModel } from '@server/models/video/video' | ||
11 | import { MVideoAccountLight, MVideoAccountLightBlacklistAllFiles, MVideoImmutable, MVideoThumbnail } from '@server/types/models' | ||
12 | import { HttpStatusCode } from '@shared/core-utils' | ||
13 | import { VideoObject } from '@shared/models' | ||
14 | import { APVideoCreator, SyncParam, syncVideoExternalAttributes } from './shared' | ||
15 | import { APVideoUpdater } from './updater' | ||
16 | |||
17 | async function fetchRemoteVideo (videoUrl: string): Promise<{ statusCode: number, videoObject: VideoObject }> { | ||
18 | logger.info('Fetching remote video %s.', videoUrl) | ||
19 | |||
20 | const { statusCode, body } = await doJSONRequest<any>(videoUrl, { activityPub: true }) | ||
21 | |||
22 | if (sanitizeAndCheckVideoTorrentObject(body) === false || checkUrlsSameHost(body.id, videoUrl) !== true) { | ||
23 | logger.debug('Remote video JSON is not valid.', { body }) | ||
24 | return { statusCode, videoObject: undefined } | ||
25 | } | ||
26 | |||
27 | return { statusCode, videoObject: body } | ||
28 | } | ||
29 | |||
30 | async function fetchRemoteVideoDescription (video: MVideoAccountLight) { | ||
31 | const host = video.VideoChannel.Account.Actor.Server.host | ||
32 | const path = video.getDescriptionAPIPath() | ||
33 | const url = REMOTE_SCHEME.HTTP + '://' + host + path | ||
34 | |||
35 | const { body } = await doJSONRequest<any>(url) | ||
36 | return body.description || '' | ||
37 | } | ||
38 | |||
39 | type GetVideoResult <T> = Promise<{ | ||
40 | video: T | ||
41 | created: boolean | ||
42 | autoBlacklisted?: boolean | ||
43 | }> | ||
44 | |||
45 | type GetVideoParamAll = { | ||
46 | videoObject: { id: string } | string | ||
47 | syncParam?: SyncParam | ||
48 | fetchType?: 'all' | ||
49 | allowRefresh?: boolean | ||
50 | } | ||
51 | |||
52 | type GetVideoParamImmutable = { | ||
53 | videoObject: { id: string } | string | ||
54 | syncParam?: SyncParam | ||
55 | fetchType: 'only-immutable-attributes' | ||
56 | allowRefresh: false | ||
57 | } | ||
58 | |||
59 | type GetVideoParamOther = { | ||
60 | videoObject: { id: string } | string | ||
61 | syncParam?: SyncParam | ||
62 | fetchType?: 'all' | 'only-video' | ||
63 | allowRefresh?: boolean | ||
64 | } | ||
65 | |||
66 | function getOrCreateVideoAndAccountAndChannel (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles> | ||
67 | function getOrCreateVideoAndAccountAndChannel (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable> | ||
68 | function getOrCreateVideoAndAccountAndChannel ( | ||
69 | options: GetVideoParamOther | ||
70 | ): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail> | ||
71 | async function getOrCreateVideoAndAccountAndChannel ( | ||
72 | options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther | ||
73 | ): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> { | ||
74 | // Default params | ||
75 | const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false } | ||
76 | const fetchType = options.fetchType || 'all' | ||
77 | const allowRefresh = options.allowRefresh !== false | ||
78 | |||
79 | // Get video url | ||
80 | const videoUrl = getAPId(options.videoObject) | ||
81 | let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType) | ||
82 | |||
83 | if (videoFromDatabase) { | ||
84 | // If allowRefresh is true, we could not call this function using 'only-immutable-attributes' fetch type | ||
85 | if (allowRefresh === true && (videoFromDatabase as MVideoThumbnail).isOutdated()) { | ||
86 | const refreshOptions = { | ||
87 | video: videoFromDatabase as MVideoThumbnail, | ||
88 | fetchedType: fetchType, | ||
89 | syncParam | ||
90 | } | ||
91 | |||
92 | if (syncParam.refreshVideo === true) { | ||
93 | videoFromDatabase = await refreshVideoIfNeeded(refreshOptions) | ||
94 | } else { | ||
95 | await JobQueue.Instance.createJobWithPromise({ | ||
96 | type: 'activitypub-refresher', | ||
97 | payload: { type: 'video', url: videoFromDatabase.url } | ||
98 | }) | ||
99 | } | ||
100 | } | ||
101 | |||
102 | return { video: videoFromDatabase, created: false } | ||
103 | } | ||
104 | |||
105 | const { videoObject } = await fetchRemoteVideo(videoUrl) | ||
106 | if (!videoObject) throw new Error('Cannot fetch remote video with url: ' + videoUrl) | ||
107 | |||
108 | try { | ||
109 | const creator = new APVideoCreator(videoObject) | ||
110 | const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(creator.create.bind(creator), syncParam.thumbnail) | ||
111 | |||
112 | await syncVideoExternalAttributes(videoCreated, videoObject, syncParam) | ||
113 | |||
114 | return { video: videoCreated, created: true, autoBlacklisted } | ||
115 | } catch (err) { | ||
116 | // Maybe a concurrent getOrCreateVideoAndAccountAndChannel call created this video | ||
117 | if (err.name === 'SequelizeUniqueConstraintError') { | ||
118 | const fallbackVideo = await fetchVideoByUrl(videoUrl, fetchType) | ||
119 | if (fallbackVideo) return { video: fallbackVideo, created: false } | ||
120 | } | ||
121 | |||
122 | throw err | ||
123 | } | ||
124 | } | ||
125 | |||
126 | async function refreshVideoIfNeeded (options: { | ||
127 | video: MVideoThumbnail | ||
128 | fetchedType: VideoFetchByUrlType | ||
129 | syncParam: SyncParam | ||
130 | }): Promise<MVideoThumbnail> { | ||
131 | if (!options.video.isOutdated()) return options.video | ||
132 | |||
133 | // We need more attributes if the argument video was fetched with not enough joints | ||
134 | const video = options.fetchedType === 'all' | ||
135 | ? options.video as MVideoAccountLightBlacklistAllFiles | ||
136 | : await VideoModel.loadByUrlAndPopulateAccount(options.video.url) | ||
137 | |||
138 | try { | ||
139 | const { videoObject } = await fetchRemoteVideo(video.url) | ||
140 | |||
141 | if (videoObject === undefined) { | ||
142 | logger.warn('Cannot refresh remote video %s: invalid body.', video.url) | ||
143 | |||
144 | await video.setAsRefreshed() | ||
145 | return video | ||
146 | } | ||
147 | |||
148 | const videoUpdater = new APVideoUpdater(videoObject, video) | ||
149 | await videoUpdater.update() | ||
150 | |||
151 | await syncVideoExternalAttributes(video, videoObject, options.syncParam) | ||
152 | |||
153 | ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId) | ||
154 | |||
155 | return video | ||
156 | } catch (err) { | ||
157 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | ||
158 | logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url) | ||
159 | |||
160 | // Video does not exist anymore | ||
161 | await video.destroy() | ||
162 | return undefined | ||
163 | } | ||
164 | |||
165 | logger.warn('Cannot refresh video %s.', options.video.url, { err }) | ||
166 | |||
167 | ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) | ||
168 | |||
169 | // Don't refresh in loop | ||
170 | await video.setAsRefreshed() | ||
171 | return video | ||
172 | } | ||
173 | } | ||
174 | |||
175 | export { | ||
176 | fetchRemoteVideo, | ||
177 | fetchRemoteVideoDescription, | ||
178 | refreshVideoIfNeeded, | ||
179 | getOrCreateVideoAndAccountAndChannel | ||
180 | } | ||
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 @@ | |||
1 | import { getAPId } from '@server/helpers/activitypub' | ||
2 | import { retryTransactionWrapper } from '@server/helpers/database-utils' | ||
3 | import { fetchVideoByUrl, VideoFetchByUrlType } from '@server/helpers/video' | ||
4 | import { JobQueue } from '@server/lib/job-queue' | ||
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) | ||
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 | |||
84 | export { | ||
85 | getOrCreateAPVideo | ||
86 | } | ||
87 | |||
88 | // --------------------------------------------------------------------------- | ||
89 | |||
90 | async 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 | } | ||
diff --git a/server/lib/activitypub/videos/index.ts b/server/lib/activitypub/videos/index.ts index b560acb76..b22062598 100644 --- a/server/lib/activitypub/videos/index.ts +++ b/server/lib/activitypub/videos/index.ts | |||
@@ -1,3 +1,4 @@ | |||
1 | export * from './federate' | 1 | export * from './federate' |
2 | export * from './fetch' | 2 | export * from './get' |
3 | export * from './refresh' | ||
3 | export * from './updater' | 4 | export * from './updater' |
diff --git a/server/lib/activitypub/videos/refresh.ts b/server/lib/activitypub/videos/refresh.ts new file mode 100644 index 000000000..205a3ccb1 --- /dev/null +++ b/server/lib/activitypub/videos/refresh.ts | |||
@@ -0,0 +1,64 @@ | |||
1 | import { logger } from '@server/helpers/logger' | ||
2 | import { PeerTubeRequestError } from '@server/helpers/requests' | ||
3 | import { VideoFetchByUrlType } from '@server/helpers/video' | ||
4 | import { ActorFollowScoreCache } from '@server/lib/files-cache' | ||
5 | import { VideoModel } from '@server/models/video/video' | ||
6 | import { MVideoAccountLightBlacklistAllFiles, MVideoThumbnail } from '@server/types/models' | ||
7 | import { HttpStatusCode } from '@shared/core-utils' | ||
8 | import { fetchRemoteVideo, SyncParam, syncVideoExternalAttributes } from './shared' | ||
9 | import { APVideoUpdater } from './updater' | ||
10 | |||
11 | async function refreshVideoIfNeeded (options: { | ||
12 | video: MVideoThumbnail | ||
13 | fetchedType: VideoFetchByUrlType | ||
14 | syncParam: SyncParam | ||
15 | }): Promise<MVideoThumbnail> { | ||
16 | if (!options.video.isOutdated()) return options.video | ||
17 | |||
18 | // We need more attributes if the argument video was fetched with not enough joints | ||
19 | const video = options.fetchedType === 'all' | ||
20 | ? options.video as MVideoAccountLightBlacklistAllFiles | ||
21 | : await VideoModel.loadByUrlAndPopulateAccount(options.video.url) | ||
22 | |||
23 | try { | ||
24 | const { videoObject } = await fetchRemoteVideo(video.url) | ||
25 | |||
26 | if (videoObject === undefined) { | ||
27 | logger.warn('Cannot refresh remote video %s: invalid body.', video.url) | ||
28 | |||
29 | await video.setAsRefreshed() | ||
30 | return video | ||
31 | } | ||
32 | |||
33 | const videoUpdater = new APVideoUpdater(videoObject, video) | ||
34 | await videoUpdater.update() | ||
35 | |||
36 | await syncVideoExternalAttributes(video, videoObject, options.syncParam) | ||
37 | |||
38 | ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId) | ||
39 | |||
40 | return video | ||
41 | } catch (err) { | ||
42 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | ||
43 | logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url) | ||
44 | |||
45 | // Video does not exist anymore | ||
46 | await video.destroy() | ||
47 | return undefined | ||
48 | } | ||
49 | |||
50 | logger.warn('Cannot refresh video %s.', options.video.url, { err }) | ||
51 | |||
52 | ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) | ||
53 | |||
54 | // Don't refresh in loop | ||
55 | await video.setAsRefreshed() | ||
56 | return video | ||
57 | } | ||
58 | } | ||
59 | |||
60 | // --------------------------------------------------------------------------- | ||
61 | |||
62 | export { | ||
63 | refreshVideoIfNeeded | ||
64 | } | ||
diff --git a/server/lib/activitypub/videos/shared/index.ts b/server/lib/activitypub/videos/shared/index.ts index 208a43705..951403493 100644 --- a/server/lib/activitypub/videos/shared/index.ts +++ b/server/lib/activitypub/videos/shared/index.ts | |||
@@ -2,4 +2,5 @@ export * from './abstract-builder' | |||
2 | export * from './creator' | 2 | export * from './creator' |
3 | export * from './object-to-model-attributes' | 3 | export * from './object-to-model-attributes' |
4 | export * from './trackers' | 4 | export * from './trackers' |
5 | export * from './url-to-object' | ||
5 | export * from './video-sync-attributes' | 6 | export * from './video-sync-attributes' |
diff --git a/server/lib/activitypub/videos/shared/url-to-object.ts b/server/lib/activitypub/videos/shared/url-to-object.ts new file mode 100644 index 000000000..b1ecac8ca --- /dev/null +++ b/server/lib/activitypub/videos/shared/url-to-object.ts | |||
@@ -0,0 +1,22 @@ | |||
1 | import { checkUrlsSameHost } from '@server/helpers/activitypub' | ||
2 | import { sanitizeAndCheckVideoTorrentObject } from '@server/helpers/custom-validators/activitypub/videos' | ||
3 | import { logger } from '@server/helpers/logger' | ||
4 | import { doJSONRequest } from '@server/helpers/requests' | ||
5 | import { VideoObject } from '@shared/models' | ||
6 | |||
7 | async function fetchRemoteVideo (videoUrl: string): Promise<{ statusCode: number, videoObject: VideoObject }> { | ||
8 | logger.info('Fetching remote video %s.', videoUrl) | ||
9 | |||
10 | const { statusCode, body } = await doJSONRequest<any>(videoUrl, { activityPub: true }) | ||
11 | |||
12 | if (sanitizeAndCheckVideoTorrentObject(body) === false || checkUrlsSameHost(body.id, videoUrl) !== true) { | ||
13 | logger.debug('Remote video JSON is not valid.', { body }) | ||
14 | return { statusCode, videoObject: undefined } | ||
15 | } | ||
16 | |||
17 | return { statusCode, videoObject: body } | ||
18 | } | ||
19 | |||
20 | export { | ||
21 | fetchRemoteVideo | ||
22 | } | ||