From b2a70e3ca2611a8831b6e490cc25dbf3066562c0 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 23 Sep 2022 11:38:18 +0200 Subject: Support refusing remote comments --- server/lib/activitypub/process/process-create.ts | 2 ++ server/lib/activitypub/video-comments.ts | 35 +++++++++++++++++++++- server/lib/moderation.ts | 38 ++++++++++++++---------- 3 files changed, 58 insertions(+), 17 deletions(-) (limited to 'server/lib') diff --git a/server/lib/activitypub/process/process-create.ts b/server/lib/activitypub/process/process-create.ts index 76ed37aae..1e6e8956c 100644 --- a/server/lib/activitypub/process/process-create.ts +++ b/server/lib/activitypub/process/process-create.ts @@ -109,8 +109,10 @@ async function processCreateVideoComment (activity: ActivityCreate, byActor: MAc let video: MVideoAccountLightBlacklistAllFiles let created: boolean let comment: MCommentOwnerVideo + try { const resolveThreadResult = await resolveThread({ url: commentObject.id, isVideo: false }) + if (!resolveThreadResult) return // Comment not accepted video = resolveThreadResult.video created = resolveThreadResult.commentCreated diff --git a/server/lib/activitypub/video-comments.ts b/server/lib/activitypub/video-comments.ts index 911c7cd30..b65baf0e9 100644 --- a/server/lib/activitypub/video-comments.ts +++ b/server/lib/activitypub/video-comments.ts @@ -4,7 +4,9 @@ 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 { 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' @@ -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() } @@ -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 +} diff --git a/server/lib/moderation.ts b/server/lib/moderation.ts index c23f5b6a6..43c58c980 100644 --- a/server/lib/moderation.ts +++ b/server/lib/moderation.ts @@ -13,18 +13,15 @@ import { MAbuseFull, MAccountDefault, MAccountLight, + MComment, MCommentAbuseAccountVideo, MCommentOwnerVideo, MUser, MVideoAbuseVideoFull, MVideoAccountLightBlacklistAllFiles } from '@server/types/models' -import { ActivityCreate } from '../../shared/models/activitypub' -import { VideoObject } from '../../shared/models/activitypub/objects' -import { VideoCommentObject } from '../../shared/models/activitypub/objects/video-comment-object' import { LiveVideoCreate, VideoCreate, VideoImportCreate } from '../../shared/models/videos' import { VideoCommentCreate } from '../../shared/models/videos/comment' -import { ActorModel } from '../models/actor/actor' import { UserModel } from '../models/user/user' import { VideoModel } from '../models/video/video' import { VideoCommentModel } from '../models/video/video-comment' @@ -36,7 +33,9 @@ export type AcceptResult = { errorMessage?: string } -// Can be filtered by plugins +// --------------------------------------------------------------------------- + +// Stub function that can be filtered by plugins function isLocalVideoAccepted (object: { videoBody: VideoCreate videoFile: VideoUploadFile @@ -45,6 +44,9 @@ function isLocalVideoAccepted (object: { return { accepted: true } } +// --------------------------------------------------------------------------- + +// Stub function that can be filtered by plugins function isLocalLiveVideoAccepted (object: { liveVideoBody: LiveVideoCreate user: UserModel @@ -52,6 +54,9 @@ function isLocalLiveVideoAccepted (object: { return { accepted: true } } +// --------------------------------------------------------------------------- + +// Stub function that can be filtered by plugins function isLocalVideoThreadAccepted (_object: { commentBody: VideoCommentCreate video: VideoModel @@ -60,6 +65,7 @@ function isLocalVideoThreadAccepted (_object: { return { accepted: true } } +// Stub function that can be filtered by plugins function isLocalVideoCommentReplyAccepted (_object: { commentBody: VideoCommentCreate parentComment: VideoCommentModel @@ -69,22 +75,18 @@ function isLocalVideoCommentReplyAccepted (_object: { return { accepted: true } } -function isRemoteVideoAccepted (_object: { - activity: ActivityCreate - videoAP: VideoObject - byActor: ActorModel -}): AcceptResult { - return { accepted: true } -} +// --------------------------------------------------------------------------- +// Stub function that can be filtered by plugins function isRemoteVideoCommentAccepted (_object: { - activity: ActivityCreate - commentAP: VideoCommentObject - byActor: ActorModel + comment: MComment }): AcceptResult { return { accepted: true } } +// --------------------------------------------------------------------------- + +// Stub function that can be filtered by plugins function isPreImportVideoAccepted (object: { videoImportBody: VideoImportCreate user: MUser @@ -92,6 +94,7 @@ function isPreImportVideoAccepted (object: { return { accepted: true } } +// Stub function that can be filtered by plugins function isPostImportVideoAccepted (object: { videoFilePath: PathLike videoFile: VideoFileModel @@ -100,6 +103,8 @@ function isPostImportVideoAccepted (object: { return { accepted: true } } +// --------------------------------------------------------------------------- + async function createVideoAbuse (options: { baseAbuse: FilteredModelAttributes videoInstance: MVideoAccountLightBlacklistAllFiles @@ -189,12 +194,13 @@ function createAccountAbuse (options: { }) } +// --------------------------------------------------------------------------- + export { isLocalLiveVideoAccepted, isLocalVideoAccepted, isLocalVideoThreadAccepted, - isRemoteVideoAccepted, isRemoteVideoCommentAccepted, isLocalVideoCommentReplyAccepted, isPreImportVideoAccepted, -- cgit v1.2.3