import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
import { logger } from '../../helpers/logger'
import { doRequest } from '../../helpers/requests'
-import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers'
+import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
import { ActorModel } from '../../models/activitypub/actor'
import { VideoModel } from '../../models/video/video'
import { VideoCommentModel } from '../../models/video/video-comment'
import { getOrCreateActorAndServerAndModel } from './actor'
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
}
return {
- url: comment.url,
+ url: comment.id,
text: comment.content,
videoId: video.id,
accountId: actor.Account.id,
inReplyToCommentId,
originCommentId,
- createdAt: new Date(comment.published),
- updatedAt: new Date(comment.updated)
+ createdAt: new Date(comment.published)
}
}
const actorUrl = body.attributedTo
if (!actorUrl) return { created: false }
- const actor = await getOrCreateActorAndServerAndModel(actorUrl)
+ 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, 'all')
const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
if (!entry) return { created: false }
- const [ comment, created ] = await VideoCommentModel.findOrCreate({
- where: {
- url: body.id
- },
- defaults: entry
- })
+ const [ comment, created ] = await VideoCommentModel.upsert<VideoCommentModel>(entry, { returning: true })
+ comment.Account = actor.Account
+ comment.Video = videoInstance
return { comment, created }
}
-async function resolveThread (url: string, comments: VideoCommentModel[] = []) {
+type ResolveThreadResult = Promise<{ video: VideoModel, parents: VideoCommentModel[] }>
+async function resolveThread (url: string, comments: VideoCommentModel[] = []): ResolveThreadResult {
// Already have this comment?
const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
if (commentFromDatabase) {
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,
return resolveThread(body.inReplyTo, comments.concat([ comment ]))
}
-
}
export {