accounts.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import * as Bluebird from 'bluebird'
  2. import { Response } from 'express'
  3. import 'express-validator'
  4. import { AccountModel } from '../../models/account/account'
  5. import { isUserDescriptionValid, isUserUsernameValid } from './users'
  6. import { exists } from './misc'
  7. function isAccountNameValid (value: string) {
  8. return isUserUsernameValid(value)
  9. }
  10. function isAccountIdValid (value: string) {
  11. return exists(value)
  12. }
  13. function isAccountDescriptionValid (value: string) {
  14. return isUserDescriptionValid(value)
  15. }
  16. function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
  17. const promise = AccountModel.load(id)
  18. return doesAccountExist(promise, res, sendNotFound)
  19. }
  20. function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
  21. const promise = AccountModel.loadLocalByName(name)
  22. return doesAccountExist(promise, res, sendNotFound)
  23. }
  24. function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
  25. return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
  26. }
  27. async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
  28. const account = await p
  29. if (!account) {
  30. if (sendNotFound === true) {
  31. res.status(404)
  32. .send({ error: 'Account not found' })
  33. .end()
  34. }
  35. return false
  36. }
  37. res.locals.account = account
  38. return true
  39. }
  40. // ---------------------------------------------------------------------------
  41. export {
  42. isAccountIdValid,
  43. doesAccountIdExist,
  44. doesLocalAccountNameExist,
  45. isAccountDescriptionValid,
  46. doesAccountNameWithHostExist,
  47. isAccountNameValid
  48. }