2018-01-10 11:18:12 -05:00
|
|
|
import { VideoCommentObject } from '../../../shared/models/activitypub/objects/video-comment-object'
|
2018-05-11 09:41:54 -04:00
|
|
|
import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
|
2018-01-10 11:18:12 -05:00
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { doRequest } from '../../helpers/requests'
|
2019-04-11 08:26:41 -04:00
|
|
|
import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
|
2018-01-10 11:18:12 -05:00
|
|
|
import { ActorModel } from '../../models/activitypub/actor'
|
|
|
|
import { VideoModel } from '../../models/video/video'
|
|
|
|
import { VideoCommentModel } from '../../models/video/video-comment'
|
|
|
|
import { getOrCreateActorAndServerAndModel } from './actor'
|
2018-08-22 10:15:35 -04:00
|
|
|
import { getOrCreateVideoAndAccountAndChannel } from './videos'
|
2018-08-22 05:51:39 -04:00
|
|
|
import * as Bluebird from 'bluebird'
|
2018-11-14 09:01:28 -05:00
|
|
|
import { checkUrlsSameHost } from '../../helpers/activitypub'
|
2018-01-10 11:18:12 -05:00
|
|
|
|
|
|
|
async function videoCommentActivityObjectToDBAttributes (video: VideoModel, actor: ActorModel, comment: VideoCommentObject) {
|
|
|
|
let originCommentId: number = null
|
|
|
|
let inReplyToCommentId: number = null
|
|
|
|
|
|
|
|
// If this is not a reply to the video (thread), create or get the parent comment
|
|
|
|
if (video.url !== comment.inReplyTo) {
|
2018-08-22 10:59:55 -04:00
|
|
|
const { comment: parent } = await addVideoComment(video, comment.inReplyTo)
|
2018-01-10 11:18:12 -05:00
|
|
|
if (!parent) {
|
|
|
|
logger.warn('Cannot fetch or get parent comment %s of comment %s.', comment.inReplyTo, comment.id)
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
originCommentId = parent.originCommentId || parent.id
|
|
|
|
inReplyToCommentId = parent.id
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2018-09-24 10:24:42 -04:00
|
|
|
url: comment.id,
|
2018-01-10 11:18:12 -05:00
|
|
|
text: comment.content,
|
|
|
|
videoId: video.id,
|
|
|
|
accountId: actor.Account.id,
|
|
|
|
inReplyToCommentId,
|
|
|
|
originCommentId,
|
2019-03-19 11:23:02 -04:00
|
|
|
createdAt: new Date(comment.published)
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-25 10:21:16 -04:00
|
|
|
async function addVideoComments (commentUrls: string[], instance: VideoModel) {
|
2018-08-22 05:51:39 -04:00
|
|
|
return Bluebird.map(commentUrls, commentUrl => {
|
|
|
|
return addVideoComment(instance, commentUrl)
|
|
|
|
}, { concurrency: CRAWL_REQUEST_CONCURRENCY })
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function addVideoComment (videoInstance: VideoModel, commentUrl: string) {
|
|
|
|
logger.info('Fetching remote video comment %s.', commentUrl)
|
|
|
|
|
|
|
|
const { body } = await doRequest({
|
|
|
|
uri: commentUrl,
|
|
|
|
json: true,
|
|
|
|
activityPub: true
|
|
|
|
})
|
|
|
|
|
2018-05-11 09:41:54 -04:00
|
|
|
if (sanitizeAndCheckVideoCommentObject(body) === false) {
|
2018-01-10 11:18:12 -05:00
|
|
|
logger.debug('Remote video comment JSON is not valid.', { body })
|
2018-08-22 10:59:55 -04:00
|
|
|
return { created: false }
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const actorUrl = body.attributedTo
|
2018-08-22 10:59:55 -04:00
|
|
|
if (!actorUrl) return { created: false }
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2018-11-14 09:01:28 -05:00
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
|
2018-12-26 04:36:24 -05:00
|
|
|
const actor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
|
2018-01-10 11:18:12 -05:00
|
|
|
const entry = await videoCommentActivityObjectToDBAttributes(videoInstance, actor, body)
|
2018-08-22 10:59:55 -04:00
|
|
|
if (!entry) return { created: false }
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-04-23 03:50:57 -04:00
|
|
|
const [ comment, created ] = await VideoCommentModel.upsert<VideoCommentModel>(entry, { returning: true })
|
2018-12-26 04:36:24 -05:00
|
|
|
comment.Account = actor.Account
|
|
|
|
comment.Video = videoInstance
|
2018-08-22 10:59:55 -04:00
|
|
|
|
|
|
|
return { comment, created }
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
2019-05-31 09:14:40 -04:00
|
|
|
type ResolveThreadResult = Promise<{ video: VideoModel, parents: VideoCommentModel[] }>
|
|
|
|
async function resolveThread (url: string, comments: VideoCommentModel[] = []): ResolveThreadResult {
|
2018-01-10 11:18:12 -05:00
|
|
|
// Already have this comment?
|
|
|
|
const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideo(url)
|
|
|
|
if (commentFromDatabase) {
|
|
|
|
let parentComments = comments.concat([ commentFromDatabase ])
|
|
|
|
|
|
|
|
// Speed up things and resolve directly the thread
|
|
|
|
if (commentFromDatabase.InReplyToVideoComment) {
|
|
|
|
const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
|
|
|
|
|
|
|
|
parentComments = parentComments.concat(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resolveThread(commentFromDatabase.Video.url, parentComments)
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Maybe it's a reply to a video?
|
2018-08-22 10:59:55 -04:00
|
|
|
// If yes, it's done: we resolved all the thread
|
2018-09-19 05:16:23 -04:00
|
|
|
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url })
|
2018-01-10 11:18:12 -05:00
|
|
|
|
|
|
|
if (comments.length !== 0) {
|
|
|
|
const firstReply = comments[ comments.length - 1 ]
|
|
|
|
firstReply.inReplyToCommentId = null
|
|
|
|
firstReply.originCommentId = null
|
|
|
|
firstReply.videoId = video.id
|
|
|
|
comments[comments.length - 1] = await firstReply.save()
|
|
|
|
|
|
|
|
for (let i = comments.length - 2; i >= 0; i--) {
|
|
|
|
const comment = comments[ i ]
|
|
|
|
comment.originCommentId = firstReply.id
|
|
|
|
comment.inReplyToCommentId = comments[ i + 1 ].id
|
|
|
|
comment.videoId = video.id
|
|
|
|
|
|
|
|
comments[i] = await comment.save()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return { video, parents: comments }
|
|
|
|
} catch (err) {
|
2018-03-26 09:54:13 -04:00
|
|
|
logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
|
2018-01-10 11:18:12 -05:00
|
|
|
|
|
|
|
if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
|
|
|
|
throw new Error('Recursion limit reached when resolving a thread')
|
|
|
|
}
|
|
|
|
|
|
|
|
const { body } = await doRequest({
|
|
|
|
uri: url,
|
|
|
|
json: true,
|
|
|
|
activityPub: true
|
|
|
|
})
|
|
|
|
|
2018-05-11 09:41:54 -04:00
|
|
|
if (sanitizeAndCheckVideoCommentObject(body) === false) {
|
2019-07-18 08:28:37 -04:00
|
|
|
throw new Error('Remote video comment JSON is not valid:' + JSON.stringify(body))
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const actorUrl = body.attributedTo
|
|
|
|
if (!actorUrl) throw new Error('Miss attributed to in comment')
|
|
|
|
|
2018-11-14 09:01:28 -05:00
|
|
|
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}`)
|
|
|
|
}
|
|
|
|
|
2018-01-10 11:18:12 -05:00
|
|
|
const actor = await getOrCreateActorAndServerAndModel(actorUrl)
|
|
|
|
const comment = new VideoCommentModel({
|
2018-09-27 05:05:50 -04:00
|
|
|
url: body.id,
|
2018-01-10 11:18:12 -05:00
|
|
|
text: body.content,
|
|
|
|
videoId: null,
|
|
|
|
accountId: actor.Account.id,
|
|
|
|
inReplyToCommentId: null,
|
|
|
|
originCommentId: null,
|
|
|
|
createdAt: new Date(body.published),
|
|
|
|
updatedAt: new Date(body.updated)
|
|
|
|
})
|
|
|
|
|
|
|
|
return resolveThread(body.inReplyTo, comments.concat([ comment ]))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
videoCommentActivityObjectToDBAttributes,
|
|
|
|
addVideoComments,
|
|
|
|
addVideoComment,
|
|
|
|
resolveThread
|
|
|
|
}
|