From 453e83ea5d81d203ba34bc43cd5c2c750ba40568 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 15 Aug 2019 11:53:26 +0200 Subject: Stronger model typings --- server/helpers/middlewares/accounts.ts | 7 +++-- server/helpers/middlewares/video-abuses.ts | 44 ++++++++------------------ server/helpers/middlewares/video-captions.ts | 4 +-- server/helpers/middlewares/video-channels.ts | 45 +++++++++++++++++++-------- server/helpers/middlewares/video-playlists.ts | 35 ++++++++++++++------- server/helpers/middlewares/videos.ts | 26 +++++++++++++--- 6 files changed, 97 insertions(+), 64 deletions(-) (limited to 'server/helpers/middlewares') diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts index 791022b97..f5aa0bada 100644 --- a/server/helpers/middlewares/accounts.ts +++ b/server/helpers/middlewares/accounts.ts @@ -1,6 +1,7 @@ import { Response } from 'express' import { AccountModel } from '../../models/account/account' import * as Bluebird from 'bluebird' +import { MAccountDefault } from '../../typings/models' function doesAccountIdExist (id: number, res: Response, sendNotFound = true) { const promise = AccountModel.load(id) @@ -15,10 +16,12 @@ function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = } function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { - return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound) + const promise = AccountModel.loadByNameWithHost(nameWithDomain) + + return doesAccountExist(promise, res, sendNotFound) } -async function doesAccountExist (p: Bluebird, res: Response, sendNotFound: boolean) { +async function doesAccountExist (p: Bluebird, res: Response, sendNotFound: boolean) { const account = await p if (!account) { diff --git a/server/helpers/middlewares/video-abuses.ts b/server/helpers/middlewares/video-abuses.ts index b23f1f021..1b573ca37 100644 --- a/server/helpers/middlewares/video-abuses.ts +++ b/server/helpers/middlewares/video-abuses.ts @@ -1,41 +1,23 @@ -import * as express from 'express' -import { VideoChannelModel } from '../../models/video/video-channel' +import { Response } from 'express' +import { VideoAbuseModel } from '../../models/video/video-abuse' -async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { - const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) +async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) { + const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId) - return processVideoChannelExist(videoChannel, res) -} - -async function doesVideoChannelIdExist (id: number, res: express.Response) { - const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) - - return processVideoChannelExist(videoChannel, res) -} - -async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { - const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain) - - return processVideoChannelExist(videoChannel, res) -} - -// --------------------------------------------------------------------------- - -export { - doesLocalVideoChannelNameExist, - doesVideoChannelIdExist, - doesVideoChannelNameWithHostExist -} - -function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) { - if (!videoChannel) { + if (videoAbuse === null) { res.status(404) - .json({ error: 'Video channel not found' }) + .json({ error: 'Video abuse not found' }) .end() return false } - res.locals.videoChannel = videoChannel + res.locals.videoAbuse = videoAbuse return true } + +// --------------------------------------------------------------------------- + +export { + doesVideoAbuseExist +} diff --git a/server/helpers/middlewares/video-captions.ts b/server/helpers/middlewares/video-captions.ts index dc3d0144b..1b2513b60 100644 --- a/server/helpers/middlewares/video-captions.ts +++ b/server/helpers/middlewares/video-captions.ts @@ -1,8 +1,8 @@ -import { VideoModel } from '../../models/video/video' import { Response } from 'express' import { VideoCaptionModel } from '../../models/video/video-caption' +import { MVideoId } from '@server/typings/models' -async function doesVideoCaptionExist (video: VideoModel, language: string, res: Response) { +async function doesVideoCaptionExist (video: MVideoId, language: string, res: Response) { const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language) if (!videoCaption) { diff --git a/server/helpers/middlewares/video-channels.ts b/server/helpers/middlewares/video-channels.ts index 1b573ca37..17b7692c5 100644 --- a/server/helpers/middlewares/video-channels.ts +++ b/server/helpers/middlewares/video-channels.ts @@ -1,23 +1,42 @@ -import { Response } from 'express' -import { VideoAbuseModel } from '../../models/video/video-abuse' +import * as express from 'express' +import { VideoChannelModel } from '../../models/video/video-channel' +import { MChannelActorAccountDefault } from '../../typings/models' -async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) { - const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId) +async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { + const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) - if (videoAbuse === null) { - res.status(404) - .json({ error: 'Video abuse not found' }) - .end() + return processVideoChannelExist(videoChannel, res) +} - return false - } +async function doesVideoChannelIdExist (id: number, res: express.Response) { + const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) - res.locals.videoAbuse = videoAbuse - return true + return processVideoChannelExist(videoChannel, res) +} + +async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { + const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain) + + return processVideoChannelExist(videoChannel, res) } // --------------------------------------------------------------------------- export { - doesVideoAbuseExist + doesLocalVideoChannelNameExist, + doesVideoChannelIdExist, + doesVideoChannelNameWithHostExist +} + +function processVideoChannelExist (videoChannel: MChannelActorAccountDefault, res: express.Response) { + if (!videoChannel) { + res.status(404) + .json({ error: 'Video channel not found' }) + .end() + + return false + } + + res.locals.videoChannel = videoChannel + return true } diff --git a/server/helpers/middlewares/video-playlists.ts b/server/helpers/middlewares/video-playlists.ts index 735bf362f..8e7484483 100644 --- a/server/helpers/middlewares/video-playlists.ts +++ b/server/helpers/middlewares/video-playlists.ts @@ -1,11 +1,31 @@ import * as express from 'express' import { VideoPlaylistModel } from '../../models/video/video-playlist' +import { MVideoPlaylist } from '../../typings/models/video/video-playlist' -async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') { - const videoPlaylist = fetchType === 'summary' - ? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined) - : await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined) +export type VideoPlaylistFetchType = 'summary' | 'all' +async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: VideoPlaylistFetchType = 'summary') { + if (fetchType === 'summary') { + const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined) + res.locals.videoPlaylistSummary = videoPlaylist + return handleVideoPlaylist(videoPlaylist, res) + } + + const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined) + res.locals.videoPlaylistFull = videoPlaylist + + return handleVideoPlaylist(videoPlaylist, res) +} + +// --------------------------------------------------------------------------- + +export { + doesVideoPlaylistExist +} + +// --------------------------------------------------------------------------- + +function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) { if (!videoPlaylist) { res.status(404) .json({ error: 'Video playlist not found' }) @@ -14,12 +34,5 @@ async function doesVideoPlaylistExist (id: number | string, res: express.Respons return false } - res.locals.videoPlaylist = videoPlaylist return true } - -// --------------------------------------------------------------------------- - -export { - doesVideoPlaylistExist -} diff --git a/server/helpers/middlewares/videos.ts b/server/helpers/middlewares/videos.ts index ceb1058ec..964f0c91a 100644 --- a/server/helpers/middlewares/videos.ts +++ b/server/helpers/middlewares/videos.ts @@ -1,9 +1,8 @@ import { Response } from 'express' import { fetchVideo, VideoFetchType } from '../video' -import { UserModel } from '../../models/account/user' import { UserRight } from '../../../shared/models/users' import { VideoChannelModel } from '../../models/video/video-channel' -import { VideoModel } from '../../models/video/video' +import { MUser, MUserAccountId, MVideoAccountLight, MVideoFullLight, MVideoWithRights } from '@server/typings/models' async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined @@ -18,11 +17,28 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi return false } - if (fetchType !== 'none') res.locals.video = video + switch (fetchType) { + case 'all': + res.locals.videoAll = video as MVideoFullLight + break + + case 'id': + res.locals.videoId = video + break + + case 'only-video': + res.locals.onlyVideo = video + break + + case 'only-video-with-rights': + res.locals.onlyVideoWithRights = video as MVideoWithRights + break + } + return true } -async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) { +async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) { if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) { const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId) if (videoChannel === null) { @@ -50,7 +66,7 @@ async function doesVideoChannelOfAccountExist (channelId: number, user: UserMode return true } -function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) { +function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response) { // Retrieve the user who did the request if (video.isOwned() === false) { res.status(403) -- cgit v1.2.3 From 0283eaac2a8e73006c66df3cf5bb9012e37450e5 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 20 Aug 2019 13:52:49 +0200 Subject: Cleanup model types --- server/helpers/middlewares/video-channels.ts | 4 ++-- server/helpers/middlewares/videos.ts | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'server/helpers/middlewares') diff --git a/server/helpers/middlewares/video-channels.ts b/server/helpers/middlewares/video-channels.ts index 17b7692c5..1595ecd94 100644 --- a/server/helpers/middlewares/video-channels.ts +++ b/server/helpers/middlewares/video-channels.ts @@ -1,6 +1,6 @@ import * as express from 'express' import { VideoChannelModel } from '../../models/video/video-channel' -import { MChannelActorAccountDefault } from '../../typings/models' +import { MChannelAccountDefault } from '@server/typings/models' async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) @@ -28,7 +28,7 @@ export { doesVideoChannelNameWithHostExist } -function processVideoChannelExist (videoChannel: MChannelActorAccountDefault, res: express.Response) { +function processVideoChannelExist (videoChannel: MChannelAccountDefault, res: express.Response) { if (!videoChannel) { res.status(404) .json({ error: 'Video channel not found' }) diff --git a/server/helpers/middlewares/videos.ts b/server/helpers/middlewares/videos.ts index 964f0c91a..74f529804 100644 --- a/server/helpers/middlewares/videos.ts +++ b/server/helpers/middlewares/videos.ts @@ -2,7 +2,7 @@ import { Response } from 'express' import { fetchVideo, VideoFetchType } from '../video' import { UserRight } from '../../../shared/models/users' import { VideoChannelModel } from '../../models/video/video-channel' -import { MUser, MUserAccountId, MVideoAccountLight, MVideoFullLight, MVideoWithRights } from '@server/typings/models' +import { MUser, MUserAccountId, MVideoAccountLight, MVideoFullLight, MVideoThumbnail, MVideoWithRights } from '@server/typings/models' async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') { const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined @@ -27,7 +27,7 @@ async function doesVideoExist (id: number | string, res: Response, fetchType: Vi break case 'only-video': - res.locals.onlyVideo = video + res.locals.onlyVideo = video as MVideoThumbnail break case 'only-video-with-rights': -- cgit v1.2.3