2018-07-10 17:02:20 +02:00
|
|
|
import * as Bull from 'bull'
|
2019-03-19 16:23:02 +01:00
|
|
|
import * as Bluebird from 'bluebird'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { processActivities } from '../../activitypub/process'
|
2018-08-22 11:51:39 +02:00
|
|
|
import { addVideoComments } from '../../activitypub/video-comments'
|
2018-05-25 16:21:16 +02:00
|
|
|
import { crawlCollectionPage } from '../../activitypub/crawl'
|
2018-09-19 11:16:23 +02:00
|
|
|
import { VideoModel } from '../../../models/video/video'
|
|
|
|
import { addVideoShares, createRates } from '../../activitypub'
|
2019-02-26 10:55:40 +01:00
|
|
|
import { createAccountPlaylists } from '../../activitypub/playlist'
|
|
|
|
import { AccountModel } from '../../../models/account/account'
|
2019-03-19 16:23:02 +01:00
|
|
|
import { AccountVideoRateModel } from '../../../models/account/account-video-rate'
|
|
|
|
import { VideoShareModel } from '../../../models/video/video-share'
|
|
|
|
import { VideoCommentModel } from '../../../models/video/video-comment'
|
2019-08-15 11:53:26 +02:00
|
|
|
import { MAccountDefault, MVideoFullLight } from '../../../typings/models'
|
2018-08-22 11:51:39 +02:00
|
|
|
|
2019-02-26 10:55:40 +01:00
|
|
|
type FetchType = 'activity' | 'video-likes' | 'video-dislikes' | 'video-shares' | 'video-comments' | 'account-playlists'
|
2017-11-22 10:29:55 +01:00
|
|
|
|
2018-01-25 15:05:18 +01:00
|
|
|
export type ActivitypubHttpFetcherPayload = {
|
2018-08-22 11:51:39 +02:00
|
|
|
uri: string
|
|
|
|
type: FetchType
|
|
|
|
videoId?: number
|
2019-02-26 10:55:40 +01:00
|
|
|
accountId?: number
|
2018-01-25 15:05:18 +01:00
|
|
|
}
|
|
|
|
|
2018-07-10 17:02:20 +02:00
|
|
|
async function processActivityPubHttpFetcher (job: Bull.Job) {
|
2018-01-25 15:05:18 +01:00
|
|
|
logger.info('Processing ActivityPub fetcher in job %d.', job.id)
|
|
|
|
|
2018-08-22 11:51:39 +02:00
|
|
|
const payload = job.data as ActivitypubHttpFetcherPayload
|
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
let video: MVideoFullLight
|
2018-08-22 11:51:39 +02:00
|
|
|
if (payload.videoId) video = await VideoModel.loadAndPopulateAccountAndServerAndTags(payload.videoId)
|
2017-11-22 10:29:55 +01:00
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
let account: MAccountDefault
|
2019-02-26 10:55:40 +01:00
|
|
|
if (payload.accountId) account = await AccountModel.load(payload.accountId)
|
|
|
|
|
2018-08-22 11:51:39 +02:00
|
|
|
const fetcherType: { [ id in FetchType ]: (items: any[]) => Promise<any> } = {
|
2019-08-02 10:53:36 +02:00
|
|
|
'activity': items => processActivities(items, { outboxUrl: payload.uri, fromFetch: true }),
|
2018-08-22 11:51:39 +02:00
|
|
|
'video-likes': items => createRates(items, video, 'like'),
|
|
|
|
'video-dislikes': items => createRates(items, video, 'dislike'),
|
|
|
|
'video-shares': items => addVideoShares(items, video),
|
2019-08-06 17:19:53 +02:00
|
|
|
'video-comments': items => addVideoComments(items),
|
2019-02-26 10:55:40 +01:00
|
|
|
'account-playlists': items => createAccountPlaylists(items, account)
|
2017-11-22 10:29:55 +01:00
|
|
|
}
|
2018-08-22 11:51:39 +02:00
|
|
|
|
2019-03-19 16:23:02 +01:00
|
|
|
const cleanerType: { [ id in FetchType ]?: (crawlStartDate: Date) => Bluebird<any> } = {
|
|
|
|
'video-likes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate),
|
|
|
|
'video-dislikes': crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate),
|
|
|
|
'video-shares': crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate),
|
|
|
|
'video-comments': crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate)
|
|
|
|
}
|
|
|
|
|
|
|
|
return crawlCollectionPage(payload.uri, fetcherType[payload.type], cleanerType[payload.type])
|
2017-11-22 10:29:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-01-25 15:05:18 +01:00
|
|
|
processActivityPubHttpFetcher
|
2017-11-22 10:29:55 +01:00
|
|
|
}
|