comments-command.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import { pick } from '@shared/core-utils'
  2. import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models'
  3. import { unwrapBody } from '../requests'
  4. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  5. export class CommentsCommand extends AbstractCommand {
  6. private lastVideoId: number | string
  7. private lastThreadId: number
  8. private lastReplyId: number
  9. listForAdmin (options: OverrideCommandOptions & {
  10. start?: number
  11. count?: number
  12. sort?: string
  13. isLocal?: boolean
  14. onLocalVideo?: boolean
  15. search?: string
  16. searchAccount?: string
  17. searchVideo?: string
  18. } = {}) {
  19. const { sort = '-createdAt' } = options
  20. const path = '/api/v1/videos/comments'
  21. const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'onLocalVideo', 'search', 'searchAccount', 'searchVideo' ]) }
  22. return this.getRequestBody<ResultList<VideoComment>>({
  23. ...options,
  24. path,
  25. query,
  26. implicitToken: true,
  27. defaultExpectedStatus: HttpStatusCode.OK_200
  28. })
  29. }
  30. listThreads (options: OverrideCommandOptions & {
  31. videoId: number | string
  32. start?: number
  33. count?: number
  34. sort?: string
  35. }) {
  36. const { start, count, sort, videoId } = options
  37. const path = '/api/v1/videos/' + videoId + '/comment-threads'
  38. return this.getRequestBody<VideoCommentThreads>({
  39. ...options,
  40. path,
  41. query: { start, count, sort },
  42. implicitToken: false,
  43. defaultExpectedStatus: HttpStatusCode.OK_200
  44. })
  45. }
  46. getThread (options: OverrideCommandOptions & {
  47. videoId: number | string
  48. threadId: number
  49. }) {
  50. const { videoId, threadId } = options
  51. const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
  52. return this.getRequestBody<VideoCommentThreadTree>({
  53. ...options,
  54. path,
  55. implicitToken: false,
  56. defaultExpectedStatus: HttpStatusCode.OK_200
  57. })
  58. }
  59. async createThread (options: OverrideCommandOptions & {
  60. videoId: number | string
  61. text: string
  62. }) {
  63. const { videoId, text } = options
  64. const path = '/api/v1/videos/' + videoId + '/comment-threads'
  65. const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
  66. ...options,
  67. path,
  68. fields: { text },
  69. implicitToken: true,
  70. defaultExpectedStatus: HttpStatusCode.OK_200
  71. }))
  72. this.lastThreadId = body.comment?.id
  73. this.lastVideoId = videoId
  74. return body.comment
  75. }
  76. async addReply (options: OverrideCommandOptions & {
  77. videoId: number | string
  78. toCommentId: number
  79. text: string
  80. }) {
  81. const { videoId, toCommentId, text } = options
  82. const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId
  83. const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
  84. ...options,
  85. path,
  86. fields: { text },
  87. implicitToken: true,
  88. defaultExpectedStatus: HttpStatusCode.OK_200
  89. }))
  90. this.lastReplyId = body.comment?.id
  91. return body.comment
  92. }
  93. async addReplyToLastReply (options: OverrideCommandOptions & {
  94. text: string
  95. }) {
  96. return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastReplyId })
  97. }
  98. async addReplyToLastThread (options: OverrideCommandOptions & {
  99. text: string
  100. }) {
  101. return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastThreadId })
  102. }
  103. async findCommentId (options: OverrideCommandOptions & {
  104. videoId: number | string
  105. text: string
  106. }) {
  107. const { videoId, text } = options
  108. const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' })
  109. return data.find(c => c.text === text).id
  110. }
  111. delete (options: OverrideCommandOptions & {
  112. videoId: number | string
  113. commentId: number
  114. }) {
  115. const { videoId, commentId } = options
  116. const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
  117. return this.deleteRequest({
  118. ...options,
  119. path,
  120. implicitToken: true,
  121. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  122. })
  123. }
  124. }