2021-08-27 08:32:44 -04:00
|
|
|
import { map } from 'bluebird'
|
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'
|
2021-03-08 08:24:11 -05:00
|
|
|
import { doJSONRequest } 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 { VideoCommentModel } from '../../models/video/video-comment'
|
2022-09-23 05:38:18 -04:00
|
|
|
import { MComment, MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video'
|
|
|
|
import { isRemoteVideoCommentAccepted } from '../moderation'
|
|
|
|
import { Hooks } from '../plugins/hooks'
|
2021-06-03 10:02:29 -04:00
|
|
|
import { getOrCreateAPActor } from './actors'
|
2022-03-23 09:24:50 -04:00
|
|
|
import { checkUrlsSameHost } from './url'
|
2021-06-02 09:47:05 -04:00
|
|
|
import { getOrCreateAPVideo } from './videos'
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
type ResolveThreadParams = {
|
2020-01-31 10:56:52 -05:00
|
|
|
url: string
|
|
|
|
comments?: MCommentOwner[]
|
|
|
|
isVideo?: boolean
|
2019-08-06 11:19:53 -04:00
|
|
|
commentCreated?: boolean
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
2019-08-20 07:52:49 -04:00
|
|
|
type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }>
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
async function addVideoComments (commentUrls: string[]) {
|
2021-08-27 08:32:44 -04:00
|
|
|
return map(commentUrls, async commentUrl => {
|
2021-03-10 03:39:17 -05:00
|
|
|
try {
|
|
|
|
await resolveThread({ url: commentUrl, isVideo: false })
|
|
|
|
} catch (err) {
|
|
|
|
logger.warn('Cannot resolve thread %s.', commentUrl, { err })
|
|
|
|
}
|
2018-08-22 05:51:39 -04:00
|
|
|
}, { concurrency: CRAWL_REQUEST_CONCURRENCY })
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
async function resolveThread (params: ResolveThreadParams): ResolveThreadResult {
|
|
|
|
const { url, isVideo } = params
|
2021-06-03 08:30:09 -04:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
if (params.commentCreated === undefined) params.commentCreated = false
|
|
|
|
if (params.comments === undefined) params.comments = []
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2021-06-03 08:30:09 -04:00
|
|
|
// If it is not a video, or if we don't know if it's a video, try to get the thread from DB
|
2020-11-10 09:26:33 -05:00
|
|
|
if (isVideo === false || isVideo === undefined) {
|
2019-08-06 11:19:53 -04:00
|
|
|
const result = await resolveCommentFromDB(params)
|
|
|
|
if (result) return result
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
try {
|
2020-11-10 09:26:33 -05:00
|
|
|
// If it is a video, or if we don't know if it's a video
|
|
|
|
if (isVideo === true || isVideo === undefined) {
|
|
|
|
// Keep await so we catch the exception
|
2021-06-03 08:30:09 -04:00
|
|
|
return await tryToResolveThreadFromVideo(params)
|
2020-11-10 09:26:33 -05:00
|
|
|
}
|
2019-08-06 11:19:53 -04:00
|
|
|
} catch (err) {
|
2021-02-26 10:26:27 -05:00
|
|
|
logger.debug('Cannot resolve thread from video %s, maybe because it was not a video', url, { err })
|
2018-11-14 09:01:28 -05:00
|
|
|
}
|
2020-11-10 08:34:04 -05:00
|
|
|
|
2021-02-26 10:26:27 -05:00
|
|
|
return resolveRemoteParentComment(params)
|
2019-08-06 11:19:53 -04:00
|
|
|
}
|
2018-11-14 09:01:28 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
export {
|
|
|
|
addVideoComments,
|
|
|
|
resolveThread
|
|
|
|
}
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
2018-08-22 10:59:55 -04:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
async function resolveCommentFromDB (params: ResolveThreadParams) {
|
|
|
|
const { url, comments, commentCreated } = params
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
const commentFromDatabase = await VideoCommentModel.loadByUrlAndPopulateReplyAndVideoUrlAndAccount(url)
|
2021-06-03 08:30:09 -04:00
|
|
|
if (!commentFromDatabase) return undefined
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2021-06-03 08:30:09 -04:00
|
|
|
let parentComments = comments.concat([ commentFromDatabase ])
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2021-06-03 08:30:09 -04:00
|
|
|
// Speed up things and resolve directly the thread
|
|
|
|
if (commentFromDatabase.InReplyToVideoComment) {
|
|
|
|
const data = await VideoCommentModel.listThreadParentComments(commentFromDatabase, undefined, 'DESC')
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2021-06-03 08:30:09 -04:00
|
|
|
parentComments = parentComments.concat(data)
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
2021-06-03 08:30:09 -04:00
|
|
|
return resolveThread({
|
|
|
|
url: commentFromDatabase.Video.url,
|
|
|
|
comments: parentComments,
|
|
|
|
isVideo: true,
|
|
|
|
commentCreated
|
|
|
|
})
|
2019-08-06 11:19:53 -04:00
|
|
|
}
|
|
|
|
|
2021-06-03 08:30:09 -04:00
|
|
|
async function tryToResolveThreadFromVideo (params: ResolveThreadParams) {
|
2019-08-06 11:19:53 -04:00
|
|
|
const { url, comments, commentCreated } = params
|
|
|
|
|
|
|
|
// Maybe it's a reply to a video?
|
|
|
|
// If yes, it's done: we resolved all the thread
|
2022-03-18 06:17:35 -04:00
|
|
|
const syncParam = { rates: true, shares: true, comments: false, thumbnail: true, refreshVideo: false }
|
2021-06-02 09:47:05 -04:00
|
|
|
const { video } = await getOrCreateAPVideo({ videoObject: url, syncParam })
|
2019-08-06 11:19:53 -04:00
|
|
|
|
2020-11-10 08:34:04 -05:00
|
|
|
if (video.isOwned() && !video.hasPrivacyForFederation()) {
|
|
|
|
throw new Error('Cannot resolve thread of video with privacy that is not compatible with federation')
|
|
|
|
}
|
|
|
|
|
2019-08-15 05:53:26 -04:00
|
|
|
let resultComment: MCommentOwnerVideo
|
2019-08-06 11:19:53 -04:00
|
|
|
if (comments.length !== 0) {
|
2020-01-31 10:56:52 -05:00
|
|
|
const firstReply = comments[comments.length - 1] as MCommentOwnerVideo
|
2019-08-06 11:19:53 -04:00
|
|
|
firstReply.inReplyToCommentId = null
|
|
|
|
firstReply.originCommentId = null
|
|
|
|
firstReply.videoId = video.id
|
|
|
|
firstReply.changed('updatedAt', true)
|
|
|
|
firstReply.Video = video
|
|
|
|
|
2022-09-23 05:38:18 -04:00
|
|
|
if (await isRemoteCommentAccepted(firstReply) !== true) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
comments[comments.length - 1] = await firstReply.save()
|
|
|
|
|
|
|
|
for (let i = comments.length - 2; i >= 0; i--) {
|
2020-01-31 10:56:52 -05:00
|
|
|
const comment = comments[i] as MCommentOwnerVideo
|
2019-08-06 11:19:53 -04:00
|
|
|
comment.originCommentId = firstReply.id
|
2020-01-31 10:56:52 -05:00
|
|
|
comment.inReplyToCommentId = comments[i + 1].id
|
2019-08-06 11:19:53 -04:00
|
|
|
comment.videoId = video.id
|
|
|
|
comment.changed('updatedAt', true)
|
|
|
|
comment.Video = video
|
|
|
|
|
2022-09-23 05:38:18 -04:00
|
|
|
if (await isRemoteCommentAccepted(comment) !== true) {
|
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
comments[i] = await comment.save()
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
2019-08-15 05:53:26 -04:00
|
|
|
resultComment = comments[0] as MCommentOwnerVideo
|
2019-08-06 11:19:53 -04:00
|
|
|
}
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
return { video, comment: resultComment, commentCreated }
|
|
|
|
}
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2021-02-26 10:26:27 -05:00
|
|
|
async function resolveRemoteParentComment (params: ResolveThreadParams) {
|
2019-08-06 11:19:53 -04:00
|
|
|
const { url, comments } = params
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
if (comments.length > ACTIVITY_PUB.MAX_RECURSION_COMMENTS) {
|
|
|
|
throw new Error('Recursion limit reached when resolving a thread')
|
|
|
|
}
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2021-03-08 08:24:11 -05:00
|
|
|
const { body } = await doJSONRequest<any>(url, { activityPub: true })
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
if (sanitizeAndCheckVideoCommentObject(body) === false) {
|
2021-02-26 10:26:27 -05:00
|
|
|
throw new Error(`Remote video comment JSON ${url} is not valid:` + JSON.stringify(body))
|
2019-08-06 11:19:53 -04:00
|
|
|
}
|
2018-11-14 09:01:28 -05:00
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
const actorUrl = body.attributedTo
|
2019-12-03 15:48:31 -05:00
|
|
|
if (!actorUrl && body.type !== 'Tombstone') throw new Error('Miss attributed to in comment')
|
2018-11-14 09:01:28 -05:00
|
|
|
|
2019-12-03 15:48:31 -05:00
|
|
|
if (actorUrl && checkUrlsSameHost(url, actorUrl) !== true) {
|
2019-08-06 11:19:53 -04:00
|
|
|
throw new Error(`Actor url ${actorUrl} has not the same host than the comment url ${url}`)
|
|
|
|
}
|
2018-01-10 11:18:12 -05:00
|
|
|
|
2019-08-06 11:19:53 -04: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 11:18:12 -05:00
|
|
|
}
|
|
|
|
|
2019-12-04 05:48:12 -05:00
|
|
|
const actor = actorUrl
|
2021-06-03 10:02:29 -04:00
|
|
|
? await getOrCreateAPActor(actorUrl, 'all')
|
2019-12-04 05:48:12 -05:00
|
|
|
: null
|
|
|
|
|
2019-08-06 11:19:53 -04:00
|
|
|
const comment = new VideoCommentModel({
|
|
|
|
url: body.id,
|
2019-12-03 15:48:31 -05:00
|
|
|
text: body.content ? body.content : '',
|
2019-08-06 11:19:53 -04:00
|
|
|
videoId: null,
|
2019-12-03 15:48:31 -05:00
|
|
|
accountId: actor ? actor.Account.id : null,
|
2019-08-06 11:19:53 -04:00
|
|
|
inReplyToCommentId: null,
|
|
|
|
originCommentId: null,
|
|
|
|
createdAt: new Date(body.published),
|
2019-12-03 15:48:31 -05:00
|
|
|
updatedAt: new Date(body.updated),
|
|
|
|
deletedAt: body.deleted ? new Date(body.deleted) : null
|
2019-08-15 05:53:26 -04:00
|
|
|
}) as MCommentOwner
|
2019-12-03 15:48:31 -05:00
|
|
|
comment.Account = actor ? actor.Account : null
|
2019-08-06 11:19:53 -04:00
|
|
|
|
|
|
|
return resolveThread({
|
|
|
|
url: body.inReplyTo,
|
|
|
|
comments: comments.concat([ comment ]),
|
|
|
|
commentCreated: true
|
|
|
|
})
|
2018-01-10 11:18:12 -05:00
|
|
|
}
|
2022-09-23 05:38:18 -04:00
|
|
|
|
|
|
|
async function isRemoteCommentAccepted (comment: MComment) {
|
|
|
|
// Already created
|
|
|
|
if (comment.id) return true
|
|
|
|
|
|
|
|
const acceptParameters = {
|
|
|
|
comment
|
|
|
|
}
|
|
|
|
|
|
|
|
const acceptedResult = await Hooks.wrapFun(
|
|
|
|
isRemoteVideoCommentAccepted,
|
|
|
|
acceptParameters,
|
|
|
|
'filter:activity-pub.remote-video-comment.create.accept.result'
|
|
|
|
)
|
|
|
|
|
|
|
|
if (!acceptedResult || acceptedResult.accepted !== true) {
|
|
|
|
logger.info('Refused to create a remote comment.', { acceptedResult, acceptParameters })
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|