comment.ts 6.43 KB
Newer Older
Chocobozzz's avatar
Chocobozzz committed
1
import * as express from 'express'
2
import { ResultList } from '../../../../shared/models'
Chocobozzz's avatar
Chocobozzz committed
3
import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
Chocobozzz's avatar
Chocobozzz committed
4
import { logger } from '../../../helpers/logger'
5
import { getFormattedObjects } from '../../../helpers/utils'
Chocobozzz's avatar
Chocobozzz committed
6
7
import { sequelizeTypescript } from '../../../initializers'
import { buildFormattedCommentTree, createVideoComment } from '../../../lib/video-comment'
8
9
10
import {
  asyncMiddleware,
  asyncRetryTransactionMiddleware,
Chocobozzz's avatar
Chocobozzz committed
11
12
  authenticate,
  optionalAuthenticate,
13
14
15
16
  paginationValidator,
  setDefaultPagination,
  setDefaultSort
} from '../../../middlewares'
Chocobozzz's avatar
Chocobozzz committed
17
import {
18
19
20
21
  addVideoCommentReplyValidator,
  addVideoCommentThreadValidator,
  listVideoCommentThreadsValidator,
  listVideoThreadCommentsValidator,
22
23
24
  removeVideoCommentValidator,
  videoCommentThreadsSortValidator
} from '../../../middlewares/validators'
Chocobozzz's avatar
Chocobozzz committed
25
import { VideoCommentModel } from '../../../models/video/video-comment'
Chocobozzz's avatar
Chocobozzz committed
26
import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
27
import { AccountModel } from '../../../models/account/account'
Chocobozzz's avatar
Chocobozzz committed
28
import { Notifier } from '../../../lib/notifier'
Chocobozzz's avatar
Chocobozzz committed
29
import { Hooks } from '../../../lib/plugins/hooks'
30
31
32
33
import { ActorModel } from '../../../models/activitypub/actor'
import { VideoChannelModel } from '../../../models/video/video-channel'
import { VideoModel } from '../../../models/video/video'
import { sendDeleteVideoComment } from '../../../lib/activitypub/send'
Chocobozzz's avatar
Chocobozzz committed
34

35
const auditLogger = auditLoggerFactory('comments')
Chocobozzz's avatar
Chocobozzz committed
36
37
38
39
40
const videoCommentRouter = express.Router()

videoCommentRouter.get('/:videoId/comment-threads',
  paginationValidator,
  videoCommentThreadsSortValidator,
Chocobozzz's avatar
Chocobozzz committed
41
  setDefaultSort,
42
  setDefaultPagination,
Chocobozzz's avatar
Chocobozzz committed
43
  asyncMiddleware(listVideoCommentThreadsValidator),
44
  optionalAuthenticate,
Chocobozzz's avatar
Chocobozzz committed
45
46
47
48
  asyncMiddleware(listVideoThreads)
)
videoCommentRouter.get('/:videoId/comment-threads/:threadId',
  asyncMiddleware(listVideoThreadCommentsValidator),
49
  optionalAuthenticate,
Chocobozzz's avatar
Chocobozzz committed
50
51
52
53
54
55
  asyncMiddleware(listVideoThreadComments)
)

videoCommentRouter.post('/:videoId/comment-threads',
  authenticate,
  asyncMiddleware(addVideoCommentThreadValidator),
56
  asyncRetryTransactionMiddleware(addVideoCommentThread)
Chocobozzz's avatar
Chocobozzz committed
57
58
59
60
)
videoCommentRouter.post('/:videoId/comments/:commentId',
  authenticate,
  asyncMiddleware(addVideoCommentReplyValidator),
61
  asyncRetryTransactionMiddleware(addVideoCommentReply)
Chocobozzz's avatar
Chocobozzz committed
62
)
Chocobozzz's avatar
Chocobozzz committed
63
64
65
videoCommentRouter.delete('/:videoId/comments/:commentId',
  authenticate,
  asyncMiddleware(removeVideoCommentValidator),
66
  asyncRetryTransactionMiddleware(removeVideoComment)
Chocobozzz's avatar
Chocobozzz committed
67
)
Chocobozzz's avatar
Chocobozzz committed
68
69
70
71
72
73
74
75
76

// ---------------------------------------------------------------------------

export {
  videoCommentRouter
}

// ---------------------------------------------------------------------------

Chocobozzz's avatar
Chocobozzz committed
77
78
79
async function listVideoThreads (req: express.Request, res: express.Response) {
  const video = res.locals.video
  const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
80

81
82
83
  let resultList: ResultList<VideoCommentModel>

  if (video.commentsEnabled === true) {
Chocobozzz's avatar
Chocobozzz committed
84
85
86
87
88
89
90
91
    const apiOptions = await Hooks.wrapObject({
      videoId: video.id,
      start: req.query.start,
      count: req.query.count,
      sort: req.query.sort,
      user: user
    }, 'filter:api.video-threads.list.params')

Chocobozzz's avatar
Chocobozzz committed
92
93
94
    resultList = await Hooks.wrapPromiseFun(
      VideoCommentModel.listThreadsForApi,
      apiOptions,
Chocobozzz's avatar
Chocobozzz committed
95
96
      'filter:api.video-threads.list.result'
    )
97
98
99
100
101
102
  } else {
    resultList = {
      total: 0,
      data: []
    }
  }
Chocobozzz's avatar
Chocobozzz committed
103
104
105
106

  return res.json(getFormattedObjects(resultList.data, resultList.total))
}

