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'
7 function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
8 const promise = AccountModel.load(parseInt(id + '', 10))
10 return doesAccountExist(promise, res, sendNotFound)
13 function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
14 const promise = AccountModel.loadLocalByName(name)
16 return doesAccountExist(promise, res, sendNotFound)
19 function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
20 const promise = AccountModel.loadByNameWithHost(nameWithDomain)
22 return doesAccountExist(promise, res, sendNotFound)
25 async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) {
26 const account = await p
29 if (sendNotFound === true) {
31 .json({ error: 'Account not found' })
37 res.locals.account = account
42 async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
43 const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10))
45 if (token !== user.feedToken) {
47 .json({ error: 'User and token mismatch' })
52 res.locals.user = user
57 // ---------------------------------------------------------------------------
61 doesLocalAccountNameExist,
62 doesAccountNameWithHostExist,
64 doesUserFeedTokenCorrespond