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