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