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

140 lines
4.3 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'
import { sanitizeAndCheckActorObject } from './actor'
import { isActivityPubUrlValid, isBaseActivityValid, isObjectValid } from './misc'
import { isDislikeActivityValid } from './rate'
import { sanitizeAndCheckVideoCommentObject } from './video-comments'
import { sanitizeAndCheckVideoTorrentObject } from './videos'
2017-11-22 09:25:03 -06:00
import { isViewActivityValid } from './view'
2018-06-13 07:55:18 -05:00
import { exists } from '../misc'
import { isCacheFileObjectValid } from './cache-file'
import { isFlagActivityValid } from './flag'
2019-02-26 03:55:40 -06:00
import { isPlaylistObjectValid } from './playlist'
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 } = {
Create: checkCreateActivity,
Update: checkUpdateActivity,
Delete: checkDeleteActivity,
Follow: checkFollowActivity,
Accept: checkAcceptActivity,
2018-01-11 10:37:49 -06:00
Reject: checkRejectActivity,
2017-11-20 02:43:39 -06:00
Announce: checkAnnounceActivity,
2017-11-23 07:19:55 -06:00
Undo: checkUndoActivity,
Like: checkLikeActivity,
View: checkViewActivity,
Flag: checkFlagActivity,
Dislike: checkDislikeActivity
2017-11-20 02:43:39 -06:00
}
2017-11-10 07:34:45 -06:00
function isActivityValid (activity: any) {
2017-11-20 02:43:39 -06:00
const checker = activityCheckers[activity.type]
// Unknown activity type
if (!checker) return false
return checker(activity)
2017-11-10 07:34:45 -06:00
}
// ---------------------------------------------------------------------------
export {
isRootActivityValid,
isActivityValid
}
2017-11-20 02:43:39 -06:00
// ---------------------------------------------------------------------------
function checkViewActivity (activity: any) {
return isBaseActivityValid(activity, 'View') &&
isViewActivityValid(activity)
}
function checkFlagActivity (activity: any) {
return isBaseActivityValid(activity, 'Flag') &&
isFlagActivityValid(activity)
}
function checkDislikeActivity (activity: any) {
return isBaseActivityValid(activity, 'Dislike') &&
isDislikeActivityValid(activity)
}
2017-11-20 02:43:39 -06:00
function checkCreateActivity (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
}
function checkUpdateActivity (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
}
function checkDeleteActivity (activity: any) {
// We don't really check objects
return isBaseActivityValid(activity, 'Delete') &&
isObjectValid(activity.object)
2017-11-20 02:43:39 -06:00
}
function checkFollowActivity (activity: any) {
return isBaseActivityValid(activity, 'Follow') &&
isObjectValid(activity.object)
2017-11-20 02:43:39 -06:00
}
function checkAcceptActivity (activity: any) {
return isBaseActivityValid(activity, 'Accept')
2017-11-20 02:43:39 -06:00
}
2018-01-11 10:37:49 -06:00
function checkRejectActivity (activity: any) {
return isBaseActivityValid(activity, 'Reject')
2018-01-11 10:37:49 -06:00
}
2017-11-20 02:43:39 -06:00
function checkAnnounceActivity (activity: any) {
return isBaseActivityValid(activity, 'Announce') &&
isObjectValid(activity.object)
2017-11-20 02:43:39 -06:00
}
function checkUndoActivity (activity: any) {
return isBaseActivityValid(activity, 'Undo') &&
(
checkFollowActivity(activity.object) ||
checkLikeActivity(activity.object) ||
checkDislikeActivity(activity.object) ||
checkAnnounceActivity(activity.object) ||
checkCreateActivity(activity.object)
)
2017-11-23 07:19:55 -06:00
}
function checkLikeActivity (activity: any) {
return isBaseActivityValid(activity, 'Like') &&
isObjectValid(activity.object)
2017-11-20 02:43:39 -06:00
}