2020-05-14 16:56:15 +02:00
|
|
|
import { cloneDeep } from 'lodash'
|
2017-12-22 10:50:07 +01:00
|
|
|
import * as Sequelize from 'sequelize'
|
2020-05-14 16:56:15 +02:00
|
|
|
import { logger } from '@server/helpers/logger'
|
|
|
|
import { sequelizeTypescript } from '@server/initializers/database'
|
2017-12-22 10:50:07 +01:00
|
|
|
import { ResultList } from '../../shared/models'
|
2017-12-22 12:10:40 +01:00
|
|
|
import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model'
|
2017-12-22 10:50:07 +01:00
|
|
|
import { VideoCommentModel } from '../models/video/video-comment'
|
2020-11-20 11:21:08 +01:00
|
|
|
import { MAccountDefault, MComment, MCommentOwnerVideo, MCommentOwnerVideoReply, MVideoFullLight } from '../types/models'
|
2020-05-14 16:56:15 +02:00
|
|
|
import { sendCreateVideoComment, sendDeleteVideoComment } from './activitypub/send'
|
2020-11-20 11:21:08 +01:00
|
|
|
import { getLocalVideoCommentActivityPubUrl } from './activitypub/url'
|
2020-05-14 16:56:15 +02:00
|
|
|
import { Hooks } from './plugins/hooks'
|
|
|
|
|
|
|
|
async function removeComment (videoCommentInstance: MCommentOwnerVideo) {
|
|
|
|
const videoCommentInstanceBefore = cloneDeep(videoCommentInstance)
|
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
if (videoCommentInstance.isOwned() || videoCommentInstance.Video.isOwned()) {
|
|
|
|
await sendDeleteVideoComment(videoCommentInstance, t)
|
|
|
|
}
|
|
|
|
|
|
|
|
markCommentAsDeleted(videoCommentInstance)
|
|
|
|
|
|
|
|
await videoCommentInstance.save()
|
|
|
|
})
|
|
|
|
|
|
|
|
logger.info('Video comment %d deleted.', videoCommentInstance.id)
|
|
|
|
|
|
|
|
Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore })
|
|
|
|
}
|
2017-12-22 10:50:07 +01:00
|
|
|
|
|
|
|
async function createVideoComment (obj: {
|
2020-01-31 16:56:52 +01:00
|
|
|
text: string
|
|
|
|
inReplyToComment: MComment | null
|
|
|
|
video: MVideoFullLight
|
2019-08-15 11:53:26 +02:00
|
|
|
account: MAccountDefault
|
2017-12-22 10:50:07 +01:00
|
|
|
}, t: Sequelize.Transaction) {
|
2018-07-25 22:01:25 +02:00
|
|
|
let originCommentId: number | null = null
|
|
|
|
let inReplyToCommentId: number | null = null
|
2017-12-22 12:10:40 +01:00
|
|
|
|
2018-07-25 22:01:25 +02:00
|
|
|
if (obj.inReplyToComment && obj.inReplyToComment !== null) {
|
2017-12-27 10:39:31 +01:00
|
|
|
originCommentId = obj.inReplyToComment.originCommentId || obj.inReplyToComment.id
|
2017-12-27 16:11:53 +01:00
|
|
|
inReplyToCommentId = obj.inReplyToComment.id
|
2017-12-22 10:50:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const comment = await VideoCommentModel.create({
|
|
|
|
text: obj.text,
|
|
|
|
originCommentId,
|
2017-12-27 16:11:53 +01:00
|
|
|
inReplyToCommentId,
|
2017-12-22 10:50:07 +01:00
|
|
|
videoId: obj.video.id,
|
2017-12-27 16:11:53 +01:00
|
|
|
accountId: obj.account.id,
|
2019-08-01 10:15:28 +02:00
|
|
|
url: new Date().toISOString()
|
|
|
|
}, { transaction: t, validate: false })
|
2017-12-22 10:50:07 +01:00
|
|
|
|
2020-11-20 11:21:08 +01:00
|
|
|
comment.url = getLocalVideoCommentActivityPubUrl(obj.video, comment)
|
2017-12-22 10:50:07 +01:00
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
const savedComment: MCommentOwnerVideoReply = await comment.save({ transaction: t })
|
2017-12-27 10:39:31 +01:00
|
|
|
savedComment.InReplyToVideoComment = obj.inReplyToComment
|
|
|
|
savedComment.Video = obj.video
|
2017-12-27 16:11:53 +01:00
|
|
|
savedComment.Account = obj.account
|
2017-12-27 10:39:31 +01:00
|
|
|
|
2018-03-27 13:33:56 +02:00
|
|
|
await sendCreateVideoComment(savedComment, t)
|
2017-12-27 10:39:31 +01:00
|
|
|
|
|
|
|
return savedComment
|
2017-12-22 10:50:07 +01:00
|
|
|
}
|
|
|
|
|
2017-12-22 12:10:40 +01:00
|
|
|
function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): VideoCommentThreadTree {
|
2017-12-22 10:50:07 +01:00
|
|
|
// Comments are sorted by id ASC
|
|
|
|
const comments = resultList.data
|
|
|
|
|
|
|
|
const comment = comments.shift()
|
2017-12-22 12:10:40 +01:00
|
|
|
const thread: VideoCommentThreadTree = {
|
2017-12-22 10:50:07 +01:00
|
|
|
comment: comment.toFormattedJSON(),
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
const idx = {
|
|
|
|
[comment.id]: thread
|
|
|
|
}
|
|
|
|
|
|
|
|
while (comments.length !== 0) {
|
|
|
|
const childComment = comments.shift()
|
|
|
|
|
2017-12-22 12:10:40 +01:00
|
|
|
const childCommentThread: VideoCommentThreadTree = {
|
2017-12-22 10:50:07 +01:00
|
|
|
comment: childComment.toFormattedJSON(),
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentCommentThread = idx[childComment.inReplyToCommentId]
|
2018-10-12 15:26:04 +02:00
|
|
|
// Maybe the parent comment was blocked by the admin/user
|
|
|
|
if (!parentCommentThread) continue
|
2017-12-22 10:50:07 +01:00
|
|
|
|
|
|
|
parentCommentThread.children.push(childCommentThread)
|
|
|
|
idx[childComment.id] = childCommentThread
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread
|
|
|
|
}
|
|
|
|
|
2020-05-14 16:56:15 +02:00
|
|
|
function markCommentAsDeleted (comment: MComment): void {
|
2019-11-15 19:05:08 +01:00
|
|
|
comment.text = ''
|
|
|
|
comment.deletedAt = new Date()
|
|
|
|
comment.accountId = null
|
|
|
|
}
|
|
|
|
|
2017-12-22 10:50:07 +01:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2020-05-14 16:56:15 +02:00
|
|
|
removeComment,
|
2017-12-22 10:50:07 +01:00
|
|
|
createVideoComment,
|
2019-11-15 19:05:08 +01:00
|
|
|
buildFormattedCommentTree,
|
|
|
|
markCommentAsDeleted
|
2017-12-22 10:50:07 +01:00
|
|
|
}
|