PeerTube/server/helpers/custom-validators/activitypub/activity.ts

152 lines
4.6 KiB
TypeScript
Raw Normal View History

2020-01-07 14:56:07 +01:00
import validator from 'validator'
2017-12-12 17:53:50 +01:00
import { Activity, ActivityType } from '../../../../shared/models/activitypub'
2021-03-05 13:49:58 +01:00
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'
import { isWatchActionObjectValid } from './watch-action'
2017-11-10 14:34:45 +01:00
function isRootActivityValid (activity: any) {
2019-05-21 09:31:53 +02:00
return isCollection(activity) || isActivity(activity)
}
function isCollection (activity: any) {
return (activity.type === 'Collection' || activity.type === 'OrderedCollection') &&
validator.isInt(activity.totalItems, { min: 0 }) &&
Array.isArray(activity.items)
}
function isActivity (activity: any) {
return isActivityPubUrlValid(activity.id) &&
exists(activity.actor) &&
(isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id))
2017-11-10 14:34:45 +01:00
}
2017-11-20 09:43:39 +01:00
const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean } = {
2021-03-05 13:49:58 +01:00
Create: isCreateActivityValid,
Update: isUpdateActivityValid,
Delete: isDeleteActivityValid,
Follow: isFollowActivityValid,
Accept: isAcceptActivityValid,
Reject: isRejectActivityValid,
Announce: isAnnounceActivityValid,
Undo: isUndoActivityValid,
Like: isLikeActivityValid,
View: isViewActivityValid,
Flag: isFlagActivityValid,
Dislike: isDislikeActivityValid
2017-11-20 09:43:39 +01:00
}
2017-11-10 14:34:45 +01:00
function isActivityValid (activity: any) {
2021-03-08 14:24:11 +01:00
const checker = activityCheckers[activity.type]
2017-11-20 09:43:39 +01:00
// Unknown activity type
if (!checker) return false
return checker(activity)
2017-11-10 14:34:45 +01:00
}
2021-03-05 13:49:58 +01:00
function isFlagActivityValid (activity: any) {
2021-03-09 15:45:24 +01:00
return isBaseActivityValid(activity, 'Flag') &&
2021-03-05 13:49:58 +01:00
isAbuseReasonValid(activity.content) &&
isActivityPubUrlValid(activity.object)
2017-11-10 14:34:45 +01:00
}
2017-11-20 09:43:39 +01:00
2021-03-05 13:49:58 +01:00
function isLikeActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Like') &&
isObjectValid(activity.object)
}
2021-03-05 13:49:58 +01:00
function isDislikeActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Dislike') &&
isObjectValid(activity.object)
}
2021-03-05 13:49:58 +01:00
function isAnnounceActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Announce') &&
isObjectValid(activity.object)
}
2021-03-05 13:49:58 +01:00
function isViewActivityValid (activity: any) {
return isBaseActivityValid(activity, 'View') &&
isActivityPubUrlValid(activity.actor) &&
isActivityPubUrlValid(activity.object)
}
2021-03-05 13:49:58 +01:00
function isCreateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Create') &&
(
isViewActivityValid(activity.object) ||
isDislikeActivityValid(activity.object) ||
isFlagActivityValid(activity.object) ||
2019-02-26 10:55:40 +01:00
isPlaylistObjectValid(activity.object) ||
isWatchActionObjectValid(activity.object) ||
isCacheFileObjectValid(activity.object) ||
sanitizeAndCheckVideoCommentObject(activity.object) ||
sanitizeAndCheckVideoTorrentObject(activity.object)
)
2017-11-20 09:43:39 +01:00
}
2021-03-05 13:49:58 +01:00
function isUpdateActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Update') &&
(
isCacheFileObjectValid(activity.object) ||
2019-02-26 10:55:40 +01:00
isPlaylistObjectValid(activity.object) ||
sanitizeAndCheckVideoTorrentObject(activity.object) ||
sanitizeAndCheckActorObject(activity.object)
)
2017-11-20 09:43:39 +01:00
}
2021-03-05 13:49:58 +01:00
function isDeleteActivityValid (activity: any) {
// We don't really check objects
return isBaseActivityValid(activity, 'Delete') &&
isObjectValid(activity.object)
2017-11-20 09:43:39 +01:00
}
2021-03-05 13:49:58 +01:00
function isFollowActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Follow') &&
isObjectValid(activity.object)
2017-11-20 09:43:39 +01:00
}
2021-03-05 13:49:58 +01:00
function isAcceptActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Accept')
2017-11-20 09:43:39 +01:00
}
2021-03-05 13:49:58 +01:00
function isRejectActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Reject')
2018-01-11 17:37:49 +01:00
}
2021-03-05 13:49:58 +01:00
function isUndoActivityValid (activity: any) {
return isBaseActivityValid(activity, 'Undo') &&
(
2021-03-05 13:49:58 +01:00
isFollowActivityValid(activity.object) ||
isLikeActivityValid(activity.object) ||
isDislikeActivityValid(activity.object) ||
isAnnounceActivityValid(activity.object) ||
isCreateActivityValid(activity.object)
)
2017-11-23 14:19:55 +01:00
}
2021-03-05 13:49:58 +01:00
// ---------------------------------------------------------------------------
export {
isRootActivityValid,
isActivityValid,
isFlagActivityValid,
isLikeActivityValid,
isDislikeActivityValid,
isAnnounceActivityValid,
isViewActivityValid,
isCreateActivityValid,
isUpdateActivityValid,
isDeleteActivityValid,
isFollowActivityValid,
isAcceptActivityValid,
isRejectActivityValid,
isUndoActivityValid
}