applesauce-common 6.2.0 → 7.0.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 (224) hide show
  1. package/README.md +1 -0
  2. package/dist/casts/_circular-import.d.ts +3 -0
  3. package/dist/casts/_circular-import.js +3 -0
  4. package/dist/casts/app-handler.d.ts +56 -0
  5. package/dist/casts/app-handler.js +105 -0
  6. package/dist/casts/app-recommendation.d.ts +26 -0
  7. package/dist/casts/app-recommendation.js +41 -0
  8. package/dist/casts/external-identities.d.ts +9 -0
  9. package/dist/casts/external-identities.js +14 -0
  10. package/dist/casts/forum-thread.d.ts +17 -0
  11. package/dist/casts/forum-thread.js +31 -0
  12. package/dist/casts/git-issue.d.ts +18 -0
  13. package/dist/casts/git-issue.js +26 -0
  14. package/dist/casts/git-patch.d.ts +49 -0
  15. package/dist/casts/git-patch.js +88 -0
  16. package/dist/casts/git-pull-request.d.ts +67 -0
  17. package/dist/casts/git-pull-request.js +122 -0
  18. package/dist/casts/git-repository.d.ts +9 -0
  19. package/dist/casts/git-repository.js +16 -0
  20. package/dist/casts/git-thread.d.ts +33 -0
  21. package/dist/casts/git-thread.js +65 -0
  22. package/dist/casts/index.d.ts +11 -0
  23. package/dist/casts/index.js +11 -0
  24. package/dist/casts/napplet.d.ts +98 -0
  25. package/dist/casts/napplet.js +196 -0
  26. package/dist/casts/note.d.ts +8 -0
  27. package/dist/casts/note.js +21 -1
  28. package/dist/casts/nsite.d.ts +134 -0
  29. package/dist/casts/nsite.js +249 -0
  30. package/dist/casts/profile.d.ts +20 -0
  31. package/dist/casts/profile.js +30 -0
  32. package/dist/casts/reaction.d.ts +1 -1
  33. package/dist/casts/relay-discovery.d.ts +4 -0
  34. package/dist/casts/relay-discovery.js +9 -1
  35. package/dist/casts/report.d.ts +28 -15
  36. package/dist/casts/report.js +42 -19
  37. package/dist/casts/share.d.ts +1 -1
  38. package/dist/casts/torrent.d.ts +5 -0
  39. package/dist/casts/torrent.js +8 -1
  40. package/dist/casts/trusted-assertions.d.ts +2 -0
  41. package/dist/casts/trusted-assertions.js +19 -18
  42. package/dist/casts/user-status.d.ts +33 -0
  43. package/dist/casts/user-status.js +69 -0
  44. package/dist/casts/user.d.ts +7 -0
  45. package/dist/casts/user.js +20 -1
  46. package/dist/factories/app-handler.d.ts +31 -0
  47. package/dist/factories/app-handler.js +50 -0
  48. package/dist/factories/app-recommendation.d.ts +21 -0
  49. package/dist/factories/app-recommendation.js +28 -0
  50. package/dist/factories/chat-message.d.ts +37 -0
  51. package/dist/factories/chat-message.js +56 -0
  52. package/dist/factories/comment.d.ts +8 -7
  53. package/dist/factories/comment.js +2 -2
  54. package/dist/factories/external-identities.d.ts +17 -0
  55. package/dist/factories/external-identities.js +28 -0
  56. package/dist/factories/forum-thread.d.ts +34 -0
  57. package/dist/factories/forum-thread.js +50 -0
  58. package/dist/factories/git-applied-status.d.ts +39 -0
  59. package/dist/factories/git-applied-status.js +53 -0
  60. package/dist/factories/git-closed-status.d.ts +18 -0
  61. package/dist/factories/git-closed-status.js +24 -0
  62. package/dist/factories/git-draft-status.d.ts +18 -0
  63. package/dist/factories/git-draft-status.js +24 -0
  64. package/dist/factories/git-issue.d.ts +19 -0
  65. package/dist/factories/git-issue.js +25 -0
  66. package/dist/factories/git-open-status.d.ts +18 -0
  67. package/dist/factories/git-open-status.js +22 -0
  68. package/dist/factories/git-patch.d.ts +59 -0
  69. package/dist/factories/git-patch.js +102 -0
  70. package/dist/factories/git-pull-request-update.d.ts +23 -0
  71. package/dist/factories/git-pull-request-update.js +33 -0
  72. package/dist/factories/git-pull-request.d.ts +39 -0
  73. package/dist/factories/git-pull-request.js +64 -0
  74. package/dist/factories/index.d.ts +22 -0
  75. package/dist/factories/index.js +22 -0
  76. package/dist/factories/internal/git-pull-request.d.ts +19 -0
  77. package/dist/factories/internal/git-pull-request.js +26 -0
  78. package/dist/factories/internal/git-status.d.ts +11 -0
  79. package/dist/factories/internal/git-status.js +12 -0
  80. package/dist/factories/internal/git-thread.d.ts +19 -0
  81. package/dist/factories/internal/git-thread.js +29 -0
  82. package/dist/factories/internal/napplet-manifest.d.ts +52 -0
  83. package/dist/factories/internal/napplet-manifest.js +85 -0
  84. package/dist/factories/internal/nsite-manifest.d.ts +45 -0
  85. package/dist/factories/internal/nsite-manifest.js +68 -0
  86. package/dist/factories/legacy-message.d.ts +2 -0
  87. package/dist/factories/legacy-message.js +9 -2
  88. package/dist/factories/named-napplet.d.ts +20 -0
  89. package/dist/factories/named-napplet.js +30 -0
  90. package/dist/factories/named-nsite.d.ts +28 -0
  91. package/dist/factories/named-nsite.js +53 -0
  92. package/dist/factories/napplet-snapshot.d.ts +18 -0
  93. package/dist/factories/napplet-snapshot.js +38 -0
  94. package/dist/factories/nsite-snapshot.d.ts +18 -0
  95. package/dist/factories/nsite-snapshot.js +29 -0
  96. package/dist/factories/reaction.d.ts +3 -2
  97. package/dist/factories/report.d.ts +48 -0
  98. package/dist/factories/report.js +66 -0
  99. package/dist/factories/root-napplet.d.ts +18 -0
  100. package/dist/factories/root-napplet.js +24 -0
  101. package/dist/factories/root-nsite.d.ts +23 -0
  102. package/dist/factories/root-nsite.js +44 -0
  103. package/dist/factories/torrent.d.ts +24 -10
  104. package/dist/factories/torrent.js +48 -44
  105. package/dist/factories/trusted-provider-list.d.ts +23 -0
  106. package/dist/factories/trusted-provider-list.js +39 -0
  107. package/dist/factories/user-status.d.ts +26 -0
  108. package/dist/factories/user-status.js +52 -0
  109. package/dist/helpers/app-data.js +15 -7
  110. package/dist/helpers/app-handler.d.ts +43 -6
  111. package/dist/helpers/app-handler.js +53 -3
  112. package/dist/helpers/app-recommendation.d.ts +24 -0
  113. package/dist/helpers/app-recommendation.js +30 -0
  114. package/dist/helpers/bookmark.js +5 -2
  115. package/dist/helpers/comment.d.ts +11 -5
  116. package/dist/helpers/comment.js +7 -1
  117. package/dist/helpers/content.d.ts +3 -1
  118. package/dist/helpers/emoji-pack.js +11 -6
  119. package/dist/helpers/emoji.d.ts +2 -2
  120. package/dist/helpers/encrypted-content-cache.js +18 -1
  121. package/dist/helpers/external-id.js +13 -19
  122. package/dist/helpers/external-identities.d.ts +29 -0
  123. package/dist/helpers/external-identities.js +52 -0
  124. package/dist/helpers/file-metadata.js +5 -6
  125. package/dist/helpers/forum-thread.d.ts +10 -0
  126. package/dist/helpers/forum-thread.js +10 -0
  127. package/dist/helpers/gift-wrap.d.ts +40 -15
  128. package/dist/helpers/gift-wrap.js +110 -33
  129. package/dist/helpers/git-issue.d.ts +13 -0
  130. package/dist/helpers/git-issue.js +19 -0
  131. package/dist/helpers/git-patch.d.ts +41 -0
  132. package/dist/helpers/git-patch.js +73 -0
  133. package/dist/helpers/git-pull-request.d.ts +57 -0
  134. package/dist/helpers/git-pull-request.js +111 -0
  135. package/dist/helpers/git-repository.d.ts +4 -0
  136. package/dist/helpers/git-repository.js +16 -0
  137. package/dist/helpers/git-status.d.ts +44 -0
  138. package/dist/helpers/git-status.js +93 -0
  139. package/dist/helpers/git.d.ts +2 -0
  140. package/dist/helpers/git.js +4 -0
  141. package/dist/helpers/groups.d.ts +5 -2
  142. package/dist/helpers/groups.js +40 -18
  143. package/dist/helpers/hashtag.d.ts +4 -2
  144. package/dist/helpers/index.d.ts +10 -0
  145. package/dist/helpers/index.js +10 -0
  146. package/dist/helpers/lists.js +14 -2
  147. package/dist/helpers/lnurl.js +1 -1
  148. package/dist/helpers/mute.js +18 -7
  149. package/dist/helpers/napplet.d.ts +89 -0
  150. package/dist/helpers/napplet.js +144 -0
  151. package/dist/helpers/nsite.d.ts +90 -0
  152. package/dist/helpers/nsite.js +217 -0
  153. package/dist/helpers/relay-discovery.d.ts +4 -0
  154. package/dist/helpers/relay-discovery.js +8 -0
  155. package/dist/helpers/reports.d.ts +35 -0
  156. package/dist/helpers/reports.js +36 -5
  157. package/dist/helpers/threading.d.ts +4 -2
  158. package/dist/helpers/torrent.d.ts +9 -6
  159. package/dist/helpers/torrent.js +23 -15
  160. package/dist/helpers/trusted-assertions.d.ts +7 -0
  161. package/dist/helpers/trusted-assertions.js +41 -2
  162. package/dist/helpers/user-status.d.ts +13 -1
  163. package/dist/helpers/user-status.js +15 -0
  164. package/dist/models/app-handler.d.ts +21 -0
  165. package/dist/models/app-handler.js +47 -0
  166. package/dist/models/git.d.ts +34 -0
  167. package/dist/models/git.js +144 -0
  168. package/dist/models/index.d.ts +3 -0
  169. package/dist/models/index.js +3 -0
  170. package/dist/models/nsite.d.ts +22 -0
  171. package/dist/models/nsite.js +67 -0
  172. package/dist/models/thread.d.ts +4 -1
  173. package/dist/models/thread.js +34 -5
  174. package/dist/models/user-status.d.ts +8 -4
  175. package/dist/models/user-status.js +16 -22
  176. package/dist/observable/filter-timeline-by-mutes.d.ts +1 -1
  177. package/dist/observable/filter-timeline-by-mutes.js +5 -1
  178. package/dist/operations/app-handler.d.ts +24 -0
  179. package/dist/operations/app-handler.js +58 -0
  180. package/dist/operations/app-recommendation.d.ts +13 -0
  181. package/dist/operations/app-recommendation.js +40 -0
  182. package/dist/operations/calendar-event.js +3 -2
  183. package/dist/operations/chat-message.d.ts +9 -0
  184. package/dist/operations/chat-message.js +12 -0
  185. package/dist/operations/client.d.ts +1 -4
  186. package/dist/operations/client.js +1 -23
  187. package/dist/operations/comment.d.ts +9 -3
  188. package/dist/operations/comment.js +12 -9
  189. package/dist/operations/external-identities.d.ts +8 -0
  190. package/dist/operations/external-identities.js +23 -0
  191. package/dist/operations/forum-thread.d.ts +3 -0
  192. package/dist/operations/forum-thread.js +6 -0
  193. package/dist/operations/gift-wrap.js +38 -14
  194. package/dist/operations/git-issue.d.ts +18 -0
  195. package/dist/operations/git-issue.js +31 -0
  196. package/dist/operations/git-patch.d.ts +30 -0
  197. package/dist/operations/git-patch.js +75 -0
  198. package/dist/operations/git-pull-request.d.ts +29 -0
  199. package/dist/operations/git-pull-request.js +74 -0
  200. package/dist/operations/git-repository.js +4 -4
  201. package/dist/operations/git-status.d.ts +28 -0
  202. package/dist/operations/git-status.js +116 -0
  203. package/dist/operations/git-thread.d.ts +13 -0
  204. package/dist/operations/git-thread.js +39 -0
  205. package/dist/operations/index.d.ts +15 -0
  206. package/dist/operations/index.js +15 -0
  207. package/dist/operations/internal/git-commits.d.ts +2 -0
  208. package/dist/operations/internal/git-commits.js +16 -0
  209. package/dist/operations/napplet.d.ts +51 -0
  210. package/dist/operations/napplet.js +121 -0
  211. package/dist/operations/nsite.d.ts +51 -0
  212. package/dist/operations/nsite.js +112 -0
  213. package/dist/operations/reaction.d.ts +12 -2
  214. package/dist/operations/reaction.js +1 -1
  215. package/dist/operations/report.d.ts +33 -0
  216. package/dist/operations/report.js +60 -0
  217. package/dist/operations/stream.js +5 -4
  218. package/dist/operations/torrent.d.ts +1 -4
  219. package/dist/operations/torrent.js +24 -11
  220. package/dist/operations/trusted-providers.d.ts +14 -0
  221. package/dist/operations/trusted-providers.js +33 -0
  222. package/dist/operations/user-status.d.ts +11 -0
  223. package/dist/operations/user-status.js +29 -0
  224. package/package.json +9 -9
