X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=server%2Flib%2Factivitypub%2Fvideo-comments.ts;h=b65baf0e95dc49469d7e17c6a10261df7b320fac;hb=0c302acb3c358b4d4d8dee45aed1de1108ea37ea;hp=760da719d4ac29752ee13477eea5d4588b2af77f;hpb=49af5ac8c2653cb0ef23479c9d3256c5b724d49d;p=github%2FChocobozzz%2FPeerTube.git diff --git a/server/lib/activitypub/video-comments.ts b/server/lib/activitypub/video-comments.ts index 760da719d..b65baf0e9 100644 --- a/server/lib/activitypub/video-comments.ts +++ b/server/lib/activitypub/video-comments.ts @@ -1,12 +1,14 @@ -import * as Bluebird from 'bluebird' -import { checkUrlsSameHost } from '../../helpers/activitypub' +import { map } from 'bluebird' import { sanitizeAndCheckVideoCommentObject } from '../../helpers/custom-validators/activitypub/video-comments' import { logger } from '../../helpers/logger' import { doJSONRequest } from '../../helpers/requests' import { ACTIVITY_PUB, CRAWL_REQUEST_CONCURRENCY } from '../../initializers/constants' import { VideoCommentModel } from '../../models/video/video-comment' -import { MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video' -import { getOrCreateActorAndServerAndModel } from './actor' +import { MComment, MCommentOwner, MCommentOwnerVideo, MVideoAccountLightBlacklistAllFiles } from '../../types/models/video' +import { isRemoteVideoCommentAccepted } from '../moderation' +import { Hooks } from '../plugins/hooks' +import { getOrCreateAPActor } from './actors' +import { checkUrlsSameHost } from './url' import { getOrCreateAPVideo } from './videos' type ResolveThreadParams = { @@ -18,7 +20,7 @@ type ResolveThreadParams = { type ResolveThreadResult = Promise<{ video: MVideoAccountLightBlacklistAllFiles, comment: MCommentOwnerVideo, commentCreated: boolean }> async function addVideoComments (commentUrls: string[]) { - return Bluebird.map(commentUrls, async commentUrl => { + return map(commentUrls, async commentUrl => { try { await resolveThread({ url: commentUrl, isVideo: false }) } catch (err) { @@ -87,7 +89,7 @@ async function tryToResolveThreadFromVideo (params: ResolveThreadParams) { // 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 syncParam = { rates: true, shares: true, comments: false, thumbnail: true, refreshVideo: false } const { video } = await getOrCreateAPVideo({ videoObject: url, syncParam }) if (video.isOwned() && !video.hasPrivacyForFederation()) { @@ -103,6 +105,10 @@ async function tryToResolveThreadFromVideo (params: ResolveThreadParams) { firstReply.changed('updatedAt', true) firstReply.Video = video + if (await isRemoteCommentAccepted(firstReply) !== true) { + return undefined + } + comments[comments.length - 1] = await firstReply.save() for (let i = comments.length - 2; i >= 0; i--) { @@ -113,6 +119,10 @@ async function tryToResolveThreadFromVideo (params: ResolveThreadParams) { comment.changed('updatedAt', true) comment.Video = video + if (await isRemoteCommentAccepted(comment) !== true) { + return undefined + } + comments[i] = await comment.save() } @@ -147,7 +157,7 @@ async function resolveRemoteParentComment (params: ResolveThreadParams) { } const actor = actorUrl - ? await getOrCreateActorAndServerAndModel(actorUrl, 'all') + ? await getOrCreateAPActor(actorUrl, 'all') : null const comment = new VideoCommentModel({ @@ -169,3 +179,26 @@ async function resolveRemoteParentComment (params: ResolveThreadParams) { commentCreated: true }) } + +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 +}