import { VideoModel } from '../../models/video/video'
import { VideoCommentModel } from '../../models/video/video-comment'
import { getOrCreateActorAndServerAndModel } from './actor'
-import { getOrCreateAccountAndVideoAndChannel } from './videos'
+import { getOrCreateVideoAndAccountAndChannel } from './videos'
import * as Bluebird from 'bluebird'
+import { checkUrlsSameHost } from '../../helpers/activitypub'
async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
let originCommentId: number = null
// 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
}
return {
- url: comment.url,
+ url: comment.id,
text: comment.content,
videoId: video.id,
accountId: actor.Account.id,
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 }
+
+ if (checkUrlsSameHost(commentUrl, actorUrl) !== true) {
+ throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${commentUrl}`)
+ }
+
+ if (checkUrlsSameHost(body.id, commentUrl) !== true) {
+ throw new Error(`Comment url ${commentUrl} host is different from the AP object id ${body.id}`)
+ }
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?
- const { video } = await getOrCreateAccountAndVideoAndChannel(url)
+ // If yes, it's done: we resolved all the thread
+ const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
if (comments.length !== 0) {
const firstReply = comments[ comments.length - 1 ]
const actorUrl = body.attributedTo
if (!actorUrl) throw new Error('Miss attributed to in comment')
+ if (checkUrlsSameHost(url, actorUrl) !== true) {
+ throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
+ }
+
+ if (checkUrlsSameHost(body.id, url) !== true) {
+ throw new Error(`Comment url ${url} host is different from the AP object id ${body.id}`)
+ }
+
const actor = await getOrCreateActorAndServerAndModel(actorUrl)
const comment = new VideoCommentModel({
- url: body.url,
+ url: body.id,
text: body.content,
videoId: null,
accountId: actor.Account.id,