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