2021-06-03 16:02:29 +02:00
|
|
|
import { StatsManager } from '@server/lib/stat-manager'
|
2017-12-12 17:53:50 +01:00
|
|
|
import { Activity, ActivityType } from '../../../../shared/models/activitypub'
|
2017-12-28 11:16:08 +01:00
|
|
|
import { logger } from '../../../helpers/logger'
|
2021-06-03 16:02:29 +02:00
|
|
|
import { APProcessorOptions } from '../../../types/activitypub-processor.model'
|
|
|
|
import { MActorDefault, MActorSignature } from '../../../types/models'
|
2022-03-23 14:24:50 +01:00
|
|
|
import { getAPId } from '../activity'
|
2021-06-03 16:02:29 +02:00
|
|
|
import { getOrCreateAPActor } from '../actors'
|
2022-03-23 14:24:50 +01:00
|
|
|
import { checkUrlsSameHost } from '../url'
|
2017-11-22 10:29:55 +01:00
|
|
|
import { processAcceptActivity } from './process-accept'
|
|
|
|
import { processAnnounceActivity } from './process-announce'
|
|
|
|
import { processCreateActivity } from './process-create'
|
|
|
|
import { processDeleteActivity } from './process-delete'
|
2021-06-03 16:02:29 +02:00
|
|
|
import { processDislikeActivity } from './process-dislike'
|
|
|
|
import { processFlagActivity } from './process-flag'
|
2017-11-22 10:29:55 +01:00
|
|
|
import { processFollowActivity } from './process-follow'
|
2017-11-23 14:19:55 +01:00
|
|
|
import { processLikeActivity } from './process-like'
|
2018-01-11 17:37:49 +01:00
|
|
|
import { processRejectActivity } from './process-reject'
|
2017-11-22 10:29:55 +01:00
|
|
|
import { processUndoActivity } from './process-undo'
|
|
|
|
import { processUpdateActivity } from './process-update'
|
2019-01-15 11:14:12 +01:00
|
|
|
import { processViewActivity } from './process-view'
|
2017-11-22 10:29:55 +01:00
|
|
|
|
2019-08-02 10:53:36 +02:00
|
|
|
const processActivity: { [ P in ActivityType ]: (options: APProcessorOptions<Activity>) => Promise<any> } = {
|
2017-11-22 10:29:55 +01:00
|
|
|
Create: processCreateActivity,
|
|
|
|
Update: processUpdateActivity,
|
|
|
|
Delete: processDeleteActivity,
|
|
|
|
Follow: processFollowActivity,
|
|
|
|
Accept: processAcceptActivity,
|
2018-01-11 17:37:49 +01:00
|
|
|
Reject: processRejectActivity,
|
2017-11-22 10:29:55 +01:00
|
|
|
Announce: processAnnounceActivity,
|
2017-11-23 14:19:55 +01:00
|
|
|
Undo: processUndoActivity,
|
2019-01-15 11:14:12 +01:00
|
|
|
Like: processLikeActivity,
|
|
|
|
Dislike: processDislikeActivity,
|
|
|
|
Flag: processFlagActivity,
|
|
|
|
View: processViewActivity
|
2017-11-22 10:29:55 +01:00
|
|
|
}
|
|
|
|
|
2018-11-14 15:01:28 +01:00
|
|
|
async function processActivities (
|
|
|
|
activities: Activity[],
|
|
|
|
options: {
|
2019-08-15 11:53:26 +02:00
|
|
|
signatureActor?: MActorSignature
|
|
|
|
inboxActor?: MActorDefault
|
2018-11-14 15:01:28 +01:00
|
|
|
outboxUrl?: string
|
2019-08-02 10:53:36 +02:00
|
|
|
fromFetch?: boolean
|
|
|
|
} = {}
|
|
|
|
) {
|
|
|
|
const { outboxUrl, signatureActor, inboxActor, fromFetch = false } = options
|
|
|
|
|
2019-08-15 11:53:26 +02:00
|
|
|
const actorsCache: { [ url: string ]: MActorSignature } = {}
|
2018-09-19 14:44:20 +02:00
|
|
|
|
2017-11-22 10:29:55 +01:00
|
|
|
for (const activity of activities) {
|
2019-08-02 10:53:36 +02:00
|
|
|
if (!signatureActor && [ 'Create', 'Announce', 'Like' ].includes(activity.type) === false) {
|
2018-09-19 15:47:55 +02:00
|
|
|
logger.error('Cannot process activity %s (type: %s) without the actor signature.', activity.id, activity.type)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-01-15 11:14:12 +01:00
|
|
|
const actorUrl = getAPId(activity.actor)
|
2018-03-22 18:40:33 +01:00
|
|
|
|
2017-11-24 14:36:28 +01:00
|
|
|
// When we fetch remote data, we don't have signature
|
2019-08-02 10:53:36 +02:00
|
|
|
if (signatureActor && actorUrl !== signatureActor.url) {
|
|
|
|
logger.warn('Signature mismatch between %s and %s, skipping.', actorUrl, signatureActor.url)
|
2017-11-24 14:36:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-08-02 10:53:36 +02:00
|
|
|
if (outboxUrl && checkUrlsSameHost(outboxUrl, actorUrl) !== true) {
|
|
|
|
logger.warn('Host mismatch between outbox URL %s and actor URL %s, skipping.', outboxUrl, actorUrl)
|
2018-11-14 15:01:28 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-06-03 16:02:29 +02:00
|
|
|
const byActor = signatureActor || actorsCache[actorUrl] || await getOrCreateAPActor(actorUrl)
|
2018-09-19 14:44:20 +02:00
|
|
|
actorsCache[actorUrl] = byActor
|
|
|
|
|
2017-11-22 10:29:55 +01:00
|
|
|
const activityProcessor = processActivity[activity.type]
|
|
|
|
if (activityProcessor === undefined) {
|
|
|
|
logger.warn('Unknown activity type %s.', activity.type, { activityId: activity.id })
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-11-23 16:55:13 +01:00
|
|
|
try {
|
2019-08-15 11:53:26 +02:00
|
|
|
await activityProcessor({ activity, byActor, inboxActor, fromFetch })
|
2021-02-26 10:28:11 +01:00
|
|
|
|
|
|
|
StatsManager.Instance.addInboxProcessedSuccess(activity.type)
|
2017-11-23 16:55:13 +01:00
|
|
|
} catch (err) {
|
2018-03-28 11:00:02 +02:00
|
|
|
logger.warn('Cannot process activity %s.', activity.type, { err })
|
2021-02-26 10:28:11 +01:00
|
|
|
|
|
|
|
StatsManager.Instance.addInboxProcessedError(activity.type)
|
2017-11-23 16:55:13 +01:00
|
|
|
}
|
2017-11-22 10:29:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
|
|
|
processActivities
|
|
|
|
}
|