aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers/middlewares
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers/middlewares')
-rw-r--r--server/helpers/middlewares/accounts.ts46
-rw-r--r--server/helpers/middlewares/index.ts7
-rw-r--r--server/helpers/middlewares/video-abuses.ts41
-rw-r--r--server/helpers/middlewares/video-blacklists.ts23
-rw-r--r--server/helpers/middlewares/video-captions.ts24
-rw-r--r--server/helpers/middlewares/video-channels.ts23
-rw-r--r--server/helpers/middlewares/video-playlists.ts25
-rw-r--r--server/helpers/middlewares/videos.ts82
8 files changed, 271 insertions, 0 deletions
diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts
new file mode 100644
index 000000000..791022b97
--- /dev/null
+++ b/server/helpers/middlewares/accounts.ts
@@ -0,0 +1,46 @@
1import { Response } from 'express'
2import { AccountModel } from '../../models/account/account'
3import * as Bluebird from 'bluebird'
4
5function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
6 const promise = AccountModel.load(id)
7
8 return doesAccountExist(promise, res, sendNotFound)
9}
10
11function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
12 const promise = AccountModel.loadLocalByName(name)
13
14 return doesAccountExist(promise, res, sendNotFound)
15}
16
17function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
18 return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
19}
20
21async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
22 const account = await p
23
24 if (!account) {
25 if (sendNotFound === true) {
26 res.status(404)
27 .send({ error: 'Account not found' })
28 .end()
29 }
30
31 return false
32 }
33
34 res.locals.account = account
35
36 return true
37}
38
39// ---------------------------------------------------------------------------
40
41export {
42 doesAccountIdExist,
43 doesLocalAccountNameExist,
44 doesAccountNameWithHostExist,
45 doesAccountExist
46}
diff --git a/server/helpers/middlewares/index.ts b/server/helpers/middlewares/index.ts
new file mode 100644
index 000000000..f91aeaa12
--- /dev/null
+++ b/server/helpers/middlewares/index.ts
@@ -0,0 +1,7 @@
1export * from './accounts'
2export * from './video-abuses'
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-abuses.ts b/server/helpers/middlewares/video-abuses.ts
new file mode 100644
index 000000000..b23f1f021
--- /dev/null
+++ b/server/helpers/middlewares/video-abuses.ts
@@ -0,0 +1,41 @@
1import * as express from 'express'
2import { VideoChannelModel } from '../../models/video/video-channel'
3
4async function doesLocalVideoChannelNameExist (name: string, res: express.Response) {
5 const videoChannel = await VideoChannelModel.loadLocalByNameAndPopulateAccount(name)
6
7 return processVideoChannelExist(videoChannel, res)
8}
9
10async function doesVideoChannelIdExist (id: number, res: express.Response) {
11 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(+id)
12
13 return processVideoChannelExist(videoChannel, res)
14}
15
16async function doesVideoChannelNameWithHostExist (nameWithDomain: string, res: express.Response) {
17 const videoChannel = await VideoChannelModel.loadByNameWithHostAndPopulateAccount(nameWithDomain)
18
19 return processVideoChannelExist(videoChannel, res)
20}
21
22// ---------------------------------------------------------------------------
23
24export {
25 doesLocalVideoChannelNameExist,
26 doesVideoChannelIdExist,
27 doesVideoChannelNameWithHostExist
28}
29
30function processVideoChannelExist (videoChannel: VideoChannelModel, res: express.Response) {
31 if (!videoChannel) {
32 res.status(404)
33 .json({ error: 'Video channel not found' })
34 .end()
35
36 return false
37 }
38
39 res.locals.videoChannel = videoChannel
40 return true
41}
diff --git a/server/helpers/middlewares/video-blacklists.ts b/server/helpers/middlewares/video-blacklists.ts
new file mode 100644
index 000000000..c79420a0c
--- /dev/null
+++ b/server/helpers/middlewares/video-blacklists.ts
@@ -0,0 +1,23 @@
1import { Response } from 'express'
2import { VideoBlacklistModel } from '../../models/video/video-blacklist'
3
4async function doesVideoBlacklistExist (videoId: number, res: Response) {
5 const videoBlacklist = await VideoBlacklistModel.loadByVideoId(videoId)
6
7 if (videoBlacklist === null) {
8 res.status(404)
9 .json({ error: 'Blacklisted video not found' })
10 .end()
11
12 return false
13 }
14
15 res.locals.videoBlacklist = videoBlacklist
16 return true
17}
18
19// ---------------------------------------------------------------------------
20
21export {
22 doesVideoBlacklistExist
23}
diff --git a/server/helpers/middlewares/video-captions.ts b/server/helpers/middlewares/video-captions.ts
new file mode 100644
index 000000000..dc3d0144b
--- /dev/null
+++ b/server/helpers/middlewares/video-captions.ts
@@ -0,0 +1,24 @@
1import { VideoModel } from '../../models/video/video'
2import { Response } from 'express'
3import { VideoCaptionModel } from '../../models/video/video-caption'
4
5async function doesVideoCaptionExist (video: VideoModel, language: string, res: Response) {
6 const videoCaption = await VideoCaptionModel.loadByVideoIdAndLanguage(video.id, language)
7
8 if (!videoCaption) {
9 res.status(404)
10 .json({ error: 'Video caption not found' })
11 .end()
12
13 return false
14 }
15
16 res.locals.videoCaption = videoCaption
17 return true
18}
19
20// ---------------------------------------------------------------------------
21
22export {
23 doesVideoCaptionExist
24}
diff --git a/server/helpers/middlewares/video-channels.ts b/server/helpers/middlewares/video-channels.ts
new file mode 100644
index 000000000..1b573ca37
--- /dev/null
+++ b/server/helpers/middlewares/video-channels.ts
@@ -0,0 +1,23 @@
1import { Response } from 'express'
2import { VideoAbuseModel } from '../../models/video/video-abuse'
3
4async function doesVideoAbuseExist (abuseId: number, videoId: number, res: Response) {
5 const videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, videoId)
6
7 if (videoAbuse === null) {
8 res.status(404)
9 .json({ error: 'Video abuse not found' })
10 .end()
11
12 return false
13 }
14
15 res.locals.videoAbuse = videoAbuse
16 return true
17}
18
19// ---------------------------------------------------------------------------
20
21export {
22 doesVideoAbuseExist
23}
diff --git a/server/helpers/middlewares/video-playlists.ts b/server/helpers/middlewares/video-playlists.ts
new file mode 100644
index 000000000..735bf362f
--- /dev/null
+++ b/server/helpers/middlewares/video-playlists.ts
@@ -0,0 +1,25 @@
1import * as express from 'express'
2import { VideoPlaylistModel } from '../../models/video/video-playlist'
3
4async function doesVideoPlaylistExist (id: number | string, res: express.Response, fetchType: 'summary' | 'all' = 'summary') {
5 const videoPlaylist = fetchType === 'summary'
6 ? await VideoPlaylistModel.loadWithAccountAndChannelSummary(id, undefined)
7 : await VideoPlaylistModel.loadWithAccountAndChannel(id, undefined)
8
9 if (!videoPlaylist) {
10 res.status(404)
11 .json({ error: 'Video playlist not found' })
12 .end()
13
14 return false
15 }
16
17 res.locals.videoPlaylist = videoPlaylist
18 return true
19}
20
21// ---------------------------------------------------------------------------
22
23export {
24 doesVideoPlaylistExist
25}
diff --git a/server/helpers/middlewares/videos.ts b/server/helpers/middlewares/videos.ts
new file mode 100644
index 000000000..ceb1058ec
--- /dev/null
+++ b/server/helpers/middlewares/videos.ts
@@ -0,0 +1,82 @@
1import { Response } from 'express'
2import { fetchVideo, VideoFetchType } from '../video'
3import { UserModel } from '../../models/account/user'
4import { UserRight } from '../../../shared/models/users'
5import { VideoChannelModel } from '../../models/video/video-channel'
6import { VideoModel } from '../../models/video/video'
7
8async function doesVideoExist (id: number | string, res: Response, fetchType: VideoFetchType = 'all') {
9 const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined
10
11 const video = await fetchVideo(id, fetchType, userId)
12
13 if (video === null) {
14 res.status(404)
15 .json({ error: 'Video not found' })
16 .end()
17
18 return false
19 }
20
21 if (fetchType !== 'none') res.locals.video = video
22 return true
23}
24
25async function doesVideoChannelOfAccountExist (channelId: number, user: UserModel, res: Response) {
26 if (user.hasRight(UserRight.UPDATE_ANY_VIDEO) === true) {
27 const videoChannel = await VideoChannelModel.loadAndPopulateAccount(channelId)
28 if (videoChannel === null) {
29 res.status(400)
30 .json({ error: 'Unknown video `video channel` on this instance.' })
31 .end()
32
33 return false
34 }
35
36 res.locals.videoChannel = videoChannel
37 return true
38 }
39
40 const videoChannel = await VideoChannelModel.loadByIdAndAccount(channelId, user.Account.id)
41 if (videoChannel === null) {
42 res.status(400)
43 .json({ error: 'Unknown video `video channel` for this account.' })
44 .end()
45
46 return false
47 }
48
49 res.locals.videoChannel = videoChannel
50 return true
51}
52
53function checkUserCanManageVideo (user: UserModel, video: VideoModel, right: UserRight, res: Response) {
54 // Retrieve the user who did the request
55 if (video.isOwned() === false) {
56 res.status(403)
57 .json({ error: 'Cannot manage a video of another server.' })
58 .end()
59 return false
60 }
61
62 // Check if the user can delete the video
63 // The user can delete it if he has the right
64 // Or if s/he is the video's account
65 const account = video.VideoChannel.Account
66 if (user.hasRight(right) === false && account.userId !== user.id) {
67 res.status(403)
68 .json({ error: 'Cannot manage a video of another user.' })
69 .end()
70 return false
71 }
72
73 return true
74}
75
76// ---------------------------------------------------------------------------
77
78export {
79 doesVideoChannelOfAccountExist,
80 doesVideoExist,
81 checkUserCanManageVideo
82}