applesauce-core 0.8.0 → 0.10.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 (78) hide show
  1. package/README.md +1 -1
  2. package/dist/event-store/database.d.ts +17 -14
  3. package/dist/event-store/database.js +50 -33
  4. package/dist/event-store/event-store.d.ts +37 -14
  5. package/dist/event-store/event-store.js +249 -96
  6. package/dist/helpers/bolt11.d.ts +9 -0
  7. package/dist/helpers/bolt11.js +15 -0
  8. package/dist/helpers/cache.js +9 -9
  9. package/dist/helpers/comment.d.ts +48 -0
  10. package/dist/helpers/comment.js +116 -0
  11. package/dist/helpers/content.d.ts +3 -0
  12. package/dist/helpers/content.js +8 -0
  13. package/dist/helpers/delete.d.ts +3 -0
  14. package/dist/helpers/delete.js +7 -0
  15. package/dist/helpers/emoji.d.ts +10 -1
  16. package/dist/helpers/emoji.js +12 -0
  17. package/dist/helpers/event.d.ts +9 -1
  18. package/dist/helpers/event.js +25 -1
  19. package/dist/helpers/external-id.d.ts +29 -0
  20. package/dist/helpers/external-id.js +20 -0
  21. package/dist/helpers/filter.d.ts +0 -2
  22. package/dist/helpers/filter.js +3 -7
  23. package/dist/helpers/hidden-tags.d.ts +48 -0
  24. package/dist/helpers/hidden-tags.js +108 -0
  25. package/dist/helpers/hidden-tags.test.d.ts +1 -0
  26. package/dist/helpers/hidden-tags.test.js +28 -0
  27. package/dist/helpers/index.d.ts +17 -7
  28. package/dist/helpers/index.js +17 -7
  29. package/dist/helpers/json.d.ts +1 -0
  30. package/dist/helpers/json.js +1 -0
  31. package/dist/helpers/lnurl.d.ts +4 -0
  32. package/dist/helpers/lnurl.js +40 -0
  33. package/dist/helpers/mailboxes.d.ts +0 -6
  34. package/dist/helpers/mailboxes.js +7 -8
  35. package/dist/helpers/mailboxes.test.js +13 -12
  36. package/dist/helpers/media-attachment.d.ts +33 -0
  37. package/dist/helpers/media-attachment.js +60 -0
  38. package/dist/helpers/pointers.d.ts +39 -6
  39. package/dist/helpers/pointers.js +102 -20
  40. package/dist/helpers/profile.d.ts +2 -7
  41. package/dist/helpers/profile.js +26 -23
  42. package/dist/helpers/string.d.ts +6 -0
  43. package/dist/helpers/string.js +2 -0
  44. package/dist/helpers/tags.d.ts +6 -0
  45. package/dist/helpers/tags.js +6 -0
  46. package/dist/helpers/threading.d.ts +10 -10
  47. package/dist/helpers/threading.js +52 -19
  48. package/dist/helpers/threading.test.d.ts +1 -0
  49. package/dist/helpers/threading.test.js +41 -0
  50. package/dist/helpers/url.d.ts +4 -1
  51. package/dist/helpers/url.js +4 -3
  52. package/dist/helpers/zap.d.ts +39 -0
  53. package/dist/helpers/zap.js +95 -0
  54. package/dist/observable/{getValue.d.ts → get-value.d.ts} +1 -0
  55. package/dist/observable/{getValue.js → get-value.js} +3 -0
  56. package/dist/observable/index.d.ts +1 -1
  57. package/dist/observable/index.js +1 -1
  58. package/dist/promise/deferred.d.ts +1 -0
  59. package/dist/promise/deferred.js +1 -0
  60. package/dist/queries/comments.d.ts +4 -0
  61. package/dist/queries/comments.js +14 -0
  62. package/dist/queries/index.d.ts +4 -2
  63. package/dist/queries/index.js +4 -2
  64. package/dist/queries/mailboxes.d.ts +1 -0
  65. package/dist/queries/mailboxes.js +1 -0
  66. package/dist/queries/profile.d.ts +1 -0
  67. package/dist/queries/profile.js +4 -3
  68. package/dist/queries/reactions.d.ts +1 -1
  69. package/dist/queries/reactions.js +1 -1
  70. package/dist/queries/simple.d.ts +3 -3
  71. package/dist/queries/simple.js +13 -13
  72. package/dist/queries/thread.d.ts +2 -0
  73. package/dist/queries/thread.js +29 -3
  74. package/dist/queries/zaps.d.ts +5 -0
  75. package/dist/queries/zaps.js +21 -0
  76. package/dist/query-store/index.d.ts +22 -12
  77. package/dist/query-store/index.js +36 -30
  78. package/package.json +14 -16
