process.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { Activity, ActivityType } from '../../../../shared/models/activitypub'
  2. import { checkUrlsSameHost, getAPId } from '../../../helpers/activitypub'
  3. import { logger } from '../../../helpers/logger'
  4. import { ActorModel } from '../../../models/activitypub/actor'
  5. import { processAcceptActivity } from './process-accept'
  6. import { processAnnounceActivity } from './process-announce'
  7. import { processCreateActivity } from './process-create'
  8. import { processDeleteActivity } from './process-delete'
  9. import { processFollowActivity } from './process-follow'
  10. import { processLikeActivity } from './process-like'
  11. import { processRejectActivity } from './process-reject'
  12. import { processUndoActivity } from './process-undo'
  13. import { processUpdateActivity } from './process-update'
  14. import { getOrCreateActorAndServerAndModel } from '../actor'
  15. import { processDislikeActivity } from './process-dislike'
  16. import { processFlagActivity } from './process-flag'
  17. import { processViewActivity } from './process-view'
  18. const processActivity: { [ P in ActivityType ]: (activity: Activity, byActor: ActorModel, inboxActor?: ActorModel) => Promise<any> } = {
  19. Create: processCreateActivity,
  20. Update: processUpdateActivity,
  21. Delete: processDeleteActivity,
  22. Follow: processFollowActivity,
  23. Accept: processAcceptActivity,
  24. Reject: processRejectActivity,
  25. Announce: processAnnounceActivity,
  26. Undo: processUndoActivity,
  27. Like: processLikeActivity,
  28. Dislike: processDislikeActivity,
  29. Flag: processFlagActivity,
  30. View: processViewActivity
  31. }
  32. async function processActivities (
  33. activities: Activity[],
  34. options: {
  35. signatureActor?: ActorModel
  36. inboxActor?: ActorModel
  37. outboxUrl?: string
  38. } = {}) {
  39. const actorsCache: { [ url: string ]: ActorModel } = {}
  40. for (const activity of activities) {
  41. if (!options.signatureActor && [ 'Create', 'Announce', 'Like' ].includes(activity.type) === false) {
  42. logger.error('Cannot process activity %s (type: %s) without the actor signature.', activity.id, activity.type)
  43. continue
  44. }
  45. const actorUrl = getAPId(activity.actor)
  46. // When we fetch remote data, we don't have signature
  47. if (options.signatureActor && actorUrl !== options.signatureActor.url) {
  48. logger.warn('Signature mismatch between %s and %s, skipping.', actorUrl, options.signatureActor.url)
  49. continue
  50. }
  51. if (options.outboxUrl && checkUrlsSameHost(options.outboxUrl, actorUrl) !== true) {
  52. logger.warn('Host mismatch between outbox URL %s and actor URL %s, skipping.', options.outboxUrl, actorUrl)
  53. continue
  54. }
  55. const byActor = options.signatureActor || actorsCache[actorUrl] || await getOrCreateActorAndServerAndModel(actorUrl)
  56. actorsCache[actorUrl] = byActor
  57. const activityProcessor = processActivity[activity.type]
  58. if (activityProcessor === undefined) {
  59. logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id })
  60. continue
  61. }
  62. try {
  63. await activityProcessor(activity, byActor, options.inboxActor)
  64. } catch (err) {
  65. logger.warn('Cannot process activity %s.', activity.type, { err })
  66. }
  67. }
  68. }
  69. export {
  70. processActivities
  71. }