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'
15 import { forwardVideoRelatedActivity } from '../send/utils'
17 async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) {
18 const objectType = activity.object.type
20 if (objectType === 'Video') {
21 return retryTransactionWrapper(processUpdateVideo, byActor, activity)
24 if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
25 // We need more attributes
26 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
27 return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
30 if (objectType === 'CacheFile') {
31 // We need more attributes
32 const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
33 return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
39 // ---------------------------------------------------------------------------
45 // ---------------------------------------------------------------------------
47 async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
48 const videoObject = activity.object as VideoTorrentObject
50 if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
51 logger.debug('Video sent by update is not valid.', { videoObject })
55 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id })
56 const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
58 const updateOptions = {
61 account: actor.Account,
62 channel: channelActor.VideoChannel,
64 overrideTo: activity.to
66 return updateVideoFromAP(updateOptions)
69 async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
70 const cacheFileObject = activity.object as CacheFileObject
72 if (!isCacheFileObjectValid(cacheFileObject)) {
73 logger.debug('Cache file object sent by update is not valid.', { cacheFileObject })
77 const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object })
79 await sequelizeTypescript.transaction(async t => {
80 const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t)
82 if (!redundancyModel) {
83 await createCacheFile(cacheFileObject, video, byActor, t)
85 await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t)
89 if (video.isOwned()) {
90 // Don't resend the activity to the sender
91 const exceptions = [ byActor ]
93 await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
97 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
98 const actorAttributesToUpdate = activity.object as ActivityPubActor
100 logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)
101 let accountOrChannelInstance: AccountModel | VideoChannelModel
102 let actorFieldsSave: object
103 let accountOrChannelFieldsSave: object
106 const avatarName = await fetchAvatarIfExists(actorAttributesToUpdate)
109 await sequelizeTypescript.transaction(async t => {
110 actorFieldsSave = actor.toJSON()
112 if (actorAttributesToUpdate.type === 'Group') accountOrChannelInstance = actor.VideoChannel
113 else accountOrChannelInstance = actor.Account
115 accountOrChannelFieldsSave = accountOrChannelInstance.toJSON()
117 await updateActorInstance(actor, actorAttributesToUpdate)
119 if (avatarName !== undefined) {
120 await updateActorAvatarInstance(actor, avatarName, t)
123 await actor.save({ transaction: t })
125 accountOrChannelInstance.set('name', actorAttributesToUpdate.name || actorAttributesToUpdate.preferredUsername)
126 accountOrChannelInstance.set('description', actorAttributesToUpdate.summary)
127 accountOrChannelInstance.set('support', actorAttributesToUpdate.support)
128 await accountOrChannelInstance.save({ transaction: t })
131 logger.info('Remote account with uuid %s updated', actorAttributesToUpdate.uuid)
133 if (actor !== undefined && actorFieldsSave !== undefined) {
134 resetSequelizeInstance(actor, actorFieldsSave)
137 if (accountOrChannelInstance !== undefined && accountOrChannelFieldsSave !== undefined) {
138 resetSequelizeInstance(accountOrChannelInstance, accountOrChannelFieldsSave)
141 // This is just a debug because we will retry the insert
142 logger.debug('Cannot update the remote account.', { err })