2019-07-23 03:40:39 -05:00
|
|
|
import { Response } from 'express'
|
2021-06-03 10:33:44 -05:00
|
|
|
import { AccountModel } from '@server/models/account/account'
|
2021-05-11 04:15:29 -05:00
|
|
|
import { UserModel } from '@server/models/user/user'
|
2021-06-03 10:33:44 -05:00
|
|
|
import { MAccountDefault } from '@server/types/models'
|
2022-11-15 07:41:55 -06:00
|
|
|
import { forceNumber } from '@shared/core-utils'
|
2021-07-16 03:42:24 -05:00
|
|
|
import { HttpStatusCode } from '@shared/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) {
|
2022-11-15 07:41:55 -06:00
|
|
|
const promise = AccountModel.load(forceNumber(id))
|
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
|
|
|
}
|
|
|
|
|
2020-12-08 07:30:29 -06:00
|
|
|
async function doesAccountExist (p: Promise<MAccountDefault>, res: Response, sendNotFound: boolean) {
|
2019-07-23 03:40:39 -05:00
|
|
|
const account = await p
|
|
|
|
|
|
|
|
if (!account) {
|
|
|
|
if (sendNotFound === true) {
|
2021-05-31 18:36:53 -05:00
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'Account not found'
|
|
|
|
})
|
2019-07-23 03:40:39 -05:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.account = account
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-11-25 04:04:18 -06:00
|
|
|
async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
|
2022-11-15 07:41:55 -06:00
|
|
|
const user = await UserModel.loadByIdWithChannels(forceNumber(id))
|
2020-08-13 08:07:23 -05:00
|
|
|
|
|
|
|
if (token !== user.feedToken) {
|
2021-05-31 18:36:53 -05:00
|
|
|
res.fail({
|
|
|
|
status: HttpStatusCode.FORBIDDEN_403,
|
|
|
|
message: 'User and token mismatch'
|
|
|
|
})
|
2020-08-13 08:07:23 -05:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.user = user
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-07-23 03:40:39 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
doesAccountIdExist,
|
|
|
|
doesLocalAccountNameExist,
|
|
|
|
doesAccountNameWithHostExist,
|
2020-08-13 08:07:23 -05:00
|
|
|
doesAccountExist,
|
|
|
|
doesUserFeedTokenCorrespond
|
2019-07-23 03:40:39 -05:00
|
|
|
}
|