Refractor comment creation from federation

pull/974/head
Chocobozzz 2018-08-22 16:59:55 +02:00
parent 1297eb5db6
commit 83e6519ba4
3 changed files with 19 additions and 45 deletions

View File

@ -9,7 +9,7 @@ import { ActorModel } from '../../../models/activitypub/actor'
import { VideoAbuseModel } from '../../../models/video/video-abuse' import { VideoAbuseModel } from '../../../models/video/video-abuse'
import { VideoCommentModel } from '../../../models/video/video-comment' import { VideoCommentModel } from '../../../models/video/video-comment'
import { getOrCreateActorAndServerAndModel } from '../actor' import { getOrCreateActorAndServerAndModel } from '../actor'
import { resolveThread } from '../video-comments' import { addVideoComment, resolveThread } from '../video-comments'
import { getOrCreateVideoAndAccountAndChannel } from '../videos' import { getOrCreateVideoAndAccountAndChannel } from '../videos'
import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils' import { forwardActivity, forwardVideoRelatedActivity } from '../send/utils'
@ -120,48 +120,19 @@ async function processCreateVideoAbuse (actor: ActorModel, videoAbuseToCreateDat
} }
async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) { async function processCreateVideoComment (byActor: ActorModel, activity: ActivityCreate) {
const comment = activity.object as VideoCommentObject const commentObject = activity.object as VideoCommentObject
const byAccount = byActor.Account const byAccount = byActor.Account
if (!byAccount) throw new Error('Cannot create video comment with the non account actor ' + byActor.url) 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 => { const { created } = await addVideoComment(video, commentObject.id)
let originCommentId = null
let inReplyToCommentId = null
if (parents.length !== 0) { if (video.isOwned() && created === true) {
const parent = parents[0] // Don't resend the activity to the sender
const exceptions = [ byActor ]
originCommentId = parent.getThreadId() await forwardVideoRelatedActivity(activity, undefined, exceptions, video)
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)
if (video.isOwned() && created === true) {
// Don't resend the activity to the sender
const exceptions = [ byActor ]
await forwardVideoRelatedActivity(activity, t, exceptions, video)
}
})
} }

View File

@ -16,7 +16,7 @@ async function videoCommentActivityObjectToDBAttributes (video: VideoModel, acto
// If this is not a reply to the video (thread), create or get the parent comment // If this is not a reply to the video (thread), create or get the parent comment
if (video.url !== comment.inReplyTo) { if (video.url !== comment.inReplyTo) {
const [ parent ] = await addVideoComment(video, comment.inReplyTo) const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
if (!parent) { if (!parent) {
logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id) logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
return undefined return undefined
@ -55,22 +55,24 @@ async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
if (sanitizeAndCheckVideoCommentObject(body) === false) { if (sanitizeAndCheckVideoCommentObject(body) === false) {
logger.debug('Remote video comment JSON is not valid.', { body }) logger.debug('Remote video comment JSON is not valid.', { body })
return undefined return { created: false }
} }
const actorUrl = body.attributedTo const actorUrl = body.attributedTo
if (!actorUrl) return [] if (!actorUrl) return { created: false }
const actor = await getOrCreateActorAndServerAndModel(actorUrl) const actor = await getOrCreateActorAndServerAndModel(actorUrl)
const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body) 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: { where: {
url: body.id url: body.id
}, },
defaults: entry defaults: entry
}) })
return { comment, created }
} }
async function resolveThread (url: string, comments: VideoCommentModel[] = []) { async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
@ -91,6 +93,7 @@ async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
try { try {
// Maybe it's a reply to a video? // Maybe it's a reply to a video?
// If yes, it's done: we resolved all the thread
const { video } = await getOrCreateVideoAndAccountAndChannel(url) const { video } = await getOrCreateVideoAndAccountAndChannel(url)
if (comments.length !== 0) { if (comments.length !== 0) {

View File

@ -6,9 +6,9 @@ import './video-blacklist'
import './video-blacklist-management' import './video-blacklist-management'
import './video-captions' import './video-captions'
import './video-channels' import './video-channels'
import './video-comme' import './video-comments'
import './video-description' import './video-description'
import './video-impo' import './video-imports'
import './video-nsfw' import './video-nsfw'
import './video-privacy' import './video-privacy'
import './video-schedule-update' import './video-schedule-update'