Chocobozzz's avatar
Chocobozzz committed
107
108
109
async function listVideoThreadComments (req: express.Request, res: express.Response) {
  const video = res.locals.video
  const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
110

111
112
113
  let resultList: ResultList<VideoCommentModel>

  if (video.commentsEnabled === true) {
Chocobozzz's avatar
Chocobozzz committed
114
115
116
    const apiOptions = await Hooks.wrapObject({
      videoId: video.id,
      threadId: res.locals.videoCommentThread.id,
Chocobozzz's avatar
Chocobozzz committed
117
      user
Chocobozzz's avatar
Chocobozzz committed
118
119
    }, 'filter:api.video-thread-comments.list.params')

Chocobozzz's avatar
Chocobozzz committed
120
121
122
    resultList = await Hooks.wrapPromiseFun(
      VideoCommentModel.listThreadCommentsForApi,
      apiOptions,
Chocobozzz's avatar
Chocobozzz committed
123
124
      'filter:api.video-thread-comments.list.result'
    )
125
126
127
128
129
130
  } else {
    resultList = {
      total: 0,
      data: []
    }
  }
Chocobozzz's avatar
Chocobozzz committed
131
132
133
134

  return res.json(buildFormattedCommentTree(resultList))
}

135
async function addVideoCommentThread (req: express.Request, res: express.Response) {
Chocobozzz's avatar
Chocobozzz committed
136
137
  const videoCommentInfo: VideoCommentCreate = req.body

138
  const comment = await sequelizeTypescript.transaction(async t => {
Chocobozzz's avatar
Chocobozzz committed
139
    const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
140

Chocobozzz's avatar
Chocobozzz committed
141
142
    return createVideoComment({
      text: videoCommentInfo.text,
143
      inReplyToComment: null,
Chocobozzz's avatar
Chocobozzz committed
144
      video: res.locals.video,
145
      account
Chocobozzz's avatar
Chocobozzz committed
146
147
148
    }, t)
  })

Chocobozzz's avatar
Chocobozzz committed
149
  Notifier.Instance.notifyOnNewComment(comment)
Chocobozzz's avatar
Chocobozzz committed
150
  auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
151

Chocobozzz's avatar
Chocobozzz committed
152
153
  Hooks.runAction('action:api.video-thread.created', { comment })

154
  return res.json({
Chocobozzz's avatar
Chocobozzz committed
155
    comment: comment.toFormattedJSON()
Chocobozzz's avatar
Chocobozzz committed
156
157
158
  }).end()
}

159
async function addVideoCommentReply (req: express.Request, res: express.Response) {
Chocobozzz's avatar
Chocobozzz committed
160
161
  const videoCommentInfo: VideoCommentCreate = req.body

162
  const comment = await sequelizeTypescript.transaction(async t => {
Chocobozzz's avatar
Chocobozzz committed
163
    const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
164

Chocobozzz's avatar
Chocobozzz committed
165
166
    return createVideoComment({
      text: videoCommentInfo.text,
167
      inReplyToComment: res.locals.videoComment,
Chocobozzz's avatar
Chocobozzz committed
168
      video: res.locals.video,
169
      account
Chocobozzz's avatar
Chocobozzz committed
170
171
    }, t)
  })
Chocobozzz's avatar
Chocobozzz committed
172

Chocobozzz's avatar
Chocobozzz committed
173
  Notifier.Instance.notifyOnNewComment(comment)
Chocobozzz's avatar
Chocobozzz committed
174
  auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
175

Chocobozzz's avatar
Chocobozzz committed
176
177
  Hooks.runAction('action:api.video-comment-reply.created', { comment })

178
  return res.json({ comment: comment.toFormattedJSON() }).end()
Chocobozzz's avatar
Chocobozzz committed
179
180
181
}

async function removeVideoComment (req: express.Request, res: express.Response) {
Chocobozzz's avatar
Chocobozzz committed
182
  const videoCommentInstance = res.locals.videoComment
Chocobozzz's avatar
Chocobozzz committed
183
184
185

  await sequelizeTypescript.transaction(async t => {
    await videoCommentInstance.destroy({ transaction: t })
186
187
188
189

    if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) {
      await sendDeleteVideoComment(videoCommentInstance, t)
    }
Chocobozzz's avatar
Chocobozzz committed
190
191
  })

Chocobozzz's avatar
Chocobozzz committed
192
  auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
Chocobozzz's avatar
Chocobozzz committed
193
  logger.info('Video comment %d deleted.', videoCommentInstance.id)
194

Chocobozzz's avatar
Chocobozzz committed
195
196
  Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstance })

197
  return res.type('json').status(204).end()
Chocobozzz's avatar
Chocobozzz committed
198
}