]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/shared/accounts.ts
Refactor video rights checker
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / shared / accounts.ts
CommitLineData
3e753302 1import { Response } from 'express'
10363c74 2import { AccountModel } from '@server/models/account/account'
7d9ba5c0 3import { UserModel } from '@server/models/user/user'
10363c74 4import { MAccountDefault } from '@server/types/models'
c0e8b12e 5import { HttpStatusCode } from '@shared/models'
3e753302 6
57f6896f
C
7function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
8 const promise = AccountModel.load(parseInt(id + '', 10))
3e753302
C
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) {
453e83ea
C
20 const promise = AccountModel.loadByNameWithHost(nameWithDomain)
21
22 return doesAccountExist(promise, res, sendNotFound)
3e753302
C
23}
24
b49f22d8 25async function doesAccountExist (p: Promise<MAccountDefault>, res: Response, sendNotFound: boolean) {
3e753302
C
26 const account = await p
27
28 if (!account) {
29 if (sendNotFound === true) {
76148b27
RK
30 res.fail({
31 status: HttpStatusCode.NOT_FOUND_404,
32 message: 'Account not found'
33 })
3e753302 34 }
3e753302
C
35 return false
36 }
37
38 res.locals.account = account
3e753302
C
39 return true
40}
41
18490b07 42async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
5beb89f2 43 const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10))
afff310e
RK
44
45 if (token !== user.feedToken) {
76148b27
RK
46 res.fail({
47 status: HttpStatusCode.FORBIDDEN_403,
48 message: 'User and token mismatch'
49 })
afff310e
RK
50 return false
51 }
52
53 res.locals.user = user
afff310e
RK
54 return true
55}
56
3e753302
C
57// ---------------------------------------------------------------------------
58
59export {
60 doesAccountIdExist,
61 doesLocalAccountNameExist,
62 doesAccountNameWithHostExist,
afff310e
RK
63 doesAccountExist,
64 doesUserFeedTokenCorrespond
3e753302 65}