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 { createOrUpdateCacheFile } from '../cache-file'
14 import { forwardVideoRelatedActivity } from '../send/utils'
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)) {
72 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
76 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
78 await sequelizeTypescript.transaction(async t => {
79 await createOrUpdateCacheFile(cacheFileObject, video, byActor, t)
82 if (video.isOwned()) {
83 // Don't resend the activity to the sender
84 const exceptions = [ byActor ]
86 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
90 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
91 const actorAttributesToUpdate = activity.object as ActivityPubActor
93 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
94 let accountOrChannelInstance: AccountModel | VideoChannelModel
95 let actorFieldsSave: object
96 let accountOrChannelFieldsSave: object
99 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
102 await sequelizeTypescript.transaction(async t => {
103 actorFieldsSave = actor.toJSON()
105 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
106 else accountOrChannelInstance = actor.Account
108 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
110 await updateActorInstance(actor, actorAttributesToUpdate)
112 if (avatarName !== undefined) {
113 await updateActorAvatarInstance(actor, avatarName, t)
116 await actor.save({ transaction: t })
118 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
119 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
120 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
121 await accountOrChannelInstance.save({ transaction: t })
124 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
126 if (actor !== undefined && actorFieldsSave !== undefined) {
127 resetSequelizeInstance(actor, actorFieldsSave)
130 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
131 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
134 // This is just a debug because we will retry the insert
135 logger.debug('Cannot update the remote account.', { err })