From b88a459664957d6ab9c417a6749b611e6cc6c0e2 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 2 Oct 2018 14:39:35 +0200 Subject: Upsert cache file on create activity --- server/lib/activitypub/cache-file.ts | 11 +++++++++++ server/lib/activitypub/process/process-create.ts | 4 ++-- server/lib/activitypub/process/process-update.ts | 11 ++--------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/server/lib/activitypub/cache-file.ts b/server/lib/activitypub/cache-file.ts index 5286d8e6d..f6f068b45 100644 --- a/server/lib/activitypub/cache-file.ts +++ b/server/lib/activitypub/cache-file.ts @@ -22,6 +22,16 @@ function cacheFileActivityObjectToDBAttributes (cacheFileObject: CacheFileObject } } +async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) { + const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t) + + if (!redundancyModel) { + await createCacheFile(cacheFileObject, video, byActor, t) + } else { + await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t) + } +} + function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) { const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) @@ -48,6 +58,7 @@ function updateCacheFile ( } export { + createOrUpdateCacheFile, createCacheFile, updateCacheFile, cacheFileActivityObjectToDBAttributes diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index c2160872f..cefe89db0 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -11,7 +11,7 @@ import { addVideoComment, resolveThread } from '../video-comments' import { getOrCreateVideoAndAccountAndChannel } from '../videos' import { forwardVideoRelatedActivity } from '../send/utils' import { Redis } from '../../redis' -import { createCacheFile } from '../cache-file' +import { createOrUpdateCacheFile } from '../cache-file' async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { const activityObject = activity.object @@ -105,7 +105,7 @@ async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) await sequelizeTypescript.transaction(async t => { - return createCacheFile(cacheFile, video, byActor, t) + return createOrUpdateCacheFile(cacheFile, video, byActor, t) }) if (video.isOwned()) { diff --git a/server/lib/activitypub/process/process-update.ts b/server/lib/activitypub/process/process-update.ts index e092a6729..bd4013555 100644 --- a/server/lib/activitypub/process/process-update.ts +++ b/server/lib/activitypub/process/process-update.ts @@ -10,8 +10,7 @@ import { fetchAvatarIfExists, updateActorAvatarInstance, updateActorInstance } f 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' +import { createOrUpdateCacheFile } from '../cache-file' import { forwardVideoRelatedActivity } from '../send/utils' async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) { @@ -77,13 +76,7 @@ async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUp const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) await sequelizeTypescript.transaction(async t => { - const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t) - - if (!redundancyModel) { - await createCacheFile(cacheFileObject, video, byActor, t) - } else { - await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t) - } + await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) }) if (video.isOwned()) { -- cgit v1.2.3