diff options
author | Chocobozzz <me@florianbigard.com> | 2021-06-03 17:33:44 +0200 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2021-06-03 18:03:36 +0200 |
commit | 10363c74c1d869f0e0c7bc4d0367b1f34d1bb6a4 (patch) | |
tree | 008f8dad8032684f46105a261b27b2d6f05b36eb /server/helpers/middlewares | |
parent | 5e08989ede1a340b9edb94465a11b1e04bf24094 (diff) | |
download | PeerTube-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.ts | 25 | ||||
-rw-r--r-- | server/helpers/middlewares/accounts.ts | 65 | ||||
-rw-r--r-- | server/helpers/middlewares/index.ts | 7 | ||||
-rw-r--r-- | server/helpers/middlewares/video-blacklists.ts | 24 | ||||
-rw-r--r-- | server/helpers/middlewares/video-captions.ts | 25 | ||||
-rw-r--r-- | server/helpers/middlewares/video-channels.ts | 43 | ||||
-rw-r--r-- | server/helpers/middlewares/video-playlists.ts | 39 | ||||
-rw-r--r-- | server/helpers/middlewares/videos.ts | 125 |
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 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { AbuseModel } from '../../models/abuse/abuse' | ||
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
4 | |||
5 | async 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 | |||
23 | export { | ||
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 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { UserModel } from '@server/models/user/user' | ||
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
4 | import { AccountModel } from '../../models/account/account' | ||
5 | import { MAccountDefault } from '../../types/models' | ||
6 | |||
7 | function 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 | |||
13 | function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { | ||
14 | const promise = AccountModel.loadLocalByName(name) | ||
15 | |||
16 | return doesAccountExist(promise, res, sendNotFound) | ||
17 | } | ||
18 | |||
19 | function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { | ||
20 | const promise = AccountModel.loadByNameWithHost(nameWithDomain) | ||
21 | |||
22 | return doesAccountExist(promise, res, sendNotFound) | ||
23 | } | ||
24 | |||
25 | async 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 | |||
42 | async 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 | |||
59 | export { | ||
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 @@ | |||
1 | export * from './abuses' | ||
2 | export * from './accounts' | ||
3 | export * from './video-blacklists' | ||
4 | export * from './video-captions' | ||
5 | export * from './video-channels' | ||
6 | export * from './video-playlists' | ||
7 | export * 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 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { VideoBlacklistModel } from '../../models/video/video-blacklist' | ||
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
4 | |||
5 | async 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 | |||
22 | export { | ||
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 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { VideoCaptionModel } from '../../models/video/video-caption' | ||
3 | import { MVideoId } from '@server/types/models' | ||
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
5 | |||
6 | async 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 | |||
23 | export { | ||
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 @@ | |||
1 | import * as express from 'express' | ||
2 | import { MChannelBannerAccountDefault } from '@server/types/models' | ||
3 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
4 | import { VideoChannelModel } from '../../models/video/video-channel' | ||
5 | |||
6 | async function doesLocalVideoChannelNameExist (name: string, res: express.Response) { | ||
7 | const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name) | ||
8 | |||
9 | return processVideoChannelExist(videoChannel, res) | ||
10 | } | ||
11 | |||
12 | async function doesVideoChannelIdExist (id: number, res: express.Response) { | ||
13 | const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id) | ||
14 | |||
15 | return processVideoChannelExist(videoChannel, res) | ||
16 | } | ||
17 | |||
18 | async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) { | ||
19 | const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain) | ||
20 | |||
21 | return processVideoChannelExist(videoChannel, res) | ||
22 | } | ||
23 | |||
24 | // --------------------------------------------------------------------------- | ||
25 | |||
26 | export { | ||
27 | doesLocalVideoChannelNameExist, | ||
28 | doesVideoChannelIdExist, | ||
29 | doesVideoChannelNameWithHostExist | ||
30 | } | ||
31 | |||
32 | function 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 @@ | |||
1 | import * as express from 'express' | ||
2 | import { VideoPlaylistModel } from '../../models/video/video-playlist' | ||
3 | import { MVideoPlaylist } from '../../types/models/video/video-playlist' | ||
4 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
5 | |||
6 | export type VideoPlaylistFetchType = 'summary' | 'all' | ||
7 | async 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 | |||
23 | export { | ||
24 | doesVideoPlaylistExist | ||
25 | } | ||
26 | |||
27 | // --------------------------------------------------------------------------- | ||
28 | |||
29 | function 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 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { fetchVideo, VideoFetchType } from '../video' | ||
3 | import { UserRight } from '../../../shared/models/users' | ||
4 | import { VideoChannelModel } from '../../models/video/video-channel' | ||
5 | import { | ||
6 | MUser, | ||
7 | MUserAccountId, | ||
8 | MVideoAccountLight, | ||
9 | MVideoFullLight, | ||
10 | MVideoIdThumbnail, | ||
11 | MVideoImmutable, | ||
12 | MVideoThumbnail, | ||
13 | MVideoWithRights | ||
14 | } from '@server/types/models' | ||
15 | import { VideoFileModel } from '@server/models/video/video-file' | ||
16 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | ||
17 | |||
18 | async 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 | |||
56 | async 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 | |||
68 | async 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 | |||
93 | function 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 | |||
120 | export { | ||
121 | doesVideoChannelOfAccountExist, | ||
122 | doesVideoExist, | ||
123 | doesVideoFileOfVideoExist, | ||
124 | checkUserCanManageVideo | ||
125 | } | ||