activity.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import * as validator from 'validator'
  2. import { Activity, ActivityType } from '../../../../shared/models/activitypub'
  3. import { sanitizeAndCheckActorObject } from './actor'
  4. import { isActivityPubUrlValid, isBaseActivityValid, isObjectValid } from './misc'
  5. import { isDislikeActivityValid } from './rate'
  6. import { sanitizeAndCheckVideoCommentObject } from './video-comments'
  7. import { sanitizeAndCheckVideoTorrentObject } from './videos'
  8. import { isViewActivityValid } from './view'
  9. import { exists } from '../misc'
  10. import { isCacheFileObjectValid } from './cache-file'
  11. import { isFlagActivityValid } from './flag'
  12. import { isPlaylistObjectValid } from './playlist'
  13. function isRootActivityValid (activity: any) {
  14. return isCollection(activity) || isActivity(activity)
  15. }
  16. function isCollection (activity: any) {
  17. return (activity.type === 'Collection' || activity.type === 'OrderedCollection') &&
  18. validator.isInt(activity.totalItems, { min: 0 }) &&
  19. Array.isArray(activity.items)
  20. }
  21. function isActivity (activity: any) {
  22. return isActivityPubUrlValid(activity.id) &&
  23. exists(activity.actor) &&
  24. (isActivityPubUrlValid(activity.actor) || isActivityPubUrlValid(activity.actor.id))
  25. }
  26. const activityCheckers: { [ P in ActivityType ]: (activity: Activity) => boolean } = {
  27. Create: checkCreateActivity,
  28. Update: checkUpdateActivity,
  29. Delete: checkDeleteActivity,
  30. Follow: checkFollowActivity,
  31. Accept: checkAcceptActivity,
  32. Reject: checkRejectActivity,
  33. Announce: checkAnnounceActivity,
  34. Undo: checkUndoActivity,
  35. Like: checkLikeActivity,
  36. View: checkViewActivity,
  37. Flag: checkFlagActivity,
  38. Dislike: checkDislikeActivity
  39. }
  40. function isActivityValid (activity: any) {
  41. const checker = activityCheckers[activity.type]
  42. // Unknown activity type
  43. if (!checker) return false
  44. return checker(activity)
  45. }
  46. // ---------------------------------------------------------------------------
  47. export {
  48. isRootActivityValid,
  49. isActivityValid
  50. }
  51. // ---------------------------------------------------------------------------
  52. function checkViewActivity (activity: any) {
  53. return isBaseActivityValid(activity, 'View') &&
  54. isViewActivityValid(activity)
  55. }
  56. function checkFlagActivity (activity: any) {
  57. return isBaseActivityValid(activity, 'Flag') &&
  58. isFlagActivityValid(activity)
  59. }
  60. function checkDislikeActivity (activity: any) {
  61. return isBaseActivityValid(activity, 'Dislike') &&
  62. isDislikeActivityValid(activity)
  63. }
  64. function checkCreateActivity (activity: any) {
  65. return isBaseActivityValid(activity, 'Create') &&
  66. (
  67. isViewActivityValid(activity.object) ||
  68. isDislikeActivityValid(activity.object) ||
  69. isFlagActivityValid(activity.object) ||
  70. isPlaylistObjectValid(activity.object) ||
  71. isCacheFileObjectValid(activity.object) ||
  72. sanitizeAndCheckVideoCommentObject(activity.object) ||
  73. sanitizeAndCheckVideoTorrentObject(activity.object)
  74. )
  75. }
  76. function checkUpdateActivity (activity: any) {
  77. return isBaseActivityValid(activity, 'Update') &&
  78. (
  79. isCacheFileObjectValid(activity.object) ||
  80. isPlaylistObjectValid(activity.object) ||
  81. sanitizeAndCheckVideoTorrentObject(activity.object) ||
  82. sanitizeAndCheckActorObject(activity.object)
  83. )
  84. }
  85. function checkDeleteActivity (activity: any) {
  86. // We don't really check objects
  87. return isBaseActivityValid(activity, 'Delete') &&
  88. isObjectValid(activity.object)
  89. }
  90. function checkFollowActivity (activity: any) {
  91. return isBaseActivityValid(activity, 'Follow') &&
  92. isObjectValid(activity.object)
  93. }
  94. function checkAcceptActivity (activity: any) {
  95. return isBaseActivityValid(activity, 'Accept')
  96. }
  97. function checkRejectActivity (activity: any) {
  98. return isBaseActivityValid(activity, 'Reject')
  99. }
  100. function checkAnnounceActivity (activity: any) {
  101. return isBaseActivityValid(activity, 'Announce') &&
  102. isObjectValid(activity.object)
  103. }
  104. function checkUndoActivity (activity: any) {
  105. return isBaseActivityValid(activity, 'Undo') &&
  106. (
  107. checkFollowActivity(activity.object) ||
  108. checkLikeActivity(activity.object) ||
  109. checkDislikeActivity(activity.object) ||
  110. checkAnnounceActivity(activity.object) ||
  111. checkCreateActivity(activity.object)
  112. )
  113. }
  114. function checkLikeActivity (activity: any) {
  115. return isBaseActivityValid(activity, 'Like') &&
  116. isObjectValid(activity.object)
  117. }