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

116 lines
3.5 KiB
TypeScript
Raw Normal View History

import { ResultList, VideoCommentThreadTree } from '@peertube/peertube-models'
import { logger } from '@server/helpers/logger.js'
import { sequelizeTypescript } from '@server/initializers/database.js'
2023-01-05 08:31:51 -06:00
import express from 'express'
import cloneDeep from 'lodash-es/cloneDeep.js'
2017-12-22 03:50:07 -06:00
import * as Sequelize from 'sequelize'
import { VideoCommentModel } from '../models/video/video-comment.js'
2023-01-05 08:31:51 -06:00
import {
MAccountDefault,
MComment,
MCommentFormattable,
MCommentOwnerVideo,
MCommentOwnerVideoReply,
MVideoFullLight
} from '../types/models/index.js'
import { sendCreateVideoComment, sendDeleteVideoComment } from './activitypub/send/index.js'
import { getLocalVideoCommentActivityPubUrl } from './activitypub/url.js'
import { Hooks } from './plugins/hooks.js'
2020-05-14 09:56:15 -05:00
2023-01-05 08:31:51 -06:00
async function removeComment (commentArg: MComment, req: express.Request, res: express.Response) {
let videoCommentInstanceBefore: MCommentOwnerVideo
2020-05-14 09:56:15 -05:00
await sequelizeTypescript.transaction(async t => {
2023-01-05 08:31:51 -06:00
const comment = await VideoCommentModel.loadByUrlAndPopulateAccountAndVideo(commentArg.url, t)
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
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
async function createVideoComment (obj: {
2020-01-31 09:56:52 -06:00
text: string
inReplyToComment: MComment | null
video: MVideoFullLight
2019-08-15 04:53:26 -05:00
account: MAccountDefault
2017-12-22 03:50:07 -06:00
}, t: Sequelize.Transaction) {
let originCommentId: number | null = null
let inReplyToCommentId: number | null = null
2017-12-22 05:10:40 -06:00
if (obj.inReplyToComment && obj.inReplyToComment !== null) {
originCommentId = obj.inReplyToComment.originCommentId || obj.inReplyToComment.id
2017-12-27 09:11:53 -06:00
inReplyToCommentId = obj.inReplyToComment.id
2017-12-22 03:50:07 -06:00
}
const comment = await VideoCommentModel.create({
text: obj.text,
originCommentId,
2017-12-27 09:11:53 -06:00
inReplyToCommentId,
2017-12-22 03:50:07 -06:00
videoId: obj.video.id,
2017-12-27 09:11:53 -06:00
accountId: obj.account.id,
2019-08-01 03:15:28 -05:00
url: new Date().toISOString()
}, { transaction: t, validate: false })
2017-12-22 03:50:07 -06:00
2020-11-20 04:21:08 -06:00
comment.url = getLocalVideoCommentActivityPubUrl(obj.video, comment)
2017-12-22 03:50:07 -06:00
2019-08-15 04:53:26 -05:00
const savedComment: MCommentOwnerVideoReply = await comment.save({ transaction: t })
savedComment.InReplyToVideoComment = obj.inReplyToComment
savedComment.Video = obj.video
2017-12-27 09:11:53 -06:00
savedComment.Account = obj.account
await sendCreateVideoComment(savedComment, t)
return savedComment
2017-12-22 03:50:07 -06:00
}
2023-01-05 08:31:51 -06:00
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]
// 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
}
// ---------------------------------------------------------------------------
export {
2020-05-14 09:56:15 -05:00
removeComment,
2017-12-22 03:50:07 -06:00
createVideoComment,
2021-06-15 02:17:19 -05:00
buildFormattedCommentTree
2017-12-22 03:50:07 -06:00
}