PeerTube/server/lib/job-queue/handlers/activitypub-cleaner.ts

199 lines
5.9 KiB
TypeScript
Raw Normal View History

2021-08-27 07:32:44 -05:00
import { map } from 'bluebird'
import { Job } from 'bull'
import { checkUrlsSameHost } from '@server/helpers/activitypub'
2021-03-05 06:49:58 -06:00
import {
isAnnounceActivityValid,
isDislikeActivityValid,
isLikeActivityValid
} from '@server/helpers/custom-validators/activitypub/activity'
import { sanitizeAndCheckVideoCommentObject } from '@server/helpers/custom-validators/activitypub/video-comments'
2021-03-09 07:01:44 -06:00
import { doJSONRequest, PeerTubeRequestError } from '@server/helpers/requests'
2021-12-28 04:36:51 -06:00
import { AP_CLEANER } from '@server/initializers/constants'
import { Redis } from '@server/lib/redis'
import { VideoModel } from '@server/models/video/video'
import { VideoCommentModel } from '@server/models/video/video-comment'
import { VideoShareModel } from '@server/models/video/video-share'
2021-07-16 03:42:24 -05:00
import { HttpStatusCode } from '@shared/models'
2021-12-28 04:41:41 -06:00
import { logger, loggerTagsFactory } from '../../../helpers/logger'
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
2021-12-28 04:41:41 -06:00
const lTags = loggerTagsFactory('ap-cleaner')
// Job to clean remote interactions off local videos
2021-08-27 07:32:44 -05:00
async function processActivityPubCleaner (_job: Job) {
2021-12-28 04:41:41 -06:00
logger.info('Processing ActivityPub cleaner.', lTags())
{
const rateUrls = await AccountVideoRateModel.listRemoteRateUrlsOfLocalVideos()
const { bodyValidator, deleter, updater } = rateOptionsFactory()
2021-08-27 07:32:44 -05:00
await map(rateUrls, async rateUrl => {
2021-12-28 04:41:41 -06:00
const result = await updateObjectIfNeeded({ url: rateUrl, bodyValidator, updater, deleter })
2021-12-28 04:41:41 -06:00
if (result?.status === 'deleted') {
const { videoId, type } = result.data
2021-12-28 04:41:41 -06:00
await VideoModel.updateRatesOf(videoId, type, undefined)
}
2021-12-28 04:36:51 -06:00
}, { concurrency: AP_CLEANER.CONCURRENCY })
}
{
const shareUrls = await VideoShareModel.listRemoteShareUrlsOfLocalVideos()
const { bodyValidator, deleter, updater } = shareOptionsFactory()
2021-08-27 07:32:44 -05:00
await map(shareUrls, async shareUrl => {
2021-12-28 04:41:41 -06:00
await updateObjectIfNeeded({ url: shareUrl, bodyValidator, updater, deleter })
2021-12-28 04:36:51 -06:00
}, { concurrency: AP_CLEANER.CONCURRENCY })
}
{
const commentUrls = await VideoCommentModel.listRemoteCommentUrlsOfLocalVideos()
const { bodyValidator, deleter, updater } = commentOptionsFactory()
2021-08-27 07:32:44 -05:00
await map(commentUrls, async commentUrl => {
2021-12-28 04:41:41 -06:00
await updateObjectIfNeeded({ url: commentUrl, bodyValidator, updater, deleter })
2021-12-28 04:36:51 -06:00
}, { concurrency: AP_CLEANER.CONCURRENCY })
}
}
// ---------------------------------------------------------------------------
export {
processActivityPubCleaner
}
// ---------------------------------------------------------------------------
2021-12-28 04:36:51 -06:00
async function updateObjectIfNeeded <T> (options: {
url: string
bodyValidator: (body: any) => boolean
updater: (url: string, newUrl: string) => Promise<T>
deleter: (url: string) => Promise<T> }
): Promise<{ data: T, status: 'deleted' | 'updated' } | null> {
2021-12-28 04:36:51 -06:00
const { url, bodyValidator, updater, deleter } = options
2021-03-09 07:01:44 -06:00
const on404OrTombstone = async () => {
2021-12-28 04:41:41 -06:00
logger.info('Removing remote AP object %s.', url, lTags(url))
const data = await deleter(url)
2021-03-09 07:01:44 -06:00
return { status: 'deleted' as 'deleted', data }
}
2021-03-09 07:01:44 -06:00
try {
const { body } = await doJSONRequest<any>(url, { activityPub: true })
2021-03-09 07:01:44 -06:00
// If not same id, check same host and update
if (!body || !body.id || !bodyValidator(body)) throw new Error(`Body or body id of ${url} is invalid`)
2021-03-09 07:01:44 -06:00
if (body.type === 'Tombstone') {
return on404OrTombstone()
}
2021-03-09 07:01:44 -06:00
const newUrl = body.id
if (newUrl !== url) {
if (checkUrlsSameHost(newUrl, url) !== true) {
throw new Error(`New url ${newUrl} has not the same host than old url ${url}`)
}
2021-12-28 04:41:41 -06:00
logger.info('Updating remote AP object %s.', url, lTags(url))
2021-03-09 07:01:44 -06:00
const data = await updater(url, newUrl)
return { status: 'updated', data }
}
2021-03-09 07:01:44 -06:00
return null
} catch (err) {
// Does not exist anymore, remove entry
if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
return on404OrTombstone()
}
2021-12-28 04:41:41 -06:00
logger.debug('Remote AP object %s is unavailable.', url, lTags(url))
2021-12-28 04:36:51 -06:00
const unavailability = await Redis.Instance.addAPUnavailability(url)
if (unavailability >= AP_CLEANER.UNAVAILABLE_TRESHOLD) {
2021-12-28 04:41:41 -06:00
logger.info('Removing unavailable AP resource %s.', url, lTags(url))
2021-12-28 04:36:51 -06:00
return on404OrTombstone()
}
return null
}
}
function rateOptionsFactory () {
return {
bodyValidator: (body: any) => isLikeActivityValid(body) || isDislikeActivityValid(body),
updater: async (url: string, newUrl: string) => {
const rate = await AccountVideoRateModel.loadByUrl(url, undefined)
rate.url = newUrl
const videoId = rate.videoId
const type = rate.type
await rate.save()
return { videoId, type }
},
deleter: async (url) => {
const rate = await AccountVideoRateModel.loadByUrl(url, undefined)
const videoId = rate.videoId
const type = rate.type
await rate.destroy()
return { videoId, type }
}
}
}
function shareOptionsFactory () {
return {
2021-03-05 06:49:58 -06:00
bodyValidator: (body: any) => isAnnounceActivityValid(body),
updater: async (url: string, newUrl: string) => {
const share = await VideoShareModel.loadByUrl(url, undefined)
share.url = newUrl
await share.save()
return undefined
},
deleter: async (url) => {
const share = await VideoShareModel.loadByUrl(url, undefined)
await share.destroy()
return undefined
}
}
}
function commentOptionsFactory () {
return {
bodyValidator: (body: any) => sanitizeAndCheckVideoCommentObject(body),
updater: async (url: string, newUrl: string) => {
const comment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(url)
comment.url = newUrl
await comment.save()
return undefined
},
deleter: async (url) => {
const comment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(url)
await comment.destroy()
return undefined
}
}
}