import { VideoAbuseModel } from '../../../models/video/video-abuse'
import { VideoCommentModel } from '../../../models/video/video-comment'
import { getOrCreateActorAndServerAndModel } from '../actor'
-import { resolveThread } from '../video-comments'
+import { addVideoComment, resolveThread } from '../video-comments'
import { getOrCreateVideoAndAccountAndChannel } from '../videos'
import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
}
async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
- const comment = activity.object as VideoCommentObject
+ const commentObject = activity.object as VideoCommentObject
const byAccount = byActor.Account
if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url)
- const { video, parents } = await resolveThread(comment.inReplyTo)
+ const { video } = await resolveThread(commentObject.inReplyTo)
- return sequelizeTypescript.transaction(async t => {
- let originCommentId = null
- let inReplyToCommentId = null
-
- if (parents.length !== 0) {
- const parent = parents[0]
-
- originCommentId = parent.getThreadId()
- inReplyToCommentId = parent.id
- }
-
- // This is a new thread
- const objectToCreate = {
- url: comment.id,
- text: comment.content,
- originCommentId,
- inReplyToCommentId,
- videoId: video.id,
- accountId: byAccount.id
- }
-
- const options = {
- where: {
- url: objectToCreate.url
- },
- defaults: objectToCreate,
- transaction: t
- }
- const [ ,created ] = await VideoCommentModel.findOrCreate(options)
+ const { created } = await addVideoComment(video, commentObject.id)
- if (video.isOwned() && created === true) {
- // Don't resend the activity to the sender
- const exceptions = [ byActor ]
+ if (video.isOwned() && created === true) {
+ // Don't resend the activity to the sender
+ const exceptions = [ byActor ]
- await forwardVideoRelatedActivity(activity, t, exceptions, video)
- }
- })
+ await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
+ }
}
// If this is not a reply to the video (thread), create or get the parent comment
if (video.url !== comment.inReplyTo) {
- const [ parent ] = await addVideoComment(video, comment.inReplyTo)
+ const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
if (!parent) {
logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
return undefined
if (sanitizeAndCheckVideoCommentObject(body) === false) {
logger.debug('Remote video comment JSON is not valid.', { body })
- return undefined
+ return { created: false }
}
const actorUrl = body.attributedTo
- if (!actorUrl) return []
+ if (!actorUrl) return { created: false }
const actor = await getOrCreateActorAndServerAndModel(actorUrl)
const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
- if (!entry) return []
+ if (!entry) return { created: false }
- return VideoCommentModel.findOrCreate({
+ const [ comment, created ] = await VideoCommentModel.findOrCreate({
where: {
url: body.id
},
defaults: entry
})
+
+ return { comment, created }
}
async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
try {
// Maybe it's a reply to a video?
+ // If yes, it's done: we resolved all the thread
const { video } = await getOrCreateVideoAndAccountAndChannel(url)
if (comments.length !== 0) {