blocklist-command.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */
  2. import { AccountBlock, BlockStatus, HttpStatusCode, ResultList, ServerBlock } from '@shared/models'
  3. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  4. type ListBlocklistOptions = OverrideCommandOptions & {
  5. start: number
  6. count: number
  7. sort?: string // default -createdAt
  8. search?: string
  9. }
  10. export class BlocklistCommand extends AbstractCommand {
  11. listMyAccountBlocklist (options: ListBlocklistOptions) {
  12. const path = '/api/v1/users/me/blocklist/accounts'
  13. return this.listBlocklist<AccountBlock>(options, path)
  14. }
  15. listMyServerBlocklist (options: ListBlocklistOptions) {
  16. const path = '/api/v1/users/me/blocklist/servers'
  17. return this.listBlocklist<ServerBlock>(options, path)
  18. }
  19. listServerAccountBlocklist (options: ListBlocklistOptions) {
  20. const path = '/api/v1/server/blocklist/accounts'
  21. return this.listBlocklist<AccountBlock>(options, path)
  22. }
  23. listServerServerBlocklist (options: ListBlocklistOptions) {
  24. const path = '/api/v1/server/blocklist/servers'
  25. return this.listBlocklist<ServerBlock>(options, path)
  26. }
  27. // ---------------------------------------------------------------------------
  28. getStatus (options: OverrideCommandOptions & {
  29. accounts?: string[]
  30. hosts?: string[]
  31. }) {
  32. const { accounts, hosts } = options
  33. const path = '/api/v1/blocklist/status'
  34. return this.getRequestBody<BlockStatus>({
  35. ...options,
  36. path,
  37. query: {
  38. accounts,
  39. hosts
  40. },
  41. implicitToken: false,
  42. defaultExpectedStatus: HttpStatusCode.OK_200
  43. })
  44. }
  45. // ---------------------------------------------------------------------------
  46. addToMyBlocklist (options: OverrideCommandOptions & {
  47. account?: string
  48. server?: string
  49. }) {
  50. const { account, server } = options
  51. const path = account
  52. ? '/api/v1/users/me/blocklist/accounts'
  53. : '/api/v1/users/me/blocklist/servers'
  54. return this.postBodyRequest({
  55. ...options,
  56. path,
  57. fields: {
  58. accountName: account,
  59. host: server
  60. },
  61. implicitToken: true,
  62. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  63. })
  64. }
  65. addToServerBlocklist (options: OverrideCommandOptions & {
  66. account?: string
  67. server?: string
  68. }) {
  69. const { account, server } = options
  70. const path = account
  71. ? '/api/v1/server/blocklist/accounts'
  72. : '/api/v1/server/blocklist/servers'
  73. return this.postBodyRequest({
  74. ...options,
  75. path,
  76. fields: {
  77. accountName: account,
  78. host: server
  79. },
  80. implicitToken: true,
  81. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  82. })
  83. }
  84. // ---------------------------------------------------------------------------
  85. removeFromMyBlocklist (options: OverrideCommandOptions & {
  86. account?: string
  87. server?: string
  88. }) {
  89. const { account, server } = options
  90. const path = account
  91. ? '/api/v1/users/me/blocklist/accounts/' + account
  92. : '/api/v1/users/me/blocklist/servers/' + server
  93. return this.deleteRequest({
  94. ...options,
  95. path,
  96. implicitToken: true,
  97. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  98. })
  99. }
  100. removeFromServerBlocklist (options: OverrideCommandOptions & {
  101. account?: string
  102. server?: string
  103. }) {
  104. const { account, server } = options
  105. const path = account
  106. ? '/api/v1/server/blocklist/accounts/' + account
  107. : '/api/v1/server/blocklist/servers/' + server
  108. return this.deleteRequest({
  109. ...options,
  110. path,
  111. implicitToken: true,
  112. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  113. })
  114. }
  115. private listBlocklist <T> (options: ListBlocklistOptions, path: string) {
  116. const { start, count, search, sort = '-createdAt' } = options
  117. return this.getRequestBody<ResultList<T>>({
  118. ...options,
  119. path,
  120. query: { start, count, sort, search },
  121. implicitToken: true,
  122. defaultExpectedStatus: HttpStatusCode.OK_200
  123. })
  124. }
  125. }