]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/activitypub/process/process-update.ts
98ab0f83def9e597de1ecfe6291ccacb468ddd36
[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/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 '../../../typings/activitypub-processor.model'
18 import { MActorSignature, MAccountIdActor } from '../../../typings/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 VideoTorrentObject
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 } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id, allowRefresh: false, fetchType: 'all' })
66 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
67
68 const account = actor.Account as MAccountIdActor
69 account.Actor = actor
70
71 const updateOptions = {
72 video,
73 videoObject,
74 account,
75 channel: channelActor.VideoChannel,
76 overrideTo: activity.to
77 }
78 return updateVideoFromAP(updateOptions)
79 }
80
81 async function processUpdateCacheFile (byActor: MActorSignature, activity: ActivityUpdate) {
82 if (await isRedundancyAccepted(activity, byActor) !== true) return
83
84 const cacheFileObject = activity.object as CacheFileObject
85
86 if (!isCacheFileObjectValid(cacheFileObject)) {
87 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
88 return undefined
89 }
90
91 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
92
93 await sequelizeTypescript.transaction(async t => {
94 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
95 })
96
97 if (video.isOwned()) {
98 // Don't resend the activity to the sender
99 const exceptions = [ byActor ]
100
101 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
102 }
103 }
104
105 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
106 const actorAttributesToUpdate = activity.object as ActivityPubActor
107
108 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.url)
109 let accountOrChannelInstance: AccountModel | VideoChannelModel
110 let actorFieldsSave: object
111 let accountOrChannelFieldsSave: object
112
113 // Fetch icon?
114 const avatarInfo = await getAvatarInfoIfExists(actorAttributesToUpdate)
115
116 try {
117 await sequelizeTypescript.transaction(async t => {
118 actorFieldsSave = actor.toJSON()
119
120 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
121 else accountOrChannelInstance = actor.Account
122
123 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
124
125 await updateActorInstance(actor, actorAttributesToUpdate)
126
127 if (avatarInfo !== undefined) {
128 const avatarOptions = Object.assign({}, avatarInfo, { onDisk: false })
129
130 await updateActorAvatarInstance(actor, avatarOptions, t)
131 }
132
133 await actor.save({ transaction: t })
134
135 accountOrChannelInstance.name = actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername
136 accountOrChannelInstance.description = actorAttributesToUpdate.summary
137
138 if (accountOrChannelInstance instanceof VideoChannelModel) accountOrChannelInstance.support = actorAttributesToUpdate.support
139
140 await accountOrChannelInstance.save({ transaction: t })
141 })
142
143 logger.info('Remote account %s updated', actorAttributesToUpdate.url)
144 } catch (err) {
145 if (actor !== undefined && actorFieldsSave !== undefined) {
146 resetSequelizeInstance(actor, actorFieldsSave)
147 }
148
149 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
150 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
151 }
152
153 // This is just a debug because we will retry the insert
154 logger.debug('Cannot update the remote account.', { err })
155 throw err
156 }
157 }
158
159 async function processUpdatePlaylist (byActor: MActorSignature, activity: ActivityUpdate) {
160 const playlistObject = activity.object as PlaylistObject
161 const byAccount = byActor.Account
162
163 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
164
165 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
166 }