1 import { Response } from 'express'
2 import { UserModel } from '@server/models/user/user'
3 import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
4 import { AccountModel } from '../../models/account/account'
5 import { MAccountDefault } from '../../types/models'
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: Promise<MAccountDefault>, res: Response, sendNotFound: boolean) {
26 const account = await p
29 if (sendNotFound === true) {
30 res.status(HttpStatusCode.NOT_FOUND_404)
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) {
46 res.status(HttpStatusCode.FORBIDDEN_403)
47 .json({ error: 'User and token mismatch' })
52 res.locals.user = user
57 // ---------------------------------------------------------------------------
61 doesLocalAccountNameExist,
62 doesAccountNameWithHostExist,
64 doesUserFeedTokenCorrespond