]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-update.ts
Basic video redundancy implementation
[github/Chocobozzz/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.id)
52 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
53
54 return updateVideoFromAP(video, videoObject, actor.Account, channelActor.VideoChannel, activity.to)
55 }
56
57 async 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)
72 }
73
74 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
75 const actorAttributesToUpdate = activity.object as ActivityPubActor
76
77 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
78 let accountOrChannelInstance: AccountModel | VideoChannelModel
79 let actorFieldsSave: object
80 let accountOrChannelFieldsSave: object
81
82 // Fetch icon?
83 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
84
85 try {
86 await sequelizeTypescript.transaction(async t => {
87 actorFieldsSave = actor.toJSON()
88
89 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
90 else accountOrChannelInstance = actor.Account
91
92 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
93
94 await updateActorInstance(actor, actorAttributesToUpdate)
95
96 if (avatarName !== undefined) {
97 await updateActorAvatarInstance(actor, avatarName, t)
98 }
99
100 await actor.save({ transaction: t })
101
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 })
106 })
107
108 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
109 } catch (err) {
110 if (actor !== undefined && actorFieldsSave !== undefined) {
111 resetSequelizeInstance(actor, actorFieldsSave)
112 }
113
114 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
115 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
116 }
117
118 // This is just a debug because we will retry the insert
119 logger.debug('Cannot update the remote account.', { err })
120 throw err
121 }
122 }