2017-11-10 07:34:45 -06:00
|
|
|
import * as validator from 'validator'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { Activity, ActivityType } from '../../../../shared/models/activitypub'
|
2018-01-11 10:37:49 -06:00
|
|
|
import {
|
2018-09-11 09:27:07 -05:00
|
|
|
isActorAcceptActivityValid,
|
|
|
|
isActorDeleteActivityValid,
|
|
|
|
isActorFollowActivityValid,
|
|
|
|
isActorRejectActivityValid,
|
2018-01-11 10:37:49 -06:00
|
|
|
isActorUpdateActivityValid
|
|
|
|
} from './actor'
|
2017-11-23 07:19:55 -06:00
|
|
|
import { isAnnounceActivityValid } from './announce'
|
2017-11-14 10:31:26 -06:00
|
|
|
import { isActivityPubUrlValid } from './misc'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { isDislikeActivityValid, isLikeActivityValid } from './rate'
|
2017-11-23 07:19:55 -06:00
|
|
|
import { isUndoActivityValid } from './undo'
|
2018-01-04 04:19:16 -06:00
|
|
|
import { isVideoCommentCreateActivityValid, isVideoCommentDeleteActivityValid } from './video-comments'
|
2017-11-10 07:34:45 -06:00
|
|
|
import {
|
2017-11-15 10:56:21 -06:00
|
|
|
isVideoFlagValid,
|
2017-11-14 10:31:26 -06:00
|
|
|
isVideoTorrentDeleteActivityValid,
|
2018-09-11 09:27:07 -05:00
|
|
|
sanitizeAndCheckVideoTorrentCreateActivity,
|
2018-05-09 09:16:22 -05:00
|
|
|
sanitizeAndCheckVideoTorrentUpdateActivity
|
2017-11-10 07:34:45 -06:00
|
|
|
} 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'
|
2018-09-11 09:27:07 -05:00
|
|
|
import { isCacheFileCreateActivityValid, isCacheFileUpdateActivityValid } from './cache-file'
|
2017-11-10 07:34:45 -06:00
|
|
|
|
|
|
|
function isRootActivityValid (activity: any) {
|
2018-05-29 08:05:03 -05:00
|
|
|
return Array.isArray(activity['@context']) && (
|
2017-11-10 07:34:45 -06:00
|
|
|
(
|
|
|
|
(activity.type === 'Collection' || activity.type === 'OrderedCollection') &&
|
|
|
|
validator.isInt(activity.totalItems, { min: 0 }) &&
|
|
|
|
Array.isArray(activity.items)
|
|
|
|
) ||
|
|
|
|
(
|
2017-11-14 10:31:26 -06:00
|
|
|
isActivityPubUrlValid(activity.id) &&
|
2018-06-13 07:55:18 -05:00
|
|
|
exists(activity.actor) &&
|
2018-03-22 12:40:33 -05:00
|
|
|
(isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id))
|
2017-11-10 07:34:45 -06:00
|
|
|
)
|
2018-05-29 08:05:03 -05:00
|
|
|
)
|
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
|
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 checkCreateActivity (activity: any) {
|
2017-11-23 07:19:55 -06:00
|
|
|
return isViewActivityValid(activity) ||
|
|
|
|
isDislikeActivityValid(activity) ||
|
2018-05-09 09:16:22 -05:00
|
|
|
sanitizeAndCheckVideoTorrentCreateActivity(activity) ||
|
2017-12-22 02:14:50 -06:00
|
|
|
isVideoFlagValid(activity) ||
|
2018-09-11 09:27:07 -05:00
|
|
|
isVideoCommentCreateActivityValid(activity) ||
|
|
|
|
isCacheFileCreateActivityValid(activity)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkUpdateActivity (activity: any) {
|
2018-09-11 09:27:07 -05:00
|
|
|
return isCacheFileUpdateActivityValid(activity) ||
|
|
|
|
sanitizeAndCheckVideoTorrentUpdateActivity(activity) ||
|
2018-01-03 09:38:50 -06:00
|
|
|
isActorUpdateActivityValid(activity)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkDeleteActivity (activity: any) {
|
|
|
|
return isVideoTorrentDeleteActivityValid(activity) ||
|
2018-01-04 04:19:16 -06:00
|
|
|
isActorDeleteActivityValid(activity) ||
|
|
|
|
isVideoCommentDeleteActivityValid(activity)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkFollowActivity (activity: any) {
|
2017-12-14 10:38:41 -06:00
|
|
|
return isActorFollowActivityValid(activity)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkAcceptActivity (activity: any) {
|
2017-12-14 10:38:41 -06:00
|
|
|
return isActorAcceptActivityValid(activity)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
2018-01-11 10:37:49 -06:00
|
|
|
function checkRejectActivity (activity: any) {
|
|
|
|
return isActorRejectActivityValid(activity)
|
|
|
|
}
|
|
|
|
|
2017-11-20 02:43:39 -06:00
|
|
|
function checkAnnounceActivity (activity: any) {
|
2017-11-23 07:19:55 -06:00
|
|
|
return isAnnounceActivityValid(activity)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
function checkUndoActivity (activity: any) {
|
2017-11-23 07:19:55 -06:00
|
|
|
return isUndoActivityValid(activity)
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkLikeActivity (activity: any) {
|
|
|
|
return isLikeActivityValid(activity)
|
2017-11-20 02:43:39 -06:00
|
|
|
}
|