@@ -0,0 +1,112 @@
1
+ import { getReplaceableAddressFromPointer, parseReplaceableAddress, } from "applesauce-core/helpers/pointers";
2
+ import { isSafeRelayURL } from "applesauce-core/helpers/relays";
3
+ import { normalizeURL } from "applesauce-core/helpers/url";
4
+ import { addNameValueTag, removeNameValueTag } from "applesauce-core/operations/tag/common";
5
+ import { modifyPublicTags } from "applesauce-core/operations/tags";
6
+ import { isSha256 } from "../helpers/blossom.js";
7
+ import { computeNsiteAggregateHash, isNsiteAddressPointer, isValidNsiteIdentifier, isValidNsitePath, NSITE_KIND, } from "../helpers/nsite.js";
8
+ function singleton(name, value) {
9
+ return modifyPublicTags((tags) => [...tags.filter((t) => t[0] !== name), ...(value ? [[name, ...value]] : [])]);
10
+ }
11
+ function pointerTag(name, input) {
12
+ const pointer = typeof input === "string" ? parseReplaceableAddress(input) : input;
13
+ if (!pointer ||
14
+ !isSha256(pointer.pubkey) ||
15
+ (name === "app" ? pointer.kind < 30000 || pointer.kind >= 40000 : !isNsiteAddressPointer(pointer)))
16
+ throw new Error(`Invalid ${name === "app" ? "app descriptor" : "nsite"} address`);
17
+ const relay = pointer.relays?.find(isSafeRelayURL);
18
+ return [name, getReplaceableAddressFromPointer(pointer), ...(relay ? [normalizeURL(relay)] : [])];
19
+ }
20
+ /**
21
+ * Sets the identifier of a named site
22
+ * @throws if the identifier is invalid or the draft is not a named site
23
+ */
24
+ export function setNsiteIdentifier(identifier) {
25
+ if (!isValidNsiteIdentifier(identifier))
26
+ throw new Error("Invalid nsite identifier");
27
+ return (draft) => {
28
+ if (draft.kind !== NSITE_KIND)
29
+ throw new Error("Only named nsites have identifiers");
30
+ return singleton("d", [identifier])(draft);
31
+ };
32
+ }
33
+ /** Sets or removes the site's title */
34
+ export function setNsiteTitle(title) {
35
+ return singleton("title", title === null ? null : [title]);
36
+ }
37
+ /** Sets or removes the site's description tag */
38
+ export function setNsiteDescription(description) {
39
+ return singleton("description", description === null ? null : [description]);
40
+ }
41
+ /** Sets or removes the site's source repository or archive URL */
42
+ export function setNsiteSource(source) {
43
+ return singleton("source", source === null ? null : [source]);
44
+ }
45
+ /** Adds a Blossom server hint */
46
+ export function addNsiteServer(server) {
47
+ return modifyPublicTags(addNameValueTag(["server", server]));
48
+ }
49
+ /** Removes a Blossom server hint */
50
+ export function removeNsiteServer(server) {
51
+ return modifyPublicTags(removeNameValueTag(["server", server]));
52
+ }
53
+ /**
54
+ * Adds or replaces an upstream app descriptor reference
55
+ * @throws if the address is not addressable
56
+ */
57
+ export function addNsiteApp(app) {
58
+ return modifyPublicTags(addNameValueTag(pointerTag("app", app)));
59
+ }
60
+ /**
61
+ * Removes an upstream app descriptor reference
62
+ * @throws if the address is not addressable
63
+ */
64
+ export function removeNsiteApp(app) {
65
+ return modifyPublicTags(removeNameValueTag(pointerTag("app", app).slice(0, 2)));
66
+ }
67
+ /**
68
+ * Sets or removes the immediate parent site address
69
+ * @throws if the address does not reference a root or named site
70
+ */
71
+ export function setNsiteParent(parent) {
72
+ return singleton("a", parent === null ? null : pointerTag("a", parent).slice(1));
73
+ }
74
+ /**
75
+ * Sets or removes the origin site address of the copy lineage
76
+ * @throws if the address does not reference a root or named site
77
+ */
78
+ export function setNsiteRoot(root) {
79
+ return singleton("A", root === null ? null : pointerTag("A", root).slice(1));
80
+ }
81
+ /**
82
+ * Sets or removes the advertised aggregate hash
83
+ * @throws if the hash is not lowercase hexadecimal SHA-256
84
+ */
85
+ export function setNsiteAggregateHash(hash) {
86
+ if (hash !== null && !isSha256(hash))
87
+ throw new Error("Invalid nsite aggregate hash");
88
+ return singleton("x", hash === null ? null : [hash, "aggregate"]);
89
+ }
90
+ /** Updates the aggregate hash from all current path tags */
91
+ export function updateNsiteAggregateHash() {
92
+ return (draft) => setNsiteAggregateHash(computeNsiteAggregateHash(draft))(draft);
93
+ }
94
+ /**
95
+ * Adds or replaces a file mapping and updates the aggregate hash
96
+ * @throws if the path or hash is invalid
97
+ */
98
+ export function setNsiteFile(path, hash) {
99
+ if (!isValidNsitePath(path) || !isSha256(hash))
100
+ throw new Error("Invalid nsite file path or hash");
101
+ return modifyPublicTags((tags) => {
102
+ const next = [...tags.filter((t) => !(t[0] === "path" && t[1] === path) && t[0] !== "x"), ["path", path, hash]];
103
+ return [...next, ["x", computeNsiteAggregateHash({ tags: next }), "aggregate"]];
104
+ });
105
+ }
106
+ /** Removes a file mapping and updates the aggregate hash */
107
+ export function removeNsiteFile(path) {
108
+ return modifyPublicTags((tags) => {
109
+ const next = tags.filter((t) => !(t[0] === "path" && t[1] === path) && t[0] !== "x");
110
+ return [...next, ["x", computeNsiteAggregateHash({ tags: next }), "aggregate"]];
111
+ });
112
+ }
@@ -1,12 +1,22 @@
1
1
  import { EventOperation } from "applesauce-core/factories";
