1
0
Fork 0
peertube/server/helpers/middlewares/accounts.ts

66 lines
1.7 KiB
TypeScript
Raw Normal View History

2019-07-23 04:40:39 -04:00
import { Response } from 'express'
import { AccountModel } from '../../models/account/account'
import * as Bluebird from 'bluebird'
2020-06-18 04:45:25 -04:00
import { MAccountDefault } from '../../types/models'
import { UserModel } from '@server/models/account/user'
2019-07-23 04:40:39 -04:00
2020-07-07 04:57:04 -04:00
function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
const promise = AccountModel.load(parseInt(id + '', 10))
2019-07-23 04:40:39 -04: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 05:53:26 -04:00
const promise = AccountModel.loadByNameWithHost(nameWithDomain)
return doesAccountExist(promise, res, sendNotFound)
2019-07-23 04:40:39 -04:00
}
2019-08-15 05:53:26 -04:00
async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) {
2019-07-23 04:40:39 -04:00
const account = await p
if (!account) {
if (sendNotFound === true) {
res.status(404)
2020-11-09 10:25:27 -05:00
.json({ error: 'Account not found' })
2019-07-23 04:40:39 -04:00
}
return false
}
res.locals.account = account
return true
}
2020-11-25 05:04:18 -05:00
async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
2020-11-09 10:25:27 -05:00
const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10))
if (token !== user.feedToken) {
2020-11-25 05:04:18 -05:00
res.status(403)
2020-11-09 10:25:27 -05:00
.json({ error: 'User and token mismatch' })
return false
}
res.locals.user = user
return true
}
2019-07-23 04:40:39 -04:00
// ---------------------------------------------------------------------------
export {
doesAccountIdExist,
doesLocalAccountNameExist,
doesAccountNameWithHostExist,
doesAccountExist,
doesUserFeedTokenCorrespond
2019-07-23 04:40:39 -04:00
}