Skip to content

Commit 5c16bd7

Browse files
committed
Instrument sendActivity()
1 parent f753d60 commit 5c16bd7

File tree

7 files changed

+215
-73
lines changed

7 files changed

+215
-73
lines changed

docs/manual/opentelemetry.md

+1
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ spans:
128128
| `activitypub.get_actor_handle` | Client | Resolves the actor handle. |
129129
| `activitypub.lookup_object` | Client | Looks up the Activity Streams object. |
130130
| `activitypub.parse_object` | Internal | Parses the Activity Streams object. |
131+
| `activitypub.send_activity` | Client | Sends the ActivityPub activity. |
131132
| `http_signatures.sign` | Internal | Signs the HTTP request. |
132133
| `http_signatures.verify` | Internal | Verifies the HTTP request signature. |
133134
| `ld_signatures.sign` | Internal | Makes the Linked Data signature. |

src/federation/handler.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { detachSignature, verifyJsonLd } from "../sig/ld.ts";
1010
import { doesActorOwnKey } from "../sig/owner.ts";
1111
import { verifyObject } from "../sig/proof.ts";
1212
import type { Recipient } from "../vocab/actor.ts";
13+
import { getTypeId } from "../vocab/type.ts";
1314
import {
1415
Activity,
1516
CryptographicKey,
@@ -411,6 +412,8 @@ export interface InboxHandlerParameters<TContextData> {
411412
inboxContextFactory(
412413
recipient: string | null,
413414
activity: unknown,
415+
activityId: string | undefined,
416+
activityType: string,
414417
): InboxContext<TContextData>;
415418
kv: KvStore;
416419
kvPrefixes: {
@@ -683,7 +686,15 @@ export async function handleInbox<TContextData>(
683686
});
684687
}
685688
try {
686-
await listener(inboxContextFactory(recipient, json), activity);
689+
await listener(
690+
inboxContextFactory(
691+
recipient,
692+
json,
693+
activity.id?.href,
694+
getTypeId(activity).href,
695+
),
696+
activity,
697+
);
687698
} catch (error) {
688699
try {
689700
await inboxErrorHandler?.(context, error as Error);

src/federation/middleware.test.ts

+39-18
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
} from "../testing/keys.ts";
3030
import { test } from "../testing/mod.ts";
3131
import { lookupObject } from "../vocab/lookup.ts";
32+
import { getTypeId } from "../vocab/type.ts";
3233
import {
3334
Activity,
3435
Create,
@@ -1217,12 +1218,18 @@ test("InboxContextImpl.forwardActivity()", async (t) => {
12171218
"id": "https://example.com/activity",
12181219
"actor": "https://example.com/person2",
12191220
};
1220-
const ctx = new InboxContextImpl(null, activity, {
1221-
data: undefined,
1222-
federation,
1223-
url: new URL("https://example.com/"),
1224-
documentLoader: fetchDocumentLoader,
1225-
});
1221+
const ctx = new InboxContextImpl(
1222+
null,
1223+
activity,
1224+
"https://example.com/activity",
1225+
"https://www.w3.org/ns/activitystreams#Create",
1226+
{
1227+
data: undefined,
1228+
federation,
1229+
url: new URL("https://example.com/"),
1230+
documentLoader: fetchDocumentLoader,
1231+
},
1232+
);
12261233
await ctx.forwardActivity(
12271234
[{ privateKey: rsaPrivateKey2, keyId: rsaPublicKey2.id! }],
12281235
{
@@ -1241,12 +1248,18 @@ test("InboxContextImpl.forwardActivity()", async (t) => {
12411248
"id": "https://example.com/activity",
12421249
"actor": "https://example.com/person2",
12431250
};
1244-
const ctx = new InboxContextImpl(null, activity, {
1245-
data: undefined,
1246-
federation,
1247-
url: new URL("https://example.com/"),
1248-
documentLoader: fetchDocumentLoader,
1249-
});
1251+
const ctx = new InboxContextImpl(
1252+
null,
1253+
activity,
1254+
"https://example.com/activity",
1255+
"https://www.w3.org/ns/activitystreams#Create",
1256+
{
1257+
data: undefined,
1258+
federation,
1259+
url: new URL("https://example.com/"),
1260+
documentLoader: fetchDocumentLoader,
1261+
},
1262+
);
12501263
await assertRejects(() =>
12511264
ctx.forwardActivity(
12521265
[{ privateKey: rsaPrivateKey2, keyId: rsaPublicKey2.id! }],
@@ -1272,6 +1285,8 @@ test("InboxContextImpl.forwardActivity()", async (t) => {
12721285
const ctx = new InboxContextImpl(
12731286
null,
12741287
await activity.toJsonLd({ contextLoader: mockDocumentLoader }),
1288+
activity.id?.href,
1289+
getTypeId(activity).href,
12751290
{
12761291
data: undefined,
12771292
federation,
@@ -1302,12 +1317,18 @@ test("InboxContextImpl.forwardActivity()", async (t) => {
13021317
rsaPublicKey3.id!,
13031318
{ contextLoader: mockDocumentLoader },
13041319
);
1305-
const ctx = new InboxContextImpl(null, activity, {
1306-
data: undefined,
1307-
federation,
1308-
url: new URL("https://example.com/"),
1309-
documentLoader: fetchDocumentLoader,
1310-
});
1320+
const ctx = new InboxContextImpl(
1321+
null,
1322+
activity,
1323+
"https://example.com/activity",
1324+
"https://www.w3.org/ns/activitystreams#Create",
1325+
{
1326+
data: undefined,
1327+
federation,
1328+
url: new URL("https://example.com/"),
1329+
documentLoader: fetchDocumentLoader,
1330+
},
1331+
);
13111332
await ctx.forwardActivity(
13121333
[{ privateKey: rsaPrivateKey2, keyId: rsaPublicKey2.id! }],
13131334
{

src/federation/middleware.ts

+31-21
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,9 @@ export class FederationImpl<TContextData> implements Federation<TContextData> {
534534
keys,
535535
activity: message.activity,
536536
activityId: message.activityId,
537+
activityType: message.activityType,
537538
inbox: new URL(message.inbox),
539+
sharedInbox: message.sharedInbox,
538540
headers: new Headers(message.headers),
539541
tracerProvider: this.tracerProvider,
540542
});
@@ -647,7 +649,12 @@ export class FederationImpl<TContextData> implements Federation<TContextData> {
647649
}
648650
try {
649651
await listener(
650-
context.toInboxContext(message.identifier, message.activity),
652+
context.toInboxContext(
653+
message.identifier,
654+
message.activity,
655+
activity.id?.href,
656+
getTypeId(activity).href,
657+
),
651658
activity,
652659
);
653660
} catch (error) {
@@ -1941,12 +1948,14 @@ export class FederationImpl<TContextData> implements Federation<TContextData> {
19411948
keys,
19421949
activity: jsonLd,
19431950
activityId: activity.id?.href,
1951+
activityType: getTypeId(activity).href,
19441952
inbox: new URL(inbox),
1953+
sharedInbox: inboxes[inbox].sharedInbox,
19451954
headers: collectionSync == null ? undefined : new Headers({
19461955
"Collection-Synchronization":
19471956
await buildCollectionSynchronizationHeader(
19481957
collectionSync,
1949-
inboxes[inbox],
1958+
inboxes[inbox].actorIds,
19501959
),
19511960
}),
19521961
tracerProvider: this.tracerProvider,
@@ -1973,14 +1982,16 @@ export class FederationImpl<TContextData> implements Federation<TContextData> {
19731982
keys: keyJwkPairs,
19741983
activity: jsonLd,
19751984
activityId: activity.id?.href,
1985+
activityType: getTypeId(activity).href,
19761986
inbox,
1987+
sharedInbox: inboxes[inbox].sharedInbox,
19771988
started: new Date().toISOString(),
19781989
attempt: 0,
19791990
headers: collectionSync == null ? {} : {
19801991
"Collection-Synchronization":
19811992
await buildCollectionSynchronizationHeader(
19821993
collectionSync,
1983-
inboxes[inbox],
1994+
inboxes[inbox].actorIds,
19841995
),
19851996
},
19861997
};
@@ -2301,8 +2312,10 @@ export class ContextImpl<TContextData> implements Context<TContextData> {
23012312
toInboxContext(
23022313
recipient: string | null,
23032314
activity: unknown,
2315+
activityId: string | undefined,
2316+
activityType: string,
23042317
): InboxContextImpl<TContextData> {
2305-
return new InboxContextImpl(recipient, activity, {
2318+
return new InboxContextImpl(recipient, activity, activityId, activityType, {
23062319
url: this.url,
23072320
federation: this.federation,
23082321
data: this.data,
@@ -3028,15 +3041,21 @@ export class InboxContextImpl<TContextData> extends ContextImpl<TContextData>
30283041
implements InboxContext<TContextData> {
30293042
readonly recipient: string | null;
30303043
readonly activity: unknown;
3044+
readonly activityId?: string;
3045+
readonly activityType: string;
30313046

30323047
constructor(
30333048
recipient: string | null,
30343049
activity: unknown,
3050+
activityId: string | undefined,
3051+
activityType: string,
30353052
options: ContextOptions<TContextData>,
30363053
) {
30373054
super(options);
30383055
this.recipient = recipient;
30393056
this.activity = activity;
3057+
this.activityId = activityId;
3058+
this.activityType = activityType;
30403059
}
30413060

30423061
forwardActivity(
@@ -3119,12 +3138,10 @@ export class InboxContextImpl<TContextData> extends ContextImpl<TContextData>
31193138
} else {
31203139
keys = [forwarder];
31213140
}
3122-
let activityId: string | undefined = undefined;
31233141
if (!hasSignature(this.activity)) {
31243142
let hasProof: boolean;
31253143
try {
31263144
const activity = await Activity.fromJsonLd(this.activity, this);
3127-
activityId = activity.id?.href;
31283145
hasProof = await activity.getProof() != null;
31293146
} catch {
31303147
hasProof = false;
@@ -3138,17 +3155,6 @@ export class InboxContextImpl<TContextData> extends ContextImpl<TContextData>
31383155
);
31393156
}
31403157
}
3141-
if (
3142-
activityId == null && typeof this.activity === "object" &&
3143-
this.activity != null
3144-
) {
3145-
activityId =
3146-
"@id" in this.activity && typeof this.activity["@id"] === "string"
3147-
? this.activity["@id"]
3148-
: "id" in this.activity && typeof this.activity.id === "string"
3149-
? this.activity.id
3150-
: undefined;
3151-
}
31523158
if (recipients === "followers") {
31533159
if (identifier == null) {
31543160
throw new Error(
@@ -3169,7 +3175,7 @@ export class InboxContextImpl<TContextData> extends ContextImpl<TContextData>
31693175
});
31703176
logger.debug("Forwarding activity {activityId} to inboxes:\n{inboxes}", {
31713177
inboxes: globalThis.Object.keys(inboxes),
3172-
activityId,
3178+
activityId: this.activityId,
31733179
activity: this.activity,
31743180
});
31753181
if (options?.immediate || this.federation.outboxQueue == null) {
@@ -3190,8 +3196,10 @@ export class InboxContextImpl<TContextData> extends ContextImpl<TContextData>
31903196
sendActivity({
31913197
keys,
31923198
activity: this.activity,
3193-
activityId: activityId,
3199+
activityId: this.activityId,
3200+
activityType: this.activityType,
31943201
inbox: new URL(inbox),
3202+
sharedInbox: inboxes[inbox].sharedInbox,
31953203
tracerProvider: this.tracerProvider,
31963204
}),
31973205
);
@@ -3201,7 +3209,7 @@ export class InboxContextImpl<TContextData> extends ContextImpl<TContextData>
32013209
}
32023210
logger.debug(
32033211
"Enqueuing activity {activityId} to forward later.",
3204-
{ activityId, activity: this.activity },
3212+
{ activityId: this.activityId, activity: this.activity },
32053213
);
32063214
const keyJwkPairs: SenderKeyJwkPair[] = [];
32073215
for (const { keyId, privateKey } of keys) {
@@ -3214,8 +3222,10 @@ export class InboxContextImpl<TContextData> extends ContextImpl<TContextData>
32143222
id: crypto.randomUUID(),
32153223
keys: keyJwkPairs,
32163224
activity: this.activity,
3217-
activityId,
3225+
activityId: this.activityId,
3226+
activityType: this.activityType,
32183227
inbox,
3228+
sharedInbox: inboxes[inbox].sharedInbox,
32193229
started: new Date().toISOString(),
32203230
attempt: 0,
32213231
headers: {},

src/federation/queue.ts

+2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ export interface OutboxMessage {
1111
keys: SenderKeyJwkPair[];
1212
activity: unknown;
1313
activityId?: string;
14+
activityType: string;
1415
inbox: string;
16+
sharedInbox: boolean;
1517
started: string;
1618
attempt: number;
1719
headers: Record<string, string>;

0 commit comments

Comments
 (0)