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 { HttpStatusCode } from '@shared/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) {
31 status: HttpStatusCode.NOT_FOUND_404,
32 message: 'Account not found'
38 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 status: HttpStatusCode.FORBIDDEN_403,
48 message: 'User and token mismatch'
53 res.locals.user = user
57 // ---------------------------------------------------------------------------
61 doesLocalAccountNameExist,
62 doesAccountNameWithHostExist,
64 doesUserFeedTokenCorrespond