]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-update.ts
516bd8d706818c37cb6b1331392bfc323ec3adef
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
1 import { isRedundancyAccepted } from '@server/lib/redundancy'
2 import { ActorImageType } from '@shared/models'
3 import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub'
4 import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
5 import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
6 import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
7 import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
8 import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
9 import { logger } from '../../../helpers/logger'
10 import { sequelizeTypescript } from '../../../initializers/database'
11 import { AccountModel } from '../../../models/account/account'
12 import { ActorModel } from '../../../models/actor/actor'
13 import { VideoChannelModel } from '../../../models/video/video-channel'
14 import { APProcessorOptions } from '../../../types/activitypub-processor.model'
15 import { MActorSignature } from '../../../types/models'
16 import { getImageInfoIfExists, updateActorImageInstance, updateActorInstance } from '../actor'
17 import { createOrUpdateCacheFile } from '../cache-file'
18 import { createOrUpdateVideoPlaylist } from '../playlist'
19 import { forwardVideoRelatedActivity } from '../send/utils'
20 import { APVideoUpdater, getOrCreateVideoAndAccountAndChannel } from '../videos'
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, 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 (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 const updater = new APVideoUpdater(videoObject, video)
75 return updater.update(activity.to)
76 }
77
78 async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
79 if (await isRedundancyAccepted(activity, byActor) !== true) return
80
81 const cacheFileObject = activity.object as CacheFileObject
82
83 if (!isCacheFileObjectValid(cacheFileObject)) {
84 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
85 return undefined
86 }
87
88 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
89
90 await sequelizeTypescript.transaction(async t => {
91 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
92 })
93
94 if (video.isOwned()) {
95 // Don't resend the activity to the sender
96 const exceptions = [ byActor ]
97
98 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
99 }
100 }
101
102 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
103 const actorAttributesToUpdate = activity.object as ActivityPubActor
104
105 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
106 let accountOrChannelInstance: AccountModel | VideoChannelModel
107 let actorFieldsSave: object
108 let accountOrChannelFieldsSave: object
109
110 // Fetch icon?
111 const avatarInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.AVATAR)
112 const bannerInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.BANNER)
113
114 try {
115 await sequelizeTypescript.transaction(async t => {
116 actorFieldsSave = actor.toJSON()
117
118 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
119 else accountOrChannelInstance = actor.Account
120
121 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
122
123 await updateActorInstance(actor, actorAttributesToUpdate)
124
125 await updateActorImageInstance(actor, ActorImageType.AVATAR, avatarInfo, t)
126 await updateActorImageInstance(actor, ActorImageType.BANNER, bannerInfo, t)
127
128 await actor.save({ transaction: t })
129
130 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
131 accountOrChannelInstance.description = actorAttributesToUpdate.summary
132
133 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
134
135 await accountOrChannelInstance.save({ transaction: t })
136 })
137
138 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
139 } catch (err) {
140 if (actor !== undefined && actorFieldsSave !== undefined) {
141 resetSequelizeInstance(actor, actorFieldsSave)
142 }
143
144 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
145 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
146 }
147
148 // This is just a debug because we will retry the insert
149 logger.debug('Cannot update the remote account.', { err })
150 throw err
151 }
152 }
153
154 async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
155 const playlistObject = activity.object as PlaylistObject
156 const byAccount = byActor.Account
157
158 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
159
160 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
161 }