PeerTube/server/helpers/middlewares/accounts.ts

50 lines
1.3 KiB
TypeScript
Raw Normal View History

2019-07-23 03:40:39 -05:00
import { Response } from 'express'
import { AccountModel } from '../../models/account/account'
import * as Bluebird from 'bluebird'
2020-06-18 03:45:25 -05:00
import { MAccountDefault } from '../../types/models'
2019-07-23 03:40:39 -05:00
2020-07-07 03:57:04 -05:00
function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
const promise = AccountModel.load(parseInt(id + '', 10))
2019-07-23 03:40:39 -05:00
return doesAccountExist(promise, res, sendNotFound)
}
function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
const promise = AccountModel.loadLocalByName(name)
return doesAccountExist(promise, res, sendNotFound)
}
function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
2019-08-15 04:53:26 -05:00
const promise = AccountModel.loadByNameWithHost(nameWithDomain)
return doesAccountExist(promise, res, sendNotFound)
2019-07-23 03:40:39 -05:00
}
2019-08-15 04:53:26 -05:00
async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) {
2019-07-23 03:40:39 -05:00
const account = await p
if (!account) {
if (sendNotFound === true) {
res.status(404)
.send({ error: 'Account not found' })
.end()
}
return false
}
res.locals.account = account
return true
}
// ---------------------------------------------------------------------------
export {
doesAccountIdExist,
doesLocalAccountNameExist,
doesAccountNameWithHostExist,
doesAccountExist
}