From 10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 3 Jun 2021 17:33:44 +0200 Subject: Move middleware utils in middlewares helpers modules should not import models --- server/lib/model-loaders/actor.ts | 16 ++++++++++ server/lib/model-loaders/index.ts | 2 ++ server/lib/model-loaders/video.ts | 64 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+) create mode 100644 server/lib/model-loaders/actor.ts create mode 100644 server/lib/model-loaders/index.ts create mode 100644 server/lib/model-loaders/video.ts (limited to 'server/lib/model-loaders') diff --git a/server/lib/model-loaders/actor.ts b/server/lib/model-loaders/actor.ts new file mode 100644 index 000000000..234cb344f --- /dev/null +++ b/server/lib/model-loaders/actor.ts @@ -0,0 +1,16 @@ + +import { ActorModel } from '../../models/actor/actor' +import { MActorAccountChannelId, MActorFull } from '../../types/models' + +type ActorFetchByUrlType = 'all' | 'association-ids' + +function fetchActorByUrl (url: string, fetchType: ActorFetchByUrlType): Promise { + if (fetchType === 'all') return ActorModel.loadByUrlAndPopulateAccountAndChannel(url) + + if (fetchType === 'association-ids') return ActorModel.loadByUrl(url) +} + +export { + ActorFetchByUrlType, + fetchActorByUrl +} diff --git a/server/lib/model-loaders/index.ts b/server/lib/model-loaders/index.ts new file mode 100644 index 000000000..9e5152cb2 --- /dev/null +++ b/server/lib/model-loaders/index.ts @@ -0,0 +1,2 @@ +export * from './actor' +export * from './video' diff --git a/server/lib/model-loaders/video.ts b/server/lib/model-loaders/video.ts new file mode 100644 index 000000000..7aaf00e89 --- /dev/null +++ b/server/lib/model-loaders/video.ts @@ -0,0 +1,64 @@ +import { VideoModel } from '@server/models/video/video' +import { + MVideoAccountLightBlacklistAllFiles, + MVideoFullLight, + MVideoIdThumbnail, + MVideoImmutable, + MVideoThumbnail, + MVideoWithRights +} from '@server/types/models' + +type VideoFetchType = 'all' | 'only-video' | 'only-video-with-rights' | 'id' | 'none' | 'only-immutable-attributes' + +function fetchVideo (id: number | string, fetchType: 'all', userId?: number): Promise +function fetchVideo (id: number | string, fetchType: 'only-immutable-attributes'): Promise +function fetchVideo (id: number | string, fetchType: 'only-video', userId?: number): Promise +function fetchVideo (id: number | string, fetchType: 'only-video-with-rights', userId?: number): Promise +function fetchVideo (id: number | string, fetchType: 'id' | 'none', userId?: number): Promise +function fetchVideo ( + id: number | string, + fetchType: VideoFetchType, + userId?: number +): Promise +function fetchVideo ( + id: number | string, + fetchType: VideoFetchType, + userId?: number +): Promise { + if (fetchType === 'all') return VideoModel.loadAndPopulateAccountAndServerAndTags(id, undefined, userId) + + if (fetchType === 'only-immutable-attributes') return VideoModel.loadImmutableAttributes(id) + + if (fetchType === 'only-video-with-rights') return VideoModel.loadWithRights(id) + + if (fetchType === 'only-video') return VideoModel.load(id) + + if (fetchType === 'id' || fetchType === 'none') return VideoModel.loadOnlyId(id) +} + +type VideoFetchByUrlType = 'all' | 'only-video' | 'only-immutable-attributes' + +function fetchVideoByUrl (url: string, fetchType: 'all'): Promise +function fetchVideoByUrl (url: string, fetchType: 'only-immutable-attributes'): Promise +function fetchVideoByUrl (url: string, fetchType: 'only-video'): Promise +function fetchVideoByUrl ( + url: string, + fetchType: VideoFetchByUrlType +): Promise +function fetchVideoByUrl ( + url: string, + fetchType: VideoFetchByUrlType +): Promise { + if (fetchType === 'all') return VideoModel.loadByUrlAndPopulateAccount(url) + + if (fetchType === 'only-immutable-attributes') return VideoModel.loadByUrlImmutableAttributes(url) + + if (fetchType === 'only-video') return VideoModel.loadByUrl(url) +} + +export { + VideoFetchType, + VideoFetchByUrlType, + fetchVideo, + fetchVideoByUrl +} -- cgit v1.2.3