1 import * as Bluebird from 'bluebird'
2 import * as Bull from 'bull'
3 import { checkUrlsSameHost } from '@server/helpers/activitypub'
5 isAnnounceActivityValid,
6 isDislikeActivityValid,
8 } from '@server/helpers/custom-validators/activitypub/activity'
9 import { sanitizeAndCheckVideoCommentObject } from '@server/helpers/custom-validators/activitypub/video-comments'
10 import { doJSONRequest, PeerTubeRequestError } from '@server/helpers/requests'
11 import { AP_CLEANER_CONCURRENCY } from '@server/initializers/constants'
12 import { VideoModel } from '@server/models/video/video'
13 import { VideoCommentModel } from '@server/models/video/video-comment'
14 import { VideoShareModel } from '@server/models/video/video-share'
15 import { HttpStatusCode } from '@shared/models'
16 import { logger } from '../../../helpers/logger'
17 import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
19 // Job to clean remote interactions off local videos
21 async function processActivityPubCleaner (_job: Bull.Job) {
22 logger.info('Processing ActivityPub cleaner.')
25 const rateUrls = await AccountVideoRateModel.listRemoteRateUrlsOfLocalVideos()
26 const { bodyValidator, deleter, updater } = rateOptionsFactory()
28 await Bluebird.map(rateUrls, async rateUrl => {
30 const result = await updateObjectIfNeeded(rateUrl, bodyValidator, updater, deleter)
32 if (result?.status === 'deleted') {
33 const { videoId, type } = result.data
35 await VideoModel.updateRatesOf(videoId, type, undefined)
38 logger.warn('Cannot update/delete remote AP rate %s.', rateUrl, { err })
40 }, { concurrency: AP_CLEANER_CONCURRENCY })
44 const shareUrls = await VideoShareModel.listRemoteShareUrlsOfLocalVideos()
45 const { bodyValidator, deleter, updater } = shareOptionsFactory()
47 await Bluebird.map(shareUrls, async shareUrl => {
49 await updateObjectIfNeeded(shareUrl, bodyValidator, updater, deleter)
51 logger.warn('Cannot update/delete remote AP share %s.', shareUrl, { err })
53 }, { concurrency: AP_CLEANER_CONCURRENCY })
57 const commentUrls = await VideoCommentModel.listRemoteCommentUrlsOfLocalVideos()
58 const { bodyValidator, deleter, updater } = commentOptionsFactory()
60 await Bluebird.map(commentUrls, async commentUrl => {
62 await updateObjectIfNeeded(commentUrl, bodyValidator, updater, deleter)
64 logger.warn('Cannot update/delete remote AP comment %s.', commentUrl, { err })
66 }, { concurrency: AP_CLEANER_CONCURRENCY })
70 // ---------------------------------------------------------------------------
73 processActivityPubCleaner
76 // ---------------------------------------------------------------------------
78 async function updateObjectIfNeeded <T> (
80 bodyValidator: (body: any) => boolean,
81 updater: (url: string, newUrl: string) => Promise<T>,
82 deleter: (url: string) => Promise<T>
83 ): Promise<{ data: T, status: 'deleted' | 'updated' } | null> {
84 const on404OrTombstone = async () => {
85 logger.info('Removing remote AP object %s.', url)
86 const data = await deleter(url)
88 return { status: 'deleted' as 'deleted', data }
92 const { body } = await doJSONRequest<any>(url, { activityPub: true })
94 // If not same id, check same host and update
95 if (!body || !body.id || !bodyValidator(body)) throw new Error(`Body or body id of ${url} is invalid`)
97 if (body.type === 'Tombstone') {
98 return on404OrTombstone()
101 const newUrl = body.id
102 if (newUrl !== url) {
103 if (checkUrlsSameHost(newUrl, url) !== true) {
104 throw new Error(`New url ${newUrl} has not the same host than old url ${url}`)
107 logger.info('Updating remote AP object %s.', url)
108 const data = await updater(url, newUrl)
110 return { status: 'updated', data }
115 // Does not exist anymore, remove entry
116 if ((err as PeerTubeRequestError).statusCode === HttpStatusCode.NOT_FOUND_404) {
117 return on404OrTombstone()
124 function rateOptionsFactory () {
126 bodyValidator: (body: any) => isLikeActivityValid(body) || isDislikeActivityValid(body),
128 updater: async (url: string, newUrl: string) => {
129 const rate = await AccountVideoRateModel.loadByUrl(url, undefined)
132 const videoId = rate.videoId
133 const type = rate.type
137 return { videoId, type }
140 deleter: async (url) => {
141 const rate = await AccountVideoRateModel.loadByUrl(url, undefined)
143 const videoId = rate.videoId
144 const type = rate.type
148 return { videoId, type }
153 function shareOptionsFactory () {
155 bodyValidator: (body: any) => isAnnounceActivityValid(body),
157 updater: async (url: string, newUrl: string) => {
158 const share = await VideoShareModel.loadByUrl(url, undefined)
166 deleter: async (url) => {
167 const share = await VideoShareModel.loadByUrl(url, undefined)
169 await share.destroy()
176 function commentOptionsFactory () {
178 bodyValidator: (body: any) => sanitizeAndCheckVideoCommentObject(body),
180 updater: async (url: string, newUrl: string) => {
181 const comment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(url)
189 deleter: async (url) => {
190 const comment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(url)
192 await comment.destroy()