diff options
Diffstat (limited to 'server/lib/job-queue/handlers/activitypub-cleaner.ts')
-rw-r--r-- | server/lib/job-queue/handlers/activitypub-cleaner.ts | 49 |
1 files changed, 27 insertions, 22 deletions
diff --git a/server/lib/job-queue/handlers/activitypub-cleaner.ts b/server/lib/job-queue/handlers/activitypub-cleaner.ts index 9dcc778fa..1caca1dcc 100644 --- a/server/lib/job-queue/handlers/activitypub-cleaner.ts +++ b/server/lib/job-queue/handlers/activitypub-cleaner.ts | |||
@@ -7,7 +7,7 @@ import { | |||
7 | isLikeActivityValid | 7 | isLikeActivityValid |
8 | } from '@server/helpers/custom-validators/activitypub/activity' | 8 | } from '@server/helpers/custom-validators/activitypub/activity' |
9 | import { sanitizeAndCheckVideoCommentObject } from '@server/helpers/custom-validators/activitypub/video-comments' | 9 | import { sanitizeAndCheckVideoCommentObject } from '@server/helpers/custom-validators/activitypub/video-comments' |
10 | import { doJSONRequest } from '@server/helpers/requests' | 10 | import { doJSONRequest, PeerTubeRequestError } from '@server/helpers/requests' |
11 | import { AP_CLEANER_CONCURRENCY } from '@server/initializers/constants' | 11 | import { AP_CLEANER_CONCURRENCY } from '@server/initializers/constants' |
12 | import { VideoModel } from '@server/models/video/video' | 12 | import { VideoModel } from '@server/models/video/video' |
13 | import { VideoCommentModel } from '@server/models/video/video-comment' | 13 | import { VideoCommentModel } from '@server/models/video/video-comment' |
@@ -81,39 +81,44 @@ async function updateObjectIfNeeded <T> ( | |||
81 | updater: (url: string, newUrl: string) => Promise<T>, | 81 | updater: (url: string, newUrl: string) => Promise<T>, |
82 | deleter: (url: string) => Promise<T> | 82 | deleter: (url: string) => Promise<T> |
83 | ): Promise<{ data: T, status: 'deleted' | 'updated' } | null> { | 83 | ): Promise<{ data: T, status: 'deleted' | 'updated' } | null> { |
84 | const { statusCode, body } = await doJSONRequest<any>(url, { activityPub: true }) | 84 | const on404OrTombstone = async () => { |
85 | |||
86 | // Does not exist anymore, remove entry | ||
87 | if (statusCode === HttpStatusCode.NOT_FOUND_404) { | ||
88 | logger.info('Removing remote AP object %s.', url) | 85 | logger.info('Removing remote AP object %s.', url) |
89 | const data = await deleter(url) | 86 | const data = await deleter(url) |
90 | 87 | ||
91 | return { status: 'deleted', data } | 88 | return { status: 'deleted' as 'deleted', data } |
92 | } | 89 | } |
93 | 90 | ||
94 | // If not same id, check same host and update | 91 | try { |
95 | if (!body || !body.id || !bodyValidator(body)) throw new Error(`Body or body id of ${url} is invalid`) | 92 | const { body } = await doJSONRequest<any>(url, { activityPub: true }) |
96 | 93 | ||
97 | if (body.type === 'Tombstone') { | 94 | // If not same id, check same host and update |
98 | logger.info('Removing remote AP object %s.', url) | 95 | if (!body || !body.id || !bodyValidator(body)) throw new Error(`Body or body id of ${url} is invalid`) |
99 | const data = await deleter(url) | ||
100 | 96 | ||
101 | return { status: 'deleted', data } | 97 | if (body.type === 'Tombstone') { |
102 | } | 98 | return on404OrTombstone() |
99 | } | ||
103 | 100 | ||
104 | const newUrl = body.id | 101 | const newUrl = body.id |
105 | if (newUrl !== url) { | 102 | if (newUrl !== url) { |
106 | if (checkUrlsSameHost(newUrl, url) !== true) { | 103 | if (checkUrlsSameHost(newUrl, url) !== true) { |
107 | throw new Error(`New url ${newUrl} has not the same host than old url ${url}`) | 104 | throw new Error(`New url ${newUrl} has not the same host than old url ${url}`) |
105 | } | ||
106 | |||
107 | logger.info('Updating remote AP object %s.', url) | ||
108 | const data = await updater(url, newUrl) | ||
109 | |||
110 | return { status: 'updated', data } | ||
108 | } | 111 | } |
109 | 112 | ||
110 | logger.info('Updating remote AP object %s.', url) | 113 | return null |
111 | const data = await updater(url, newUrl) | 114 | } catch (err) { |
115 | // Does not exist anymore, remove entry | ||
116 | if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) { | ||
117 | return on404OrTombstone() | ||
118 | } | ||
112 | 119 | ||
113 | return { status: 'updated', data } | 120 | throw err |
114 | } | 121 | } |
115 | |||
116 | return null | ||
117 | } | 122 | } |
118 | 123 | ||
119 | function rateOptionsFactory () { | 124 | function rateOptionsFactory () { |