diff options
Diffstat (limited to 'server/helpers/middlewares')
-rw-r--r-- | server/helpers/middlewares/abuses.ts | 47 | ||||
-rw-r--r-- | server/helpers/middlewares/accounts.ts | 4 | ||||
-rw-r--r-- | server/helpers/middlewares/index.ts | 2 | ||||
-rw-r--r-- | server/helpers/middlewares/video-abuses.ts | 32 |
4 files changed, 50 insertions, 35 deletions
diff --git a/server/helpers/middlewares/abuses.ts b/server/helpers/middlewares/abuses.ts new file mode 100644 index 000000000..be8c8b449 --- /dev/null +++ b/server/helpers/middlewares/abuses.ts | |||
@@ -0,0 +1,47 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { AbuseModel } from '../../models/abuse/abuse' | ||
3 | import { fetchVideo } from '../video' | ||
4 | |||
5 | // FIXME: deprecated in 2.3. Remove this function | ||
6 | async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) { | ||
7 | const abuseId = parseInt(abuseIdArg + '', 10) | ||
8 | let abuse = await AbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID) | ||
9 | |||
10 | if (!abuse) { | ||
11 | const userId = res.locals.oauth?.token.User.id | ||
12 | const video = await fetchVideo(videoUUID, 'all', userId) | ||
13 | |||
14 | if (video) abuse = await AbuseModel.loadByIdAndVideoId(abuseId, video.id) | ||
15 | } | ||
16 | |||
17 | if (abuse === null) { | ||
18 | res.status(404) | ||
19 | .json({ error: 'Video abuse not found' }) | ||
20 | |||
21 | return false | ||
22 | } | ||
23 | |||
24 | res.locals.abuse = abuse | ||
25 | return true | ||
26 | } | ||
27 | |||
28 | async function doesAbuseExist (abuseId: number | string, res: Response) { | ||
29 | const abuse = await AbuseModel.loadById(parseInt(abuseId + '', 10)) | ||
30 | |||
31 | if (!abuse) { | ||
32 | res.status(404) | ||
33 | .json({ error: 'Abuse not found' }) | ||
34 | |||
35 | return false | ||
36 | } | ||
37 | |||
38 | res.locals.abuse = abuse | ||
39 | return true | ||
40 | } | ||
41 | |||
42 | // --------------------------------------------------------------------------- | ||
43 | |||
44 | export { | ||
45 | doesAbuseExist, | ||
46 | doesVideoAbuseExist | ||
47 | } | ||
diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts index bddea7eaa..29b4ed1a6 100644 --- a/server/helpers/middlewares/accounts.ts +++ b/server/helpers/middlewares/accounts.ts | |||
@@ -3,8 +3,8 @@ import { AccountModel } from '../../models/account/account' | |||
3 | import * as Bluebird from 'bluebird' | 3 | import * as Bluebird from 'bluebird' |
4 | import { MAccountDefault } from '../../types/models' | 4 | import { MAccountDefault } from '../../types/models' |
5 | 5 | ||
6 | function doesAccountIdExist (id: number, res: Response, sendNotFound = true) { | 6 | function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) { |
7 | const promise = AccountModel.load(id) | 7 | const promise = AccountModel.load(parseInt(id + '', 10)) |
8 | 8 | ||
9 | return doesAccountExist(promise, res, sendNotFound) | 9 | return doesAccountExist(promise, res, sendNotFound) |
10 | } | 10 | } |
diff --git a/server/helpers/middlewares/index.ts b/server/helpers/middlewares/index.ts index f91aeaa12..f57f3ad31 100644 --- a/server/helpers/middlewares/index.ts +++ b/server/helpers/middlewares/index.ts | |||
@@ -1,5 +1,5 @@ | |||
1 | export * from './abuses' | ||
1 | export * from './accounts' | 2 | export * from './accounts' |
2 | export * from './video-abuses' | ||
3 | export * from './video-blacklists' | 3 | export * from './video-blacklists' |
4 | export * from './video-captions' | 4 | export * from './video-captions' |
5 | export * from './video-channels' | 5 | export * from './video-channels' |
diff --git a/server/helpers/middlewares/video-abuses.ts b/server/helpers/middlewares/video-abuses.ts deleted file mode 100644 index 97a5724b6..000000000 --- a/server/helpers/middlewares/video-abuses.ts +++ /dev/null | |||
@@ -1,32 +0,0 @@ | |||
1 | import { Response } from 'express' | ||
2 | import { VideoAbuseModel } from '../../models/video/video-abuse' | ||
3 | import { fetchVideo } from '../video' | ||
4 | |||
5 | async function doesVideoAbuseExist (abuseIdArg: number | string, videoUUID: string, res: Response) { | ||
6 | const abuseId = parseInt(abuseIdArg + '', 10) | ||
7 | let videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, null, videoUUID) | ||
8 | |||
9 | if (!videoAbuse) { | ||
10 | const userId = res.locals.oauth?.token.User.id | ||
11 | const video = await fetchVideo(videoUUID, 'all', userId) | ||
12 | |||
13 | if (video) videoAbuse = await VideoAbuseModel.loadByIdAndVideoId(abuseId, video.id) | ||
14 | } | ||
15 | |||
16 | if (videoAbuse === null) { | ||
17 | res.status(404) | ||
18 | .json({ error: 'Video abuse not found' }) | ||
19 | .end() | ||
20 | |||
21 | return false | ||
22 | } | ||
23 | |||
24 | res.locals.videoAbuse = videoAbuse | ||
25 | return true | ||
26 | } | ||
27 | |||
28 | // --------------------------------------------------------------------------- | ||
29 | |||
30 | export { | ||
31 | doesVideoAbuseExist | ||
32 | } | ||