aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/middlewares
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-06-03 17:33:44 +0200
committerChocobozzz <me@florianbigard.com>2021-06-03 18:03:36 +0200
commit10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4 (patch)
tree008f8dad8032684f46105a261b27b2d6f05b36eb /server/helpers/middlewares
parent5e08989ede1a340b9edb94465a11b1e04bf24094 (diff)
downloadPeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.gz
PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.tar.zst
PeerTube-10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4.zip
Move middleware utils in middlewares
helpers modules should not import models
Diffstat (limited to 'server/helpers/middlewares')
-rw-r--r--server/helpers/middlewares/abuses.ts25
-rw-r--r--server/helpers/middlewares/accounts.ts65
-rw-r--r--server/helpers/middlewares/index.ts7
-rw-r--r--server/helpers/middlewares/video-blacklists.ts24
-rw-r--r--server/helpers/middlewares/video-captions.ts25
-rw-r--r--server/helpers/middlewares/video-channels.ts43
-rw-r--r--server/helpers/middlewares/video-playlists.ts39
-rw-r--r--server/helpers/middlewares/videos.ts125
8 files changed, 0 insertions, 353 deletions
diff --git a/server/helpers/middlewares/abuses.ts b/server/helpers/middlewares/abuses.ts
deleted file mode 100644
index f0b1caba8..000000000
--- a/server/helpers/middlewares/abuses.ts
+++ /dev/null
@@ -1,25 +0,0 @@
1import { Response } from 'express'
2import { AbuseModel } from '../../models/abuse/abuse'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4
5async function doesAbuseExist (abuseId: number | string, res: Response) {
6 const abuse = await AbuseModel.loadByIdWithReporter(parseInt(abuseId + '', 10))
7
8 if (!abuse) {
9 res.fail({
10 status: HttpStatusCode.NOT_FOUND_404,
11 message: 'Abuse not found'
12 })
13
14 return false
15 }
16
17 res.locals.abuse = abuse
18 return true
19}
20
21// ---------------------------------------------------------------------------
22
23export {
24 doesAbuseExist
25}
diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts
deleted file mode 100644
index 7db79bc48..000000000
--- a/server/helpers/middlewares/accounts.ts
+++ /dev/null
@@ -1,65 +0,0 @@
1import { Response } from 'express'
2import { UserModel } from '@server/models/user/user'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4import { AccountModel } from '../../models/account/account'
5import { MAccountDefault } from '../../types/models'
6
7function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
8 const promise = AccountModel.load(parseInt(id + '', 10))
9
10 return doesAccountExist(promise, res, sendNotFound)
11}
12
13function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
14 const promise = AccountModel.loadLocalByName(name)
15
16 return doesAccountExist(promise, res, sendNotFound)
17}
18
19function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
20 const promise = AccountModel.loadByNameWithHost(nameWithDomain)
21
22 return doesAccountExist(promise, res, sendNotFound)
23}
24
25async function doesAccountExist (p: Promise<MAccountDefault>, res: Response, sendNotFound: boolean) {
26 const account = await p
27
28 if (!account) {
29 if (sendNotFound === true) {
30 res.fail({
31 status: HttpStatusCode.NOT_FOUND_404,
32 message: 'Account not found'
33 })
34 }
35 return false
36 }
37
38 res.locals.account = account
39 return true
40}
41
42async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
43 const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10))
44
45 if (token !== user.feedToken) {
46 res.fail({
47 status: HttpStatusCode.FORBIDDEN_403,
48 message: 'User and token mismatch'
49 })
50 return false
51 }
52
53 res.locals.user = user
54 return true
55}
56
57// ---------------------------------------------------------------------------
58
59export {
60 doesAccountIdExist,
61 doesLocalAccountNameExist,
62 doesAccountNameWithHostExist,
63 doesAccountExist,
64 doesUserFeedTokenCorrespond
65}
diff --git a/server/helpers/middlewares/index.ts b/server/helpers/middlewares/index.ts
deleted file mode 100644
index f57f3ad31..000000000
--- a/server/helpers/middlewares/index.ts
+++ /dev/null
@@ -1,7 +0,0 @@
1export * from './abuses'
2export * from './accounts'
3export * from './video-blacklists'
4export * from './video-captions'
5export * from './video-channels'
6export * from './video-playlists'
7export * from './videos'
diff --git a/server/helpers/middlewares/video-blacklists.ts b/server/helpers/middlewares/video-blacklists.ts
deleted file mode 100644
index 3494fd6b0..000000000
--- a/server/helpers/middlewares/video-blacklists.ts
+++ /dev/null
@@ -1,24 +0,0 @@
1import { Response } from 'express'
2import { VideoBlacklistModel } from '../../models/video/video-blacklist'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4
5async function doesVideoBlacklistExist (videoId: number, res: Response) {
6 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId)
7
8 if (videoBlacklist === null) {
9 res.fail({
10 status: HttpStatusCode.NOT_FOUND_404,
11 message: 'Blacklisted video not found'
12 })
13 return false
14 }
15
16 res.locals.videoBlacklist = videoBlacklist
17 return true
18}
19
20// ---------------------------------------------------------------------------
21
22export {
23 doesVideoBlacklistExist
24}
diff --git a/server/helpers/middlewares/video-captions.ts b/server/helpers/middlewares/video-captions.ts
deleted file mode 100644
index 2a12c4813..000000000
--- a/server/helpers/middlewares/video-captions.ts
+++ /dev/null
@@ -1,25 +0,0 @@
1import { Response } from 'express'
2import { VideoCaptionModel } from '../../models/video/video-caption'
3import { MVideoId } from '@server/types/models'
4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
5
6async function doesVideoCaptionExist (video: MVideoId, language: string, res: Response) {
7 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language)
8
9 if (!videoCaption) {
10 res.fail({
11 status: HttpStatusCode.NOT_FOUND_404,
12 message: 'Video caption not found'
13 })
14 return false
15 }
16
17 res.locals.videoCaption = videoCaption
18 return true
19}
20
21// ---------------------------------------------------------------------------
22
23export {
24 doesVideoCaptionExist
25}
diff --git a/server/helpers/middlewares/video-channels.ts b/server/helpers/middlewares/video-channels.ts
deleted file mode 100644
index f5ed5ef0f..000000000
--- a/server/helpers/middlewares/video-channels.ts
+++ /dev/null
@@ -1,43 +0,0 @@
1import * as express from 'express'
2import { MChannelBannerAccountDefault } from '@server/types/models'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4import { VideoChannelModel } from '../../models/video/video-channel'
5
6async function doesLocalVideoChannelNameExist (name: string, res: express.Response) {
7 const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
8
9 return processVideoChannelExist(videoChannel, res)
10}
11
12async function doesVideoChannelIdExist (id: number, res: express.Response) {
13 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
14
15 return processVideoChannelExist(videoChannel, res)
16}
17
18async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
19 const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)
20
21 return processVideoChannelExist(videoChannel, res)
22}
23
24// ---------------------------------------------------------------------------
25
26export {
27 doesLocalVideoChannelNameExist,
28 doesVideoChannelIdExist,
29 doesVideoChannelNameWithHostExist
30}
31
32function processVideoChannelExist (videoChannel: MChannelBannerAccountDefault, res: express.Response) {
33 if (!videoChannel) {
34 res.fail({
35 status: HttpStatusCode.NOT_FOUND_404,
36 message: 'Video channel not found'
37 })
38 return false
39 }
40
41 res.locals.videoChannel = videoChannel
42 return true
43}
diff --git a/server/helpers/middlewares/video-playlists.ts b/server/helpers/middlewares/video-playlists.ts
deleted file mode 100644
index 3faeab677..000000000
--- a/server/helpers/middlewares/video-playlists.ts
+++ /dev/null
@@ -1,39 +0,0 @@
1import * as express from 'express'
2import { VideoPlaylistModel } from '../../models/video/video-playlist'
3import { MVideoPlaylist } from '../../types/models/video/video-playlist'
4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
5
6export type VideoPlaylistFetchType = 'summary' | 'all'
7async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: VideoPlaylistFetchType = 'summary') {
8 if (fetchType === 'summary') {
9 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined)
10 res.locals.videoPlaylistSummary = videoPlaylist
11
12 return handleVideoPlaylist(videoPlaylist, res)
13 }
14
15 const videoPlaylist = await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined)
16 res.locals.videoPlaylistFull = videoPlaylist
17
18 return handleVideoPlaylist(videoPlaylist, res)
19}
20
21// ---------------------------------------------------------------------------
22
23export {
24 doesVideoPlaylistExist
25}
26
27// ---------------------------------------------------------------------------
28
29function handleVideoPlaylist (videoPlaylist: MVideoPlaylist, res: express.Response) {
30 if (!videoPlaylist) {
31 res.fail({
32 status: HttpStatusCode.NOT_FOUND_404,
33 message: 'Video playlist not found'
34 })
35 return false
36 }
37
38 return true
39}
diff --git a/server/helpers/middlewares/videos.ts b/server/helpers/middlewares/videos.ts
deleted file mode 100644
index 52b934eb7..000000000
--- a/server/helpers/middlewares/videos.ts
+++ /dev/null
@@ -1,125 +0,0 @@
1import { Response } from 'express'
2import { fetchVideo, VideoFetchType } from '../video'
3import { UserRight } from '../../../shared/models/users'
4import { VideoChannelModel } from '../../models/video/video-channel'
5import {
6 MUser,
7 MUserAccountId,
8 MVideoAccountLight,
9 MVideoFullLight,
10 MVideoIdThumbnail,
11 MVideoImmutable,
12 MVideoThumbnail,
13 MVideoWithRights
14} from '@server/types/models'
15import { VideoFileModel } from '@server/models/video/video-file'
16import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
17
18async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
19 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
20
21 const video = await fetchVideo(id, fetchType, userId)
22
23 if (video === null) {
24 res.fail({
25 status: HttpStatusCode.NOT_FOUND_404,
26 message: 'Video not found'
27 })
28 return false
29 }
30
31 switch (fetchType) {
32 case 'all':
33 res.locals.videoAll = video as MVideoFullLight
34 break
35
36 case 'only-immutable-attributes':
37 res.locals.onlyImmutableVideo = video as MVideoImmutable
38 break
39
40 case 'id':
41 res.locals.videoId = video as MVideoIdThumbnail
42 break
43
44 case 'only-video':
45 res.locals.onlyVideo = video as MVideoThumbnail
46 break
47
48 case 'only-video-with-rights':
49 res.locals.onlyVideoWithRights = video as MVideoWithRights
50 break
51 }
52
53 return true
54}
55
56async function doesVideoFileOfVideoExist (id: number, videoIdOrUUID: number | string, res: Response) {
57 if (!await VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID)) {
58 res.fail({
59 status: HttpStatusCode.NOT_FOUND_404,
60 message: 'VideoFile matching Video not found'
61 })
62 return false
63 }
64
65 return true
66}
67
68async function doesVideoChannelOfAccountExist (channelId: number, user: MUserAccountId, res: Response) {
69 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
70
71 if (videoChannel === null) {
72 res.fail({ message: 'Unknown video "video channel" for this instance.' })
73 return false
74 }
75
76 // Don't check account id if the user can update any video
77 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
78 res.locals.videoChannel = videoChannel
79 return true
80 }
81
82 if (videoChannel.Account.id !== user.Account.id) {
83 res.fail({
84 message: 'Unknown video "video channel" for this account.'
85 })
86 return false
87 }
88
89 res.locals.videoChannel = videoChannel
90 return true
91}
92
93function checkUserCanManageVideo (user: MUser, video: MVideoAccountLight, right: UserRight, res: Response, onlyOwned = true) {
94 // Retrieve the user who did the request
95 if (onlyOwned && video.isOwned() === false) {
96 res.fail({
97 status: HttpStatusCode.FORBIDDEN_403,
98 message: 'Cannot manage a video of another server.'
99 })
100 return false
101 }
102
103 // Check if the user can delete the video
104 // The user can delete it if he has the right
105 // Or if s/he is the video's account
106 const account = video.VideoChannel.Account
107 if (user.hasRight(right) === false && account.userId !== user.id) {
108 res.fail({
109 status: HttpStatusCode.FORBIDDEN_403,
110 message: 'Cannot manage a video of another user.'
111 })
112 return false
113 }
114
115 return true
116}
117
118// ---------------------------------------------------------------------------
119
120export {
121 doesVideoChannelOfAccountExist,
122 doesVideoExist,
123 doesVideoFileOfVideoExist,
124 checkUserCanManageVideo
125}