@@ -1,9 +1,31 @@
1
- import { getAddressPointerFromTag, getEventPointerFromTag } from "./pointers.js";
1
+ import { getAddressPointerFromATag } from "./pointers.js";
2
+ import { getOrComputeCachedValue } from "./cache.js";
3
+ import { safeRelayUrls } from "./relays.js";
2
4
  export const Nip10ThreadRefsSymbol = Symbol.for("nip10-thread-refs");
5
+ /**
6
+ * Gets an EventPointer form a NIP-10 threading "e" tag
7
+ * @throws
8
+ */
9
+ export function getEventPointerFromThreadTag(tag) {
10
+ if (!tag[1])
11
+ throw new Error("Missing event id in tag");
12
+ let pointer = { id: tag[1] };
13
+ if (tag[2])
14
+ pointer.relays = safeRelayUrls([tag[2]]);
15
+ // get author from NIP-18 quote tags, nip-22 comments tags, or nip-10 thread tags
16
+ if (tag[0] === "e" &&
17
+ (tag[3] === "root" || tag[3] === "reply" || tag[3] === "mention") &&
18
+ tag[4] &&
19
+ tag[4].length === 64) {
20
+ // NIP-10 "e" tag
21
+ pointer.author = tag[4];
22
+ }
23
+ return pointer;
24
+ }
3
25
  /** Parses NIP-10 tags and handles legacy behavior */
