2018-05-11 08:41:54 -05:00
|
|
|
import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments'
|
2018-01-10 10:18:12 -06:00
|
|
|
import { logger } from '../../helpers/logger'
|
|
|
|
import { doRequest } from '../../helpers/requests'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants'
|
2018-01-10 10:18:12 -06:00
|
|
|
import { VideoCommentModel } from '../../models/video/video-comment'
|
|
|
|
import { getOrCreateActorAndServerAndModel } from './actor'
|
2018-08-22 09:15:35 -05:00
|
|
|
import { getOrCreateVideoAndAccountAndChannel } from './videos'
|
2018-08-22 04:51:39 -05:00
|
|
|
import * as Bluebird from 'bluebird'
|
2018-11-14 08:01:28 -06:00
|
|
|
import { checkUrlsSameHost } from '../../helpers/activitypub'
|
2019-08-20 06:52:49 -05:00
|
|
|
import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../typings/models/video'
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
type ResolveThreadParams = {
|
2020-01-31 09:56:52 -06:00
|
|
|
url: string
|
|
|
|
comments?: MCommentOwner[]
|
|
|
|
isVideo?: boolean
|
2019-08-06 10:19:53 -05:00
|
|
|
commentCreated?: boolean
|
2018-01-10 10:18:12 -06:00
|
|
|
}
|
2019-08-20 06:52:49 -05:00
|
|
|
type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }>
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
async function addVideoComments (commentUrls: string[]) {
|
2018-08-22 04:51:39 -05:00
|
|
|
return Bluebird.map(commentUrls, commentUrl => {
|
2019-08-06 10:19:53 -05:00
|
|
|
return resolveThread({ url: commentUrl, isVideo: false })
|
2018-08-22 04:51:39 -05:00
|
|
|
}, { concurrency: CRAWL_REQUEST_CONCURRENCY })
|
2018-01-10 10:18:12 -06:00
|
|
|
}
|
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
async function resolveThread (params: ResolveThreadParams): ResolveThreadResult {
|
|
|
|
const { url, isVideo } = params
|
|
|
|
if (params.commentCreated === undefined) params.commentCreated = false
|
|
|
|
if (params.comments === undefined) params.comments = []
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2020-01-31 09:56:52 -06:00
|
|
|
// Already have this comment?
|
2019-08-06 10:19:53 -05:00
|
|
|
if (isVideo !== true) {
|
|
|
|
const result = await resolveCommentFromDB(params)
|
|
|
|
if (result) return result
|
2018-01-10 10:18:12 -06:00
|
|
|
}
|
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
try {
|
|
|
|
if (isVideo !== false) return await tryResolveThreadFromVideo(params)
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
return resolveParentComment(params)
|
|
|
|
} catch (err) {
|
|
|
|
logger.debug('Cannot get or create account and video and channel for reply %s, fetch comment', url, { err })
|
2018-11-14 08:01:28 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
return resolveParentComment(params)
|
2018-11-14 08:01:28 -06:00
|
|
|
}
|
2019-08-06 10:19:53 -05:00
|
|
|
}
|
2018-11-14 08:01:28 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
export {
|
|
|
|
addVideoComments,
|
|
|
|
resolveThread
|
|
|
|
}
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
// ---------------------------------------------------------------------------
|
2018-08-22 09:59:55 -05:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
async function resolveCommentFromDB (params: ResolveThreadParams) {
|
|
|
|
const { url, comments, commentCreated } = params
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url)
|
2018-01-10 10:18:12 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
return resolveThread({
|
|
|
|
url: commentFromDatabase.Video.url,
|
|
|
|
comments: parentComments,
|
|
|
|
isVideo: true,
|
|
|
|
commentCreated
|
|
|
|
})
|
2018-01-10 10:18:12 -06:00
|
|
|
}
|
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
async function tryResolveThreadFromVideo (params: ResolveThreadParams) {
|
|
|
|
const { url, comments, commentCreated } = params
|
|
|
|
|
|
|
|
// Maybe it's a reply to a video?
|
|
|
|
// If yes, it's done: we resolved all the thread
|
|
|
|
const syncParam = { likes: true, dislikes: true, shares: true, comments: false, thumbnail: true, refreshVideo: false }
|
|
|
|
const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: url, syncParam })
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
let resultComment: MCommentOwnerVideo
|
2019-08-06 10:19:53 -05:00
|
|
|
if (comments.length !== 0) {
|
2020-01-31 09:56:52 -06:00
|
|
|
const firstReply = comments[comments.length - 1] as MCommentOwnerVideo
|
2019-08-06 10:19:53 -05:00
|
|
|
firstReply.inReplyToCommentId = null
|
|
|
|
firstReply.originCommentId = null
|
|
|
|
firstReply.videoId = video.id
|
|
|
|
firstReply.changed('updatedAt', true)
|
|
|
|
firstReply.Video = video
|
|
|
|
|
|
|
|
comments[comments.length - 1] = await firstReply.save()
|
|
|
|
|
|
|
|
for (let i = comments.length - 2; i >= 0; i--) {
|
2020-01-31 09:56:52 -06:00
|
|
|
const comment = comments[i] as MCommentOwnerVideo
|
2019-08-06 10:19:53 -05:00
|
|
|
comment.originCommentId = firstReply.id
|
2020-01-31 09:56:52 -06:00
|
|
|
comment.inReplyToCommentId = comments[i + 1].id
|
2019-08-06 10:19:53 -05:00
|
|
|
comment.videoId = video.id
|
|
|
|
comment.changed('updatedAt', true)
|
|
|
|
comment.Video = video
|
|
|
|
|
|
|
|
comments[i] = await comment.save()
|
2018-01-10 10:18:12 -06:00
|
|
|
}
|
|
|
|
|
2019-08-15 04:53:26 -05:00
|
|
|
resultComment = comments[0] as MCommentOwnerVideo
|
2019-08-06 10:19:53 -05:00
|
|
|
}
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
return { video, comment: resultComment, commentCreated }
|
|
|
|
}
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
async function resolveParentComment (params: ResolveThreadParams) {
|
|
|
|
const { url, comments } = params
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
|
|
|
|
throw new Error('Recursion limit reached when resolving a thread')
|
|
|
|
}
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
const { body } = await doRequest({
|
|
|
|
uri: url,
|
|
|
|
json: true,
|
|
|
|
activityPub: true
|
|
|
|
})
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
if (sanitizeAndCheckVideoCommentObject(body) === false) {
|
|
|
|
throw new Error('Remote video comment JSON is not valid:' + JSON.stringify(body))
|
|
|
|
}
|
2018-11-14 08:01:28 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
const actorUrl = body.attributedTo
|
2019-12-03 14:48:31 -06:00
|
|
|
if (!actorUrl && body.type !== 'Tombstone') throw new Error('Miss attributed to in comment')
|
2018-11-14 08:01:28 -06:00
|
|
|
|
2019-12-03 14:48:31 -06:00
|
|
|
if (actorUrl && checkUrlsSameHost(url, actorUrl) !== true) {
|
2019-08-06 10:19:53 -05:00
|
|
|
throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
|
|
|
|
}
|
2018-01-10 10:18:12 -06:00
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
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 10:18:12 -06:00
|
|
|
}
|
|
|
|
|
2019-12-04 04:48:12 -06:00
|
|
|
const actor = actorUrl
|
|
|
|
? await getOrCreateActorAndServerAndModel(actorUrl, 'all')
|
|
|
|
: null
|
|
|
|
|
2019-08-06 10:19:53 -05:00
|
|
|
const comment = new VideoCommentModel({
|
|
|
|
url: body.id,
|
2019-12-03 14:48:31 -06:00
|
|
|
text: body.content ? body.content : '',
|
2019-08-06 10:19:53 -05:00
|
|
|
videoId: null,
|
2019-12-03 14:48:31 -06:00
|
|
|
accountId: actor ? actor.Account.id : null,
|
2019-08-06 10:19:53 -05:00
|
|
|
inReplyToCommentId: null,
|
|
|
|
originCommentId: null,
|
|
|
|
createdAt: new Date(body.published),
|
2019-12-03 14:48:31 -06:00
|
|
|
updatedAt: new Date(body.updated),
|
|
|
|
deletedAt: body.deleted ? new Date(body.deleted) : null
|
2019-08-15 04:53:26 -05:00
|
|
|
}) as MCommentOwner
|
2019-12-03 14:48:31 -06:00
|
|
|
comment.Account = actor ? actor.Account : null
|
2019-08-06 10:19:53 -05:00
|
|
|
|
|
|
|
return resolveThread({
|
|
|
|
url: body.inReplyTo,
|
|
|
|
comments: comments.concat([ comment ]),
|
|
|
|
commentCreated: true
|
|
|
|
})
|
2018-01-10 10:18:12 -06:00
|
|
|
}
|