]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/activitypub/process/process-update.ts
Fix videos list user NSFW policy
[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
C
12import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
13import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
14import { createCacheFile, updateCacheFile } from '../cache-file'
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
71 if (!isCacheFileObjectValid(cacheFileObject) === false) {
72 logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
73 return undefined
74 }
75
76 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
77 if (!redundancyModel) {
4157cdb1 78 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.id })
c48e82b5
C
79 return createCacheFile(cacheFileObject, video, byActor)
80 }
81
82 return updateCacheFile(cacheFileObject, redundancyModel, byActor)
0d0e8dd0 83}
265ba139 84
90d4bb81 85async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
2422c46b 86 const actorAttributesToUpdate = activity.object as ActivityPubActor
265ba139 87
2422c46b
C
88 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
89 let accountOrChannelInstance: AccountModel | VideoChannelModel
265ba139 90 let actorFieldsSave: object
2422c46b 91 let accountOrChannelFieldsSave: object
265ba139
C
92
93 // Fetch icon?
2422c46b 94 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
265ba139
C
95
96 try {
97 await sequelizeTypescript.transaction(async t => {
a5625b41 98 actorFieldsSave = actor.toJSON()
265ba139 99
2422c46b
C
100 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
101 else accountOrChannelInstance = actor.Account
102
103 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
104
105 await updateActorInstance(actor, actorAttributesToUpdate)
265ba139 106
a5625b41
C
107 if (avatarName !== undefined) {
108 await updateActorAvatarInstance(actor, avatarName, t)
265ba139
C
109 }
110
111 await actor.save({ transaction: t })
112
2422c46b
C
113 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
114 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
115 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
116 await accountOrChannelInstance.save({ transaction: t })
265ba139
C
117 })
118
2422c46b 119 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
265ba139 120 } catch (err) {
a5625b41
C
121 if (actor !== undefined && actorFieldsSave !== undefined) {
122 resetSequelizeInstance(actor, actorFieldsSave)
265ba139
C
123 }
124
2422c46b
C
125 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
126 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
265ba139
C
127 }
128
129 // This is just a debug because we will retry the insert
d5b7d911 130 logger.debug('Cannot update the remote account.', { err })
265ba139
C
131 throw err
132 }
133}