4
- export function interpretThreadTags(event) {
5
- const eTags = event.tags.filter((t) => t[0] === "e" && t[1]);
6
- const aTags = event.tags.filter((t) => t[0] === "a" && t[1]);
26
+ export function interpretThreadTags(tags) {
27
+ const eTags = tags.filter((t) => t[0] === "e" && t[1]);
28
+ const aTags = tags.filter((t) => t[0] === "a" && t[1]);
7
29
  // find the root and reply tags.
8
30
  let rootETag = eTags.find((t) => t[3] === "root");
9
31
  let replyETag = eTags.find((t) => t[3] === "reply");
@@ -42,20 +64,31 @@ export function interpretThreadTags(event) {
42
64
  }
43
65
  /** Returns the parsed NIP-10 tags for an event */
44
66
  export function getNip10References(event) {
45
- let refs = event[Nip10ThreadRefsSymbol];
46
- if (!refs) {
47
- const e = event;
48
- const tags = interpretThreadTags(e);
49
- refs = event[Nip10ThreadRefsSymbol] = {
50
- root: tags.root && {
51
- e: tags.root.e && getEventPointerFromTag(tags.root.e),
52
- a: tags.root.a && getAddressPointerFromTag(tags.root.a),
53
- },
54
- reply: tags.reply && {
55
- e: tags.reply.e && getEventPointerFromTag(tags.reply.e),
56
- a: tags.reply.a && getAddressPointerFromTag(tags.reply.a),
57
- },
67
+ return getOrComputeCachedValue(event, Nip10ThreadRefsSymbol, () => {
68
+ const tags = interpretThreadTags(event.tags);
69
+ let root;
70
+ if (tags.root) {
71
+ try {
72
+ root = {
73
+ e: tags.root.e && getEventPointerFromThreadTag(tags.root.e),
74
+ a: tags.root.a && getAddressPointerFromATag(tags.root.a),
75
+ };
76
+ }
77
+ catch (error) { }
78
+ }
79
+ let reply;
80
+ if (tags.reply) {
81
+ try {
82
+ reply = {
83
+ e: tags.reply.e && getEventPointerFromThreadTag(tags.reply.e),
84
+ a: tags.reply.a && getAddressPointerFromATag(tags.reply.a),
85
+ };
86
+ }
87
+ catch (error) { }
88
+ }
89
+ return {
90
+ root,
91
+ reply,
58
92
  };
59
- }
60
- return refs;
93
+ });
61
94
  }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,41 @@
1
+ import { describe, expect, it } from "vitest";
2
+ import { interpretThreadTags } from "./threading.js";
3
+ describe("threading helpers", () => {
4
+ describe("interpretThreadTags", () => {
5
+ it("should handle legacy tags", () => {
6
+ expect(interpretThreadTags([
7
+ ["e", "root-id"],
8
+ ["e", "reply-id"],
9
+ ])).toEqual({ root: { a: undefined, e: ["e", "root-id"] }, reply: { a: undefined, e: ["e", "reply-id"] } });
10
+ });
11
+ it("should handle nip-10 tags", () => {
12
+ expect(interpretThreadTags([
13
+ ["e", "root-id", "relay", "root"],
14
+ ["e", "reply-id", "relay", "reply"],
15
+ ])).toEqual({
16
+ root: { a: undefined, e: ["e", "root-id", "relay", "root"] },
17
+ reply: { a: undefined, e: ["e", "reply-id", "relay", "reply"] },
18
+ });
19
+ });
20
+ it("should ignore mention nip-10 tags", () => {
21
+ expect(interpretThreadTags([
22
+ ["e", "root-id", "relay", "root"],
23
+ ["e", "mention-id", "relay", "mention"],
24
+ ["e", "reply-id", "relay", "reply"],
25
+ ])).toEqual({
26
+ root: { a: undefined, e: ["e", "root-id", "relay", "root"] },
27
+ reply: { a: undefined, e: ["e", "reply-id", "relay", "reply"] },
28
+ });
29
+ });
30
+ it("should handle single nip-10 tags", () => {
31
+ expect(interpretThreadTags([["e", "root-id", "relay", "root"]])).toEqual({
32
+ root: { a: undefined, e: ["e", "root-id", "relay", "root"] },
33
+ reply: { a: undefined, e: ["e", "root-id", "relay", "root"] },
34
+ });
35
+ expect(interpretThreadTags([["e", "reply-id", "relay", "reply"]])).toEqual({
36
+ root: { a: undefined, e: ["e", "reply-id", "relay", "reply"] },
37
+ reply: { a: undefined, e: ["e", "reply-id", "relay", "reply"] },
38
+ });
39
+ });
40
+ });
41
+ });
@@ -4,8 +4,11 @@ export declare const IMAGE_EXT: string[];
4
4
  export declare const VIDEO_EXT: string[];
5
5
  export declare const STREAM_EXT: string[];
6
6
  export declare const AUDIO_EXT: string[];
7
- export declare function isVisualMediaURL(url: string | URL): boolean;
7
+ /** Checks if a url is a image URL */
8
8
  export declare function isImageURL(url: string | URL): boolean;
9
+ /** Checks if a url is a video URL */
9
10
  export declare function isVideoURL(url: string | URL): boolean;
11
+ /** Checks if a url is a stream URL */
10
12
  export declare function isStreamURL(url: string | URL): boolean;
13
+ /** Checks if a url is a audio URL */
11
14
  export declare function isAudioURL(url: string | URL): boolean;
@@ -4,24 +4,25 @@ export const IMAGE_EXT = [".svg", ".gif", ".png", ".jpg", ".jpeg", ".webp", ".av
4
4
  export const VIDEO_EXT = [".mp4", ".mkv", ".webm", ".mov"];
5
5
  export const STREAM_EXT = [".m3u8"];
6
6
  export const AUDIO_EXT = [".mp3", ".wav", ".ogg", ".aac"];
7
- export function isVisualMediaURL(url) {
8
- return isImageURL(url) || isVideoURL(url) || isStreamURL(url);
9
- }
7
+ /** Checks if a url is a image URL */
10
8
  export function isImageURL(url) {
11
9
  url = convertToUrl(url);
12
10
  const filename = getURLFilename(url);
13
11
  return !!filename && IMAGE_EXT.some((ext) => filename.endsWith(ext));
14
12
  }
13
+ /** Checks if a url is a video URL */
15
14
  export function isVideoURL(url) {
16
15
  url = convertToUrl(url);
17
16
  const filename = getURLFilename(url);
18
17
  return !!filename && VIDEO_EXT.some((ext) => filename.endsWith(ext));
19
18
  }
19
+ /** Checks if a url is a stream URL */
20
20
  export function isStreamURL(url) {
21
21
  url = convertToUrl(url);
22
22
  const filename = getURLFilename(url);
23
23
  return !!filename && STREAM_EXT.some((ext) => filename.endsWith(ext));
24
24
  }
25
+ /** Checks if a url is a audio URL */
25
26
  export function isAudioURL(url) {
26
27
  url = convertToUrl(url);
27
28
  const filename = getURLFilename(url);
@@ -0,0 +1,39 @@
1
+ import { NostrEvent } from "nostr-tools";
2
+ export declare const ZapRequestSymbol: unique symbol;
3
+ export declare const ZapFromSymbol: unique symbol;
4
+ export declare const ZapInvoiceSymbol: unique symbol;
5
+ export declare const ZapEventPointerSymbol: unique symbol;
6
+ export declare const ZapAddressPointerSymbol: unique symbol;
7
+ /** Returns the senders pubkey */
8
+ export declare function getZapSender(zap: NostrEvent): string;
9
+ /**
10
+ * Gets the receivers pubkey
11
+ * @throws
12
+ */
13
+ export declare function getZapRecipient(zap: NostrEvent): string;
14
+ /** Returns the parsed bolt11 invoice */
15
+ export declare function getZapPayment(zap: NostrEvent): import("./bolt11.js").ParsedInvoice | undefined;
16
+ /** Gets the AddressPointer that was zapped */
17
+ export declare function getZapAddressPointer(zap: NostrEvent): import("nostr-tools/nip19").AddressPointer | null;
18
+ /** Gets the EventPointer that was zapped */
19
+ export declare function getZapEventPointer(zap: NostrEvent): import("nostr-tools/nip19").EventPointer | null;
20
+ /** Gets the preimage for the bolt11 invoice */
21
+ export declare function getZapPreimage(zap: NostrEvent): string | undefined;
22
+ /**
23
+ * Returns the zap request event inside the zap receipt
24
+ * @throws
25
+ */
26
+ export declare function getZapRequest(zap: NostrEvent): import("nostr-tools").Event;
27
+ /**
28
+ * Checks if the zap is valid
29
+ * DOES NOT validate LNURL address
30
+ */
31
+ export declare function isValidZap(zap?: NostrEvent): boolean;
32
+ export type ZapSplit = {
33
+ pubkey: string;
34
+ percent: number;
35
+ weight: number;
36
+ relay?: string;
37
+ };
38
+ /** Returns the zap splits for an event */
39
+ export declare function getZapSplits(event: NostrEvent): ZapSplit[] | undefined;
@@ -0,0 +1,95 @@
1
+ import { kinds, nip57 } from "nostr-tools";
2
+ import { getOrComputeCachedValue } from "./cache.js";
3
+ import { getTagValue } from "./event.js";
4
+ import { isATag, isETag } from "./tags.js";
5
+ import { getAddressPointerFromATag, getEventPointerFromETag } from "./pointers.js";
6
+ import { parseBolt11 } from "./bolt11.js";
7
+ export const ZapRequestSymbol = Symbol.for("zap-request");
8
+ export const ZapFromSymbol = Symbol.for("zap-from");
9
+ export const ZapInvoiceSymbol = Symbol.for("zap-bolt11");
10
+ export const ZapEventPointerSymbol = Symbol.for("zap-event-pointer");
11
+ export const ZapAddressPointerSymbol = Symbol.for("zap-address-pointer");
12
+ /** Returns the senders pubkey */
13
+ export function getZapSender(zap) {
14
+ return getTagValue(zap, "P") || getZapRequest(zap).pubkey;
15
+ }
16
+ /**
17
+ * Gets the receivers pubkey
18
+ * @throws
19
+ */
20
+ export function getZapRecipient(zap) {
21
+ const recipient = getTagValue(zap, "p");
22
+ if (!recipient)
23
+ throw new Error("Missing recipient");
24
+ return recipient;
25
+ }
26
+ /** Returns the parsed bolt11 invoice */
27
+ export function getZapPayment(zap) {
28
+ return getOrComputeCachedValue(zap, ZapInvoiceSymbol, () => {
29
+ const bolt11 = getTagValue(zap, "bolt11");
30
+ return bolt11 ? parseBolt11(bolt11) : undefined;
31
+ });
32
+ }
33
+ /** Gets the AddressPointer that was zapped */
34
+ export function getZapAddressPointer(zap) {
35
+ return getOrComputeCachedValue(zap, ZapAddressPointerSymbol, () => {
36
+ const a = zap.tags.find(isATag);
37
+ return a ? getAddressPointerFromATag(a) : null;
38
+ });
39
+ }
40
+ /** Gets the EventPointer that was zapped */
41
+ export function getZapEventPointer(zap) {
42
+ return getOrComputeCachedValue(zap, ZapEventPointerSymbol, () => {
43
+ const e = zap.tags.find(isETag);
44
+ return e ? getEventPointerFromETag(e) : null;
45
+ });
46
+ }
47
+ /** Gets the preimage for the bolt11 invoice */
48
+ export function getZapPreimage(zap) {
49
+ return getTagValue(zap, "preimage");
50
+ }
51
+ /**
52
+ * Returns the zap request event inside the zap receipt
53
+ * @throws
54
+ */
55
+ export function getZapRequest(zap) {
56
+ return getOrComputeCachedValue(zap, ZapRequestSymbol, () => {
57
+ const description = getTagValue(zap, "description");
58
+ if (!description)
59
+ throw new Error("Missing description tag");
60
+ const error = nip57.validateZapRequest(description);
61
+ if (error)
62
+ throw new Error(error);
63
+ return JSON.parse(description);
64
+ });
65
+ }
66
+ /**
67
+ * Checks if the zap is valid
68
+ * DOES NOT validate LNURL address
69
+ */
70
+ export function isValidZap(zap) {
71
+ if (!zap)
72
+ return false;
73
+ if (zap.kind !== kinds.Zap)
74
+ return false;
75
+ try {
76
+ getZapRequest(zap);
77
+ getZapRecipient(zap);
78
+ return true;
79
+ }
80
+ catch (error) {
81
+ return false;
82
+ }
83
+ }
84
+ /** Returns the zap splits for an event */
85
+ export function getZapSplits(event) {
86
+ const tags = event.tags.filter((t) => t[0] === "zap" && t[1] && t[3]);
87
+ if (tags.length > 0) {
88
+ const targets = tags
89
+ .map((t) => ({ pubkey: t[1], relay: t[2], weight: parseFloat(t[3]) }))
90
+ .filter((p) => Number.isFinite(p.weight));
91
+ const total = targets.reduce((v, p) => v + p.weight, 0);
92
+ return targets.map((p) => ({ ...p, percent: p.weight / total }));
93
+ }
94
+ return undefined;
95
+ }
@@ -1,2 +1,3 @@
1
1
  import { Observable } from "rxjs";
2
+ /** Subscribes and returns the observables current or next value */
2
3
  export declare function getValue<T>(observable: Observable<T>): T | Promise<T>;
@@ -1,7 +1,10 @@
1
1
  import { BehaviorSubject } from "rxjs";
2
+ /** Subscribes and returns the observables current or next value */
2
3
  export function getValue(observable) {
3
4
  if (observable instanceof BehaviorSubject)
4
5
  return observable.value;
6
+ if (Reflect.has(observable, "value"))
7
+ return Reflect.get(observable, "value");
5
8
  return new Promise((res) => {
6
9
  const sub = observable.subscribe((v) => {
7
10
  res(v);
@@ -1,2 +1,2 @@
1
- export * from "./getValue.js";
1
+ export * from "./get-value.js";
2
2
  export * from "./share-latest-value.js";
@@ -1,2 +1,2 @@
1
- export * from "./getValue.js";
1
+ export * from "./get-value.js";
2
2
  export * from "./share-latest-value.js";
@@ -2,4 +2,5 @@ export type Deferred<T> = Promise<T> & {
2
2
  resolve: (value?: T | PromiseLike<T>) => void;
3
3
  reject: (reason?: any) => void;
4
4
  };
5
+ /** Creates a controlled promise */
5
6
  export declare function createDefer<T>(): Deferred<T>;
@@ -1,3 +1,4 @@
1
+ /** Creates a controlled promise */
1
2
  export function createDefer() {
2
3
  let _resolve;
3
4
  let _reject;
@@ -0,0 +1,4 @@
1
+ import { NostrEvent } from "nostr-tools";
2
+ import { Query } from "../query-store/index.js";
3
+ /** Returns all NIP-22 comment replies for the event */
4
+ export declare function CommentsQuery(parent: NostrEvent): Query<NostrEvent[]>;
@@ -0,0 +1,14 @@
1
+ import { COMMENT_KIND, getEventUID } from "../helpers/index.js";
2
+ import { isParameterizedReplaceableKind } from "nostr-tools/kinds";
3
+ /** Returns all NIP-22 comment replies for the event */
4
+ export function CommentsQuery(parent) {
5
+ return {
6
+ key: `${getEventUID(parent)}-comments`,
7
+ run: (events) => {
8
+ const filter = { kinds: [COMMENT_KIND], "#e": [parent.id] };
9
+ if (isParameterizedReplaceableKind(parent.kind))
10
+ filter["#a"] = [getEventUID(parent)];
11
+ return events.timeline(filter);
12
+ },
13
+ };
14
+ }
@@ -1,5 +1,7 @@
1
- export * from "./simple.js";
2
- export * from "./profile.js";
1
+ export * from "./comments.js";
3
2
  export * from "./mailboxes.js";
3
+ export * from "./profile.js";
4
4
  export * from "./reactions.js";
5
+ export * from "./simple.js";
5
6
  export * from "./thread.js";
7
+ export * from "./zaps.js";
@@ -1,5 +1,7 @@
1
- export * from "./simple.js";
2
- export * from "./profile.js";
1
+ export * from "./comments.js";
3
2
  export * from "./mailboxes.js";
3
+ export * from "./profile.js";
4
4
  export * from "./reactions.js";
5
+ export * from "./simple.js";
5
6
  export * from "./thread.js";
7
+ export * from "./zaps.js";
@@ -1,4 +1,5 @@
1
1
  import { Query } from "../query-store/index.js";
2
+ /** A query that gets and parses the inbox and outbox relays for a pubkey */
2
3
  export declare function MailboxesQuery(pubkey: string): Query<{
3
4
  inboxes: string[];
4
5
  outboxes: string[];
@@ -1,6 +1,7 @@
1
1
  import { kinds } from "nostr-tools";
2
2
  import { map } from "rxjs/operators";
3
3
  import { getInboxes, getOutboxes } from "../helpers/mailboxes.js";
4
+ /** A query that gets and parses the inbox and outbox relays for a pubkey */
4
5
  export function MailboxesQuery(pubkey) {
5
6
  return {
6
7
  key: pubkey,
@@ -1,3 +1,4 @@
1
1
  import { ProfileContent } from "../helpers/profile.js";
2
2
  import { Query } from "../query-store/index.js";
3
+ /** A query that gets and parses the kind 0 metadata for a pubkey */
3
4
  export declare function ProfileQuery(pubkey: string): Query<ProfileContent | undefined>;
@@ -1,11 +1,12 @@
1
1
  import { kinds } from "nostr-tools";
2
- import { map } from "rxjs/operators";
3
- import { getProfileContent } from "../helpers/profile.js";
2
+ import { filter, map } from "rxjs/operators";
3
+ import { getProfileContent, isValidProfile } from "../helpers/profile.js";
4
+ /** A query that gets and parses the kind 0 metadata for a pubkey */
4
5
  export function ProfileQuery(pubkey) {
5
6
  return {
6
7
  key: pubkey,
7
8
  run: (events) => {
8
- return events.replaceable(kinds.Metadata, pubkey).pipe(map((event) => event && getProfileContent(event)));
9
+ return events.replaceable(kinds.Metadata, pubkey).pipe(filter(isValidProfile), map((event) => event && getProfileContent(event)));
9
10
  },
10
11
  };
11
12
  }
@@ -1,4 +1,4 @@
1
1
  import { NostrEvent } from "nostr-tools";
2
2
  import { Query } from "../query-store/index.js";
3
- /** Creates a query that returns all reactions to an event (supports replaceable events) */
3
+ /** A query that returns all reactions to an event (supports replaceable events) */
4
4
  export declare function ReactionsQuery(event: NostrEvent): Query<NostrEvent[]>;
@@ -1,6 +1,6 @@
1
1
  import { kinds } from "nostr-tools";
2
2
  import { getEventUID, isReplaceable } from "../helpers/event.js";
3
- /** Creates a query that returns all reactions to an event (supports replaceable events) */
3
+ /** A query that returns all reactions to an event (supports replaceable events) */
4
4
  export function ReactionsQuery(event) {
5
5
  return {
6
6
  key: getEventUID(event),
@@ -1,13 +1,13 @@
1
1
  import { Filter, NostrEvent } from "nostr-tools";
2
2
  import { Query } from "../query-store/index.js";
3
3
  /** Creates a Query that returns a single event or undefined */
4
- export declare function SingleEventQuery(uid: string): Query<NostrEvent | undefined>;
4
+ export declare function SingleEventQuery(id: string): Query<NostrEvent | undefined>;
5
5
  /** Creates a Query that returns a multiple events in a map */
6
- export declare function MultipleEventsQuery(uids: string[]): Query<Map<string, NostrEvent>>;
6
+ export declare function MultipleEventsQuery(ids: string[]): Query<Map<string, NostrEvent>>;
7
7
  /** Creates a Query returning the latest version of a replaceable event */
8
8
  export declare function ReplaceableQuery(kind: number, pubkey: string, d?: string): Query<NostrEvent | undefined>;
9
9
  /** Creates a Query that returns an array of sorted events matching the filters */
10
- export declare function TimelineQuery(filters: Filter | Filter[]): Query<NostrEvent[]>;
10
+ export declare function TimelineQuery(filters: Filter | Filter[], keepOldVersions?: boolean): Query<NostrEvent[]>;
11
11
  /** Creates a Query that returns a directory of events by their UID */
12
12
  export declare function ReplaceableSetQuery(pointers: {
13
13
  kind: number;
@@ -1,17 +1,17 @@
1
- import stringify from "json-stringify-deterministic";
1
+ import hash_sum from "hash-sum";
2
2
  import { getReplaceableUID } from "../helpers/event.js";
3
3
  /** Creates a Query that returns a single event or undefined */
4
- export function SingleEventQuery(uid) {
4
+ export function SingleEventQuery(id) {
5
5
  return {
6
- key: uid,
7
- run: (events) => events.event(uid),
6
+ key: id,
7
+ run: (events) => events.event(id),
8
8
  };
9
9
  }
10
10
  /** Creates a Query that returns a multiple events in a map */
11
- export function MultipleEventsQuery(uids) {
11
+ export function MultipleEventsQuery(ids) {
12
12
  return {
13
- key: uids.join(","),
14
- run: (events) => events.events(uids),
13
+ key: ids.join(","),
14
+ run: (events) => events.events(ids),
15
15
  };
16
16
  }
17
17
  /** Creates a Query returning the latest version of a replaceable event */
@@ -22,17 +22,17 @@ export function ReplaceableQuery(kind, pubkey, d) {
22
22
  };
23
23
  }
24
24
  /** Creates a Query that returns an array of sorted events matching the filters */
25
- export function TimelineQuery(filters) {
25
+ export function TimelineQuery(filters, keepOldVersions) {
26
+ filters = Array.isArray(filters) ? filters : [filters];
26
27
  return {
27
- key: stringify(filters),
28
- run: (events) => events.timeline(Array.isArray(filters) ? filters : [filters]),
28
+ key: hash_sum(filters) + (keepOldVersions ? "-history" : ""),
29
+ run: (events) => events.timeline(filters, keepOldVersions),
29
30
  };
30
31
  }
31
32
  /** Creates a Query that returns a directory of events by their UID */
32
33
  export function ReplaceableSetQuery(pointers) {
33
- const cords = pointers.map((pointer) => getReplaceableUID(pointer.kind, pointer.pubkey, pointer.identifier));
34
34
  return {
35
- key: stringify(pointers),
36
- run: (events) => events.events(cords),
35
+ key: hash_sum(pointers),
36
+ run: (events) => events.replaceableSet(pointers),
37
37
  };
38
38
  }
@@ -21,3 +21,5 @@ export type ThreadQueryOptions = {
21
21
  kinds?: number[];
22
22
  };
23
23
  export declare function ThreadQuery(root: string | AddressPointer | EventPointer, opts?: ThreadQueryOptions): Query<Thread>;
24
+ /** A query that gets all legacy and NIP-10, and NIP-22 replies for an event */
25
+ export declare function RepliesQuery(event: NostrEvent, overrideKinds?: number[]): Query<NostrEvent[]>;
@@ -1,8 +1,10 @@
1
1
  import { kinds } from "nostr-tools";
2
+ import { isParameterizedReplaceableKind } from "nostr-tools/kinds";
2
3
  import { map } from "rxjs/operators";
3
- import { getNip10References } from "../helpers/threading.js";
4
- import { getCoordinateFromAddressPointer, isAddressPointer } from "../helpers/pointers.js";
5
- import { getEventUID } from "../helpers/event.js";
4
+ import { getNip10References, interpretThreadTags } from "../helpers/threading.js";
5
+ import { getCoordinateFromAddressPointer, isAddressPointer, isEventPointer } from "../helpers/pointers.js";
6
+ import { getEventUID, getReplaceableUID, getTagValue, isEvent } from "../helpers/event.js";
7
+ import { COMMENT_KIND } from "../helpers/comment.js";
6
8
  const defaultOptions = {
7
9
  kinds: [kinds.ShortTextNote],
8
10
  };
@@ -64,3 +66,27 @@ export function ThreadQuery(root, opts) {
64
66
  })),
65
67
  };
66
68
  }
69
+ /** A query that gets all legacy and NIP-10, and NIP-22 replies for an event */
70
+ export function RepliesQuery(event, overrideKinds) {
71
+ return {
72
+ key: getEventUID(event),
73
+ run: (events) => {
74
+ const kinds = overrideKinds || event.kind === 1 ? [1, COMMENT_KIND] : [COMMENT_KIND];
75
+ const filter = { kinds };
76
+ if (isEvent(parent) || isEventPointer(event))
77
+ filter["#e"] = [event.id];
78
+ const address = isParameterizedReplaceableKind(event.kind)
79
+ ? getReplaceableUID(event.kind, event.pubkey, getTagValue(event, "d"))
80
+ : undefined;
81
+ if (address) {
82
+ filter["#a"] = [address];
83
+ }
84
+ return events.timeline(filter).pipe(map((events) => {
85
+ return events.filter((e) => {
86
+ const refs = interpretThreadTags(e.tags);
87
+ return refs.reply?.e?.[1] === event.id || refs.reply?.a?.[1] === address;
88
+ });
89
+ }));
90
+ },
91
+ };
92
+ }
@@ -0,0 +1,5 @@
1
+ import { AddressPointer, EventPointer } from "nostr-tools/nip19";
2
+ import { NostrEvent } from "nostr-tools";
3
+ import { Query } from "../query-store/index.js";
4
+ /** A query that gets all zap events for an event */
5
+ export declare function EventZapsQuery(id: string | EventPointer | AddressPointer): Query<NostrEvent[]>;
@@ -0,0 +1,21 @@
1
+ import { map } from "rxjs";
2
+ import { kinds } from "nostr-tools";
3
+ import { getCoordinateFromAddressPointer, isAddressPointer } from "../helpers/pointers.js";
4
+ import { isValidZap } from "../helpers/zap.js";
5
+ /** A query that gets all zap events for an event */
6
+ export function EventZapsQuery(id) {
7
+ return {
8
+ key: JSON.stringify(id),
9
+ run: (events) => {
10
+ if (isAddressPointer(id)) {
11
+ return events
12
+ .timeline([{ kinds: [kinds.Zap], "#a": [getCoordinateFromAddressPointer(id)] }])
13
+ .pipe(map((events) => events.filter(isValidZap)));
14
+ }
15
+ else {
16
+ id = typeof id === "string" ? id : id.id;
17
+ return events.timeline([{ kinds: [kinds.Zap], "#e": [id] }]).pipe(map((events) => events.filter(isValidZap)));
18
+ }
19
+ },
20
+ };
21
+ }