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