@fedify/vocab 2.0.0-dev.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (167) hide show
  1. package/LICENSE +20 -0
  2. package/deno.json +31 -0
  3. package/dist/accept.yaml +15 -0
  4. package/dist/activity.yaml +98 -0
  5. package/dist/actor.test.d.ts +2 -0
  6. package/dist/actor.test.js +6095 -0
  7. package/dist/add.yaml +16 -0
  8. package/dist/announce.yaml +30 -0
  9. package/dist/application.yaml +324 -0
  10. package/dist/arrive.yaml +15 -0
  11. package/dist/article.yaml +46 -0
  12. package/dist/audio.yaml +11 -0
  13. package/dist/block.yaml +16 -0
  14. package/dist/chatmessage.yaml +50 -0
  15. package/dist/collection.yaml +154 -0
  16. package/dist/collectionpage.yaml +55 -0
  17. package/dist/create.yaml +28 -0
  18. package/dist/dataintegrityproof.yaml +56 -0
  19. package/dist/delete.yaml +27 -0
  20. package/dist/deno-B-ypIMwF.js +1282 -0
  21. package/dist/didservice.yaml +22 -0
  22. package/dist/dislike.yaml +14 -0
  23. package/dist/document.yaml +31 -0
  24. package/dist/emoji.yaml +12 -0
  25. package/dist/emojireact.yaml +17 -0
  26. package/dist/endpoints.yaml +85 -0
  27. package/dist/event.yaml +11 -0
  28. package/dist/export.yaml +9 -0
  29. package/dist/flag.yaml +15 -0
  30. package/dist/follow.yaml +19 -0
  31. package/dist/group.yaml +324 -0
  32. package/dist/hashtag.yaml +14 -0
  33. package/dist/ignore.yaml +14 -0
  34. package/dist/image.yaml +9 -0
  35. package/dist/intransitiveactivity.yaml +15 -0
  36. package/dist/invite.yaml +14 -0
  37. package/dist/join.yaml +14 -0
  38. package/dist/key.yaml +28 -0
  39. package/dist/leave.yaml +14 -0
  40. package/dist/like.yaml +16 -0
  41. package/dist/link.yaml +101 -0
  42. package/dist/listen.yaml +12 -0
  43. package/dist/lookup.test.d.ts +2 -0
  44. package/dist/lookup.test.js +690 -0
  45. package/dist/mention.yaml +9 -0
  46. package/dist/mod.cjs +42036 -0
  47. package/dist/mod.d.cts +15329 -0
  48. package/dist/mod.d.ts +15330 -0
  49. package/dist/mod.js +41936 -0
  50. package/dist/move.yaml +15 -0
  51. package/dist/multikey.yaml +36 -0
  52. package/dist/note.yaml +48 -0
  53. package/dist/object.yaml +404 -0
  54. package/dist/offer.yaml +15 -0
  55. package/dist/orderedcollection.yaml +39 -0
  56. package/dist/orderedcollectionpage.yaml +50 -0
  57. package/dist/organization.yaml +324 -0
  58. package/dist/page.yaml +11 -0
  59. package/dist/person.yaml +324 -0
  60. package/dist/place.yaml +75 -0
  61. package/dist/profile.yaml +26 -0
  62. package/dist/propertyvalue.yaml +32 -0
  63. package/dist/question.yaml +103 -0
  64. package/dist/read.yaml +13 -0
  65. package/dist/reject.yaml +14 -0
  66. package/dist/relationship.yaml +52 -0
  67. package/dist/remove.yaml +14 -0
  68. package/dist/service.yaml +324 -0
  69. package/dist/source.yaml +26 -0
  70. package/dist/tentativeaccept.yaml +14 -0
  71. package/dist/tentativereject.yaml +14 -0
  72. package/dist/tombstone.yaml +24 -0
  73. package/dist/travel.yaml +16 -0
  74. package/dist/type-CNuABalk.js +13 -0
  75. package/dist/type.test.d.ts +2 -0
  76. package/dist/type.test.js +24 -0
  77. package/dist/undo.yaml +26 -0
  78. package/dist/update.yaml +58 -0
  79. package/dist/utils-BSWXlrig.js +13 -0
  80. package/dist/video.yaml +11 -0
  81. package/dist/view.yaml +13 -0
  82. package/dist/vocab-DBispxj5.js +41603 -0
  83. package/dist/vocab.test.d.ts +2 -0
  84. package/dist/vocab.test.js +1304 -0
  85. package/package.json +79 -0
  86. package/scripts/codegen.ts +20 -0
  87. package/src/__snapshots__/vocab.test.ts.snap +7903 -0
  88. package/src/accept.yaml +15 -0
  89. package/src/activity.yaml +98 -0
  90. package/src/actor.test.ts +263 -0
  91. package/src/actor.ts +293 -0
  92. package/src/add.yaml +16 -0
  93. package/src/announce.yaml +30 -0
  94. package/src/application.yaml +324 -0
  95. package/src/arrive.yaml +15 -0
  96. package/src/article.yaml +46 -0
  97. package/src/audio.yaml +11 -0
  98. package/src/block.yaml +16 -0
  99. package/src/chatmessage.yaml +50 -0
  100. package/src/collection.yaml +154 -0
  101. package/src/collectionpage.yaml +55 -0
  102. package/src/constants.ts +11 -0
  103. package/src/create.yaml +28 -0
  104. package/src/dataintegrityproof.yaml +56 -0
  105. package/src/delete.yaml +27 -0
  106. package/src/didservice.yaml +22 -0
  107. package/src/dislike.yaml +14 -0
  108. package/src/document.yaml +31 -0
  109. package/src/emoji.yaml +12 -0
  110. package/src/emojireact.yaml +17 -0
  111. package/src/endpoints.yaml +85 -0
  112. package/src/event.yaml +11 -0
  113. package/src/export.yaml +9 -0
  114. package/src/flag.yaml +15 -0
  115. package/src/follow.yaml +19 -0
  116. package/src/group.yaml +324 -0
  117. package/src/handle.ts +104 -0
  118. package/src/hashtag.yaml +14 -0
  119. package/src/ignore.yaml +14 -0
  120. package/src/image.yaml +9 -0
  121. package/src/intransitiveactivity.yaml +15 -0
  122. package/src/invite.yaml +14 -0
  123. package/src/join.yaml +14 -0
  124. package/src/key.yaml +28 -0
  125. package/src/keys.ts +50 -0
  126. package/src/leave.yaml +14 -0
  127. package/src/like.yaml +16 -0
  128. package/src/link.yaml +101 -0
  129. package/src/listen.yaml +12 -0
  130. package/src/lookup.test.ts +681 -0
  131. package/src/lookup.ts +318 -0
  132. package/src/mention.yaml +9 -0
  133. package/src/mod.ts +57 -0
  134. package/src/move.yaml +15 -0
  135. package/src/multikey.yaml +36 -0
  136. package/src/note.yaml +48 -0
  137. package/src/object.yaml +404 -0
  138. package/src/offer.yaml +15 -0
  139. package/src/orderedcollection.yaml +39 -0
  140. package/src/orderedcollectionpage.yaml +50 -0
  141. package/src/organization.yaml +324 -0
  142. package/src/page.yaml +11 -0
  143. package/src/person.yaml +324 -0
  144. package/src/place.yaml +75 -0
  145. package/src/profile.yaml +26 -0
  146. package/src/propertyvalue.yaml +32 -0
  147. package/src/question.yaml +103 -0
  148. package/src/read.yaml +13 -0
  149. package/src/reject.yaml +14 -0
  150. package/src/relationship.yaml +52 -0
  151. package/src/remove.yaml +14 -0
  152. package/src/service.yaml +324 -0
  153. package/src/source.yaml +26 -0
  154. package/src/tentativeaccept.yaml +14 -0
  155. package/src/tentativereject.yaml +14 -0
  156. package/src/tombstone.yaml +24 -0
  157. package/src/travel.yaml +16 -0
  158. package/src/type.test.ts +20 -0
  159. package/src/type.ts +102 -0
  160. package/src/undo.yaml +26 -0
  161. package/src/update.yaml +58 -0
  162. package/src/utils.ts +9 -0
  163. package/src/video.yaml +11 -0
  164. package/src/view.yaml +13 -0
  165. package/src/vocab.bench.ts +204 -0
  166. package/src/vocab.test.ts +2014 -0
  167. package/tsdown.config.ts +65 -0
