]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/job-queue/handlers/activitypub-refresher.ts
Create a dedicated table to track video thumbnails
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / activitypub-refresher.ts
1 import * as Bull from 'bull'
2 import { logger } from '../../../helpers/logger'
3 import { fetchVideoByUrl } from '../../../helpers/video'
4 import { refreshActorIfNeeded, refreshVideoIfNeeded, refreshVideoPlaylistIfNeeded } from '../../activitypub'
5 import { ActorModel } from '../../../models/activitypub/actor'
6 import { VideoPlaylistModel } from '../../../models/video/video-playlist'
7
8 export type RefreshPayload = {
9 type: 'video' | 'video-playlist' | 'actor'
10 url: string
11 }
12
13 async function refreshAPObject (job: Bull.Job) {
14 const payload = job.data as RefreshPayload
15
16 logger.info('Processing AP refresher in job %d for %s.', job.id, payload.url)
17
18 if (payload.type === 'video') return refreshVideo(payload.url)
19 if (payload.type === 'video-playlist') return refreshVideoPlaylist(payload.url)
20 if (payload.type === 'actor') return refreshActor(payload.url)
21 }
22
23 // ---------------------------------------------------------------------------
24
25 export {
26 refreshAPObject
27 }
28
29 // ---------------------------------------------------------------------------
30
31 async function refreshVideo (videoUrl: string) {
32 const fetchType = 'all' as 'all'
33 const syncParam = { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true }
34
35 const videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType)
36 if (videoFromDatabase) {
37 const refreshOptions = {
38 video: videoFromDatabase,
39 fetchedType: fetchType,
40 syncParam
41 }
42
43 await refreshVideoIfNeeded(refreshOptions)
44 }
45 }
46
47 async function refreshActor (actorUrl: string) {
48 const fetchType = 'all' as 'all'
49 const actor = await ActorModel.loadByUrlAndPopulateAccountAndChannel(actorUrl)
50
51 if (actor) {
52 await refreshActorIfNeeded(actor, fetchType)
53 }
54 }
55
56 async function refreshVideoPlaylist (playlistUrl: string) {
57 const playlist = await VideoPlaylistModel.loadByUrlAndPopulateAccount(playlistUrl)
58
59 if (playlist) {
60 await refreshVideoPlaylistIfNeeded(playlist)
61 }
62 }