]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Adding frontend peertubeHelpers.getBaseRouterRoute. (#4153)
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
CommitLineData
7d9ba5c0
C
1import { isRedundancyAccepted } from '@server/lib/redundancy'
2import { ActorImageType } from '@shared/models'
de6310b2 3import { ActivityUpdate, CacheFileObject, VideoObject } from '../../../../shared/models/activitypub'
265ba139 4import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
7d9ba5c0
C
5import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
6import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
7import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
06215f15 8import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
da854ddd 9import { logger } from '../../../helpers/logger'
80fdaf06 10import { sequelizeTypescript } from '../../../initializers/database'
265ba139 11import { AccountModel } from '../../../models/account/account'
7d9ba5c0 12import { ActorModel } from '../../../models/actor/actor'
2422c46b 13import { VideoChannelModel } from '../../../models/video/video-channel'
7d9ba5c0 14import { APProcessorOptions } from '../../../types/activitypub-processor.model'
c56faf0d 15import { MActorSignature } from '../../../types/models'
2cb03dc1 16import { getImageInfoIfExists, updateActorImageInstance, updateActorInstance } from '../actor'
b88a4596 17import { createOrUpdateCacheFile } from '../cache-file'
418d092a 18import { createOrUpdateVideoPlaylist } from '../playlist'
7d9ba5c0 19import { forwardVideoRelatedActivity } from '../send/utils'
304a84d5 20import { APVideoUpdater, getOrCreateAPVideo } from '../videos'
1198edf4
C
21
22async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
23 const { activity, byActor } = options
0d0e8dd0 24
2422c46b 25 const objectType = activity.object.type
e4f97bab 26
2422c46b 27 if (objectType === 'Video') {
c56faf0d 28 return retryTransactionWrapper(processUpdateVideo, activity)
c48e82b5
C
29 }
30
31 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
e587e0ec
C
32 // We need more attributes
33 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
34 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
e4f97bab 35 }
0d0e8dd0 36
c48e82b5 37 if (objectType === 'CacheFile') {
e587e0ec
C
38 // We need more attributes
39 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
40 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
c48e82b5
C
41 }
42
418d092a
C
43 if (objectType === 'Playlist') {
44 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
45 }
46
3cd0734f 47 return undefined
e4f97bab
C
48}
49
50// ---------------------------------------------------------------------------
51
52export {
53 processUpdateActivity
54}
55
56// ---------------------------------------------------------------------------
57
c56faf0d 58async function processUpdateVideo (activity: ActivityUpdate) {
de6310b2 59 const videoObject = activity.object as VideoObject
50d6de9c 60
3cd0734f
C
61 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
62 logger.debug('Video sent by update is not valid.', { videoObject })
63 return undefined
64 }
2186386c 65
304a84d5 66 const { video, created } = await getOrCreateAPVideo({
92315d97
C
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
c56faf0d
C
74 const updater = new APVideoUpdater(videoObject, video)
75 return updater.update(activity.to)
c48e82b5
C
76}
77
453e83ea 78async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
8c9e7875
C
79 if (await isRedundancyAccepted(activity, byActor) !== true) return
80
c48e82b5
C
81 const cacheFileObject = activity.object as CacheFileObject
82
e5565833
C
83 if (!isCacheFileObjectValid(cacheFileObject)) {
84 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
c48e82b5
C
85 return undefined
86 }
87
304a84d5 88 const { video } = await getOrCreateAPVideo({ videoObject: cacheFileObject.object })
e5565833
C
89
90 await sequelizeTypescript.transaction(async t => {
b88a4596 91 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
e5565833 92 })
c48e82b5 93
e5565833
C
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 }
0d0e8dd0 100}
265ba139 101
90d4bb81 102async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
2422c46b 103 const actorAttributesToUpdate = activity.object as ActivityPubActor
265ba139 104
57cfff78 105 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
2422c46b 106 let accountOrChannelInstance: AccountModel | VideoChannelModel
265ba139 107 let actorFieldsSave: object
2422c46b 108 let accountOrChannelFieldsSave: object
265ba139
C
109
110 // Fetch icon?
2cb03dc1
C
111 const avatarInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.AVATAR)
112 const bannerInfo = getImageInfoIfExists(actorAttributesToUpdate, ActorImageType.BANNER)
265ba139
C
113
114 try {
115 await sequelizeTypescript.transaction(async t => {
a5625b41 116 actorFieldsSave = actor.toJSON()
265ba139 117
2422c46b
C
118 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
119 else accountOrChannelInstance = actor.Account
120
121 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
122
123 await updateActorInstance(actor, actorAttributesToUpdate)
265ba139 124
213e30ef
C
125 await updateActorImageInstance(actor, ActorImageType.AVATAR, avatarInfo, t)
126 await updateActorImageInstance(actor, ActorImageType.BANNER, bannerInfo, t)
265ba139
C
127
128 await actor.save({ transaction: t })
129
1735c825
C
130 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
131 accountOrChannelInstance.description = actorAttributesToUpdate.summary
132
133 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
134
2422c46b 135 await accountOrChannelInstance.save({ transaction: t })
265ba139
C
136 })
137
57cfff78 138 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
265ba139 139 } catch (err) {
a5625b41
C
140 if (actor !== undefined && actorFieldsSave !== undefined) {
141 resetSequelizeInstance(actor, actorFieldsSave)
265ba139
C
142 }
143
2422c46b
C
144 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
145 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
265ba139
C
146 }
147
148 // This is just a debug because we will retry the insert
d5b7d911 149 logger.debug('Cannot update the remote account.', { err })
265ba139
C
150 throw err
151 }
152}
418d092a 153
453e83ea 154async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
418d092a
C
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}