aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/activitypub/process/process.ts
blob: c602bf2181edf6f2fc45df13bbc32b443a63b44a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { Activity, ActivityType } from '../../../../shared/models/activitypub'
import { checkUrlsSameHost, getAPId } from '../../../helpers/activitypub'
import { logger } from '../../../helpers/logger'
import { processAcceptActivity } from './process-accept'
import { processAnnounceActivity } from './process-announce'
import { processCreateActivity } from './process-create'
import { processDeleteActivity } from './process-delete'
import { processFollowActivity } from './process-follow'
import { processLikeActivity } from './process-like'
import { processRejectActivity } from './process-reject'
import { processUndoActivity } from './process-undo'
import { processUpdateActivity } from './process-update'
import { getOrCreateActorAndServerAndModel } from '../actor'
import { processDislikeActivity } from './process-dislike'
import { processFlagActivity } from './process-flag'
import { processViewActivity } from './process-view'
import { APProcessorOptions } from '../../../typings/activitypub-processor.model'
import { MActorDefault, MActorSignature } from '../../../typings/models'

const processActivity: { [ P in ActivityType ]: (options: APProcessorOptions<Activity>) => Promise<any> } = {
  Create: processCreateActivity,
  Update: processUpdateActivity,
  Delete: processDeleteActivity,
  Follow: processFollowActivity,
  Accept: processAcceptActivity,
  Reject: processRejectActivity,
  Announce: processAnnounceActivity,
  Undo: processUndoActivity,
  Like: processLikeActivity,
  Dislike: processDislikeActivity,
  Flag: processFlagActivity,
  View: processViewActivity
}

async function processActivities (
  activities: Activity[],
  options: {
    signatureActor?: MActorSignature
    inboxActor?: MActorDefault
    outboxUrl?: string
    fromFetch?: boolean
  } = {}
) {
  const { outboxUrl, signatureActor, inboxActor, fromFetch = false } = options

  const actorsCache: { [ url: string ]: MActorSignature } = {}

  for (const activity of activities) {
    if (!signatureActor && [ 'Create', 'Announce', 'Like' ].includes(activity.type) === false) {
      logger.error('Cannot process activity %s (type: %s) without the actor signature.', activity.id, activity.type)
      continue
    }

    const actorUrl = getAPId(activity.actor)

    // When we fetch remote data, we don't have signature
    if (signatureActor && actorUrl !== signatureActor.url) {
      logger.warn('Signature mismatch between %s and %s, skipping.', actorUrl, signatureActor.url)
      continue
    }

    if (outboxUrl && checkUrlsSameHost(outboxUrl, actorUrl) !== true) {
      logger.warn('Host mismatch between outbox URL %s and actor URL %s, skipping.', outboxUrl, actorUrl)
      continue
    }

    const byActor = signatureActor || actorsCache[actorUrl] || await getOrCreateActorAndServerAndModel(actorUrl)
    actorsCache[actorUrl] = byActor

    const activityProcessor = processActivity[activity.type]
    if (activityProcessor === undefined) {
      logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id })
      continue
    }

    try {
      await activityProcessor({ activity, byActor, inboxActor, fromFetch })
    } catch (err) {
      logger.warn('Cannot process activity %s.', activity.type, { err })
    }
  }
}

export {
  processActivities
}