PeerTube/server/lib/video-comment.ts

83 lines
2.5 KiB
TypeScript
Raw Normal View History

2017-12-22 10:50:07 +01:00
import * as Sequelize from 'sequelize'
import { ResultList } from '../../shared/models'
2017-12-22 12:10:40 +01:00
import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model'
2017-12-27 16:11:53 +01:00
import { AccountModel } from '../models/account/account'
2017-12-22 10:50:07 +01:00
import { VideoModel } from '../models/video/video'
import { VideoCommentModel } from '../models/video/video-comment'
2017-12-27 16:11:53 +01:00
import { getVideoCommentActivityPubUrl } from './activitypub'
import { sendCreateVideoComment } from './activitypub/send'
2017-12-22 10:50:07 +01:00
async function createVideoComment (obj: {
text: string,
inReplyToComment: VideoCommentModel | null,
2017-12-22 10:50:07 +01:00
video: VideoModel
2017-12-27 16:11:53 +01:00
account: AccountModel
2017-12-22 10:50:07 +01:00
}, t: Sequelize.Transaction) {
let originCommentId: number | null = null
let inReplyToCommentId: number | null = null
2017-12-22 12:10:40 +01:00
if (obj.inReplyToComment && obj.inReplyToComment !== null) {
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,
2017-12-22 12:10:40 +01:00
url: 'fake url'
}, { transaction: t, validate: false })
2017-12-22 10:50:07 +01:00
comment.set('url', getVideoCommentActivityPubUrl(obj.video, comment))
const savedComment = await comment.save({ transaction: t })
savedComment.InReplyToVideoComment = obj.inReplyToComment
savedComment.Video = obj.video
2017-12-27 16:11:53 +01:00
savedComment.Account = obj.account
await sendCreateVideoComment(savedComment, t)
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]
// 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 {
createVideoComment,
buildFormattedCommentTree
}