]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Add ability to forbid followers
[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
2422c46b
C
98 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
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
2422c46b
C
123 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
124 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
125 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
126 await accountOrChannelInstance.save({ transaction: t })
265ba139
C
127 })
128
2422c46b 129 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
265ba139 130 } catch (err) {
a5625b41
C
131 if (actor !== undefined && actorFieldsSave !== undefined) {
132 resetSequelizeInstance(actor, actorFieldsSave)
265ba139
C
133 }
134
2422c46b
C
135 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
136 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
265ba139
C
137 }
138
139 // This is just a debug because we will retry the insert
d5b7d911 140 logger.debug('Cannot update the remote account.', { err })
265ba139
C
141 throw err
142 }
143}
418d092a
C
144
145async function processUpdatePlaylist (byActor: ActorModel, activity: ActivityUpdate) {
146 const playlistObject = activity.object as PlaylistObject
147 const byAccount = byActor.Account
148
149 if (!byAccount) throw new Error('Cannot update video playlist with the non account actor ' + byActor.url)
150
151 await createOrUpdateVideoPlaylist(playlistObject, byAccount, activity.to)
152}