2024-03-29 08:25:03 -05:00
|
|
|
import { AutomaticTagPolicy, ResultList, UserRight, VideoCommentPolicy, VideoCommentThreadTree } from '@peertube/peertube-models'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { logger } from '@server/helpers/logger.js'
|
|
|
|
import { sequelizeTypescript } from '@server/initializers/database.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { AccountModel } from '@server/models/account/account.js'
|
|
|
|
import { AccountAutomaticTagPolicyModel } from '@server/models/automatic-tag/account-automatic-tag-policy.js'
|
2023-01-05 08:31:51 -06:00
|
|
|
import express from 'express'
|
2023-07-31 07:34:36 -05:00
|
|
|
import cloneDeep from 'lodash-es/cloneDeep.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { Transaction } from 'sequelize'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { VideoCommentModel } from '../models/video/video-comment.js'
|
2023-01-05 08:31:51 -06:00
|
|
|
import {
|
|
|
|
MComment,
|
|
|
|
MCommentFormattable,
|
|
|
|
MCommentOwnerVideo,
|
2024-03-29 08:25:03 -05:00
|
|
|
MCommentOwnerVideoReply, MUserAccountId, MVideoAccountLight,
|
2023-01-05 08:31:51 -06:00
|
|
|
MVideoFullLight
|
2023-07-31 07:34:36 -05:00
|
|
|
} from '../types/models/index.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { sendCreateVideoCommentIfNeeded, sendDeleteVideoComment, sendReplyApproval } from './activitypub/send/index.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { getLocalVideoCommentActivityPubUrl } from './activitypub/url.js'
|
2024-03-29 08:25:03 -05:00
|
|
|
import { AutomaticTagger } from './automatic-tags/automatic-tagger.js'
|
|
|
|
import { setAndSaveCommentAutomaticTags } from './automatic-tags/automatic-tags.js'
|
|
|
|
import { Notifier } from './notifier/notifier.js'
|
2023-07-31 07:34:36 -05:00
|
|
|
import { Hooks } from './plugins/hooks.js'
|
2020-05-14 09:56:15 -05:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
export async function removeComment (commentArg: MComment, req: express.Request, res: express.Response) {
|
2023-01-05 08:31:51 -06:00
|
|
|
let videoCommentInstanceBefore: MCommentOwnerVideo
|
2020-05-14 09:56:15 -05:00
|
|
|
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
2024-03-29 08:25:03 -05:00
|
|
|
const comment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideoAndReply(commentArg.url, t)
|
2023-01-05 08:31:51 -06:00
|
|
|
|
|
|
|
videoCommentInstanceBefore = cloneDeep(comment)
|
|
|
|
|
|
|
|
if (comment.isOwned() || comment.Video.isOwned()) {
|
|
|
|
await sendDeleteVideoComment(comment, t)
|
2020-05-14 09:56:15 -05:00
|
|
|
}
|
|
|
|
|
2023-01-05 08:31:51 -06:00
|
|
|
comment.markAsDeleted()
|
2020-05-14 09:56:15 -05:00
|
|
|
|
2023-01-05 08:31:51 -06:00
|
|
|
await comment.save({ transaction: t })
|
2020-05-14 09:56:15 -05:00
|
|
|
|
2023-01-05 08:31:51 -06:00
|
|
|
logger.info('Video comment %d deleted.', comment.id)
|
|
|
|
})
|
2020-05-14 09:56:15 -05:00
|
|
|
|
2021-11-24 07:33:14 -06:00
|
|
|
Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore, req, res })
|
2020-05-14 09:56:15 -05:00
|
|
|
}
|
2017-12-22 03:50:07 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
export async function approveComment (commentArg: MComment) {
|
|
|
|
await sequelizeTypescript.transaction(async t => {
|
|
|
|
const comment = await VideoCommentModel.loadByIdAndPopulateVideoAndAccountAndReply(commentArg.id, t)
|
|
|
|
|
|
|
|
const oldHeldForReview = comment.heldForReview
|
|
|
|
|
|
|
|
comment.heldForReview = false
|
|
|
|
await comment.save({ transaction: t })
|
|
|
|
|
|
|
|
if (comment.isOwned()) {
|
|
|
|
await sendCreateVideoCommentIfNeeded(comment, t)
|
|
|
|
} else {
|
|
|
|
sendReplyApproval(comment, 'ApproveReply')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (oldHeldForReview !== comment.heldForReview) {
|
|
|
|
Notifier.Instance.notifyOnNewCommentApproval(comment)
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.info('Video comment %d approved.', comment.id)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function createLocalVideoComment (options: {
|
2020-01-31 09:56:52 -06:00
|
|
|
text: string
|
|
|
|
inReplyToComment: MComment | null
|
|
|
|
video: MVideoFullLight
|
2024-03-29 08:25:03 -05:00
|
|
|
user: MUserAccountId
|
|
|
|
}) {
|
|
|
|
const { user, video, text, inReplyToComment } = options
|
|
|
|
|
2018-07-25 15:01:25 -05:00
|
|
|
let originCommentId: number | null = null
|
|
|
|
let inReplyToCommentId: number | null = null
|
2017-12-22 05:10:40 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
if (inReplyToComment && inReplyToComment !== null) {
|
|
|
|
originCommentId = inReplyToComment.originCommentId || inReplyToComment.id
|
|
|
|
inReplyToCommentId = inReplyToComment.id
|
2017-12-22 03:50:07 -06:00
|
|
|
}
|
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
return sequelizeTypescript.transaction(async transaction => {
|
|
|
|
const account = await AccountModel.load(user.Account.id, transaction)
|
|
|
|
|
|
|
|
const automaticTags = await new AutomaticTagger().buildCommentsAutomaticTags({
|
|
|
|
ownerAccount: video.VideoChannel.Account,
|
|
|
|
text,
|
|
|
|
transaction
|
|
|
|
})
|
|
|
|
|
|
|
|
const heldForReview = await shouldCommentBeHeldForReview({ user, video, automaticTags, transaction })
|
|
|
|
|
|
|
|
const comment = await VideoCommentModel.create({
|
|
|
|
text,
|
|
|
|
originCommentId,
|
|
|
|
inReplyToCommentId,
|
|
|
|
videoId: video.id,
|
|
|
|
accountId: account.id,
|
|
|
|
heldForReview,
|
|
|
|
url: new Date().toISOString()
|
|
|
|
}, { transaction, validate: false })
|
|
|
|
|
|
|
|
comment.url = getLocalVideoCommentActivityPubUrl(video, comment)
|
|
|
|
|
|
|
|
const savedComment: MCommentOwnerVideoReply = await comment.save({ transaction })
|
2017-12-22 03:50:07 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
await setAndSaveCommentAutomaticTags({ comment: savedComment, automaticTags, transaction })
|
2017-12-22 03:50:07 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
savedComment.InReplyToVideoComment = inReplyToComment
|
|
|
|
savedComment.Video = video
|
|
|
|
savedComment.Account = account
|
2017-12-27 03:39:31 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
await sendCreateVideoCommentIfNeeded(savedComment, transaction)
|
2017-12-27 03:39:31 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
return savedComment
|
|
|
|
})
|
2017-12-22 03:50:07 -06:00
|
|
|
}
|
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
export function buildFormattedCommentTree (resultList: ResultList<MCommentFormattable>): VideoCommentThreadTree {
|
2017-12-22 03:50:07 -06:00
|
|
|
// Comments are sorted by id ASC
|
|
|
|
const comments = resultList.data
|
|
|
|
|
|
|
|
const comment = comments.shift()
|
2017-12-22 05:10:40 -06:00
|
|
|
const thread: VideoCommentThreadTree = {
|
2017-12-22 03:50:07 -06:00
|
|
|
comment: comment.toFormattedJSON(),
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
const idx = {
|
|
|
|
[comment.id]: thread
|
|
|
|
}
|
|
|
|
|
|
|
|
while (comments.length !== 0) {
|
|
|
|
const childComment = comments.shift()
|
|
|
|
|
2017-12-22 05:10:40 -06:00
|
|
|
const childCommentThread: VideoCommentThreadTree = {
|
2017-12-22 03:50:07 -06:00
|
|
|
comment: childComment.toFormattedJSON(),
|
|
|
|
children: []
|
|
|
|
}
|
|
|
|
|
|
|
|
const parentCommentThread = idx[childComment.inReplyToCommentId]
|
2018-10-12 08:26:04 -05:00
|
|
|
// Maybe the parent comment was blocked by the admin/user
|
|
|
|
if (!parentCommentThread) continue
|
2017-12-22 03:50:07 -06:00
|
|
|
|
|
|
|
parentCommentThread.children.push(childCommentThread)
|
|
|
|
idx[childComment.id] = childCommentThread
|
|
|
|
}
|
|
|
|
|
|
|
|
return thread
|
|
|
|
}
|
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
export async function shouldCommentBeHeldForReview (options: {
|
|
|
|
user: MUserAccountId
|
|
|
|
video: MVideoAccountLight
|
|
|
|
automaticTags: { name: string, accountId: number }[]
|
|
|
|
transaction?: Transaction
|
|
|
|
}) {
|
|
|
|
const { user, video, transaction, automaticTags } = options
|
|
|
|
|
|
|
|
if (video.isOwned() && user) {
|
|
|
|
if (user.hasRight(UserRight.MANAGE_ANY_VIDEO_COMMENT)) return false
|
|
|
|
if (user.Account.id === video.VideoChannel.accountId) return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (video.commentsPolicy === VideoCommentPolicy.REQUIRES_APPROVAL) return true
|
|
|
|
if (video.isOwned() !== true) return false
|
2017-12-22 03:50:07 -06:00
|
|
|
|
2024-03-29 08:25:03 -05:00
|
|
|
const ownerAccountTags = automaticTags
|
|
|
|
.filter(t => t.accountId === video.VideoChannel.accountId)
|
|
|
|
.map(t => t.name)
|
|
|
|
|
|
|
|
if (ownerAccountTags.length === 0) return false
|
|
|
|
|
|
|
|
return AccountAutomaticTagPolicyModel.hasPolicyOnTags({
|
|
|
|
accountId: video.VideoChannel.accountId,
|
|
|
|
policy: AutomaticTagPolicy.REVIEW_COMMENT,
|
|
|
|
tags: ownerAccountTags,
|
|
|
|
transaction
|
|
|
|
})
|
2017-12-22 03:50:07 -06:00
|
|
|
}
|