2019-07-23 10:40:39 +02:00
|
|
|
import { Response } from 'express'
|
2020-08-13 15:07:23 +02:00
|
|
|
import { UserModel } from '@server/models/account/user'
|
2020-12-07 14:32:36 +01:00
|
|
|
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
|
2020-12-08 14:30:29 +01:00
|
|
|
import { AccountModel } from '../../models/account/account'
|
|
|
|
import { MAccountDefault } from '../../types/models'
|
2019-07-23 10:40:39 +02:00
|
|
|
|
2020-07-07 10:57:04 +02:00
|
|
|
function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
|
|
|
|
const promise = AccountModel.load(parseInt(id + '', 10))
|
2019-07-23 10:40:39 +02: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 11:53:26 +02:00
|
|
|
const promise = AccountModel.loadByNameWithHost(nameWithDomain)
|
|
|
|
|
|
|
|
return doesAccountExist(promise, res, sendNotFound)
|
2019-07-23 10:40:39 +02:00
|
|
|
}
|
|
|
|
|
2020-12-08 14:30:29 +01:00
|
|
|
async function doesAccountExist (p: Promise<MAccountDefault>, res: Response, sendNotFound: boolean) {
|
2019-07-23 10:40:39 +02:00
|
|
|
const account = await p
|
|
|
|
|
|
|
|
if (!account) {
|
|
|
|
if (sendNotFound === true) {
|
2020-12-07 14:32:36 +01:00
|
|
|
res.status(HttpStatusCode.NOT_FOUND_404)
|
2020-11-09 16:25:27 +01:00
|
|
|
.json({ error: 'Account not found' })
|
2019-07-23 10:40:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.account = account
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-25 11:04:18 +01:00
|
|
|
async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
|
2020-11-09 16:25:27 +01:00
|
|
|
const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10))
|
2020-08-13 15:07:23 +02:00
|
|
|
|
|
|
|
if (token !== user.feedToken) {
|
2020-12-07 14:32:36 +01:00
|
|
|
res.status(HttpStatusCode.FORBIDDEN_403)
|
2020-11-09 16:25:27 +01:00
|
|
|
.json({ error: 'User and token mismatch' })
|
2020-08-13 15:07:23 +02:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.user = user
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-23 10:40:39 +02:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
doesAccountIdExist,
|
|
|
|
doesLocalAccountNameExist,
|
|
|
|
doesAccountNameWithHostExist,
|
2020-08-13 15:07:23 +02:00
|
|
|
doesAccountExist,
|
|
|
|
doesUserFeedTokenCorrespond
|
2019-07-23 10:40:39 +02:00
|
|
|
}
|