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, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
10 import { getOrCreateVideoAndAccountAndChannel, updateVideoFromAP, getOrCreateVideoChannelFromVideoObject } 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) {
17 const actor = await getOrCreateActorAndServerAndModel(activity.actor)
18 const objectType = activity.object.type
20 if (objectType === 'Video') {
21 return retryTransactionWrapper(processUpdateVideo, actor, activity)
24 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
25 return retryTransactionWrapper(processUpdateActor, actor, activity)
28 if (objectType === 'CacheFile') {
29 return retryTransactionWrapper(processUpdateCacheFile, actor, activity)
35 // ---------------------------------------------------------------------------
41 // ---------------------------------------------------------------------------
43 async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
44 const videoObject = activity.object as VideoTorrentObject
46 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
47 logger.debug('Video sent by update is not valid.', { videoObject })
51 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id })
52 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
54 const updateOptions = {
57 account: actor.Account,
58 channel: channelActor.VideoChannel,
60 overrideTo: activity.to
62 return updateVideoFromAP(updateOptions)
65 async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
66 const cacheFileObject = activity.object as CacheFileObject
68 if (!isCacheFileObjectValid(cacheFileObject) === false) {
69 logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
73 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
74 if (!redundancyModel) {
75 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.id })
76 return createCacheFile(cacheFileObject, video, byActor)
79 return updateCacheFile(cacheFileObject, redundancyModel, byActor)
82 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
83 const actorAttributesToUpdate = activity.object as ActivityPubActor
85 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
86 let accountOrChannelInstance: AccountModel | VideoChannelModel
87 let actorFieldsSave: object
88 let accountOrChannelFieldsSave: object
91 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
94 await sequelizeTypescript.transaction(async t => {
95 actorFieldsSave = actor.toJSON()
97 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
98 else accountOrChannelInstance = actor.Account
100 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
102 await updateActorInstance(actor, actorAttributesToUpdate)
104 if (avatarName !== undefined) {
105 await updateActorAvatarInstance(actor, avatarName, t)
108 await actor.save({ transaction: t })
110 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
111 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
112 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
113 await accountOrChannelInstance.save({ transaction: t })
116 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
118 if (actor !== undefined && actorFieldsSave !== undefined) {
119 resetSequelizeInstance(actor, actorFieldsSave)
122 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
123 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
126 // This is just a debug because we will retry the insert
127 logger.debug('Cannot update the remote account.', { err })