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'
|
2021-02-26 09:26:27 -06:00
|
|
|
import { exists } from '../misc'
|
2019-01-15 04:14:12 -06:00
|
|
|
import { sanitizeAndCheckActorObject } from './actor'
|
2021-02-26 09:26:27 -06:00
|
|
|
import { isCacheFileObjectValid } from './cache-file'
|
2019-01-15 04:14:12 -06:00
|
|
|
import { isActivityPubUrlValid, isBaseActivityValid, isObjectValid } from './misc'
|
2021-02-26 09:26:27 -06:00
|
|
|
import { isPlaylistObjectValid } from './playlist'
|
2019-01-15 04:14:12 -06:00
|
|
|
import { sanitizeAndCheckVideoCommentObject } from './video-comments'
|
|
|
|
import { sanitizeAndCheckVideoTorrentObject } from './videos'
|
2022-03-24 07:36:47 -05:00
|
|
|
import { isWatchActionObjectValid } from './watch-action'
|
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) {
|
2021-03-09 08:45:24 -06:00
|
|
|
return isBaseActivityValid(activity, 'Flag') &&
|
2021-03-05 06:49:58 -06:00
|
|
|
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)
|
2019-01-15 04:14:12 -06:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:49:58 -06:00
|
|
|
function isDislikeActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Dislike') &&
|
|
|
|
isObjectValid(activity.object)
|
2019-01-15 04:14:12 -06:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:49:58 -06:00
|
|
|
function isAnnounceActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'Announce') &&
|
|
|
|
isObjectValid(activity.object)
|
2021-02-26 09:26:27 -06:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:49:58 -06:00
|
|
|
function isViewActivityValid (activity: any) {
|
|
|
|
return isBaseActivityValid(activity, 'View') &&
|
|
|
|
isActivityPubUrlValid(activity.actor) &&
|
|
|
|
isActivityPubUrlValid(activity.object)
|
2019-01-15 04:14:12 -06:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:49:58 -06:00
|
|
|
function isCreateActivityValid (activity: any) {
|
2019-01-15 04:14:12 -06:00
|
|
|
return isBaseActivityValid(activity, 'Create') &&
|
|
|
|
(
|
|
|
|
isViewActivityValid(activity.object) ||
|
|
|
|
isDislikeActivityValid(activity.object) ||
|
|
|
|
isFlagActivityValid(activity.object) ||
|
2019-02-26 03:55:40 -06:00
|
|
|
isPlaylistObjectValid(activity.object) ||
|
2022-03-24 07:36:47 -05:00
|
|
|
isWatchActionObjectValid(activity.object) ||
|
2019-01-15 04:14:12 -06:00
|
|
|
|
|
|
|
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) {
|
2019-01-15 04:14:12 -06:00
|
|
|
return isBaseActivityValid(activity, 'Update') &&
|
|
|
|
(
|
|
|
|
isCacheFileObjectValid(activity.object) ||
|
2019-02-26 03:55:40 -06:00
|
|
|
isPlaylistObjectValid(activity.object) ||
|
2019-01-15 04:14:12 -06:00
|
|
|
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) {
|
2019-01-15 04:14:12 -06:00
|
|
|
// 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) {
|
2019-01-15 04:14:12 -06:00
|
|
|
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) {
|
2019-01-15 04:14:12 -06:00
|
|
|
return isBaseActivityValid(activity, 'Accept')
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:49:58 -06:00
|
|
|
function isRejectActivityValid (activity: any) {
|
2019-01-15 04:14:12 -06:00
|
|
|
return isBaseActivityValid(activity, 'Reject')
|
2018-01-11 10:37:49 -06:00
|
|
|
}
|
|
|
|
|
2021-03-05 06:49:58 -06:00
|
|
|
function isUndoActivityValid (activity: any) {
|
2019-01-15 04:14:12 -06:00
|
|
|
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)
|
2019-01-15 04:14:12 -06:00
|
|
|
)
|
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
|
|
|
|
}
|