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

150 lines
4.5 KiB
TypeScript
Raw Normal View History

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