2
- import { NostrEvent } from "applesauce-core/helpers/event";
2
+ import { NostrEvent, Rumor } from "applesauce-core/helpers/event";
3
3
  import { Emoji } from "../helpers/emoji.js";
4
4
  /** Sets the content for a reaction event */
5
5
  export declare function setReaction(emoji?: string | Emoji): EventOperation;
6
+ /**
7
+ * The event a reaction points to. A full event/rumor carries its tags (so an
8
+ * addressable target can be resolved to an "a" tag), or a lightweight
9
+ * `{ id, pubkey, kind }` pointer for cases where only the identity is known.
10
+ */
11
+ export type ReactionParent = NostrEvent | Rumor | {
12
+ id: string;
13
+ pubkey: string;
14
+ kind: number;
15
+ };
6
16
  /**
7
17
  * Includes NIP-25 "e", "p", "k", and "a" tags for a reaction event to point to a parent event
8
18
  * @param event - Event being reacted to
9
19
  * @param getEventRelayHint - Optional function to get relay hint for event ID
10
20
  * @param getPubkeyRelayHint - Optional function to get relay hint for pubkey
11
21
  */
12
- export declare function setReactionParent(event: NostrEvent, getEventRelayHint?: (eventId: string) => Promise<string | undefined>, getPubkeyRelayHint?: (pubkey: string) => Promise<string | undefined>): EventOperation;
22
+ export declare function setReactionParent(event: ReactionParent, getEventRelayHint?: (eventId: string) => Promise<string | undefined>, getPubkeyRelayHint?: (pubkey: string) => Promise<string | undefined>): EventOperation;
@@ -26,7 +26,7 @@ export function setReactionParent(event, getEventRelayHint, getPubkeyRelayHint)
26
26
  pubkey: event.pubkey,
