import validator from 'validator' ; import { Activity , ActivityType } from '../../../../shared/models/activitypub' ; import { isAbuseReasonValid } from '../abuses' ; import { exists } from '../misc' ; import { sanitizeAndCheckActorObject } from './actor' ; import { isCacheFileObjectValid } from './cache-file' ; import { isActivityPubUrlValid , isBaseActivityValid , isObjectValid } from './misc' ; import { isPlaylistObjectValid } from './playlist' ; import { sanitizeAndCheckVideoCommentObject } from './video-comments' ; import { sanitizeAndCheckVideoTorrentObject } from './videos' ; function isRootActivityValid ( activity ) { return isCollection ( activity ) || isActivity ( activity ) ; } function isCollection ( activity ) { return ( activity . type === 'Collection' || activity . type === 'OrderedCollection' ) && validator . isInt ( activity . totalItems , { min : 0 } ) && Array . isArray ( activity . items ) ; } function isActivity ( activity ) { return isActivityPubUrlValid ( activity . id ) && exists ( activity . actor ) && ( isActivityPubUrlValid ( activity . actor ) || isActivityPubUrlValid ( activity . actor . id ) ) ; } const activityCheckers = { Create : isCreateActivityValid , Update : isUpdateActivityValid , Delete : isDeleteActivityValid , Follow : isFollowActivityValid , Accept : isAcceptActivityValid , Reject : isRejectActivityValid , Announce : isAnnounceActivityValid , Undo : isUndoActivityValid , Like : isLikeActivityValid , View : isViewActivityValid , Flag : isFlagActivityValid , Dislike : isDislikeActivityValid } ; function isActivityValid ( activity ) { const checker = activityCheckers [ activity . type ] ; if ( ! checker ) return false ; return checker ( activity ) ; } function isFlagActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Flag' ) && isAbuseReasonValid ( activity . content ) && isActivityPubUrlValid ( activity . object ) ; } function isLikeActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Like' ) && isObjectValid ( activity . object ) ; } function isDislikeActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Dislike' ) && isObjectValid ( activity . object ) ; } function isAnnounceActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Announce' ) && isObjectValid ( activity . object ) ; } function isViewActivityValid ( activity ) { return isBaseActivityValid ( activity , 'View' ) && isActivityPubUrlValid ( activity . actor ) && isActivityPubUrlValid ( activity . object ) ; } function isCreateActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Create' ) && ( isViewActivityValid ( activity . object ) || isDislikeActivityValid ( activity . object ) || isFlagActivityValid ( activity . object ) || isPlaylistObjectValid ( activity . object ) || isCacheFileObjectValid ( activity . object ) || sanitizeAndCheckVideoCommentObject ( activity . object ) || sanitizeAndCheckVideoTorrentObject ( activity . object ) ) ; } function isUpdateActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Update' ) && ( isCacheFileObjectValid ( activity . object ) || isPlaylistObjectValid ( activity . object ) || sanitizeAndCheckVideoTorrentObject ( activity . object ) || sanitizeAndCheckActorObject ( activity . object ) ) ; } function isDeleteActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Delete' ) && isObjectValid ( activity . object ) ; } function isFollowActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Follow' ) && isObjectValid ( activity . object ) ; } function isAcceptActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Accept' ) ; } function isRejectActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Reject' ) ; } function isUndoActivityValid ( activity ) { return isBaseActivityValid ( activity , 'Undo' ) && ( isFollowActivityValid ( activity . object ) || isLikeActivityValid ( activity . object ) || isDislikeActivityValid ( activity . object ) || isAnnounceActivityValid ( activity . object ) || isCreateActivityValid ( activity . object ) ) ; } export { isRootActivityValid , isActivityValid , isFlagActivityValid , isLikeActivityValid , isDislikeActivityValid , isAnnounceActivityValid , isViewActivityValid , isCreateActivityValid , isUpdateActivityValid , isDeleteActivityValid , isFollowActivityValid , isAcceptActivityValid , isRejectActivityValid , isUndoActivityValid } ;