aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/videos/comments-command.ts
blob: f0d163a0775f4114abe092cd6fb7a3b15e7569e7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import { pick } from 'lodash'
import { HttpStatusCode, ResultList, VideoComment, VideoCommentThreads, VideoCommentThreadTree } from '@shared/models'
import { unwrapBody } from '../requests'
import { AbstractCommand, OverrideCommandOptions } from '../shared'

export class CommentsCommand extends AbstractCommand {

  private lastVideoId: number | string
  private lastThreadId: number
  private lastReplyId: number

  listForAdmin (options: OverrideCommandOptions & {
    start?: number
    count?: number
    sort?: string
    isLocal?: boolean
    search?: string
    searchAccount?: string
    searchVideo?: string
  } = {}) {
    const { sort = '-createdAt' } = options
    const path = '/api/v1/videos/comments'

    const query = { sort, ...pick(options, [ 'start', 'count', 'isLocal', 'search', 'searchAccount', 'searchVideo' ]) }

    return this.getRequestBody<ResultList<VideoComment>>({
      ...options,

      path,
      query,
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.OK_200
    })
  }

  listThreads (options: OverrideCommandOptions & {
    videoId: number | string
    start?: number
    count?: number
    sort?: string
  }) {
    const { start, count, sort, videoId } = options
    const path = '/api/v1/videos/' + videoId + '/comment-threads'

    return this.getRequestBody<VideoCommentThreads>({
      ...options,

      path,
      query: { start, count, sort },
      implicitToken: false,
      defaultExpectedStatus: HttpStatusCode.OK_200
    })
  }

  getThread (options: OverrideCommandOptions & {
    videoId: number | string
    threadId: number
  }) {
    const { videoId, threadId } = options
    const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId

    return this.getRequestBody<VideoCommentThreadTree>({
      ...options,

      path,
      implicitToken: false,
      defaultExpectedStatus: HttpStatusCode.OK_200
    })
  }

  async createThread (options: OverrideCommandOptions & {
    videoId: number | string
    text: string
  }) {
    const { videoId, text } = options
    const path = '/api/v1/videos/' + videoId + '/comment-threads'

    const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
      ...options,

      path,
      fields: { text },
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.OK_200
    }))

    this.lastThreadId = body.comment?.id
    this.lastVideoId = videoId

    return body.comment
  }

  async addReply (options: OverrideCommandOptions & {
    videoId: number | string
    toCommentId: number
    text: string
  }) {
    const { videoId, toCommentId, text } = options
    const path = '/api/v1/videos/' + videoId + '/comments/' + toCommentId

    const body = await unwrapBody<{ comment: VideoComment }>(this.postBodyRequest({
      ...options,

      path,
      fields: { text },
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.OK_200
    }))

    this.lastReplyId = body.comment?.id

    return body.comment
  }

  async addReplyToLastReply (options: OverrideCommandOptions & {
    text: string
  }) {
    return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastReplyId })
  }

  async addReplyToLastThread (options: OverrideCommandOptions & {
    text: string
  }) {
    return this.addReply({ ...options, videoId: this.lastVideoId, toCommentId: this.lastThreadId })
  }

  async findCommentId (options: OverrideCommandOptions & {
    videoId: number | string
    text: string
  }) {
    const { videoId, text } = options
    const { data } = await this.listThreads({ videoId, count: 25, sort: '-createdAt' })

    return data.find(c => c.text === text).id
  }

  delete (options: OverrideCommandOptions & {
    videoId: number | string
    commentId: number
  }) {
    const { videoId, commentId } = options
    const path = '/api/v1/videos/' + videoId + '/comments/' + commentId

    return this.deleteRequest({
      ...options,

      path,
      implicitToken: true,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }
}