]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Bumped to version v1.0.0-beta.13
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
CommitLineData
3cd0734f 1import { ActivityUpdate, 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'
a5625b41 9import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
f37dc0dd 10import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
3cd0734f 11import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
0d0e8dd0
C
12
13async function processUpdateActivity (activity: ActivityUpdate) {
50d6de9c 14 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
2422c46b 15 const objectType = activity.object.type
e4f97bab 16
2422c46b 17 if (objectType === 'Video') {
90d4bb81 18 return retryTransactionWrapper(processUpdateVideo, actor, activity)
2422c46b 19 } else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
90d4bb81 20 return retryTransactionWrapper(processUpdateActor, actor, activity)
e4f97bab 21 }
0d0e8dd0 22
3cd0734f 23 return undefined
e4f97bab
C
24}
25
26// ---------------------------------------------------------------------------
27
28export {
29 processUpdateActivity
30}
31
32// ---------------------------------------------------------------------------
33
90d4bb81 34async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
3cd0734f 35 const videoObject = activity.object as VideoTorrentObject
50d6de9c 36
3cd0734f
C
37 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
38 logger.debug('Video sent by update is not valid.', { videoObject })
39 return undefined
40 }
2186386c 41
1297eb5d 42 const { video } = await getOrCreateVideoAndAccountAndChannel(videoObject.id)
f37dc0dd 43 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
2ccaeeb3 44
1297eb5d 45 return updateVideoFromAP(video, videoObject, actor, channelActor, activity.to)
0d0e8dd0 46}
265ba139 47
90d4bb81 48async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
2422c46b 49 const actorAttributesToUpdate = activity.object as ActivityPubActor
265ba139 50
2422c46b
C
51 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
52 let accountOrChannelInstance: AccountModel | VideoChannelModel
265ba139 53 let actorFieldsSave: object
2422c46b 54 let accountOrChannelFieldsSave: object
265ba139
C
55
56 // Fetch icon?
2422c46b 57 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
265ba139
C
58
59 try {
60 await sequelizeTypescript.transaction(async t => {
a5625b41 61 actorFieldsSave = actor.toJSON()
265ba139 62
2422c46b
C
63 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
64 else accountOrChannelInstance = actor.Account
65
66 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
67
68 await updateActorInstance(actor, actorAttributesToUpdate)
265ba139 69
a5625b41
C
70 if (avatarName !== undefined) {
71 await updateActorAvatarInstance(actor, avatarName, t)
265ba139
C
72 }
73
74 await actor.save({ transaction: t })
75
2422c46b
C
76 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
77 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
78 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
79 await accountOrChannelInstance.save({ transaction: t })
265ba139
C
80 })
81
2422c46b 82 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
265ba139 83 } catch (err) {
a5625b41
C
84 if (actor !== undefined && actorFieldsSave !== undefined) {
85 resetSequelizeInstance(actor, actorFieldsSave)
265ba139
C
86 }
87
2422c46b
C
88 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
89 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
265ba139
C
90 }
91
92 // This is just a debug because we will retry the insert
d5b7d911 93 logger.debug('Cannot update the remote account.', { err })
265ba139
C
94 throw err
95 }
96}