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

61 lines
2.0 KiB
TypeScript
Raw Normal View History

import * as Bull from 'bull'
2021-06-03 14:30:09 +02:00
import { refreshVideoPlaylistIfNeeded } from '@server/lib/activitypub/playlists'
2021-06-02 09:35:01 +02:00
import { refreshVideoIfNeeded } from '@server/lib/activitypub/videos'
import { loadVideoByUrl } from '@server/lib/model-loaders'
2021-05-11 11:15:29 +02:00
import { RefreshPayload } from '@shared/models'
import { logger } from '../../../helpers/logger'
2021-05-11 11:15:29 +02:00
import { ActorModel } from '../../../models/actor/actor'
import { VideoPlaylistModel } from '../../../models/video/video-playlist'
2021-06-03 16:02:29 +02:00
import { refreshActorIfNeeded } from '../../activitypub/actors'
async function refreshAPObject (job: Bull.Job) {
const payload = job.data as RefreshPayload
2018-12-04 15:12:54 +01:00
2019-01-14 11:30:15 +01:00
logger.info('Processing AP refresher in job %d for %s.', job.id, payload.url)
2019-01-14 11:30:15 +01:00
if (payload.type === 'video') return refreshVideo(payload.url)
2019-03-19 14:13:53 +01:00
if (payload.type === 'video-playlist') return refreshVideoPlaylist(payload.url)
2019-01-14 11:30:15 +01:00
if (payload.type === 'actor') return refreshActor(payload.url)
}
// ---------------------------------------------------------------------------
export {
refreshAPObject
}
// ---------------------------------------------------------------------------
2019-01-14 11:30:15 +01:00
async function refreshVideo (videoUrl: string) {
const fetchType = 'all' as 'all'
const syncParam = { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true }
const videoFromDatabase = await loadVideoByUrl(videoUrl, fetchType)
if (videoFromDatabase) {
const refreshOptions = {
video: videoFromDatabase,
fetchedType: fetchType,
syncParam
}
await refreshVideoIfNeeded(refreshOptions)
}
}
2019-01-14 11:30:15 +01:00
async function refreshActor (actorUrl: string) {
const fetchType = 'all' as 'all'
const actor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorUrl)
if (actor) {
await refreshActorIfNeeded(actor, fetchType)
}
2019-03-19 14:13:53 +01:00
}
async function refreshVideoPlaylist (playlistUrl: string) {
const playlist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(playlistUrl)
2019-01-14 11:30:15 +01:00
2019-03-19 14:13:53 +01:00
if (playlist) {
await refreshVideoPlaylistIfNeeded(playlist)
}
2019-01-14 11:30:15 +01:00
}