-import { ActivityUpdate, VideoTorrentObject } from '../../../../shared/models/activitypub'
+import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
import { logger } from '../../../helpers/logger'
import { AccountModel } from '../../../models/account/account'
import { ActorModel } from '../../../models/activitypub/actor'
import { VideoChannelModel } from '../../../models/video/video-channel'
-import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
-import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannel, updateVideoFromAP } from '../videos'
+import { fetchAvatarIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor'
+import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
+import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
+import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
+import { createCacheFile, updateCacheFile } from '../cache-file'
-async function processUpdateActivity (activity: ActivityUpdate) {
- const actor = await getOrCreateActorAndServerAndModel(activity.actor)
+async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) {
const objectType = activity.object.type
if (objectType === 'Video') {
- return retryTransactionWrapper(processUpdateVideo, actor, activity)
- } else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
- return retryTransactionWrapper(processUpdateActor, actor, activity)
+ return retryTransactionWrapper(processUpdateVideo, byActor, activity)
+ }
+
+ if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
+ // We need more attributes
+ const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
+ return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
+ }
+
+ if (objectType === 'CacheFile') {
+ // We need more attributes
+ const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
+ return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
}
return undefined
return undefined
}
- const { video } = await getOrCreateVideoAndAccountAndChannel(videoObject.id)
- const channelActor = await getOrCreateVideoChannel(videoObject)
+ const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id })
+ const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
+
+ const updateOptions = {
+ video,
+ videoObject,
+ account: actor.Account,
+ channel: channelActor.VideoChannel,
+ updateViews: true,
+ overrideTo: activity.to
+ }
+ return updateVideoFromAP(updateOptions)
+}
+
+async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
+ const cacheFileObject = activity.object as CacheFileObject
+
+ if (!isCacheFileObjectValid(cacheFileObject) === false) {
+ logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
+ return undefined
+ }
+
+ const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
+ if (!redundancyModel) {
+ const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.id })
+ return createCacheFile(cacheFileObject, video, byActor)
+ }
- return updateVideoFromAP(video, videoObject, actor, channelActor, activity.to)
+ return updateCacheFile(cacheFileObject, redundancyModel, byActor)
}
async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {