2024-03-29 08:25:03 -05:00
|
|
|
import { pick } from '@peertube/peertube-core-utils'
|
2023-07-31 07:34:36 -05:00
|
|
|
import {
|
|
|
|
HttpStatusCode,
|
|
|
|
ResultList,
|
|
|
|
ThreadsResultList,
|
|
|
|
UserRight,
|
|
|
|
VideoCommentCreate,
|
2024-03-29 08:25:03 -05:00
|
|
|
VideoCommentPolicy,
|
2023-07-31 07:34:36 -05:00
|
|
|
VideoCommentThreads
|
|
|
|
} from '@peertube/peertube-models'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { getServerActor } from '@server/models/application/application.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { MCommentFormattable } from '@server/types/models/index.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import express from 'express'
|
|
|
|
import { CommentAuditView, auditLoggerFactory, getAuditIdFromRes } from '../../../helpers/audit-logger.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { getFormattedObjects } from '../../../helpers/utils.js'
|
|
|
|
import { Notifier } from '../../../lib/notifier/index.js'
|
|
|
|
import { Hooks } from '../../../lib/plugins/hooks.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { approveComment, buildFormattedCommentTree, createLocalVideoComment, removeComment } from '../../../lib/video-comment.js'
|
2018-06-13 07:27:40 -05:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
2019-03-19 04:35:15 -05:00
|
|
|
authenticate,
|
2020-11-13 09:38:23 -06:00
|
|
|
ensureUserHasRight,
|
2019-03-19 04:35:15 -05:00
|
|
|
optionalAuthenticate,
|
2018-06-13 07:27:40 -05:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort
|
2023-07-31 07:34:36 -05:00
|
|
|
} from '../../../middlewares/index.js'
|
2017-12-22 03:50:07 -06:00
|
|
|
import {
|
2018-06-13 07:27:40 -05:00
|
|
|
addVideoCommentReplyValidator,
|
|
|
|
addVideoCommentThreadValidator,
|
2024-03-29 08:25:03 -05:00
|
|
|
approveVideoCommentValidator,
|
|
|
|
listAllVideoCommentsForAdminValidator,
|
2018-06-13 07:27:40 -05:00
|
|
|
listVideoCommentThreadsValidator,
|
|
|
|
listVideoThreadCommentsValidator,
|
2018-10-05 04:15:06 -05:00
|
|
|
removeVideoCommentValidator,
|
2024-03-29 08:25:03 -05:00
|
|
|
videoCommentThreadsSortValidator,
|
|
|
|
videoCommentsValidator
|
2023-07-31 07:34:36 -05:00
|
|
|
} from '../../../middlewares/validators/index.js'
|
|
|
|
import { VideoCommentModel } from '../../../models/video/video-comment.js'
|
2017-12-22 03:50:07 -06:00
|
|
|
|
2018-07-31 07:04:26 -05:00
|
|
|
const auditLogger = auditLoggerFactory('comments')
|
2017-12-22 03:50:07 -06:00
|
|
|
const videoCommentRouter = express.Router()
|
|
|
|
|
|
|
|
videoCommentRouter.get('/:videoId/comment-threads',
|
|
|
|
paginationValidator,
|
|
|
|
videoCommentThreadsSortValidator,
|
2018-01-17 03:50:33 -06:00
|
|
|
setDefaultSort,
|
2018-01-18 03:53:54 -06:00
|
|
|
setDefaultPagination,
|
2017-12-22 03:50:07 -06:00
|
|
|
asyncMiddleware(listVideoCommentThreadsValidator),
|
2018-10-12 08:26:04 -05:00
|
|
|
optionalAuthenticate,
|
2017-12-22 03:50:07 -06:00
|
|
|
asyncMiddleware(listVideoThreads)
|
|
|
|
)
|
|
|
|
videoCommentRouter.get('/:videoId/comment-threads/:threadId',
|
|
|
|
asyncMiddleware(listVideoThreadCommentsValidator),
|
2018-10-12 08:26:04 -05:00
|
|
|
optionalAuthenticate,
|
2017-12-22 03:50:07 -06:00
|
|
|
asyncMiddleware(listVideoThreadComments)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoCommentRouter.post('/:videoId/comment-threads',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(addVideoCommentThreadValidator),
|
2018-06-13 07:27:40 -05:00
|
|
|
asyncRetryTransactionMiddleware(addVideoCommentThread)
|
2017-12-22 03:50:07 -06:00
|
|
|
)
|
|
|
|
videoCommentRouter.post('/:videoId/comments/:commentId',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(addVideoCommentReplyValidator),
|
2018-06-13 07:27:40 -05:00
|
|
|
asyncRetryTransactionMiddleware(addVideoCommentReply)
|
2017-12-22 03:50:07 -06:00
|
|
|
)
|
2018-01-04 04:19:16 -06:00
|
|
|
videoCommentRouter.delete('/:videoId/comments/:commentId',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(removeVideoCommentValidator),
|
2018-06-13 07:27:40 -05:00
|
|
|
asyncRetryTransactionMiddleware(removeVideoComment)
|
2018-01-04 04:19:16 -06:00
|
|
|
)
|
2017-12-22 03:50:07 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
videoCommentRouter.post('/:videoId/comments/:commentId/approve',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(approveVideoCommentValidator),
|
|
|
|
asyncMiddleware(approveVideoComment)
|
|
|
|
)
|
|
|
|
|
2020-11-13 09:38:23 -06:00
|
|
|
videoCommentRouter.get('/comments',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
|
|
|
|
paginationValidator,
|
|
|
|
videoCommentsValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
2024-03-29 08:25:03 -05:00
|
|
|
asyncMiddleware(listAllVideoCommentsForAdminValidator),
|
2020-11-13 09:38:23 -06:00
|
|
|
asyncMiddleware(listComments)
|
|
|
|
)
|
|
|
|
|
2017-12-22 03:50:07 -06:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoCommentRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-11-13 09:38:23 -06:00
|
|
|
async function listComments (req: express.Request, res: express.Response) {
|
|
|
|
const options = {
|
2024-03-29 08:25:03 -05:00
|
|
|
...pick(req.query, [
|
|
|
|
'start',
|
|
|
|
'count',
|
|
|
|
'sort',
|
|
|
|
'isLocal',
|
|
|
|
'onLocalVideo',
|
|
|
|
'search',
|
|
|
|
'searchAccount',
|
|
|
|
'searchVideo',
|
|
|
|
'autoTagOneOf'
|
|
|
|
]),
|
|
|
|
|
|
|
|
videoId: res.locals.onlyImmutableVideo?.id,
|
|
|
|
videoChannelOwnerId: res.locals.videoChannel?.id,
|
|
|
|
autoTagOfAccountId: (await getServerActor()).Account.id,
|
|
|
|
heldForReview: undefined
|
2020-11-13 09:38:23 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
const resultList = await VideoCommentModel.listCommentsForApi(options)
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
total: resultList.total,
|
2024-03-29 08:25:03 -05:00
|
|
|
data: resultList.data.map(c => c.toFormattedForAdminOrUserJSON())
|
2020-11-13 09:38:23 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-19 04:35:15 -05:00
|
|
|
async function listVideoThreads (req: express.Request, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
const video = res.locals.onlyVideo
|
2019-03-19 04:35:15 -05:00
|
|
|
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
|
2018-10-12 08:26:04 -05:00
|
|
|
|
2023-01-05 08:31:51 -06:00
|
|
|
let resultList: ThreadsResultList<MCommentFormattable>
|
2018-01-03 03:12:36 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
if (video.commentsPolicy !== VideoCommentPolicy.DISABLED) {
|
2019-07-18 07:28:37 -05:00
|
|
|
const apiOptions = await Hooks.wrapObject({
|
2024-03-29 08:25:03 -05:00
|
|
|
video,
|
2019-07-18 07:28:37 -05:00
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
sort: req.query.sort,
|
2019-08-15 04:53:26 -05:00
|
|
|
user
|
2019-07-18 07:28:37 -05:00
|
|
|
}, 'filter:api.video-threads.list.params')
|
|
|
|
|
2019-07-19 10:30:41 -05:00
|
|
|
resultList = await Hooks.wrapPromiseFun(
|
2024-02-22 03:12:04 -06:00
|
|
|
VideoCommentModel.listThreadsForApi.bind(VideoCommentModel),
|
2019-07-19 10:30:41 -05:00
|
|
|
apiOptions,
|
2019-07-18 07:28:37 -05:00
|
|
|
'filter:api.video-threads.list.result'
|
|
|
|
)
|
2018-01-03 03:12:36 -06:00
|
|
|
} else {
|
|
|
|
resultList = {
|
|
|
|
total: 0,
|
2021-02-19 02:50:13 -06:00
|
|
|
totalNotDeletedComments: 0,
|
2018-01-03 03:12:36 -06:00
|
|
|
data: []
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 03:50:07 -06:00
|
|
|
|
2021-02-19 02:50:13 -06:00
|
|
|
return res.json({
|
|
|
|
...getFormattedObjects(resultList.data, resultList.total),
|
|
|
|
totalNotDeletedComments: resultList.totalNotDeletedComments
|
2021-07-09 07:15:11 -05:00
|
|
|
} as VideoCommentThreads)
|
2017-12-22 03:50:07 -06:00
|
|
|
}
|
|
|
|
|
2019-03-19 04:35:15 -05:00
|
|
|
async function listVideoThreadComments (req: express.Request, res: express.Response) {
|
2019-08-15 04:53:26 -05:00
|
|
|
const video = res.locals.onlyVideo
|
2019-03-19 04:35:15 -05:00
|
|
|
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
|
2018-10-12 08:26:04 -05:00
|
|
|
|
2023-01-05 08:31:51 -06:00
|
|
|
let resultList: ResultList<MCommentFormattable>
|
2018-01-03 03:12:36 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
if (video.commentsPolicy !== VideoCommentPolicy.DISABLED) {
|
2019-07-18 07:28:37 -05:00
|
|
|
const apiOptions = await Hooks.wrapObject({
|
2024-03-29 08:25:03 -05:00
|
|
|
video,
|
2019-07-18 07:28:37 -05:00
|
|
|
threadId: res.locals.videoCommentThread.id,
|
2019-07-22 04:14:58 -05:00
|
|
|
user
|
2019-07-18 07:28:37 -05:00
|
|
|
}, 'filter:api.video-thread-comments.list.params')
|
|
|
|
|
2019-07-19 10:30:41 -05:00
|
|
|
resultList = await Hooks.wrapPromiseFun(
|
2024-02-22 03:12:04 -06:00
|
|
|
VideoCommentModel.listThreadCommentsForApi.bind(VideoCommentModel),
|
2019-07-19 10:30:41 -05:00
|
|
|
apiOptions,
|
2019-07-18 07:28:37 -05:00
|
|
|
'filter:api.video-thread-comments.list.result'
|
|
|
|
)
|
2018-01-03 03:12:36 -06:00
|
|
|
} else {
|
|
|
|
resultList = {
|
|
|
|
total: 0,
|
|
|
|
data: []
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 03:50:07 -06:00
|
|
|
|
2020-11-27 04:48:20 -06:00
|
|
|
if (resultList.data.length === 0) {
|
2021-05-31 18:36:53 -05:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'No comments were found'
|
|
|
|
})
|
2020-11-27 04:48:20 -06:00
|
|
|
}
|
|
|
|
|
2017-12-22 03:50:07 -06:00
|
|
|
return res.json(buildFormattedCommentTree(resultList))
|
|
|
|
}
|
|
|
|
|
2018-06-13 07:27:40 -05:00
|
|
|
async function addVideoCommentThread (req: express.Request, res: express.Response) {
|
2017-12-22 03:50:07 -06:00
|
|
|
const videoCommentInfo: VideoCommentCreate = req.body
|
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
const comment = await createLocalVideoComment({
|
|
|
|
text: videoCommentInfo.text,
|
|
|
|
inReplyToComment: null,
|
|
|
|
video: res.locals.videoAll,
|
|
|
|
user: res.locals.oauth.token.User
|
2017-12-22 03:50:07 -06:00
|
|
|
})
|
|
|
|
|
2018-12-26 03:36:24 -06:00
|
|
|
Notifier.Instance.notifyOnNewComment(comment)
|
2018-09-19 10:02:16 -05:00
|
|
|
auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
|
2018-07-31 07:04:26 -05:00
|
|
|
|
2021-11-24 07:33:14 -06:00
|
|
|
Hooks.runAction('action:api.video-thread.created', { comment, req, res })
|
2019-07-18 07:28:37 -05:00
|
|
|
|
2020-05-14 09:56:15 -05:00
|
|
|
return res.json({ comment: comment.toFormattedJSON() })
|
2017-12-22 03:50:07 -06:00
|
|
|
}
|
|
|
|
|
2018-06-13 07:27:40 -05:00
|
|
|
async function addVideoCommentReply (req: express.Request, res: express.Response) {
|
2017-12-22 03:50:07 -06:00
|
|
|
const videoCommentInfo: VideoCommentCreate = req.body
|
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
const comment = await createLocalVideoComment({
|
|
|
|
text: videoCommentInfo.text,
|
|
|
|
inReplyToComment: res.locals.videoCommentFull,
|
|
|
|
video: res.locals.videoAll,
|
|
|
|
user: res.locals.oauth.token.User
|
2017-12-22 03:50:07 -06:00
|
|
|
})
|
2018-01-04 04:19:16 -06:00
|
|
|
|
2018-12-26 03:36:24 -06:00
|
|
|
Notifier.Instance.notifyOnNewComment(comment)
|
2018-09-19 10:02:16 -05:00
|
|
|
auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
|
2018-07-31 07:04:26 -05:00
|
|
|
|
2021-11-24 07:33:14 -06:00
|
|
|
Hooks.runAction('action:api.video-comment-reply.created', { comment, req, res })
|
2019-07-18 07:28:37 -05:00
|
|
|
|
2020-05-14 09:56:15 -05:00
|
|
|
return res.json({ comment: comment.toFormattedJSON() })
|
2018-01-04 04:19:16 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeVideoComment (req: express.Request, res: express.Response) {
|
2024-03-29 08:25:03 -05:00
|
|
|
const comment = res.locals.videoCommentFull
|
2019-11-15 12:05:08 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
await removeComment(comment, req, res)
|
|
|
|
|
|
|
|
auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
|
|
|
|
|
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
}
|
2018-01-04 04:19:16 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
async function approveVideoComment (req: express.Request, res: express.Response) {
|
|
|
|
await approveComment(res.locals.videoCommentFull)
|
2019-07-18 07:28:37 -05:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
return res.sendStatus(HttpStatusCode.NO_CONTENT_204)
|
2018-01-04 04:19:16 -06:00
|
|
|
}
|