abuses-command.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { pick } from '@shared/core-utils'
  2. import {
  3. AbuseFilter,
  4. AbuseMessage,
  5. AbusePredefinedReasonsString,
  6. AbuseState,
  7. AbuseUpdate,
  8. AbuseVideoIs,
  9. AdminAbuse,
  10. HttpStatusCode,
  11. ResultList,
  12. UserAbuse
  13. } from '@shared/models'
  14. import { unwrapBody } from '../requests/requests'
  15. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  16. export class AbusesCommand extends AbstractCommand {
  17. report (options: OverrideCommandOptions & {
  18. reason: string
  19. accountId?: number
  20. videoId?: number
  21. commentId?: number
  22. predefinedReasons?: AbusePredefinedReasonsString[]
  23. startAt?: number
  24. endAt?: number
  25. }) {
  26. const path = '/api/v1/abuses'
  27. const video = options.videoId
  28. ? {
  29. id: options.videoId,
  30. startAt: options.startAt,
  31. endAt: options.endAt
  32. }
  33. : undefined
  34. const comment = options.commentId
  35. ? { id: options.commentId }
  36. : undefined
  37. const account = options.accountId
  38. ? { id: options.accountId }
  39. : undefined
  40. const body = {
  41. account,
  42. video,
  43. comment,
  44. reason: options.reason,
  45. predefinedReasons: options.predefinedReasons
  46. }
  47. return unwrapBody<{ abuse: { id: number } }>(this.postBodyRequest({
  48. ...options,
  49. path,
  50. fields: body,
  51. implicitToken: true,
  52. defaultExpectedStatus: HttpStatusCode.OK_200
  53. }))
  54. }
  55. getAdminList (options: OverrideCommandOptions & {
  56. start?: number
  57. count?: number
  58. sort?: string
  59. id?: number
  60. predefinedReason?: AbusePredefinedReasonsString
  61. search?: string
  62. filter?: AbuseFilter
  63. state?: AbuseState
  64. videoIs?: AbuseVideoIs
  65. searchReporter?: string
  66. searchReportee?: string
  67. searchVideo?: string
  68. searchVideoChannel?: string
  69. } = {}) {
  70. const toPick: (keyof typeof options)[] = [
  71. 'count',
  72. 'filter',
  73. 'id',
  74. 'predefinedReason',
  75. 'search',
  76. 'searchReportee',
  77. 'searchReporter',
  78. 'searchVideo',
  79. 'searchVideoChannel',
  80. 'sort',
  81. 'start',
  82. 'state',
  83. 'videoIs'
  84. ]
  85. const path = '/api/v1/abuses'
  86. const defaultQuery = { sort: 'createdAt' }
  87. const query = { ...defaultQuery, ...pick(options, toPick) }
  88. return this.getRequestBody<ResultList<AdminAbuse>>({
  89. ...options,
  90. path,
  91. query,
  92. implicitToken: true,
  93. defaultExpectedStatus: HttpStatusCode.OK_200
  94. })
  95. }
  96. getUserList (options: OverrideCommandOptions & {
  97. start?: number
  98. count?: number
  99. sort?: string
  100. id?: number
  101. search?: string
  102. state?: AbuseState
  103. }) {
  104. const toPick: (keyof typeof options)[] = [
  105. 'id',
  106. 'search',
  107. 'state',
  108. 'start',
  109. 'count',
  110. 'sort'
  111. ]
  112. const path = '/api/v1/users/me/abuses'
  113. const defaultQuery = { sort: 'createdAt' }
  114. const query = { ...defaultQuery, ...pick(options, toPick) }
  115. return this.getRequestBody<ResultList<UserAbuse>>({
  116. ...options,
  117. path,
  118. query,
  119. implicitToken: true,
  120. defaultExpectedStatus: HttpStatusCode.OK_200
  121. })
  122. }
  123. update (options: OverrideCommandOptions & {
  124. abuseId: number
  125. body: AbuseUpdate
  126. }) {
  127. const { abuseId, body } = options
  128. const path = '/api/v1/abuses/' + abuseId
  129. return this.putBodyRequest({
  130. ...options,
  131. path,
  132. fields: body,
  133. implicitToken: true,
  134. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  135. })
  136. }
  137. delete (options: OverrideCommandOptions & {
  138. abuseId: number
  139. }) {
  140. const { abuseId } = options
  141. const path = '/api/v1/abuses/' + abuseId
  142. return this.deleteRequest({
  143. ...options,
  144. path,
  145. implicitToken: true,
  146. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  147. })
  148. }
  149. listMessages (options: OverrideCommandOptions & {
  150. abuseId: number
  151. }) {
  152. const { abuseId } = options
  153. const path = '/api/v1/abuses/' + abuseId + '/messages'
  154. return this.getRequestBody<ResultList<AbuseMessage>>({
  155. ...options,
  156. path,
  157. implicitToken: true,
  158. defaultExpectedStatus: HttpStatusCode.OK_200
  159. })
  160. }
  161. deleteMessage (options: OverrideCommandOptions & {
  162. abuseId: number
  163. messageId: number
  164. }) {
  165. const { abuseId, messageId } = options
  166. const path = '/api/v1/abuses/' + abuseId + '/messages/' + messageId
  167. return this.deleteRequest({
  168. ...options,
  169. path,
  170. implicitToken: true,
  171. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  172. })
  173. }
  174. addMessage (options: OverrideCommandOptions & {
  175. abuseId: number
  176. message: string
  177. }) {
  178. const { abuseId, message } = options
  179. const path = '/api/v1/abuses/' + abuseId + '/messages'
  180. return this.postBodyRequest({
  181. ...options,
  182. path,
  183. fields: { message },
  184. implicitToken: true,
  185. defaultExpectedStatus: HttpStatusCode.OK_200
  186. })
  187. }
  188. }