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