1 import { Response } from 'express'
2 import { AccountModel } from '../../models/account/account'
3 import * as Bluebird from 'bluebird'
4 import { MAccountDefault } from '../../types/models'
5 import { UserModel } from '@server/models/account/user'
6 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
8 function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
9 const promise = AccountModel.load(parseInt(id + '', 10))
11 return doesAccountExist(promise, res, sendNotFound)
14 function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
15 const promise = AccountModel.loadLocalByName(name)
17 return doesAccountExist(promise, res, sendNotFound)
20 function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
21 const promise = AccountModel.loadByNameWithHost(nameWithDomain)
23 return doesAccountExist(promise, res, sendNotFound)
26 async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) {
27 const account = await p
30 if (sendNotFound === true) {
31 res.status(HttpStatusCode.NOT_FOUND_404)
32 .json({ error: 'Account not found' })
38 res.locals.account = account
43 async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
44 const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10))
46 if (token !== user.feedToken) {
47 res.status(HttpStatusCode.FORBIDDEN_403)
48 .json({ error: 'User and token mismatch' })
53 res.locals.user = user
58 // ---------------------------------------------------------------------------
62 doesLocalAccountNameExist,
63 doesAccountNameWithHostExist,
65 doesUserFeedTokenCorrespond