]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/controllers/api/videos/comment.ts
Add last login date to users
[github/Chocobozzz/PeerTube.git] / server / controllers / api / videos / comment.ts
... / ...
CommitLineData
1import * as express from 'express'
2import { cloneDeep } from 'lodash'
3import { ResultList } from '../../../../shared/models'
4import { VideoCommentCreate } from '../../../../shared/models/videos/video-comment.model'
5import { logger } from '../../../helpers/logger'
6import { getFormattedObjects } from '../../../helpers/utils'
7import { sequelizeTypescript } from '../../../initializers'
8import { buildFormattedCommentTree, createVideoComment, markCommentAsDeleted } from '../../../lib/video-comment'
9import {
10 asyncMiddleware,
11 asyncRetryTransactionMiddleware,
12 authenticate,
13 optionalAuthenticate,
14 paginationValidator,
15 setDefaultPagination,
16 setDefaultSort
17} from '../../../middlewares'
18import {
19 addVideoCommentReplyValidator,
20 addVideoCommentThreadValidator,
21 listVideoCommentThreadsValidator,
22 listVideoThreadCommentsValidator,
23 removeVideoCommentValidator,
24 videoCommentThreadsSortValidator
25} from '../../../middlewares/validators'
26import { VideoCommentModel } from '../../../models/video/video-comment'
27import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
28import { AccountModel } from '../../../models/account/account'
29import { Notifier } from '../../../lib/notifier'
30import { Hooks } from '../../../lib/plugins/hooks'
31import { sendDeleteVideoComment } from '../../../lib/activitypub/send'
32
33const auditLogger = auditLoggerFactory('comments')
34const videoCommentRouter = express.Router()
35
36videoCommentRouter.get('/:videoId/comment-threads',
37 paginationValidator,
38 videoCommentThreadsSortValidator,
39 setDefaultSort,
40 setDefaultPagination,
41 asyncMiddleware(listVideoCommentThreadsValidator),
42 optionalAuthenticate,
43 asyncMiddleware(listVideoThreads)
44)
45videoCommentRouter.get('/:videoId/comment-threads/:threadId',
46 asyncMiddleware(listVideoThreadCommentsValidator),
47 optionalAuthenticate,
48 asyncMiddleware(listVideoThreadComments)
49)
50
51videoCommentRouter.post('/:videoId/comment-threads',
52 authenticate,
53 asyncMiddleware(addVideoCommentThreadValidator),
54 asyncRetryTransactionMiddleware(addVideoCommentThread)
55)
56videoCommentRouter.post('/:videoId/comments/:commentId',
57 authenticate,
58 asyncMiddleware(addVideoCommentReplyValidator),
59 asyncRetryTransactionMiddleware(addVideoCommentReply)
60)
61videoCommentRouter.delete('/:videoId/comments/:commentId',
62 authenticate,
63 asyncMiddleware(removeVideoCommentValidator),
64 asyncRetryTransactionMiddleware(removeVideoComment)
65)
66
67// ---------------------------------------------------------------------------
68
69export {
70 videoCommentRouter
71}
72
73// ---------------------------------------------------------------------------
74
75async function listVideoThreads (req: express.Request, res: express.Response) {
76 const video = res.locals.onlyVideo
77 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
78
79 let resultList: ResultList<VideoCommentModel>
80
81 if (video.commentsEnabled === true) {
82 const apiOptions = await Hooks.wrapObject({
83 videoId: video.id,
84 start: req.query.start,
85 count: req.query.count,
86 sort: req.query.sort,
87 user
88 }, 'filter:api.video-threads.list.params')
89
90 resultList = await Hooks.wrapPromiseFun(
91 VideoCommentModel.listThreadsForApi,
92 apiOptions,
93 'filter:api.video-threads.list.result'
94 )
95 } else {
96 resultList = {
97 total: 0,
98 data: []
99 }
100 }
101
102 return res.json(getFormattedObjects(resultList.data, resultList.total))
103}
104
105async function listVideoThreadComments (req: express.Request, res: express.Response) {
106 const video = res.locals.onlyVideo
107 const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
108
109 let resultList: ResultList<VideoCommentModel>
110
111 if (video.commentsEnabled === true) {
112 const apiOptions = await Hooks.wrapObject({
113 videoId: video.id,
114 threadId: res.locals.videoCommentThread.id,
115 user
116 }, 'filter:api.video-thread-comments.list.params')
117
118 resultList = await Hooks.wrapPromiseFun(
119 VideoCommentModel.listThreadCommentsForApi,
120 apiOptions,
121 'filter:api.video-thread-comments.list.result'
122 )
123 } else {
124 resultList = {
125 total: 0,
126 data: []
127 }
128 }
129
130 return res.json(buildFormattedCommentTree(resultList))
131}
132
133async function addVideoCommentThread (req: express.Request, res: express.Response) {
134 const videoCommentInfo: VideoCommentCreate = req.body
135
136 const comment = await sequelizeTypescript.transaction(async t => {
137 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
138
139 return createVideoComment({
140 text: videoCommentInfo.text,
141 inReplyToComment: null,
142 video: res.locals.videoAll,
143 account
144 }, t)
145 })
146
147 Notifier.Instance.notifyOnNewComment(comment)
148 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
149
150 Hooks.runAction('action:api.video-thread.created', { comment })
151
152 return res.json({
153 comment: comment.toFormattedJSON()
154 }).end()
155}
156
157async function addVideoCommentReply (req: express.Request, res: express.Response) {
158 const videoCommentInfo: VideoCommentCreate = req.body
159
160 const comment = await sequelizeTypescript.transaction(async t => {
161 const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
162
163 return createVideoComment({
164 text: videoCommentInfo.text,
165 inReplyToComment: res.locals.videoCommentFull,
166 video: res.locals.videoAll,
167 account
168 }, t)
169 })
170
171 Notifier.Instance.notifyOnNewComment(comment)
172 auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
173
174 Hooks.runAction('action:api.video-comment-reply.created', { comment })
175
176 return res.json({ comment: comment.toFormattedJSON() }).end()
177}
178
179async function removeVideoComment (req: express.Request, res: express.Response) {
180 const videoCommentInstance = res.locals.videoCommentFull
181 const videoCommentInstanceBefore = cloneDeep(videoCommentInstance)
182
183 await sequelizeTypescript.transaction(async t => {
184 if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) {
185 await sendDeleteVideoComment(videoCommentInstance, t)
186 }
187
188 markCommentAsDeleted(videoCommentInstance)
189
190 await videoCommentInstance.save()
191 })
192
193 auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
194 logger.info('Video comment %d deleted.', videoCommentInstance.id)
195
196 Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore })
197
198 return res.type('json').status(204).end()
199}