diff options
author | Chocobozzz <me@florianbigard.com> | 2018-10-02 14:39:35 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2018-10-02 14:40:03 +0200 |
commit | b88a459664957d6ab9c417a6749b611e6cc6c0e2 (patch) | |
tree | 182ff8c00155cbdaba3f0c054ae944b2f282b7ab | |
parent | 278711b595aaaa6c3f77c160cc41b1b48c997e9a (diff) | |
download | PeerTube-b88a459664957d6ab9c417a6749b611e6cc6c0e2.tar.gz PeerTube-b88a459664957d6ab9c417a6749b611e6cc6c0e2.tar.zst PeerTube-b88a459664957d6ab9c417a6749b611e6cc6c0e2.zip |
Upsert cache file on create activity
-rw-r--r-- | server/lib/activitypub/cache-file.ts | 11 | ||||
-rw-r--r-- | server/lib/activitypub/process/process-create.ts | 4 | ||||
-rw-r--r-- | 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 | |||
22 | } | 22 | } |
23 | } | 23 | } |
24 | 24 | ||
25 | async function createOrUpdateCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) { | ||
26 | const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t) | ||
27 | |||
28 | if (!redundancyModel) { | ||
29 | await createCacheFile(cacheFileObject, video, byActor, t) | ||
30 | } else { | ||
31 | await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t) | ||
32 | } | ||
33 | } | ||
34 | |||
25 | function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) { | 35 | function createCacheFile (cacheFileObject: CacheFileObject, video: VideoModel, byActor: { id?: number }, t: Transaction) { |
26 | const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) | 36 | const attributes = cacheFileActivityObjectToDBAttributes(cacheFileObject, video, byActor) |
27 | 37 | ||
@@ -48,6 +58,7 @@ function updateCacheFile ( | |||
48 | } | 58 | } |
49 | 59 | ||
50 | export { | 60 | export { |
61 | createOrUpdateCacheFile, | ||
51 | createCacheFile, | 62 | createCacheFile, |
52 | updateCacheFile, | 63 | updateCacheFile, |
53 | cacheFileActivityObjectToDBAttributes | 64 | 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' | |||
11 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' | 11 | import { getOrCreateVideoAndAccountAndChannel } from '../videos' |
12 | import { forwardVideoRelatedActivity } from '../send/utils' | 12 | import { forwardVideoRelatedActivity } from '../send/utils' |
13 | import { Redis } from '../../redis' | 13 | import { Redis } from '../../redis' |
14 | import { createCacheFile } from '../cache-file' | 14 | import { createOrUpdateCacheFile } from '../cache-file' |
15 | 15 | ||
16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { | 16 | async function processCreateActivity (activity: ActivityCreate, byActor: ActorModel) { |
17 | const activityObject = activity.object | 17 | const activityObject = activity.object |
@@ -105,7 +105,7 @@ async function processCacheFile (byActor: ActorModel, activity: ActivityCreate) | |||
105 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) | 105 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFile.object }) |
106 | 106 | ||
107 | await sequelizeTypescript.transaction(async t => { | 107 | await sequelizeTypescript.transaction(async t => { |
108 | return createCacheFile(cacheFile, video, byActor, t) | 108 | return createOrUpdateCacheFile(cacheFile, video, byActor, t) |
109 | }) | 109 | }) |
110 | 110 | ||
111 | if (video.isOwned()) { | 111 | 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 | |||
10 | import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos' | 10 | import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos' |
11 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' | 11 | import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos' |
12 | import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file' | 12 | import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file' |
13 | import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy' | 13 | import { createOrUpdateCacheFile } from '../cache-file' |
14 | import { createCacheFile, updateCacheFile } from '../cache-file' | ||
15 | import { forwardVideoRelatedActivity } from '../send/utils' | 14 | import { forwardVideoRelatedActivity } from '../send/utils' |
16 | 15 | ||
17 | async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) { | 16 | async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) { |
@@ -77,13 +76,7 @@ async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUp | |||
77 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) | 76 | const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.object }) |
78 | 77 | ||
79 | await sequelizeTypescript.transaction(async t => { | 78 | await sequelizeTypescript.transaction(async t => { |
80 | const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id, t) | 79 | await createOrUpdateCacheFile(cacheFileObject, video, byActor, t) |
81 | |||
82 | if (!redundancyModel) { | ||
83 | await createCacheFile(cacheFileObject, video, byActor, t) | ||
84 | } else { | ||
85 | await updateCacheFile(cacheFileObject, redundancyModel, video, byActor, t) | ||
86 | } | ||
87 | }) | 80 | }) |
88 | 81 | ||
89 | if (video.isOwned()) { | 82 | if (video.isOwned()) { |