]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Merge branch 'release/1.4.0' into develop
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
CommitLineData
c48e82b5 1import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
265ba139 2import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
06215f15 3import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
da854ddd 4import { logger } from '../../../helpers/logger'
3fd3ab2d 5import { sequelizeTypescript } from '../../../initializers'
265ba139 6import { AccountModel } from '../../../models/account/account'
50d6de9c 7import { ActorModel } from '../../../models/activitypub/actor'
2422c46b 8import { VideoChannelModel } from '../../../models/video/video-channel'
557b13ae 9import { getAvatarInfoIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor'
e587e0ec 10import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
3cd0734f 11import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
c48e82b5 12import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
b88a4596 13import { createOrUpdateCacheFile } from '../cache-file'
e5565833 14import { forwardVideoRelatedActivity } from '../send/utils'
418d092a
C
15import { PlaylistObject } from '../../../../shared/models/activitypub/objects/playlist-object'
16import { createOrUpdateVideoPlaylist } from '../playlist'
1198edf4 17import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
f92e7f76 18import { MActorSignature, MAccountIdActor } from '../../../typings/models'
1198edf4
C
19
20async function processUpdateActivity (options: APProcessorOptions<ActivityUpdate>) {
21 const { activity, byActor } = options
0d0e8dd0 22
2422c46b 23 const objectType = activity.object.type
e4f97bab 24
2422c46b 25 if (objectType === 'Video') {
e587e0ec 26 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
c48e82b5
C
27 }
28
29 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
e587e0ec
C
30 // We need more attributes
31 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
32 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
e4f97bab 33 }
0d0e8dd0 34
c48e82b5 35 if (objectType === 'CacheFile') {
e587e0ec
C
36 // We need more attributes
37 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
38 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
c48e82b5
C
39 }
40
418d092a
C
41 if (objectType === 'Playlist') {
42 return retryTransactionWrapper(processUpdatePlaylist, byActor, activity)
43 }
44
3cd0734f 45 return undefined
e4f97bab
C
46}
47
48// ---------------------------------------------------------------------------
49
50export {
51 processUpdateActivity
52}
53
54// ---------------------------------------------------------------------------
55
453e83ea 56async function processUpdateVideo (actor: MActorSignature, activity: ActivityUpdate) {
3cd0734f 57 const videoObject = activity.object as VideoTorrentObject
50d6de9c 58
3cd0734f
C
59 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
60 logger.debug('Video sent by update is not valid.', { videoObject })
61 return undefined
62 }
2186386c 63
453e83ea 64 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false, fetchType: 'all' })
f37dc0dd 65 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
2ccaeeb3 66
f92e7f76
C
67 const account = actor.Account as MAccountIdActor
68 account.Actor = actor
69
d4defe07
C
70 const updateOptions = {
71 video,
72 videoObject,
f92e7f76 73 account,
d4defe07 74 channel: channelActor.VideoChannel,
d4defe07
C
75 overrideTo: activity.to
76 }
77 return updateVideoFromAP(updateOptions)
c48e82b5
C
78}
79
453e83ea 80async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
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
e5565833
C
88 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
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?
557b13ae 111 const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate)
265ba139
C
112
113 try {
114 await sequelizeTypescript.transaction(async t => {
a5625b41 115 actorFieldsSave = actor.toJSON()
265ba139 116
2422c46b
C
117 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
118 else accountOrChannelInstance = actor.Account
119
120 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
121
122 await updateActorInstance(actor, actorAttributesToUpdate)
265ba139 123
557b13ae
C
124 if (avatarInfo !== undefined) {
125 const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false })
126
127 await updateActorAvatarInstance(actor, avatarOptions, t)
265ba139
C
128 }
129
130 await actor.save({ transaction: t })
131
1735c825
C
132 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
133 accountOrChannelInstance.description = actorAttributesToUpdate.summary
134
135 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
136
2422c46b 137 await accountOrChannelInstance.save({ transaction: t })
265ba139
C
138 })
139
57cfff78 140 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
265ba139 141 } catch (err) {
a5625b41
C
142 if (actor !== undefined && actorFieldsSave !== undefined) {
143 resetSequelizeInstance(actor, actorFieldsSave)
265ba139
C
144 }
145
2422c46b
C
146 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
147 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
265ba139
C
148 }
149
150 // This is just a debug because we will retry the insert
d5b7d911 151 logger.debug('Cannot update the remote account.', { err })
265ba139
C
152 throw err
153 }
154}
418d092a 155
453e83ea 156async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
418d092a
C
157 const playlistObject = activity.object as PlaylistObject
158 const byAccount = byActor.Account
159
160 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
161
162 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
163}