]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-update.ts
ad3bb392dd8cf4ae121519eaaef939507829b835
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
1 import { ActivityUpdate, CacheFileObject, VideoObject } 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/database'
6 import { AccountModel } from '../../../models/account/account'
7 import { ActorModel } from '../../../models/activitypub/actor'
8 import { VideoChannelModel } from '../../../models/video/video-channel'
9 import { getImageInfoIfExists, updateActorImageInstance, updateActorInstance } from '../actor'
10 import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
11 import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
12 import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
13 import { createOrUpdateCacheFile } from '../cache-file'
14 import { forwardVideoRelatedActivity } from '../send/utils'
15 import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
16 import { createOrUpdateVideoPlaylist } from '../playlist'
17 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
18 import { MActorSignature, MAccountIdActor } from '../../../types/models'
19 import { isRedundancyAccepted } from '@server/lib/redundancy'
20 import { ActorImageType } from '@shared/models'
21
22 async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
23 const { activity, byActor } = options
24
25 const objectType = activity.object.type
26
27 if (objectType === 'Video') {
28 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
29 }
30
31 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
32 // We need more attributes
33 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
34 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
35 }
36
37 if (objectType === 'CacheFile') {
38 // We need more attributes
39 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
40 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
41 }
42
43 if (objectType === 'Playlist') {
44 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
45 }
46
47 return undefined
48 }
49
50 // ---------------------------------------------------------------------------
51
52 export {
53 processUpdateActivity
54 }
55
56 // ---------------------------------------------------------------------------
57
58 async function processUpdateVideo (actor: MActorSignature, activity: ActivityUpdate) {
59 const videoObject = activity.object as VideoObject
60
61 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
62 logger.debug('Video sent by update is not valid.', { videoObject })
63 return undefined
64 }
65
66 const { video, created } = await getOrCreateVideoAndAccountAndChannel({
67 videoObject: videoObject.id,
68 allowRefresh: false,
69 fetchType: 'all'
70 })
71 // We did not have this video, it has been created so no need to update
72 if (created) return
73
74 // Load new channel
75 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
76
77 const account = actor.Account as MAccountIdActor
78 account.Actor = actor
79
80 const updateOptions = {
81 video,
82 videoObject,
83 account,
84 channel: channelActor.VideoChannel,
85 overrideTo: activity.to
86 }
87 return updateVideoFromAP(updateOptions)
88 }
89
90 async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
91 if (await isRedundancyAccepted(activity, byActor) !== true) return
92
93 const cacheFileObject = activity.object as CacheFileObject
94
95 if (!isCacheFileObjectValid(cacheFileObject)) {
96 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
97 return undefined
98 }
99
100 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
101
102 await sequelizeTypescript.transaction(async t => {
103 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
104 })
105
106 if (video.isOwned()) {
107 // Don't resend the activity to the sender
108 const exceptions = [ byActor ]
109
110 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
111 }
112 }
113
114 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
115 const actorAttributesToUpdate = activity.object as ActivityPubActor
116
117 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
118 let accountOrChannelInstance: AccountModel | VideoChannelModel
119 let actorFieldsSave: object
120 let accountOrChannelFieldsSave: object
121
122 // Fetch icon?
123 const avatarInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.AVATAR)
124 const bannerInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.BANNER)
125
126 try {
127 await sequelizeTypescript.transaction(async t => {
128 actorFieldsSave = actor.toJSON()
129
130 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
131 else accountOrChannelInstance = actor.Account
132
133 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
134
135 await updateActorInstance(actor, actorAttributesToUpdate)
136
137 for (const imageInfo of [ avatarInfo, bannerInfo ]) {
138 if (!imageInfo) continue
139
140 const imageOptions = Object.assign({}, imageInfo, { onDisk: false })
141
142 await updateActorImageInstance(actor, imageOptions, t)
143 }
144
145 await actor.save({ transaction: t })
146
147 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
148 accountOrChannelInstance.description = actorAttributesToUpdate.summary
149
150 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
151
152 await accountOrChannelInstance.save({ transaction: t })
153 })
154
155 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
156 } catch (err) {
157 if (actor !== undefined && actorFieldsSave !== undefined) {
158 resetSequelizeInstance(actor, actorFieldsSave)
159 }
160
161 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
162 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
163 }
164
165 // This is just a debug because we will retry the insert
166 logger.debug('Cannot update the remote account.', { err })
167 throw err
168 }
169 }
170
171 async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
172 const playlistObject = activity.object as PlaylistObject
173 const byAccount = byActor.Account
174
175 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
176
177 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
178 }