1
0
Fork 0
peertube/server/helpers/custom-validators/activitypub/activity.ts

150 lines
4.5 KiB
TypeScript
Raw Normal View History

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