2017-12-22 04:50:07 -05:00
|
|
|
import * as express from 'express'
|
2021-02-19 05:23:51 -05:00
|
|
|
import { HttpStatusCode } from '../../../../shared/core-utils/miscs/http-error-codes'
|
2021-02-19 03:50:13 -05:00
|
|
|
import { ResultList, ThreadsResultList, UserRight } from '../../../../shared/models'
|
2021-05-11 05:27:40 -04:00
|
|
|
import { VideoCommentCreate } from '../../../../shared/models/videos/comment/video-comment.model'
|
2020-05-14 10:56:15 -04:00
|
|
|
import { auditLoggerFactory, CommentAuditView, getAuditIdFromRes } from '../../../helpers/audit-logger'
|
2017-12-28 05:16:08 -05:00
|
|
|
import { getFormattedObjects } from '../../../helpers/utils'
|
2020-05-07 08:58:24 -04:00
|
|
|
import { sequelizeTypescript } from '../../../initializers/database'
|
2020-05-14 10:56:15 -04:00
|
|
|
import { Notifier } from '../../../lib/notifier'
|
|
|
|
import { Hooks } from '../../../lib/plugins/hooks'
|
|
|
|
import { buildFormattedCommentTree, createVideoComment, removeComment } from '../../../lib/video-comment'
|
2018-06-13 08:27:40 -04:00
|
|
|
import {
|
|
|
|
asyncMiddleware,
|
|
|
|
asyncRetryTransactionMiddleware,
|
2019-03-19 05:35:15 -04:00
|
|
|
authenticate,
|
2020-11-13 10:38:23 -05:00
|
|
|
ensureUserHasRight,
|
2019-03-19 05:35:15 -04:00
|
|
|
optionalAuthenticate,
|
2018-06-13 08:27:40 -04:00
|
|
|
paginationValidator,
|
|
|
|
setDefaultPagination,
|
|
|
|
setDefaultSort
|
|
|
|
} from '../../../middlewares'
|
2017-12-22 04:50:07 -05:00
|
|
|
import {
|
2018-06-13 08:27:40 -04:00
|
|
|
addVideoCommentReplyValidator,
|
|
|
|
addVideoCommentThreadValidator,
|
2020-11-13 10:38:23 -05:00
|
|
|
listVideoCommentsValidator,
|
2018-06-13 08:27:40 -04:00
|
|
|
listVideoCommentThreadsValidator,
|
|
|
|
listVideoThreadCommentsValidator,
|
2018-10-05 05:15:06 -04:00
|
|
|
removeVideoCommentValidator,
|
2020-11-13 10:38:23 -05:00
|
|
|
videoCommentsValidator,
|
2018-10-05 05:15:06 -04:00
|
|
|
videoCommentThreadsSortValidator
|
|
|
|
} from '../../../middlewares/validators'
|
2018-09-20 04:13:13 -04:00
|
|
|
import { AccountModel } from '../../../models/account/account'
|
2020-05-14 10:56:15 -04:00
|
|
|
import { VideoCommentModel } from '../../../models/video/video-comment'
|
2017-12-22 04:50:07 -05:00
|
|
|
|
2018-07-31 08:04:26 -04:00
|
|
|
const auditLogger = auditLoggerFactory('comments')
|
2017-12-22 04:50:07 -05:00
|
|
|
const videoCommentRouter = express.Router()
|
|
|
|
|
|
|
|
videoCommentRouter.get('/:videoId/comment-threads',
|
|
|
|
paginationValidator,
|
|
|
|
videoCommentThreadsSortValidator,
|
2018-01-17 04:50:33 -05:00
|
|
|
setDefaultSort,
|
2018-01-18 04:53:54 -05:00
|
|
|
setDefaultPagination,
|
2017-12-22 04:50:07 -05:00
|
|
|
asyncMiddleware(listVideoCommentThreadsValidator),
|
2018-10-12 09:26:04 -04:00
|
|
|
optionalAuthenticate,
|
2017-12-22 04:50:07 -05:00
|
|
|
asyncMiddleware(listVideoThreads)
|
|
|
|
)
|
|
|
|
videoCommentRouter.get('/:videoId/comment-threads/:threadId',
|
|
|
|
asyncMiddleware(listVideoThreadCommentsValidator),
|
2018-10-12 09:26:04 -04:00
|
|
|
optionalAuthenticate,
|
2017-12-22 04:50:07 -05:00
|
|
|
asyncMiddleware(listVideoThreadComments)
|
|
|
|
)
|
|
|
|
|
|
|
|
videoCommentRouter.post('/:videoId/comment-threads',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(addVideoCommentThreadValidator),
|
2018-06-13 08:27:40 -04:00
|
|
|
asyncRetryTransactionMiddleware(addVideoCommentThread)
|
2017-12-22 04:50:07 -05:00
|
|
|
)
|
|
|
|
videoCommentRouter.post('/:videoId/comments/:commentId',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(addVideoCommentReplyValidator),
|
2018-06-13 08:27:40 -04:00
|
|
|
asyncRetryTransactionMiddleware(addVideoCommentReply)
|
2017-12-22 04:50:07 -05:00
|
|
|
)
|
2018-01-04 05:19:16 -05:00
|
|
|
videoCommentRouter.delete('/:videoId/comments/:commentId',
|
|
|
|
authenticate,
|
|
|
|
asyncMiddleware(removeVideoCommentValidator),
|
2018-06-13 08:27:40 -04:00
|
|
|
asyncRetryTransactionMiddleware(removeVideoComment)
|
2018-01-04 05:19:16 -05:00
|
|
|
)
|
2017-12-22 04:50:07 -05:00
|
|
|
|
2020-11-13 10:38:23 -05:00
|
|
|
videoCommentRouter.get('/comments',
|
|
|
|
authenticate,
|
|
|
|
ensureUserHasRight(UserRight.SEE_ALL_COMMENTS),
|
|
|
|
paginationValidator,
|
|
|
|
videoCommentsValidator,
|
|
|
|
setDefaultSort,
|
|
|
|
setDefaultPagination,
|
|
|
|
listVideoCommentsValidator,
|
|
|
|
asyncMiddleware(listComments)
|
|
|
|
)
|
|
|
|
|
2017-12-22 04:50:07 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoCommentRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
2020-11-13 10:38:23 -05:00
|
|
|
async function listComments (req: express.Request, res: express.Response) {
|
|
|
|
const options = {
|
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
sort: req.query.sort,
|
|
|
|
|
|
|
|
isLocal: req.query.isLocal,
|
|
|
|
search: req.query.search,
|
|
|
|
searchAccount: req.query.searchAccount,
|
|
|
|
searchVideo: req.query.searchVideo
|
|
|
|
}
|
|
|
|
|
|
|
|
const resultList = await VideoCommentModel.listCommentsForApi(options)
|
|
|
|
|
|
|
|
return res.json({
|
|
|
|
total: resultList.total,
|
|
|
|
data: resultList.data.map(c => c.toFormattedAdminJSON())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-03-19 05:35:15 -04:00
|
|
|
async function listVideoThreads (req: express.Request, res: express.Response) {
|
2019-08-15 05:53:26 -04:00
|
|
|
const video = res.locals.onlyVideo
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
|
2018-10-12 09:26:04 -04:00
|
|
|
|
2021-02-19 03:50:13 -05:00
|
|
|
let resultList: ThreadsResultList<VideoCommentModel>
|
2018-01-03 04:12:36 -05:00
|
|
|
|
|
|
|
if (video.commentsEnabled === true) {
|
2019-07-18 08:28:37 -04:00
|
|
|
const apiOptions = await Hooks.wrapObject({
|
|
|
|
videoId: video.id,
|
2020-05-22 11:06:26 -04:00
|
|
|
isVideoOwned: video.isOwned(),
|
2019-07-18 08:28:37 -04:00
|
|
|
start: req.query.start,
|
|
|
|
count: req.query.count,
|
|
|
|
sort: req.query.sort,
|
2019-08-15 05:53:26 -04:00
|
|
|
user
|
2019-07-18 08:28:37 -04:00
|
|
|
}, 'filter:api.video-threads.list.params')
|
|
|
|
|
2019-07-19 11:30:41 -04:00
|
|
|
resultList = await Hooks.wrapPromiseFun(
|
|
|
|
VideoCommentModel.listThreadsForApi,
|
|
|
|
apiOptions,
|
2019-07-18 08:28:37 -04:00
|
|
|
'filter:api.video-threads.list.result'
|
|
|
|
)
|
2018-01-03 04:12:36 -05:00
|
|
|
} else {
|
|
|
|
resultList = {
|
|
|
|
total: 0,
|
2021-02-19 03:50:13 -05:00
|
|
|
totalNotDeletedComments: 0,
|
2018-01-03 04:12:36 -05:00
|
|
|
data: []
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 04:50:07 -05:00
|
|
|
|
2021-02-19 03:50:13 -05:00
|
|
|
return res.json({
|
|
|
|
...getFormattedObjects(resultList.data, resultList.total),
|
|
|
|
totalNotDeletedComments: resultList.totalNotDeletedComments
|
|
|
|
})
|
2017-12-22 04:50:07 -05:00
|
|
|
}
|
|
|
|
|
2019-03-19 05:35:15 -04:00
|
|
|
async function listVideoThreadComments (req: express.Request, res: express.Response) {
|
2019-08-15 05:53:26 -04:00
|
|
|
const video = res.locals.onlyVideo
|
2019-03-19 05:35:15 -04:00
|
|
|
const user = res.locals.oauth ? res.locals.oauth.token.User : undefined
|
2018-10-12 09:26:04 -04:00
|
|
|
|
2018-01-03 04:12:36 -05:00
|
|
|
let resultList: ResultList<VideoCommentModel>
|
|
|
|
|
|
|
|
if (video.commentsEnabled === true) {
|
2019-07-18 08:28:37 -04:00
|
|
|
const apiOptions = await Hooks.wrapObject({
|
|
|
|
videoId: video.id,
|
2020-05-22 11:06:26 -04:00
|
|
|
isVideoOwned: video.isOwned(),
|
2019-07-18 08:28:37 -04:00
|
|
|
threadId: res.locals.videoCommentThread.id,
|
2019-07-22 05:14:58 -04:00
|
|
|
user
|
2019-07-18 08:28:37 -04:00
|
|
|
}, 'filter:api.video-thread-comments.list.params')
|
|
|
|
|
2019-07-19 11:30:41 -04:00
|
|
|
resultList = await Hooks.wrapPromiseFun(
|
|
|
|
VideoCommentModel.listThreadCommentsForApi,
|
|
|
|
apiOptions,
|
2019-07-18 08:28:37 -04:00
|
|
|
'filter:api.video-thread-comments.list.result'
|
|
|
|
)
|
2018-01-03 04:12:36 -05:00
|
|
|
} else {
|
|
|
|
resultList = {
|
|
|
|
total: 0,
|
|
|
|
data: []
|
|
|
|
}
|
|
|
|
}
|
2017-12-22 04:50:07 -05:00
|
|
|
|
2020-11-27 05:48:20 -05:00
|
|
|
if (resultList.data.length === 0) {
|
2021-05-31 19:36:53 -04:00
|
|
|
return res.fail({
|
|
|
|
status: HttpStatusCode.NOT_FOUND_404,
|
|
|
|
message: 'No comments were found'
|
|
|
|
})
|
2020-11-27 05:48:20 -05:00
|
|
|
}
|
|
|
|
|
2017-12-22 04:50:07 -05:00
|
|
|
return res.json(buildFormattedCommentTree(resultList))
|
|
|
|
}
|
|
|
|
|
2018-06-13 08:27:40 -04:00
|
|
|
async function addVideoCommentThread (req: express.Request, res: express.Response) {
|
2017-12-22 04:50:07 -05:00
|
|
|
const videoCommentInfo: VideoCommentCreate = req.body
|
|
|
|
|
2018-06-13 08:27:40 -04:00
|
|
|
const comment = await sequelizeTypescript.transaction(async t => {
|
2019-03-19 05:35:15 -04:00
|
|
|
const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
|
2018-09-20 04:13:13 -04:00
|
|
|
|
2017-12-22 04:50:07 -05:00
|
|
|
return createVideoComment({
|
|
|
|
text: videoCommentInfo.text,
|
2017-12-27 04:39:31 -05:00
|
|
|
inReplyToComment: null,
|
2019-08-15 05:53:26 -04:00
|
|
|
video: res.locals.videoAll,
|
2018-09-20 04:13:13 -04:00
|
|
|
account
|
2017-12-22 04:50:07 -05:00
|
|
|
}, t)
|
|
|
|
})
|
|
|
|
|
2018-12-26 04:36:24 -05:00
|
|
|
Notifier.Instance.notifyOnNewComment(comment)
|
2018-09-19 11:02:16 -04:00
|
|
|
auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
|
2018-07-31 08:04:26 -04:00
|
|
|
|
2019-07-18 08:28:37 -04:00
|
|
|
Hooks.runAction('action:api.video-thread.created', { comment })
|
|
|
|
|
2020-05-14 10:56:15 -04:00
|
|
|
return res.json({ comment: comment.toFormattedJSON() })
|
2017-12-22 04:50:07 -05:00
|
|
|
}
|
|
|
|
|
2018-06-13 08:27:40 -04:00
|
|
|
async function addVideoCommentReply (req: express.Request, res: express.Response) {
|
2017-12-22 04:50:07 -05:00
|
|
|
const videoCommentInfo: VideoCommentCreate = req.body
|
|
|
|
|
2018-06-13 08:27:40 -04:00
|
|
|
const comment = await sequelizeTypescript.transaction(async t => {
|
2019-03-19 05:35:15 -04:00
|
|
|
const account = await AccountModel.load(res.locals.oauth.token.User.Account.id, t)
|
2018-09-20 04:13:13 -04:00
|
|
|
|
2017-12-22 04:50:07 -05:00
|
|
|
return createVideoComment({
|
|
|
|
text: videoCommentInfo.text,
|
2019-08-15 05:53:26 -04:00
|
|
|
inReplyToComment: res.locals.videoCommentFull,
|
|
|
|
video: res.locals.videoAll,
|
2018-09-20 04:13:13 -04:00
|
|
|
account
|
2017-12-22 04:50:07 -05:00
|
|
|
}, t)
|
|
|
|
})
|
2018-01-04 05:19:16 -05:00
|
|
|
|
2018-12-26 04:36:24 -05:00
|
|
|
Notifier.Instance.notifyOnNewComment(comment)
|
2018-09-19 11:02:16 -04:00
|
|
|
auditLogger.create(getAuditIdFromRes(res), new CommentAuditView(comment.toFormattedJSON()))
|
2018-07-31 08:04:26 -04:00
|
|
|
|
2019-07-18 08:28:37 -04:00
|
|
|
Hooks.runAction('action:api.video-comment-reply.created', { comment })
|
|
|
|
|
2020-05-14 10:56:15 -04:00
|
|
|
return res.json({ comment: comment.toFormattedJSON() })
|
2018-01-04 05:19:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function removeVideoComment (req: express.Request, res: express.Response) {
|
2019-08-15 05:53:26 -04:00
|
|
|
const videoCommentInstance = res.locals.videoCommentFull
|
2019-11-15 13:05:08 -05:00
|
|
|
|
2020-05-14 10:56:15 -04:00
|
|
|
await removeComment(videoCommentInstance)
|
2018-01-04 05:19:16 -05:00
|
|
|
|
2019-07-18 08:28:37 -04:00
|
|
|
auditLogger.delete(getAuditIdFromRes(res), new CommentAuditView(videoCommentInstance.toFormattedJSON()))
|
|
|
|
|
2020-12-07 08:32:36 -05:00
|
|
|
return res.type('json')
|
|
|
|
.status(HttpStatusCode.NO_CONTENT_204)
|
|
|
|
.end()
|
2018-01-04 05:19:16 -05:00
|
|
|
}
|