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