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

95 lines
2.8 KiB
TypeScript
Raw Normal View History

2017-11-10 14:34:45 +01:00
import * as validator from 'validator'
2017-12-12 17:53:50 +01:00
import { Activity, ActivityType } from '../../../../shared/models/activitypub'
2017-12-14 17:38:41 +01:00
import { isActorAcceptActivityValid, isActorDeleteActivityValid, isActorFollowActivityValid } from './actor'
2017-11-23 14:19:55 +01:00
import { isAnnounceActivityValid } from './announce'
2017-11-14 17:31:26 +01:00
import { isActivityPubUrlValid } from './misc'
2017-12-12 17:53:50 +01:00
import { isDislikeActivityValid, isLikeActivityValid } from './rate'
2017-11-23 14:19:55 +01:00
import { isUndoActivityValid } from './undo'
2017-12-14 17:38:41 +01:00
import { isVideoChannelDeleteActivityValid, isVideoChannelUpdateActivityValid } from './video-channels'
2017-11-10 14:34:45 +01:00
import {
2017-11-15 17:56:21 +01:00
isVideoFlagValid,
2017-12-14 17:38:41 +01:00
isVideoTorrentCreateActivityValid,
2017-11-14 17:31:26 +01:00
isVideoTorrentDeleteActivityValid,
isVideoTorrentUpdateActivityValid
2017-11-10 14:34:45 +01:00
} from './videos'
2017-11-22 16:25:03 +01:00
import { isViewActivityValid } from './view'
2017-11-10 14:34:45 +01:00
function isRootActivityValid (activity: any) {
return Array.isArray(activity['@context']) &&
(
(activity.type === 'Collection' || activity.type === 'OrderedCollection') &&
validator.isInt(activity.totalItems, { min: 0 }) &&
Array.isArray(activity.items)
) ||
(
2017-11-14 17:31:26 +01:00
isActivityPubUrlValid(activity.id) &&
isActivityPubUrlValid(activity.actor)
2017-11-10 14:34:45 +01:00
)
}
2017-11-20 09:43:39 +01:00
const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean } = {
Create: checkCreateActivity,
Update: checkUpdateActivity,
Delete: checkDeleteActivity,
Follow: checkFollowActivity,
Accept: checkAcceptActivity,
Announce: checkAnnounceActivity,
2017-11-23 14:19:55 +01:00
Undo: checkUndoActivity,
Like: checkLikeActivity
2017-11-20 09:43:39 +01:00
}
2017-11-10 14:34:45 +01:00
function isActivityValid (activity: any) {
2017-11-20 09:43:39 +01:00
const checker = activityCheckers[activity.type]
// Unknown activity type
if (!checker) return false
return checker(activity)
2017-11-10 14:34:45 +01:00
}
// ---------------------------------------------------------------------------
export {
isRootActivityValid,
isActivityValid
}
2017-11-20 09:43:39 +01:00
// ---------------------------------------------------------------------------
function checkCreateActivity (activity: any) {
2017-11-23 14:19:55 +01:00
return isViewActivityValid(activity) ||
isDislikeActivityValid(activity) ||
2017-12-14 17:38:41 +01:00
isVideoTorrentCreateActivityValid(activity) ||
2017-11-23 14:19:55 +01:00
isVideoFlagValid(activity)
2017-11-20 09:43:39 +01:00
}
function checkUpdateActivity (activity: any) {
return isVideoTorrentUpdateActivityValid(activity) ||
isVideoChannelUpdateActivityValid(activity)
}
function checkDeleteActivity (activity: any) {
return isVideoTorrentDeleteActivityValid(activity) ||
isVideoChannelDeleteActivityValid(activity) ||
2017-12-14 17:38:41 +01:00
isActorDeleteActivityValid(activity)
2017-11-20 09:43:39 +01:00
}
function checkFollowActivity (activity: any) {
2017-12-14 17:38:41 +01:00
return isActorFollowActivityValid(activity)
2017-11-20 09:43:39 +01:00
}
function checkAcceptActivity (activity: any) {
2017-12-14 17:38:41 +01:00
return isActorAcceptActivityValid(activity)
2017-11-20 09:43:39 +01:00
}
function checkAnnounceActivity (activity: any) {
2017-11-23 14:19:55 +01:00
return isAnnounceActivityValid(activity)
2017-11-20 09:43:39 +01:00
}
function checkUndoActivity (activity: any) {
2017-11-23 14:19:55 +01:00
return isUndoActivityValid(activity)
}
function checkLikeActivity (activity: any) {
return isLikeActivityValid(activity)
2017-11-20 09:43:39 +01:00
}