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