]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Optimize SQL requests of videos AP endpoints
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
CommitLineData
c48e82b5 1import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
265ba139 2import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
06215f15 3import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
da854ddd 4import { logger } from '../../../helpers/logger'
3fd3ab2d 5import { sequelizeTypescript } from '../../../initializers'
265ba139 6import { AccountModel } from '../../../models/account/account'
50d6de9c 7import { ActorModel } from '../../../models/activitypub/actor'
2422c46b 8import { VideoChannelModel } from '../../../models/video/video-channel'
a5625b41 9import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
c48e82b5 10import { getOrCreateVideoAndAccountAndChannel, updateVideoFromAP, getOrCreateVideoChannelFromVideoObject } from '../videos'
3cd0734f 11import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
c48e82b5
C
12import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
13import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
14import { createCacheFile, updateCacheFile } from '../cache-file'
0d0e8dd0
C
15
16async function processUpdateActivity (activity: ActivityUpdate) {
50d6de9c 17 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
2422c46b 18 const objectType = activity.object.type
e4f97bab 19
2422c46b 20 if (objectType === 'Video') {
90d4bb81 21 return retryTransactionWrapper(processUpdateVideo, actor, activity)
c48e82b5
C
22 }
23
24 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
90d4bb81 25 return retryTransactionWrapper(processUpdateActor, actor, activity)
e4f97bab 26 }
0d0e8dd0 27
c48e82b5
C
28 if (objectType === 'CacheFile') {
29 return retryTransactionWrapper(processUpdateCacheFile, actor, activity)
30 }
31
3cd0734f 32 return undefined
e4f97bab
C
33}
34
35// ---------------------------------------------------------------------------
36
37export {
38 processUpdateActivity
39}
40
41// ---------------------------------------------------------------------------
42
90d4bb81 43async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
3cd0734f 44 const videoObject = activity.object as VideoTorrentObject
50d6de9c 45
3cd0734f
C
46 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
47 logger.debug('Video sent by update is not valid.', { videoObject })
48 return undefined
49 }
2186386c 50
1297eb5d 51 const { video } = await getOrCreateVideoAndAccountAndChannel(videoObject.id)
f37dc0dd 52 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
2ccaeeb3 53
c48e82b5
C
54 return updateVideoFromAP(video, videoObject, actor.Account, channelActor.VideoChannel, activity.to)
55}
56
57async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
58 const cacheFileObject = activity.object as CacheFileObject
59
60 if (!isCacheFileObjectValid(cacheFileObject) === false) {
61 logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
62 return undefined
63 }
64
65 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
66 if (!redundancyModel) {
67 const { video } = await getOrCreateVideoAndAccountAndChannel(cacheFileObject.id)
68 return createCacheFile(cacheFileObject, video, byActor)
69 }
70
71 return updateCacheFile(cacheFileObject, redundancyModel, byActor)
0d0e8dd0 72}
265ba139 73
90d4bb81 74async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
2422c46b 75 const actorAttributesToUpdate = activity.object as ActivityPubActor
265ba139 76
2422c46b
C
77 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
78 let accountOrChannelInstance: AccountModel | VideoChannelModel
265ba139 79 let actorFieldsSave: object
2422c46b 80 let accountOrChannelFieldsSave: object
265ba139
C
81
82 // Fetch icon?
2422c46b 83 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
265ba139
C
84
85 try {
86 await sequelizeTypescript.transaction(async t => {
a5625b41 87 actorFieldsSave = actor.toJSON()
265ba139 88
2422c46b
C
89 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
90 else accountOrChannelInstance = actor.Account
91
92 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
93
94 await updateActorInstance(actor, actorAttributesToUpdate)
265ba139 95
a5625b41
C
96 if (avatarName !== undefined) {
97 await updateActorAvatarInstance(actor, avatarName, t)
265ba139
C
98 }
99
100 await actor.save({ transaction: t })
101
2422c46b
C
102 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
103 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
104 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
105 await accountOrChannelInstance.save({ transaction: t })
265ba139
C
106 })
107
2422c46b 108 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
265ba139 109 } catch (err) {
a5625b41
C
110 if (actor !== undefined && actorFieldsSave !== undefined) {
111 resetSequelizeInstance(actor, actorFieldsSave)
265ba139
C
112 }
113
2422c46b
C
114 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
115 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
265ba139
C
116 }
117
118 // This is just a debug because we will retry the insert
d5b7d911 119 logger.debug('Cannot update the remote account.', { err })
265ba139
C
120 throw err
121 }
122}