]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Optimize AP fetch
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
CommitLineData
7d9ba5c0 1import { isRedundancyAccepted } from '@server/lib/redundancy'
de6310b2 2import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub'
265ba139 3import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
7d9ba5c0
C
4import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
5import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
6import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
136d7efd 7import { retryTransactionWrapper } from '../../../helpers/database-utils'
da854ddd 8import { logger } from '../../../helpers/logger'
80fdaf06 9import { sequelizeTypescript } from '../../../initializers/database'
7d9ba5c0 10import { ActorModel } from '../../../models/actor/actor'
7d9ba5c0 11import { APProcessorOptions } from '../../../types/activitypub-processor.model'
136d7efd
C
12import { MActorFull, MActorSignature } from '../../../types/models'
13import { APActorUpdater } from '../actors/updater'
b88a4596 14import { createOrUpdateCacheFile } from '../cache-file'
49af5ac8 15import { createOrUpdateVideoPlaylist } from '../playlists'
7d9ba5c0 16import { forwardVideoRelatedActivity } from '../send/utils'
304a84d5 17import { APVideoUpdater, getOrCreateAPVideo } from '../videos'
1198edf4
C
18
19async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
20 const { activity, byActor } = options
0d0e8dd0 21
2422c46b 22 const objectType = activity.object.type
e4f97bab 23
2422c46b 24 if (objectType === 'Video') {
c56faf0d 25 return retryTransactionWrapper(processUpdateVideo, activity)
c48e82b5
C
26 }
27
28 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
e587e0ec
C
29 // We need more attributes
30 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
31 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
e4f97bab 32 }
0d0e8dd0 33
c48e82b5 34 if (objectType === 'CacheFile') {
e587e0ec
C
35 // We need more attributes
36 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
37 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
c48e82b5
C
38 }
39
418d092a
C
40 if (objectType === 'Playlist') {
41 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
42 }
43
3cd0734f 44 return undefined
e4f97bab
C
45}
46
47// ---------------------------------------------------------------------------
48
49export {
50 processUpdateActivity
51}
52
53// ---------------------------------------------------------------------------
54
c56faf0d 55async function processUpdateVideo (activity: ActivityUpdate) {
de6310b2 56 const videoObject = activity.object as VideoObject
50d6de9c 57
3cd0734f
C
58 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
59 logger.debug('Video sent by update is not valid.', { videoObject })
60 return undefined
61 }
2186386c 62
304a84d5 63 const { video, created } = await getOrCreateAPVideo({
92315d97
C
64 videoObject: videoObject.id,
65 allowRefresh: false,
66 fetchType: 'all'
67 })
68 // We did not have this video, it has been created so no need to update
69 if (created) return
70
c56faf0d
C
71 const updater = new APVideoUpdater(videoObject, video)
72 return updater.update(activity.to)
c48e82b5
C
73}
74
453e83ea 75async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
8c9e7875
C
76 if (await isRedundancyAccepted(activity, byActor) !== true) return
77
c48e82b5
C
78 const cacheFileObject = activity.object as CacheFileObject
79
e5565833
C
80 if (!isCacheFileObjectValid(cacheFileObject)) {
81 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
c48e82b5
C
82 return undefined
83 }
84
304a84d5 85 const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })
e5565833
C
86
87 await sequelizeTypescript.transaction(async t => {
b88a4596 88 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
e5565833 89 })
c48e82b5 90
e5565833
C
91 if (video.isOwned()) {
92 // Don't resend the activity to the sender
93 const exceptions = [ byActor ]
94
95 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
96 }
0d0e8dd0 97}
265ba139 98
136d7efd
C
99async function processUpdateActor (actor: MActorFull, activity: ActivityUpdate) {
100 const actorObject = activity.object as ActivityPubActor
265ba139 101
136d7efd 102 logger.debug('Updating remote account "%s".', actorObject.url)
265ba139 103
136d7efd
C
104 const updater = new APActorUpdater(actorObject, actor)
105 return updater.update()
265ba139 106}
418d092a 107
453e83ea 108async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
418d092a
C
109 const playlistObject = activity.object as PlaylistObject
110 const byAccount = byActor.Account
111
112 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
113
37a44fc9 114 await createOrUpdateVideoPlaylist(playlistObject, activity.to)
418d092a 115}