PeerTube/server/core/lib/video-comment.ts

183 lines
6.0 KiB
TypeScript
Raw Normal View History

import { AutomaticTagPolicy, ResultList, UserRight, VideoCommentPolicy, VideoCommentThreadTree } from '@peertube/peertube-models'
import { logger } from '@server/helpers/logger.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
import { AccountModel } from '@server/models/account/account.js'
import { AccountAutomaticTagPolicyModel } from '@server/models/automatic-tag/account-automatic-tag-policy.js'
2023-01-05 15:31:51 +01:00
import express from 'express'
import cloneDeep from 'lodash-es/cloneDeep.js'
import { Transaction } from 'sequelize'
import { VideoCommentModel } from '../models/video/video-comment.js'
2023-01-05 15:31:51 +01:00
import {
MComment,
MCommentFormattable,
MCommentOwnerVideo,
MCommentOwnerVideoReply, MUserAccountId, MVideoAccountLight,
2023-01-05 15:31:51 +01:00
MVideoFullLight
} from '../types/models/index.js'
import { sendCreateVideoCommentIfNeeded, sendDeleteVideoComment, sendReplyApproval } from './activitypub/send/index.js'
import { getLocalVideoCommentActivityPubUrl } from './activitypub/url.js'
import { AutomaticTagger } from './automatic-tags/automatic-tagger.js'
import { setAndSaveCommentAutomaticTags } from './automatic-tags/automatic-tags.js'
import { Notifier } from './notifier/notifier.js'
import { Hooks } from './plugins/hooks.js'
2020-05-14 16:56:15 +02:00
export async function removeComment (commentArg: MComment, req: express.Request, res: express.Response) {
2023-01-05 15:31:51 +01:00
let videoCommentInstanceBefore: MCommentOwnerVideo
2020-05-14 16:56:15 +02:00
await sequelizeTypescript.transaction(async t => {
const comment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideoAndReply(commentArg.url, t)
2023-01-05 15:31:51 +01:00
videoCommentInstanceBefore = cloneDeep(comment)
if (comment.isOwned() || comment.Video.isOwned()) {
await sendDeleteVideoComment(comment, t)
2020-05-14 16:56:15 +02:00
}
2023-01-05 15:31:51 +01:00
comment.markAsDeleted()
2020-05-14 16:56:15 +02:00
2023-01-05 15:31:51 +01:00
await comment.save({ transaction: t })
2020-05-14 16:56:15 +02:00
2023-01-05 15:31:51 +01:00
logger.info('Video comment %d deleted.', comment.id)
})
2020-05-14 16:56:15 +02:00
Hooks.runAction('action:api.video-comment.deleted', { comment: videoCommentInstanceBefore, req, res })
2020-05-14 16:56:15 +02:00
}
2017-12-22 10:50:07 +01: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 16:56:52 +01:00
text: string
inReplyToComment: MComment | null
video: MVideoFullLight
user: MUserAccountId
}) {
const { user, video, text, inReplyToComment } = options
let originCommentId: number | null = null
let inReplyToCommentId: number | null = null
2017-12-22 12:10:40 +01:00
if (inReplyToComment && inReplyToComment !== null) {
originCommentId = inReplyToComment.originCommentId || inReplyToComment.id
inReplyToCommentId = inReplyToComment.id
2017-12-22 10:50:07 +01: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 10:50:07 +01:00
await setAndSaveCommentAutomaticTags({ comment: savedComment, automaticTags, transaction })
2017-12-22 10:50:07 +01:00
savedComment.InReplyToVideoComment = inReplyToComment
savedComment.Video = video
savedComment.Account = account
await sendCreateVideoCommentIfNeeded(savedComment, transaction)
return savedComment
})
2017-12-22 10:50:07 +01:00
}
export function buildFormattedCommentTree (resultList: ResultList<MCommentFormattable>): 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]
// 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
}
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 10:50:07 +01: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 10:50:07 +01:00
}