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.id)
52 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
54 return updateVideoFromAP(video, videoObject, actor.Account, channelActor.VideoChannel, activity.to)
57 async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
58 const cacheFileObject = activity.object as CacheFileObject
60 if (!isCacheFileObjectValid(cacheFileObject) === false) {
61 logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
65 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
66 if (!redundancyModel) {
67 const { video } = await getOrCreateVideoAndAccountAndChannel(cacheFileObject.id)
68 return createCacheFile(cacheFileObject, video, byActor)
71 return updateCacheFile(cacheFileObject, redundancyModel, byActor)
74 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
75 const actorAttributesToUpdate = activity.object as ActivityPubActor
77 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
78 let accountOrChannelInstance: AccountModel | VideoChannelModel
79 let actorFieldsSave: object
80 let accountOrChannelFieldsSave: object
83 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
86 await sequelizeTypescript.transaction(async t => {
87 actorFieldsSave = actor.toJSON()
89 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
90 else accountOrChannelInstance = actor.Account
92 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
94 await updateActorInstance(actor, actorAttributesToUpdate)
96 if (avatarName !== undefined) {
97 await updateActorAvatarInstance(actor, avatarName, t)
100 await actor.save({ transaction: t })
102 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
103 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
104 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
105 await accountOrChannelInstance.save({ transaction: t })
108 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
110 if (actor !== undefined && actorFieldsSave !== undefined) {
111 resetSequelizeInstance(actor, actorFieldsSave)
114 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
115 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
118 // This is just a debug because we will retry the insert
119 logger.debug('Cannot update the remote account.', { err })