1 import { Response } from 'express'
2 import { AccountModel } from '@server/models/account/account'
3 import { UserModel } from '@server/models/user/user'
4 import { MAccountDefault } from '@server/types/models'
5 import { forceNumber } from '@shared/core-utils'
6 import { HttpStatusCode } from '@shared/models'
8 function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
9 const promise = AccountModel.load(forceNumber(id))
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: Promise<MAccountDefault>, res: Response, sendNotFound: boolean) {
27 const account = await p
30 if (sendNotFound === true) {
32 status: HttpStatusCode.NOT_FOUND_404,
33 message: 'Account not found'
39 res.locals.account = account
43 async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) {
44 const user = await UserModel.loadByIdWithChannels(forceNumber(id))
46 if (token !== user.feedToken) {
48 status: HttpStatusCode.FORBIDDEN_403,
49 message: 'User and token mismatch'
54 res.locals.user = user
58 // ---------------------------------------------------------------------------
62 doesLocalAccountNameExist,
63 doesAccountNameWithHostExist,
65 doesUserFeedTokenCorrespond