27
27
  relays: pubkeyHint ? [pubkeyHint] : undefined,
28
28
  });
29
- if (isAddressableKind(event.kind)) {
29
+ if (isAddressableKind(event.kind) && "tags" in event) {
30
30
  // include "a" tag
31
31
  const identifier = getTagValue(event, "d");
32
32
  if (identifier)
@@ -0,0 +1,33 @@
1
+ import { EventOperation } from "applesauce-core/factories";
2
+ import { ReportReason, ReportTarget } from "../helpers/reports.js";
3
+ /** An event identity required when reporting an event or its media */
4
+ export type ReportEventTarget = {
5
+ /** The event ID */
6
+ id: string;
7
+ /** The event author's public key */
8
+ pubkey: string;
9
+ };
10
+ /**
11
+ * Adds or updates a reported user
12
+ * @throws if the public key or reason is invalid
13
+ */
14
+ export declare function addReportUser(pubkey: string, reason: ReportReason): EventOperation;
15
+ /**
16
+ * Adds or updates a reported event and includes its author
17
+ * @throws if the event identity or reason is invalid
18
+ */
19
+ export declare function addReportEvent(event: ReportEventTarget, reason: ReportReason): EventOperation;
20
+ /**
21
+ * Adds or updates a reported blob and includes its containing event and author
22
+ * @throws if the hash, event identity, or reason is invalid
23
+ */
24
+ export declare function addReportBlob(hash: string, event: ReportEventTarget, reason: ReportReason): EventOperation;
25
+ /** Removes matching target tags and preserves all other tags */
26
+ export declare function removeReportTarget(type: ReportTarget["type"], value: string): EventOperation;
27
+ /**
28
+ * Adds a media server or direct media URL without rewriting its path
29
+ * @throws if the URL is invalid or does not use HTTP or HTTPS
30
+ */
31
+ export declare function addReportServer(server: string): EventOperation;
32
+ /** Removes a media server or direct media URL */
33
+ export declare function removeReportServer(server: string): EventOperation;
@@ -0,0 +1,60 @@
1
+ import { modifyPublicTags } from "applesauce-core/operations/tags";
2
+ import { isReportIdentifier, ReportReason } from "../helpers/reports.js";
3
+ function check(value, reason) {
4
+ if (!isReportIdentifier(value))
5
+ throw new Error("Expected a 32-byte lowercase hex report target");
6
+ if (reason !== undefined && !Object.values(ReportReason).includes(reason))
7
+ throw new Error("Invalid report reason");
8
+ }
9
+ function put(tags, name, value, reason) {
10
+ const existing = tags.find((t) => t[0] === name && t[1] === value);
11
+ if (existing && reason === undefined)
12
+ return tags;
13
+ return [...tags.filter((t) => t[0] !== name || t[1] !== value), reason ? [name, value, reason] : [name, value]];
14
+ }
15
+ /**
16
+ * Adds or updates a reported user
17
+ * @throws if the public key or reason is invalid
18
+ */
19
+ export function addReportUser(pubkey, reason) {
20
+ check(pubkey, reason);
21
+ return modifyPublicTags((tags) => put(tags, "p", pubkey, reason));
22
+ }
23
+ /**
24
+ * Adds or updates a reported event and includes its author
25
+ * @throws if the event identity or reason is invalid
26
+ */
27
+ export function addReportEvent(event, reason) {
28
+ check(event.id, reason);
29
+ check(event.pubkey);
30
+ return modifyPublicTags((tags) => put(put(tags, "p", event.pubkey), "e", event.id, reason));
31
+ }
32
+ /**
33
+ * Adds or updates a reported blob and includes its containing event and author
34
+ * @throws if the hash, event identity, or reason is invalid
35
+ */
36
+ export function addReportBlob(hash, event, reason) {
37
+ check(hash, reason);
38
+ check(event.id);
39
+ check(event.pubkey);
40
+ return modifyPublicTags((tags) => put(put(put(tags, "p", event.pubkey), "e", event.id), "x", hash, reason));
41
+ }
42
+ /** Removes matching target tags and preserves all other tags */
43
+ export function removeReportTarget(type, value) {
44
+ const name = { user: "p", event: "e", blob: "x" }[type];
45
+ return modifyPublicTags((tags) => tags.filter((t) => t[0] !== name || t[1] !== value));
46
+ }
47
+ /**
48
+ * Adds a media server or direct media URL without rewriting its path
49
+ * @throws if the URL is invalid or does not use HTTP or HTTPS
50
+ */
51
+ export function addReportServer(server) {
52
+ const url = new URL(server);
53
+ if (url.protocol !== "http:" && url.protocol !== "https:")
54
+ throw new Error("Expected an HTTP media URL");
55
+ return modifyPublicTags((tags) => tags.some((t) => t[0] === "server" && t[1] === server) ? tags : [...tags, ["server", server]]);
56
+ }
57
+ /** Removes a media server or direct media URL */
58
+ export function removeReportServer(server) {
59
+ return modifyPublicTags((tags) => tags.filter((t) => t[0] !== "server" || t[1] !== server));
60
+ }
@@ -1,3 +1,4 @@
1
+ import { serializeURL } from "applesauce-core/helpers/url";
1
2
  import { ensureMarkedProfilePointerTag } from "applesauce-core/helpers/factory";
2
3
  import { addNameValueTag, removeNameValueTag } from "applesauce-core/operations/tag/common";
3
4
  import { includeSingletonTag, modifyPublicTags } from "applesauce-core/operations/tags";
@@ -17,11 +18,11 @@ export function setImage(image) {
17
18
  }
18
19
  /** Sets the streaming URL for a live event */
19
20
  export function setStreamingUrl(url) {
20
- return includeSingletonTag(["streaming", new URL(url).toString()], true);
21
+ return includeSingletonTag(["streaming", serializeURL(url)], true);
21
22
  }
22
23
  /** Sets the recording URL for a live event (typically after the event ends) */
23
24
  export function setRecordingUrl(url) {
24
- return includeSingletonTag(["recording", new URL(url).toString()], true);
25
+ return includeSingletonTag(["recording", serializeURL(url)], true);
25
26
  }
26
27
  /** Sets the start timestamp for a live event */
27
28
  export function setStartTime(start) {
@@ -59,11 +60,11 @@ export function removeParticipant(pubkey) {
59
60
  }
60
61
  /** Adds a relay to the live event's relay list */
61
62
  export function addRelay(relay) {
62
- return modifyPublicTags(addNameValueTag(["relays", new URL(relay).toString()], true));
63
+ return modifyPublicTags(addNameValueTag(["relays", serializeURL(relay)], true));
63
64
  }
64
65
  /** Removes a relay from the live event's relay list */
65
66
  export function removeRelay(relay) {
66
- return modifyPublicTags(removeNameValueTag(["relays", new URL(relay).toString()]));
67
+ return modifyPublicTags(removeNameValueTag(["relays", serializeURL(relay)]));
67
68
  }
68
69
  /** Sets a pinned live chat message */
69
70
  export function setPinnedMessage(eventId) {
@@ -26,10 +26,7 @@ export declare function setTorrentCategoryPath(categoryPath: string): EventOpera
26
26
  export declare function removeTorrentCategoryPath(): EventOperation;
27
27
  /** Adds an external identifier */
28
28
  export declare function addTorrentExternalIdentifier(identifier: TorrentExternalIdentifier): EventOperation;
29
- /**
30
- * Adds an external identifier using prefix, id, and optional mediaType
31
- * Convenience function that creates the identifier object automatically
32
- */
29
+ /** Adds an external identifier built from a prefix, id, and optional media type */
33
30
  export declare function addTorrentExternalIdentifierByParts(prefix: TorrentExternalIdentifierPrefix, id: string, mediaType?: string): EventOperation;
34
31
  /** Removes an external identifier */
35
32
  export declare function removeTorrentExternalIdentifier(identifier: string): EventOperation;
@@ -11,9 +11,7 @@ export function setTorrentTitle(title) {
11
11
  }
12
12
  /** Adds a file entry to the torrent */
13
13
  export function addTorrentFile(file) {
14
- const tag = file.size
15
- ? ["file", file.name, file.size.toString()]
16
- : ["file", file.name];
14
+ const tag = file.size !== undefined ? ["file", file.name, file.size.toString()] : ["file", file.name];
17
15
  return modifyPublicTags(addNameValueTag(tag, false));
18
16
  }
19
17
  /** Removes a file entry from the torrent */
@@ -28,13 +26,31 @@ export function addTorrentTracker(tracker) {
28
26
  export function removeTorrentTracker(tracker) {
29
27
  return modifyPublicTags(removeNameValueTag(["tracker", tracker]));
30
28
  }
29
+ // Replaces the first `i` tag with the given prefix and removes the rest, leaving other `i` tags untouched
30
+ function setPrefixedIdentifierTag(prefix, value) {
31
+ return (tags) => {
32
+ const tag = ["i", `${prefix}:${value}`];
33
+ const index = tags.findIndex((t) => t[0] === "i" && t[1]?.startsWith(`${prefix}:`));
34
+ if (index === -1)
35
+ return [...tags, tag];
36
+ return tags.flatMap((t, i) => {
37
+ if (i === index)
38
+ return [tag];
39
+ return t[0] === "i" && t[1]?.startsWith(`${prefix}:`) ? [] : [t];
40
+ });
41
+ };
42
+ }
43
+ // Removes every `i` tag with the given prefix
44
+ function removePrefixedIdentifierTags(prefix) {
45
+ return (tags) => tags.filter((t) => !(t[0] === "i" && t[1]?.startsWith(`${prefix}:`)));
46
+ }
31
47
  /** Sets the newznab category ID (replaces any existing newznab category) */
32
48
  export function setTorrentCategory(categoryId) {
33
- return modifyPublicTags(setSingletonTag(["i", `newznab:${categoryId}`]));
49
+ return modifyPublicTags(setPrefixedIdentifierTag("newznab", String(categoryId)));
34
50
  }
35
51
  /** Removes the newznab category ID */
36
52
  export function removeTorrentCategory() {
37
- return modifyPublicTags((tags) => tags.filter((t) => !(t[0] === "i" && t[1]?.startsWith("newznab:"))));
53
+ return modifyPublicTags(removePrefixedIdentifierTags("newznab"));
38
54
  }
39
55
  /** Adds a search tag (for searchability) */
40
56
  export function addTorrentSearchTag(tag) {
@@ -46,20 +62,17 @@ export function removeTorrentSearchTag(tag) {
46
62
  }
47
63
  /** Sets the category path (tcat) */
48
64
  export function setTorrentCategoryPath(categoryPath) {
49
- return modifyPublicTags(setSingletonTag(["i", `tcat:${categoryPath}`]));
65
+ return modifyPublicTags(setPrefixedIdentifierTag("tcat", categoryPath));
50
66
  }
51
67
  /** Removes the category path (tcat) */
52
68
  export function removeTorrentCategoryPath() {
53
- return modifyPublicTags((tags) => tags.filter((t) => !(t[0] === "i" && t[1]?.startsWith("tcat:"))));
69
+ return modifyPublicTags(removePrefixedIdentifierTags("tcat"));
54
70
  }
55
71
  /** Adds an external identifier */
56
72
  export function addTorrentExternalIdentifier(identifier) {
57
73
  return modifyPublicTags(addNameValueTag(["i", identifier.identifier], false));
58
74
  }
59
- /**
60
- * Adds an external identifier using prefix, id, and optional mediaType
61
- * Convenience function that creates the identifier object automatically
62
- */
75
+ /** Adds an external identifier built from a prefix, id, and optional media type */
63
76
  export function addTorrentExternalIdentifierByParts(prefix, id, mediaType) {
64
77
  const identifier = createTorrentExternalIdentifier(prefix, id, mediaType);
65
78
  return addTorrentExternalIdentifier(identifier);
@@ -0,0 +1,14 @@
1
+ import { TagOperation } from "applesauce-core/factories";
2
+ import { TrustedProvider } from "../helpers/trusted-assertions.js";
3
+ /** A provider slot, optionally narrowed to a single service pubkey */
4
+ export type TrustedProviderSelector = Pick<TrustedProvider, "kind" | "tag"> & {
5
+ servicePubkey?: string;
6
+ };
7
+ /** Adds a provider to a kind 10040 tag list, or updates the relay hint of an existing entry */
8
+ export declare function addProviderTag(provider: TrustedProvider): TagOperation;
9
+ /** Removes a provider from a kind 10040 tag list, or every provider in the slot if no pubkey is given */
10
+ export declare function removeProviderTag(selector: TrustedProviderSelector): TagOperation;
11
+ /** Replaces every provider in the `kind:tag` slot with a single provider */
12
+ export declare function setProviderTag(provider: TrustedProvider): TagOperation;
13
+ /** Removes a service pubkey from every slot in a kind 10040 tag list */
14
+ export declare function removeServiceTags(servicePubkey: string): TagOperation;
@@ -0,0 +1,33 @@
1
+ import { createProviderTag, parseProviderTag } from "../helpers/trusted-assertions.js";
2
+ /** Checks if a tag matches a provider selector */
3
+ function matchesProvider(tag, selector) {
4
+ const parsed = parseProviderTag(tag);
5
+ if (!parsed || parsed.kind !== selector.kind || parsed.tag !== selector.tag)
6
+ return false;
7
+ return selector.servicePubkey === undefined || parsed.servicePubkey === selector.servicePubkey;
8
+ }
9
+ /** Adds a provider to a kind 10040 tag list, or updates the relay hint of an existing entry */
10
+ export function addProviderTag(provider) {
11
+ return (tags) => {
12
+ const tag = createProviderTag(provider);
13
+ const index = tags.findIndex((t) => matchesProvider(t, provider));
14
+ if (index === -1)
15
+ return [...tags, tag];
16
+ return tags.map((t, i) => (i === index ? tag : t));
17
+ };
18
+ }
19
+ /** Removes a provider from a kind 10040 tag list, or every provider in the slot if no pubkey is given */
20
+ export function removeProviderTag(selector) {
21
+ return (tags) => tags.filter((t) => !matchesProvider(t, selector));
22
+ }
23
+ /** Replaces every provider in the `kind:tag` slot with a single provider */
24
+ export function setProviderTag(provider) {
25
+ return (tags) => [
26
+ ...tags.filter((t) => !matchesProvider(t, { kind: provider.kind, tag: provider.tag })),
27
+ createProviderTag(provider),
28
+ ];
29
+ }
30
+ /** Removes a service pubkey from every slot in a kind 10040 tag list */
31
+ export function removeServiceTags(servicePubkey) {
32
+ return (tags) => tags.filter((t) => parseProviderTag(t)?.servicePubkey !== servicePubkey);
33
+ }
@@ -0,0 +1,11 @@
1
+ import { EventOperation } from "applesauce-core/factories";
2
+ import { UserStatusPointer, UserStatusType } from "../helpers/user-status.js";
3
+ /** Sets the status type ("d" tag) of a user status */
4
+ export declare function setStatusType(type: UserStatusType): EventOperation;
5
+ /**
6
+ * Sets the single URL, profile, event, or address the status links to
7
+ * @param link a URL string or a {@link UserStatusPointer}
8
+ */
9
+ export declare function setStatusLink(link: string | UserStatusPointer): EventOperation;
10
+ /** Removes any "r", "p", "e", or "a" link tags from the status */
11
+ export declare function clearStatusLink(): EventOperation;
@@ -0,0 +1,29 @@
1
+ import { addAddressPointerTag, addEventPointerTag, addProfilePointerTag, setSingletonTag, } from "applesauce-core/operations/tag/common";
2
+ import { modifyPublicTags } from "applesauce-core/operations/tags";
3
+ const LINK_TAGS = ["r", "p", "e", "a"];
4
+ const removeLinkTags = (tags) => tags.filter((t) => !LINK_TAGS.includes(t[0]));
5
+ /** Sets the status type ("d" tag) of a user status */
6
+ export function setStatusType(type) {
7
+ return modifyPublicTags(setSingletonTag(["d", type], true));
8
+ }
9
+ /**
10
+ * Sets the single URL, profile, event, or address the status links to
11
+ * @param link a URL string or a {@link UserStatusPointer}
12
+ */
13
+ export function setStatusLink(link) {
14
+ const pointer = typeof link === "string" ? { type: "url", data: link } : link;
15
+ switch (pointer.type) {
16
+ case "url":
17
+ return modifyPublicTags(removeLinkTags, (tags) => [...tags, ["r", pointer.data]]);
18
+ case "nprofile":
19
+ return modifyPublicTags(removeLinkTags, addProfilePointerTag(pointer.data));
20
+ case "nevent":
21
+ return modifyPublicTags(removeLinkTags, addEventPointerTag(pointer.data));
22
+ case "naddr":
23
+ return modifyPublicTags(removeLinkTags, addAddressPointerTag(pointer.data));
24
+ }
25
+ }
26
+ /** Removes any "r", "p", "e", or "a" link tags from the status */
27
+ export function clearStatusLink() {
28
+ return modifyPublicTags(removeLinkTags);
29
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-common",
3
- "version": "6.2.0",
3
+ "version": "7.0.0",
4
4
  "description": "Common NIP-specific helpers and models for applesauce",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -82,8 +82,9 @@
82
82
  }
83
83
  },
84
84
  "dependencies": {
85
+ "@noble/hashes": "^2.2.0",
85
86
  "@scure/base": "^2.2.0",
86
- "applesauce-core": "^6.2.0",
87
+ "applesauce-core": "^7.0.0",
87
88
  "hash-sum": "^2.0.0",
88
89
  "light-bolt11-decoder": "^3.2.0",
89
90
  "nanoid": "^5.0.9",
@@ -92,16 +93,15 @@
92
93
  "devDependencies": {
93
94
  "@hirez_io/observer-spy": "^2.2.0",
94
95
  "@nostrability/schemata": "^0.3.2",
95
- "@types/debug": "^4.1.12",
96
96
  "@types/hash-sum": "^1.0.2",
97
- "@types/node": "^22.19.1",
97
+ "@types/node": "^22.20.5",
98
98
  "ajv": "^8.20.0",
99
99
  "ajv-formats": "^3.0.1",
100
- "nostr-tools": "^2.19",
101
- "rimraf": "^6.0.1",
102
- "typescript": "^5.8.3",
103
- "vitest": "^4.0.15",
104
- "yaml": "^2.9.0"
100
+ "nostr-tools": "^2.25.2",
101
+ "rimraf": "^6.1.3",
102
+ "typescript": "^7.0.2",
103
+ "vitest": "^4.1.11",
104
+ "yaml": "^2.9.1"
105
105
  },
106
106
  "funding": {
107
107
  "type": "lightning",