]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/middlewares/accounts.ts
Cleanup models directory organization
[github/Chocobozzz/PeerTube.git] / server / helpers / middlewares / accounts.ts
... / ...
CommitLineData
1import { Response } from 'express'
2import { UserModel } from '@server/models/user/user'
3import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4import { AccountModel } from '../../models/account/account'
5import { MAccountDefault } from '../../types/models'
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.status(HttpStatusCode.NOT_FOUND_404)
31 .json({ error: 'Account not found' })
32 }
33
34 return false
35 }
36
37 res.locals.account = account
38
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.status(HttpStatusCode.FORBIDDEN_403)
47 .json({ error: 'User and token mismatch' })
48
49 return false
50 }
51
52 res.locals.user = user
53
54 return true
55}
56
57// ---------------------------------------------------------------------------
58
59export {
60 doesAccountIdExist,
61 doesLocalAccountNameExist,
62 doesAccountNameWithHostExist,
63 doesAccountExist,
64 doesUserFeedTokenCorrespond
65}