1 import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
2 import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
3 import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
4 import { logger } from '../../../helpers/logger'
5 import { sequelizeTypescript } from '../../../initializers'
6 import { AccountModel } from '../../../models/account/account'
7 import { ActorModel } from '../../../models/activitypub/actor'
8 import { VideoChannelModel } from '../../../models/video/video-channel'
9 import { fetchAvatarIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor'
10 import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
11 import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
12 import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
13 import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
14 import { createCacheFile, updateCacheFile } from '../cache-file'
16 async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) {
17 const objectType = activity.object.type
19 if (objectType === 'Video') {
20 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
23 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
24 // We need more attributes
25 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
26 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
29 if (objectType === 'CacheFile') {
30 // We need more attributes
31 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
32 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
38 // ---------------------------------------------------------------------------
44 // ---------------------------------------------------------------------------
46 async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
47 const videoObject = activity.object as VideoTorrentObject
49 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
50 logger.debug('Video sent by update is not valid.', { videoObject })
54 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id })
55 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
57 const updateOptions = {
60 account: actor.Account,
61 channel: channelActor.VideoChannel,
63 overrideTo: activity.to
65 return updateVideoFromAP(updateOptions)
68 async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
69 const cacheFileObject = activity.object as CacheFileObject
71 if (!isCacheFileObjectValid(cacheFileObject) === false) {
72 logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
76 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
77 if (!redundancyModel) {
78 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.id })
79 return createCacheFile(cacheFileObject, video, byActor)
82 return updateCacheFile(cacheFileObject, redundancyModel, byActor)
85 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
86 const actorAttributesToUpdate = activity.object as ActivityPubActor
88 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
89 let accountOrChannelInstance: AccountModel | VideoChannelModel
90 let actorFieldsSave: object
91 let accountOrChannelFieldsSave: object
94 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
97 await sequelizeTypescript.transaction(async t => {
98 actorFieldsSave = actor.toJSON()
100 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
101 else accountOrChannelInstance = actor.Account
103 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
105 await updateActorInstance(actor, actorAttributesToUpdate)
107 if (avatarName !== undefined) {
108 await updateActorAvatarInstance(actor, avatarName, t)
111 await actor.save({ transaction: t })
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 })
119 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
121 if (actor !== undefined && actorFieldsSave !== undefined) {
122 resetSequelizeInstance(actor, actorFieldsSave)
125 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
126 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
129 // This is just a debug because we will retry the insert
130 logger.debug('Cannot update the remote account.', { err })