aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/shared/accounts.ts
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/middlewares/validators/shared/accounts.ts
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/middlewares/validators/shared/accounts.ts')
-rw-r--r--server/middlewares/validators/shared/accounts.ts65
1 files changed, 65 insertions, 0 deletions
diff --git a/server/middlewares/validators/shared/accounts.ts b/server/middlewares/validators/shared/accounts.ts
new file mode 100644
index 000000000..04da15441
--- /dev/null
+++ b/server/middlewares/validators/shared/accounts.ts
@@ -0,0 +1,65 @@
1import { Response } from 'express'
2import { AccountModel } from '@server/models/account/account'
3import { UserModel } from '@server/models/user/user'
4import { MAccountDefault } from '@server/types/models'
5import { HttpStatusCode } from '@shared/core-utils'
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}