2017-12-22 03:50:07 -06:00
|
|
|
import * as Sequelize from 'sequelize'
|
|
|
|
import { ResultList } from '../../shared/models'
|
2017-12-22 05:10:40 -06:00
|
|
|
import { VideoCommentThreadTree } from '../../shared/models/videos/video-comment.model'
|
2017-12-22 03:50:07 -06:00
|
|
|
import { VideoCommentModel } from '../models/video/video-comment'
|
2017-12-27 09:11:53 -06:00
|
|
|
import { getVideoCommentActivityPubUrl } from './activitypub'
|
2018-03-27 06:33:56 -05:00
|
|
|
import { sendCreateVideoComment } from './activitypub/send'
|
2019-08-15 04:53:26 -05:00
|
|
|
import { MAccountDefault, MComment, MCommentOwnerVideoReply, MVideoFullLight } from '../typings/models'
|
2017-12-22 03:50:07 -06:00
|
|
|
|
|
|
|
async function createVideoComment (obj: {
|
|
|
|
text: string,
|
2019-08-15 04:53:26 -05:00
|
|
|
inReplyToComment: MComment | null,
|
|
|
|
video: MVideoFullLight,
|
|
|
|
account: MAccountDefault
|
2017-12-22 03:50:07 -06:00
|
|
|
}, t: Sequelize.Transaction) {
|
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
|
|
|
|
2018-07-25 15:01:25 -05:00
|
|
|
if (obj.inReplyToComment && obj.inReplyToComment !== null) {
|
2017-12-27 03:39:31 -06:00
|
|
|
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
|
|
|
|
2019-08-01 03:15:28 -05:00
|
|
|
comment.url = getVideoCommentActivityPubUrl(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 })
|
2017-12-27 03:39:31 -06:00
|
|
|
savedComment.InReplyToVideoComment = obj.inReplyToComment
|
|
|
|
savedComment.Video = obj.video
|
2017-12-27 09:11:53 -06:00
|
|
|
savedComment.Account = obj.account
|
2017-12-27 03:39:31 -06:00
|
|
|
|
2018-03-27 06:33:56 -05:00
|
|
|
await sendCreateVideoComment(savedComment, t)
|
2017-12-27 03:39:31 -06:00
|
|
|
|
|
|
|
return savedComment
|
2017-12-22 03:50:07 -06:00
|
|
|
}
|
|
|
|
|
2017-12-22 05:10:40 -06:00
|
|
|
function buildFormattedCommentTree (resultList: ResultList<VideoCommentModel>): 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
|
|
|
createVideoComment,
|
|
|
|
buildFormattedCommentTree
|
|
|
|
}
|