@@ -0,0 +1,15 @@
1
+ $schema: ../../vocab-tools/schema.yaml
2
+ name: Accept
3
+ compactName: Accept
4
+ uri: "https://www.w3.org/ns/activitystreams#Accept"
5
+ extends: "https://www.w3.org/ns/activitystreams#Activity"
6
+ entity: true
7
+ description: |
8
+ Indicates that the `actor` accepts the `object`. The `target` property can be
9
+ used in certain circumstances to indicate the context into which the `object`
10
+ has been accepted.
11
+ defaultContext:
12
+ - "https://w3id.org/identity/v1"
13
+ - "https://www.w3.org/ns/activitystreams"
14
+ - "https://w3id.org/security/data-integrity/v1"
15
+ properties: []
@@ -0,0 +1,98 @@
1
+ $schema: ../../vocab-tools/schema.yaml
2
+ name: Activity
3
+ compactName: Activity
4
+ uri: "https://www.w3.org/ns/activitystreams#Activity"
5
+ extends: "https://www.w3.org/ns/activitystreams#Object"
6
+ entity: true
7
+ description: |
8
+ An Activity is a subtype of {@link Object} that describes some form of action
9
+ that may happen, is currently happening, or has already happened.
10
+ The {@link Activity} type itself serves as an abstract base type for all types
11
+ of activities. It is important to note that the {@link Activity} type itself
12
+ does not carry any specific semantics about the kind of action being taken.
13
+ defaultContext:
14
+ - "https://w3id.org/identity/v1"
15
+ - "https://www.w3.org/ns/activitystreams"
16
+ - "https://w3id.org/security/v1"
17
+ - "https://w3id.org/security/data-integrity/v1"
18
+
19
+ properties:
20
+ - pluralName: actors
21
+ singularName: actor
22
+ singularAccessor: true
23
+ compactName: actor
24
+ uri: "https://www.w3.org/ns/activitystreams#actor"
25
+ subpropertyOf: "https://www.w3.org/ns/activitystreams#attributedTo"
26
+ description: |
27
+ Describes one or more entities that either performed or are expected to
28
+ perform the activity. Any single activity can have multiple actors.
29
+ The actor MAY be specified using an indirect {@link Link}.
30
+ range:
31
+ - "https://www.w3.org/ns/activitystreams#Application"
32
+ - "https://www.w3.org/ns/activitystreams#Group"
33
+ - "https://www.w3.org/ns/activitystreams#Organization"
34
+ - "https://www.w3.org/ns/activitystreams#Person"
35
+ - "https://www.w3.org/ns/activitystreams#Service"
36
+
37
+ - pluralName: objects
38
+ singularName: object
39
+ singularAccessor: true
40
+ compactName: object
41
+ uri: "https://www.w3.org/ns/activitystreams#object"
42
+ description: |
43
+ When used within an {@link Activity}, describes the direct object of
44
+ the activity. For instance, in the activity "John added a movie to his
45
+ wishlist", the object of the activity is the movie added.
46
+ range:
47
+ - "https://www.w3.org/ns/activitystreams#Object"
48
+
49
+ - pluralName: targets
50
+ singularName: target
51
+ singularAccessor: true
52
+ compactName: target
53
+ uri: "https://www.w3.org/ns/activitystreams#target"
54
+ description: |
55
+ Describes the indirect object, or target, of the activity. The precise
56
+ meaning of the target is largely dependent on the type of action being
57
+ described but will often be the object of the English preposition "to".
58
+ For instance, in the activity "John added a movie to his wishlist",
59
+ the target of the activity is John's wishlist. An activity can have more
60
+ than one target.
61
+ range:
62
+ - "https://www.w3.org/ns/activitystreams#Object"
63
+
64
+ - pluralName: results
65
+ singularName: result
66
+ singularAccessor: true
67
+ compactName: result
68
+ uri: "https://www.w3.org/ns/activitystreams#result"
69
+ description: |
70
+ Describes the result of the activity. For instance, if a particular action
71
+ results in the creation of a new resource, the result property can be used
72
+ to describe that new resource.
73
+ range:
74
+ - "https://www.w3.org/ns/activitystreams#Object"
75
+
76
+ - pluralName: origins
77
+ singularName: origin
78
+ singularAccessor: true
79
+ compactName: origin
80
+ uri: "https://www.w3.org/ns/activitystreams#origin"
81
+ description: |
82
+ Describes an indirect object of the activity from which the activity is
83
+ directed. The precise meaning of the origin is the object of the English
84
+ preposition "from". For instance, in the activity "John moved an item to
85
+ List B from List A", the origin of the activity is "List A".
86
+ range:
87
+ - "https://www.w3.org/ns/activitystreams#Object"
88
+
89
+ - pluralName: instruments
90
+ singularName: instrument
91
+ singularAccessor: true
92
+ compactName: instrument
93
+ uri: "https://www.w3.org/ns/activitystreams#instrument"
94
+ description: |
95
+ Identifies one or more objects used (or to be used) in the completion of
96
+ an {@link Activity}.
97
+ range:
98
+ - "https://www.w3.org/ns/activitystreams#Object"
@@ -0,0 +1,263 @@
1
+ import { test } from "@fedify/fixture";
2
+ import * as fc from "fast-check";
3
+ import fetchMock from "fetch-mock";
4
+ import {
5
+ deepStrictEqual,
6
+ ok,
7
+ rejects,
8
+ strictEqual,
9
+ throws,
10
+ } from "node:assert/strict";
11
+ import {
12
+ type Actor,
13
+ getActorClassByTypeName,
14
+ getActorHandle,
15
+ getActorTypeName,
16
+ isActor,
17
+ normalizeActorHandle,
18
+ } from "./actor.ts";
19
+ import { Application, Group, Organization, Person, Service } from "./vocab.ts";
20
+
21
+ function actorClass(): fc.Arbitrary<
22
+ | typeof Application
23
+ | typeof Group
24
+ | typeof Organization
25
+ | typeof Person
26
+ | typeof Service
27
+ > {
28
+ return fc.constantFrom(Application, Group, Organization, Person, Service);
29
+ }
30
+
31
+ function actorClassAndInstance(): fc.Arbitrary<
32
+ | [typeof Application, Application]
33
+ | [typeof Group, Group]
34
+ | [typeof Organization, Organization]
35
+ | [typeof Person, Person]
36
+ | [typeof Service, Service]
37
+ > {
38
+ return actorClass().map((cls) =>
39
+ [cls, new cls({})] as (
40
+ | [typeof Application, Application]
41
+ | [typeof Group, Group]
42
+ | [typeof Organization, Organization]
43
+ | [typeof Person, Person]
44
+ | [typeof Service, Service]
45
+ )
46
+ );
47
+ }
48
+
49
+ function actor(): fc.Arbitrary<Actor> {
50
+ return actorClassAndInstance().map(([, instance]) => instance);
51
+ }
52
+
53
+ test("isActor()", () => {
54
+ fc.assert(fc.property(actor(), (actor) => ok(isActor(actor))));
55
+ fc.assert(
56
+ fc.property(
57
+ fc.anything({
58
+ withBigInt: true,
59
+ withBoxedValues: true,
60
+ withDate: true,
61
+ withMap: true,
62
+ withNullPrototype: true,
63
+ withObjectString: true,
64
+ withSet: true,
65
+ withTypedArray: true,
66
+ withSparseArray: true,
67
+ }),
68
+ (nonActor) => ok(!isActor(nonActor)),
69
+ ),
70
+ );
71
+ });
72
+
73
+ test("getActorTypeName()", () => {
74
+ fc.assert(
75
+ fc.property(
76
+ actorClassAndInstance(),
77
+ ([cls, instance]) =>
78
+ deepStrictEqual(getActorTypeName(instance), cls.name),
79
+ ),
80
+ );
81
+ });
82
+
83
+ test("getActorClassByTypeName()", () => {
84
+ fc.assert(
85
+ fc.property(
86
+ actorClassAndInstance(),
87
+ ([cls, instance]) =>
88
+ strictEqual(
89
+ getActorClassByTypeName(getActorTypeName(instance)),
90
+ cls,
91
+ ),
92
+ ),
93
+ );
94
+ });
95
+
96
+ test({
97
+ name: "getActorHandle()",
98
+ permissions: { env: true, read: true },
99
+ async fn(t) {
100
+ fetchMock.spyGlobal();
101
+
102
+ fetchMock.get(
103
+ "begin:https://foo.example.com/.well-known/webfinger?",
104
+ {
105
+ body: { subject: "acct:johndoe@foo.example.com" },
106
+ headers: { "Content-Type": "application/jrd+json" },
107
+ },
108
+ );
109
+
110
+ const actorId = new URL("https://foo.example.com/@john");
111
+ const actor = new Person({
112
+ id: actorId,
113
+ preferredUsername: "john",
114
+ });
115
+
116
+ await t.step("WebFinger subject", async () => {
117
+ deepStrictEqual(await getActorHandle(actor), "@johndoe@foo.example.com");
118
+ deepStrictEqual(
119
+ await getActorHandle(actor, { trimLeadingAt: true }),
120
+ "johndoe@foo.example.com",
121
+ );
122
+ deepStrictEqual(
123
+ await getActorHandle(actorId),
124
+ "@johndoe@foo.example.com",
125
+ );
126
+ deepStrictEqual(
127
+ await getActorHandle(actorId, { trimLeadingAt: true }),
128
+ "johndoe@foo.example.com",
129
+ );
130
+ });
131
+
132
+ fetchMock.removeRoutes();
133
+ fetchMock.get(
134
+ "begin:https://foo.example.com/.well-known/webfinger?",
135
+ {
136
+ body: {
137
+ subject: "https://foo.example.com/@john",
138
+ aliases: [
139
+ "acct:john@bar.example.com",
140
+ "acct:johndoe@foo.example.com",
141
+ ],
142
+ },
143
+ headers: { "Content-Type": "application/jrd+json" },
144
+ },
145
+ );
146
+
147
+ await t.step("WebFinger aliases", async () => {
148
+ deepStrictEqual(await getActorHandle(actor), "@johndoe@foo.example.com");
149
+ deepStrictEqual(
150
+ await getActorHandle(actor, { trimLeadingAt: true }),
151
+ "johndoe@foo.example.com",
152
+ );
153
+ deepStrictEqual(
154
+ await getActorHandle(actorId),
155
+ "@johndoe@foo.example.com",
156
+ );
157
+ deepStrictEqual(
158
+ await getActorHandle(actorId, { trimLeadingAt: true }),
159
+ "johndoe@foo.example.com",
160
+ );
161
+ });
162
+
163
+ fetchMock.get(
164
+ "begin:https://bar.example.com/.well-known/webfinger?",
165
+ {
166
+ body: {
167
+ subject: "acct:john@bar.example.com",
168
+ aliases: [
169
+ "https://foo.example.com/@john",
170
+ ],
171
+ },
172
+ headers: { "Content-Type": "application/jrd+json" },
173
+ },
174
+ );
175
+
176
+ await t.step("cross-origin WebFinger resources", async () => {
177
+ deepStrictEqual(await getActorHandle(actor), "@john@bar.example.com");
178
+ });
179
+
180
+ fetchMock.removeRoutes();
181
+ fetchMock.get(
182
+ "begin:https://foo.example.com/.well-known/webfinger?",
183
+ { status: 404 },
184
+ );
185
+
186
+ await t.step("no WebFinger", async () => {
187
+ deepStrictEqual(await getActorHandle(actor), "@john@foo.example.com");
188
+ rejects(() => getActorHandle(actorId), TypeError);
189
+ });
190
+
191
+ fetchMock.hardReset();
192
+ },
193
+ });
194
+
195
+ test("normalizeActorHandle()", () => {
196
+ deepStrictEqual(normalizeActorHandle("@foo@BAR.COM"), "@foo@bar.com");
197
+ deepStrictEqual(normalizeActorHandle("@BAZ@☃-⌘.com"), "@BAZ@☃-⌘.com");
198
+ deepStrictEqual(
199
+ normalizeActorHandle("@qux@xn--maana-pta.com"),
200
+ "@qux@mañana.com",
201
+ );
202
+ deepStrictEqual(
203
+ normalizeActorHandle("@quux@XN--MAANA-PTA.COM"),
204
+ "@quux@mañana.com",
205
+ );
206
+ deepStrictEqual(
207
+ normalizeActorHandle("@quux@MAÑANA.COM"),
208
+ "@quux@mañana.com",
209
+ );
210
+
211
+ deepStrictEqual(
212
+ normalizeActorHandle("@foo@BAR.COM", { trimLeadingAt: true }),
213
+ "foo@bar.com",
214
+ );
215
+ deepStrictEqual(
216
+ normalizeActorHandle("@BAZ@☃-⌘.com", { trimLeadingAt: true }),
217
+ "BAZ@☃-⌘.com",
218
+ );
219
+ deepStrictEqual(
220
+ normalizeActorHandle("@qux@xn--maana-pta.com", { trimLeadingAt: true }),
221
+ "qux@mañana.com",
222
+ );
223
+ deepStrictEqual(
224
+ normalizeActorHandle("@quux@XN--MAANA-PTA.COM", { trimLeadingAt: true }),
225
+ "quux@mañana.com",
226
+ );
227
+ deepStrictEqual(
228
+ normalizeActorHandle("@quux@MAÑANA.COM", { trimLeadingAt: true }),
229
+ "quux@mañana.com",
230
+ );
231
+
232
+ deepStrictEqual(
233
+ normalizeActorHandle("@foo@BAR.COM", { punycode: true }),
234
+ "@foo@bar.com",
235
+ );
236
+ deepStrictEqual(
237
+ normalizeActorHandle("@BAZ@☃-⌘.com", { punycode: true }),
238
+ "@BAZ@xn----dqo34k.com",
239
+ );
240
+ deepStrictEqual(
241
+ normalizeActorHandle("@qux@xn--maana-pta.com", { punycode: true }),
242
+ "@qux@xn--maana-pta.com",
243
+ );
244
+ deepStrictEqual(
245
+ normalizeActorHandle("@quux@XN--MAANA-PTA.COM", { punycode: true }),
246
+ "@quux@xn--maana-pta.com",
247
+ );
248
+ deepStrictEqual(
249
+ normalizeActorHandle("@quux@MAÑANA.COM", { punycode: true }),
250
+ "@quux@xn--maana-pta.com",
251
+ );
252
+
253
+ throws(() => normalizeActorHandle(""));
254
+ throws(() => normalizeActorHandle("@"));
255
+ throws(() => normalizeActorHandle("foo"));
256
+ throws(() => normalizeActorHandle("@foo"));
257
+ throws(() => normalizeActorHandle("@@foo.com"));
258
+ throws(() => normalizeActorHandle("@foo@"));
259
+ throws(() => normalizeActorHandle("foo@bar.com@baz.com"));
260
+ throws(() => normalizeActorHandle("@foo@bar.com@baz.com"));
261
+ });
262
+
263
+ // cSpell: ignore maana
package/src/actor.ts ADDED
@@ -0,0 +1,293 @@
1
+ import type { GetUserAgentOptions } from "@fedify/vocab-runtime";
2
+ import { lookupWebFinger } from "@fedify/webfinger";
3
+ import { SpanStatusCode, trace, type TracerProvider } from "@opentelemetry/api";
4
+ import { domainToASCII, domainToUnicode } from "node:url";
5
+ import metadata from "../deno.json" with { type: "json" };
6
+ import { getTypeId } from "./type.ts";
7
+ import { Application, Group, Organization, Person, Service } from "./vocab.ts";
8
+
9
+ /**
10
+ * Actor types are {@link Object} types that are capable of performing
11
+ * activities.
12
+ */
13
+ export type Actor = Application | Group | Organization | Person | Service;
14
+
15
+ /**
16
+ * Checks if the given object is an {@link Actor}.
17
+ * @param object The object to check.
18
+ * @returns `true` if the given object is an {@link Actor}.
19
+ */
20
+ export function isActor(object: unknown): object is Actor {
21
+ return (
22
+ object instanceof Application ||
23
+ object instanceof Group ||
24
+ object instanceof Organization ||
25
+ object instanceof Person ||
26
+ object instanceof Service
27
+ );
28
+ }
29
+
30
+ /**
31
+ * A string representation of an actor type name.
32
+ */
33
+ export type ActorTypeName =
34
+ | "Application"
35
+ | "Group"
36
+ | "Organization"
37
+ | "Person"
38
+ | "Service";
39
+
40
+ /**
41
+ * Gets the type name of the given actor.
42
+ * @param actor The actor to get the type name of.
43
+ * @returns The type name of the given actor.
44
+ */
45
+ export function getActorTypeName(
46
+ actor: Actor,
47
+ ): ActorTypeName {
48
+ if (actor instanceof Application) return "Application";
49
+ else if (actor instanceof Group) return "Group";
50
+ else if (actor instanceof Organization) return "Organization";
51
+ else if (actor instanceof Person) return "Person";
52
+ else if (actor instanceof Service) return "Service";
53
+ throw new Error("Unknown actor type.");
54
+ }
55
+
56
+ /**
57
+ * Gets the actor class by the given type name.
58
+ * @param typeName The type name to get the actor class by.
59
+ * @returns The actor class by the given type name.
60
+ */
61
+ export function getActorClassByTypeName(
62
+ typeName: ActorTypeName,
63
+ ):
64
+ | typeof Application
65
+ | typeof Group
66
+ | typeof Organization
67
+ | typeof Person
68
+ | typeof Service {
69
+ switch (typeName) {
70
+ case "Application":
71
+ return Application;
72
+ case "Group":
73
+ return Group;
74
+ case "Organization":
75
+ return Organization;
76
+ case "Person":
77
+ return Person;
78
+ case "Service":
79
+ return Service;
80
+ }
81
+ throw new Error("Unknown actor type name.");
82
+ }
83
+
84
+ /**
85
+ * Options for {@link getActorHandle}.
86
+ * @since 1.3.0
87
+ */
88
+ export interface GetActorHandleOptions extends NormalizeActorHandleOptions {
89
+ /**
90
+ * The options for making `User-Agent` header.
91
+ * If a string is given, it is used as the `User-Agent` header value.
92
+ * If an object is given, it is passed to {@link getUserAgent} to generate
93
+ * the `User-Agent` header value.
94
+ * @since 1.3.0
95
+ */
96
+ userAgent?: GetUserAgentOptions | string;
97
+
98
+ /**
99
+ * The OpenTelemetry tracer provider. If omitted, the global tracer provider
100
+ * is used.
101
+ * @since 1.3.0
102
+ */
103
+ tracerProvider?: TracerProvider;
104
+ }
105
+
106
+ /**
107
+ * Gets the actor handle, of the form `@username@domain`, from the given actor
108
+ * or an actor URI.
109
+ *
110
+ * @example
111
+ * ``` typescript
112
+ * // Get the handle of an actor object:
113
+ * await getActorHandle(
114
+ * new Person({ id: new URL("https://fosstodon.org/users/hongminhee") })
115
+ * );
116
+ *
117
+ * // Get the handle of an actor URI:
118
+ * await getActorHandle(new URL("https://fosstodon.org/users/hongminhee"));
119
+ * ```
120
+ *
121
+ * @param actor The actor or actor URI to get the handle from.
122
+ * @param options The extra options for getting the actor handle.
123
+ * @returns The actor handle. It starts with `@` and is followed by the
124
+ * username and domain, separated by `@` by default (it can be
125
+ * customized with the options).
126
+ * @throws {TypeError} If the actor does not have enough information to get the
127
+ * handle.
128
+ * @since 0.4.0
129
+ */
130
+ export async function getActorHandle(
131
+ actor: Actor | URL,
132
+ options: GetActorHandleOptions = {},
133
+ ): Promise<`@${string}@${string}` | `${string}@${string}`> {
134
+ const tracerProvider = options.tracerProvider ?? trace.getTracerProvider();
135
+ const tracer = tracerProvider.getTracer(
136
+ metadata.name,
137
+ metadata.version,
138
+ );
139
+ return await tracer.startActiveSpan(
140
+ "activitypub.get_actor_handle",
141
+ async (span) => {
142
+ if (isActor(actor)) {
143
+ if (actor.id != null) {
144
+ span.setAttribute("activitypub.actor.id", actor.id.href);
145
+ }
146
+ span.setAttribute("activitypub.actor.type", getTypeId(actor).href);
147
+ }
148
+ try {
149
+ return await getActorHandleInternal(actor, options);
150
+ } catch (error) {
151
+ span.setStatus({
152
+ code: SpanStatusCode.ERROR,
153
+ message: String(error),
154
+ });
155
+ throw error;
156
+ } finally {
157
+ span.end();
158
+ }
159
+ },
160
+ );
161
+ }
162
+
163
+ async function getActorHandleInternal(
164
+ actor: Actor | URL,
165
+ options: GetActorHandleOptions = {},
166
+ ): Promise<`@${string}@${string}` | `${string}@${string}`> {
167
+ const actorId = actor instanceof URL ? actor : actor.id;
168
+ if (actorId != null) {
169
+ const result = await lookupWebFinger(actorId, {
170
+ userAgent: options.userAgent,
171
+ tracerProvider: options.tracerProvider,
172
+ });
173
+ if (result != null) {
174
+ const aliases = [...(result.aliases ?? [])];
175
+ if (result.subject != null) aliases.unshift(result.subject);
176
+ for (const alias of aliases) {
177
+ const match = alias.match(/^acct:([^@]+)@([^@]+)$/);
178
+ if (match != null) {
179
+ const hostname = new URL(`https://${match[2]}/`).hostname;
180
+ if (
181
+ hostname !== actorId.hostname &&
182
+ !await verifyCrossOriginActorHandle(
183
+ actorId.href,
184
+ alias,
185
+ options.userAgent,
186
+ options.tracerProvider,
187
+ )
188
+ ) {
189
+ continue;
190
+ }
191
+ return normalizeActorHandle(`@${match[1]}@${match[2]}`, options);
192
+ }
193
+ }
194
+ }
195
+ }
196
+ if (
197
+ !(actor instanceof URL) && actor.preferredUsername != null &&
198
+ actor.id != null
199
+ ) {
200
+ return normalizeActorHandle(
201
+ `@${actor.preferredUsername}@${actor.id.host}`,
202
+ options,
203
+ );
204
+ }
205
+ throw new TypeError(
206
+ "Actor does not have enough information to get the handle.",
207
+ );
208
+ }
209
+
210
+ async function verifyCrossOriginActorHandle(
211
+ actorId: string,
212
+ alias: string,
213
+ userAgent: GetUserAgentOptions | string | undefined,
214
+ tracerProvider: TracerProvider | undefined,
215
+ ): Promise<boolean> {
216
+ const response = await lookupWebFinger(alias, { userAgent, tracerProvider });
217
+ if (response == null) return false;
218
+ for (const alias of response.aliases ?? []) {
219
+ if (new URL(alias).href === actorId) return true;
220
+ }
221
+ return false;
222
+ }
223
+
224
+ /**
225
+ * Options for {@link normalizeActorHandle}.
226
+ * @since 0.9.0
227
+ */
228
+ export interface NormalizeActorHandleOptions {
229
+ /**
230
+ * Whether to trim the leading `@` from the actor handle. Turned off by
231
+ * default.
232
+ */
233
+ trimLeadingAt?: boolean;
234
+
235
+ /**
236
+ * Whether to convert the domain part of the actor handle to punycode, if it
237
+ * is an internationalized domain name. Turned off by default.
238
+ */
239
+ punycode?: boolean;
240
+ }
241
+
242
+ /**
243
+ * Normalizes the given actor handle.
244
+ * @param handle The full handle of the actor to normalize.
245
+ * @param options The options for normalizing the actor handle.
246
+ * @returns The normalized actor handle.
247
+ * @throws {TypeError} If the actor handle is invalid.
248
+ * @since 0.9.0
249
+ */
250
+ export function normalizeActorHandle(
251
+ handle: string,
252
+ options: NormalizeActorHandleOptions = {},
253
+ ): `@${string}@${string}` | `${string}@${string}` {
254
+ handle = handle.replace(/^@/, "");
255
+ const atPos = handle.indexOf("@");
256
+ if (atPos < 1) throw new TypeError("Invalid actor handle.");
257
+ let domain = handle.substring(atPos + 1);
258
+ if (domain.length < 1 || domain.includes("@")) {
259
+ throw new TypeError("Invalid actor handle.");
260
+ }
261
+ domain = domain.toLowerCase();
262
+ domain = options.punycode ? domainToASCII(domain) : domainToUnicode(domain);
263
+ domain = domain.toLowerCase();
264
+ const user = handle.substring(0, atPos);
265
+ return options.trimLeadingAt ? `${user}@${domain}` : `@${user}@${domain}`;
266
+ }
267
+
268
+ /**
269
+ * The object that can be a recipient of an activity.
270
+ *
271
+ * Note that every {@link Actor} is also a {@link Recipient}.
272
+ */
273
+ export interface Recipient {
274
+ /**
275
+ * The URI of the actor.
276
+ */
277
+ readonly id: URL | null;
278
+
279
+ /**
280
+ * The URI of the actor's inbox.
281
+ */
282
+ readonly inboxId: URL | null;
283
+
284
+ /**
285
+ * The endpoints of the actor.
286
+ */
287
+ readonly endpoints?: {
288
+ /**
289
+ * The URI of the actor's shared inbox.
290
+ */
291
+ sharedInbox: URL | null;
292
+ } | null;
293
+ }
package/src/add.yaml ADDED
@@ -0,0 +1,16 @@
1
+ $schema: ../../vocab-tools/schema.yaml
2
+ name: Add
3
+ compactName: Add
4
+ uri: "https://www.w3.org/ns/activitystreams#Add"
5
+ extends: "https://www.w3.org/ns/activitystreams#Activity"
6
+ entity: true
7
+ description: |
8
+ Indicates that the `actor` has added the `object` to the `target`.
9
+ If the `target` property is not explicitly specified, the target would need
10
+ to be determined implicitly by context. The `origin` can be used to identify
11
+ the context from which the `object` originated.
12
+ defaultContext:
13
+ - "https://w3id.org/identity/v1"
14
+ - "https://www.w3.org/ns/activitystreams"
15
+ - "https://w3id.org/security/data-integrity/v1"
16
+ properties: []