applesauce-core 0.0.0-next-20250423151245 → 0.0.0-next-20250424151127
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.
- package/dist/event-store/database.js +3 -3
- package/dist/event-store/event-store.js +4 -4
- package/dist/helpers/__tests__/bookmarks.test.d.ts +1 -0
- package/dist/helpers/__tests__/bookmarks.test.js +88 -0
- package/dist/helpers/__tests__/contacts.test.d.ts +1 -0
- package/dist/helpers/__tests__/contacts.test.js +34 -0
- package/dist/helpers/__tests__/events.test.d.ts +1 -0
- package/dist/helpers/__tests__/events.test.js +32 -0
- package/dist/helpers/__tests__/mutes.test.d.ts +1 -0
- package/dist/helpers/__tests__/mutes.test.js +55 -0
- package/dist/helpers/bookmarks.d.ts +6 -1
- package/dist/helpers/bookmarks.js +52 -7
- package/dist/helpers/contacts.d.ts +11 -0
- package/dist/helpers/contacts.js +34 -0
- package/dist/helpers/event.d.ts +8 -3
- package/dist/helpers/event.js +21 -13
- package/dist/helpers/lists.js +2 -2
- package/dist/helpers/mutes.d.ts +8 -0
- package/dist/helpers/mutes.js +66 -5
- package/dist/helpers/nip-19.d.ts +14 -0
- package/dist/helpers/nip-19.js +29 -0
- package/dist/helpers/pointers.js +3 -3
- package/dist/observable/__tests__/listen-latest-updates.test.d.ts +1 -0
- package/dist/observable/__tests__/listen-latest-updates.test.js +55 -0
- package/dist/observable/defined.d.ts +3 -0
- package/dist/observable/defined.js +5 -0
- package/dist/observable/get-observable-value.d.ts +4 -1
- package/dist/observable/get-observable-value.js +4 -1
- package/dist/observable/index.d.ts +3 -1
- package/dist/observable/index.js +3 -1
- package/dist/observable/listen-latest-updates.d.ts +5 -0
- package/dist/observable/listen-latest-updates.js +12 -0
- package/dist/queries/blossom.js +1 -6
- package/dist/queries/bookmarks.d.ts +5 -5
- package/dist/queries/bookmarks.js +18 -17
- package/dist/queries/channels.js +41 -53
- package/dist/queries/comments.js +6 -9
- package/dist/queries/contacts.d.ts +6 -1
- package/dist/queries/contacts.js +21 -9
- package/dist/queries/mailboxes.js +4 -7
- package/dist/queries/mutes.d.ts +6 -6
- package/dist/queries/mutes.js +20 -19
- package/dist/queries/pins.d.ts +1 -0
- package/dist/queries/pins.js +4 -6
- package/dist/queries/profile.js +1 -6
- package/dist/queries/reactions.js +11 -14
- package/dist/queries/simple.js +5 -22
- package/dist/queries/thread.js +45 -51
- package/dist/queries/user-status.js +23 -29
- package/dist/queries/zaps.js +10 -13
- package/dist/query-store/query-store.d.ts +7 -6
- package/dist/query-store/query-store.js +13 -8
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { binarySearch, insertEventIntoDescendingList } from "nostr-tools/utils";
|
|
2
2
|
import { Subject } from "rxjs";
|
|
3
|
-
import { getEventUID, getIndexableTags,
|
|
3
|
+
import { getEventUID, getIndexableTags, createReplaceableAddress, isReplaceable } from "../helpers/event.js";
|
|
4
4
|
import { INDEXABLE_TAGS } from "./common.js";
|
|
5
5
|
import { logger } from "../logger.js";
|
|
6
6
|
import { LRU } from "../helpers/lru.js";
|
|
@@ -73,12 +73,12 @@ export class Database {
|
|
|
73
73
|
}
|
|
74
74
|
/** Checks if the database contains a replaceable event without touching it */
|
|
75
75
|
hasReplaceable(kind, pubkey, d) {
|
|
76
|
-
const events = this.replaceable.get(
|
|
76
|
+
const events = this.replaceable.get(createReplaceableAddress(kind, pubkey, d));
|
|
77
77
|
return !!events && events.length > 0;
|
|
78
78
|
}
|
|
79
79
|
/** Gets an array of replaceable events */
|
|
80
80
|
getReplaceable(kind, pubkey, d) {
|
|
81
|
-
return this.replaceable.get(
|
|
81
|
+
return this.replaceable.get(createReplaceableAddress(kind, pubkey, d));
|
|
82
82
|
}
|
|
83
83
|
/** Inserts an event into the database and notifies all subscriptions */
|
|
84
84
|
addEvent(event) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { kinds } from "nostr-tools";
|
|
2
2
|
import { insertEventIntoDescendingList } from "nostr-tools/utils";
|
|
3
|
-
import {
|
|
3
|
+
import { isAddressableKind } from "nostr-tools/kinds";
|
|
4
4
|
import { defer, distinctUntilChanged, EMPTY, endWith, filter, finalize, from, map, merge, mergeMap, mergeWith, of, repeat, scan, take, takeUntil, tap, } from "rxjs";
|
|
5
5
|
import { Database } from "./database.js";
|
|
6
|
-
import { FromCacheSymbol, getEventUID, getReplaceableIdentifier,
|
|
6
|
+
import { FromCacheSymbol, getEventUID, getReplaceableIdentifier, createReplaceableAddress, getTagValue, isReplaceable, } from "../helpers/event.js";
|
|
7
7
|
import { matchFilters } from "../helpers/filter.js";
|
|
8
8
|
import { addSeenRelay, getSeenRelays } from "../helpers/relays.js";
|
|
9
9
|
import { getDeleteCoordinates, getDeleteIds } from "../helpers/delete.js";
|
|
@@ -54,7 +54,7 @@ export class EventStore {
|
|
|
54
54
|
else {
|
|
55
55
|
if (this.deletedIds.has(event.id))
|
|
56
56
|
return true;
|
|
57
|
-
if (
|
|
57
|
+
if (isAddressableKind(event.kind)) {
|
|
58
58
|
const deleted = this.deletedCoords.get(getEventUID(event));
|
|
59
59
|
if (deleted)
|
|
60
60
|
return deleted > event.created_at;
|
|
@@ -295,7 +295,7 @@ export class EventStore {
|
|
|
295
295
|
}
|
|
296
296
|
/** Creates an observable that subscribes to the latest version of an array of replaceable events*/
|
|
297
297
|
replaceableSet(pointers) {
|
|
298
|
-
const uids = new Set(pointers.map((p) =>
|
|
298
|
+
const uids = new Set(pointers.map((p) => createReplaceableAddress(p.kind, p.pubkey, p.identifier)));
|
|
299
299
|
return merge(
|
|
300
300
|
// start with existing events
|
|
301
301
|
defer(() => from(pointers.map((p) => this.getReplaceable(p.kind, p.pubkey, p.identifier)))),
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { kinds } from "nostr-tools";
|
|
3
|
+
import { mergeBookmarks } from "../bookmarks.js";
|
|
4
|
+
describe("mergeBookmarks", () => {
|
|
5
|
+
it("should merge bookmarks and handle duplicates", () => {
|
|
6
|
+
// Create test data with some duplicates
|
|
7
|
+
const eventPointer1 = {
|
|
8
|
+
id: "event1",
|
|
9
|
+
relays: ["wss://relay1.com/", "wss://relay2.com/"],
|
|
10
|
+
author: "author1",
|
|
11
|
+
};
|
|
12
|
+
const eventPointer2 = {
|
|
13
|
+
id: "event1", // Same ID as eventPointer1
|
|
14
|
+
relays: ["wss://relay2.com/", "wss://relay3.com/"],
|
|
15
|
+
author: "author1",
|
|
16
|
+
};
|
|
17
|
+
const eventPointer3 = {
|
|
18
|
+
id: "event2",
|
|
19
|
+
relays: ["wss://relay1.com/"],
|
|
20
|
+
author: "author2",
|
|
21
|
+
};
|
|
22
|
+
const addressPointer1 = {
|
|
23
|
+
kind: kinds.LongFormArticle,
|
|
24
|
+
pubkey: "pubkey1",
|
|
25
|
+
identifier: "article1",
|
|
26
|
+
relays: ["wss://relay1.com/", "wss://relay2.com/"],
|
|
27
|
+
};
|
|
28
|
+
const addressPointer2 = {
|
|
29
|
+
kind: kinds.LongFormArticle,
|
|
30
|
+
pubkey: "pubkey1",
|
|
31
|
+
identifier: "article1", // Same as addressPointer1
|
|
32
|
+
relays: ["wss://relay3.com/"],
|
|
33
|
+
};
|
|
34
|
+
const bookmark1 = {
|
|
35
|
+
notes: [eventPointer1],
|
|
36
|
+
articles: [addressPointer1],
|
|
37
|
+
hashtags: ["tag1", "tag2"],
|
|
38
|
+
urls: ["https://example1.com/"],
|
|
39
|
+
};
|
|
40
|
+
const bookmark2 = {
|
|
41
|
+
notes: [eventPointer2, eventPointer3],
|
|
42
|
+
articles: [addressPointer2],
|
|
43
|
+
hashtags: ["tag2", "tag3"],
|
|
44
|
+
urls: ["https://example1.com/", "https://example2.com/"],
|
|
45
|
+
};
|
|
46
|
+
const result = mergeBookmarks(bookmark1, bookmark2);
|
|
47
|
+
// Check that duplicates are properly merged
|
|
48
|
+
expect(result.notes).toHaveLength(2); // event1 should be merged, plus event2
|
|
49
|
+
expect(result.articles).toHaveLength(1); // article1 should be merged
|
|
50
|
+
expect(result.hashtags).toHaveLength(3); // unique tags
|
|
51
|
+
expect(result.urls).toHaveLength(2); // unique urls
|
|
52
|
+
// Check that relays are merged for duplicate event
|
|
53
|
+
const mergedEvent = result.notes.find((note) => note.id === "event1");
|
|
54
|
+
expect(mergedEvent?.relays).toHaveLength(3);
|
|
55
|
+
expect(mergedEvent?.relays).toContain("wss://relay1.com/");
|
|
56
|
+
expect(mergedEvent?.relays).toContain("wss://relay2.com/");
|
|
57
|
+
expect(mergedEvent?.relays).toContain("wss://relay3.com/");
|
|
58
|
+
// Check that relays are merged for duplicate article
|
|
59
|
+
const mergedArticle = result.articles[0];
|
|
60
|
+
expect(mergedArticle.relays).toHaveLength(3);
|
|
61
|
+
expect(mergedArticle.relays).toContain("wss://relay1.com/");
|
|
62
|
+
expect(mergedArticle.relays).toContain("wss://relay2.com/");
|
|
63
|
+
expect(mergedArticle.relays).toContain("wss://relay3.com/");
|
|
64
|
+
// Check that hashtags are unique
|
|
65
|
+
expect(result.hashtags).toContain("tag1");
|
|
66
|
+
expect(result.hashtags).toContain("tag2");
|
|
67
|
+
expect(result.hashtags).toContain("tag3");
|
|
68
|
+
// Check that urls are unique
|
|
69
|
+
expect(result.urls).toContain("https://example1.com/");
|
|
70
|
+
expect(result.urls).toContain("https://example2.com/");
|
|
71
|
+
});
|
|
72
|
+
it("should handle undefined bookmarks", () => {
|
|
73
|
+
const bookmark = {
|
|
74
|
+
notes: [{ id: "event1", relays: ["wss://relay1.com/"] }],
|
|
75
|
+
articles: [],
|
|
76
|
+
hashtags: ["tag1"],
|
|
77
|
+
urls: ["https://example.com/"],
|
|
78
|
+
};
|
|
79
|
+
const result = mergeBookmarks(bookmark, undefined);
|
|
80
|
+
expect(result).toEqual(bookmark);
|
|
81
|
+
expect(mergeBookmarks(undefined, undefined)).toEqual({
|
|
82
|
+
notes: [],
|
|
83
|
+
articles: [],
|
|
84
|
+
hashtags: [],
|
|
85
|
+
urls: [],
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { mergeContacts } from "../contacts.js";
|
|
3
|
+
describe("mergeContacts", () => {
|
|
4
|
+
it("should merge contacts and remove duplicates", () => {
|
|
5
|
+
// Create some test profile pointers
|
|
6
|
+
const pointer1 = { pubkey: "pubkey1", relays: ["relay1"] };
|
|
7
|
+
const pointer2 = { pubkey: "pubkey2" }; // No relays
|
|
8
|
+
const pointer3 = { pubkey: "pubkey3", relays: ["relay3"] };
|
|
9
|
+
const pointer4 = { pubkey: "pubkey1" }; // Duplicate pubkey without relays
|
|
10
|
+
// Test merging arrays of pointers
|
|
11
|
+
const result1 = mergeContacts([pointer1, pointer2], [pointer3, pointer4]);
|
|
12
|
+
// Should have 3 unique pubkeys
|
|
13
|
+
expect(result1.length).toBe(3);
|
|
14
|
+
// Check that the duplicate was handled correctly (last one should win)
|
|
15
|
+
const pubkey1Entry = result1.find((p) => p.pubkey === "pubkey1");
|
|
16
|
+
expect(pubkey1Entry).toBeDefined();
|
|
17
|
+
expect(pubkey1Entry?.relays).toBeUndefined();
|
|
18
|
+
// Test with undefined values
|
|
19
|
+
const result2 = mergeContacts([pointer1], undefined, [pointer2, undefined]);
|
|
20
|
+
expect(result2.length).toBe(2);
|
|
21
|
+
// Test with single pointers
|
|
22
|
+
const result3 = mergeContacts(pointer1, pointer2, pointer1);
|
|
23
|
+
expect(result3.length).toBe(2);
|
|
24
|
+
// Test with empty arrays
|
|
25
|
+
const result4 = mergeContacts([], [pointer1], []);
|
|
26
|
+
expect(result4.length).toBe(1);
|
|
27
|
+
// Test with pointers that have and don't have relays
|
|
28
|
+
const pointer5 = { pubkey: "pubkey5", relays: ["relay5"] };
|
|
29
|
+
const pointer6 = { pubkey: "pubkey5" }; // Same pubkey without relays
|
|
30
|
+
const result5 = mergeContacts([pointer5], [pointer6]);
|
|
31
|
+
expect(result5.length).toBe(1);
|
|
32
|
+
expect(result5[0].relays).toBeUndefined();
|
|
33
|
+
});
|
|
34
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { kinds } from "nostr-tools";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
import { FakeUser } from "../../__tests__/fixtures.js";
|
|
4
|
+
import { getReplaceableAddress } from "../event.js";
|
|
5
|
+
const user = new FakeUser();
|
|
6
|
+
describe("getReplaceableAddress", () => {
|
|
7
|
+
it("should throw an error for non-replaceable events", () => {
|
|
8
|
+
const normalEvent = user.note("Hello world");
|
|
9
|
+
expect(() => {
|
|
10
|
+
getReplaceableAddress(normalEvent);
|
|
11
|
+
}).toThrow("Event is not replaceable or addressable");
|
|
12
|
+
});
|
|
13
|
+
it("should return the correct address for replaceable events", () => {
|
|
14
|
+
const replaceableEvent = user.event({
|
|
15
|
+
kind: kinds.Metadata,
|
|
16
|
+
content: JSON.stringify({ name: "Test User" }),
|
|
17
|
+
tags: [],
|
|
18
|
+
});
|
|
19
|
+
const expectedAddress = `${kinds.Metadata}:${user.pubkey}:`;
|
|
20
|
+
expect(getReplaceableAddress(replaceableEvent)).toBe(expectedAddress);
|
|
21
|
+
});
|
|
22
|
+
it("should include the identifier for addressable events", () => {
|
|
23
|
+
const identifier = "test-profile";
|
|
24
|
+
const addressableEvent = user.event({
|
|
25
|
+
kind: 30000, // Parameterized replaceable event
|
|
26
|
+
content: "Test content",
|
|
27
|
+
tags: [["d", identifier]],
|
|
28
|
+
});
|
|
29
|
+
const expectedAddress = `30000:${user.pubkey}:${identifier}`;
|
|
30
|
+
expect(getReplaceableAddress(addressableEvent)).toBe(expectedAddress);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { matchMutes } from "../mutes.js";
|
|
3
|
+
import { FakeUser } from "../../__tests__/fixtures.js";
|
|
4
|
+
const mutedUser = new FakeUser();
|
|
5
|
+
const nonMutedUser = new FakeUser();
|
|
6
|
+
const thread = nonMutedUser.note("Hello world");
|
|
7
|
+
// Create a mutes object with a pubkey to mute
|
|
8
|
+
const mutes = {
|
|
9
|
+
pubkeys: new Set([mutedUser.pubkey]),
|
|
10
|
+
threads: new Set([thread.id]),
|
|
11
|
+
hashtags: new Set(["nostr"]),
|
|
12
|
+
words: new Set(["GM"]),
|
|
13
|
+
};
|
|
14
|
+
describe("matchMutes", () => {
|
|
15
|
+
it("should match events with muted pubkeys", () => {
|
|
16
|
+
const mutedEvent = mutedUser.note("Hello world");
|
|
17
|
+
const nonMutedEvent = nonMutedUser.note("Hello world");
|
|
18
|
+
// The event with the muted pubkey should match
|
|
19
|
+
expect(matchMutes(mutes, mutedEvent)).toBe(true);
|
|
20
|
+
// The event with a different pubkey should not match
|
|
21
|
+
expect(matchMutes(mutes, nonMutedEvent)).toBe(false);
|
|
22
|
+
});
|
|
23
|
+
it("should match events with muted hashtags", () => {
|
|
24
|
+
// Create events with and without the muted hashtag
|
|
25
|
+
const eventWithMutedHashtag = nonMutedUser.note("Hello world");
|
|
26
|
+
eventWithMutedHashtag.tags.push(["t", "nostr"]);
|
|
27
|
+
const eventWithDifferentHashtag = nonMutedUser.note("Hello world");
|
|
28
|
+
eventWithDifferentHashtag.tags.push(["t", "bitcoin"]);
|
|
29
|
+
const eventWithNoHashtag = nonMutedUser.note("Hello world");
|
|
30
|
+
// The event with the muted hashtag should match
|
|
31
|
+
expect(matchMutes(mutes, eventWithMutedHashtag)).toBe(true);
|
|
32
|
+
// The events without the muted hashtag should not match
|
|
33
|
+
expect(matchMutes(mutes, eventWithDifferentHashtag)).toBe(false);
|
|
34
|
+
expect(matchMutes(mutes, eventWithNoHashtag)).toBe(false);
|
|
35
|
+
});
|
|
36
|
+
it("should match events within threads", () => {
|
|
37
|
+
// Create a reply to the thread
|
|
38
|
+
const reply = nonMutedUser.note("Hello world");
|
|
39
|
+
reply.tags.push(["e", thread.id, "", "root"]);
|
|
40
|
+
// The reply should match the mute
|
|
41
|
+
expect(matchMutes(mutes, reply)).toBe(true);
|
|
42
|
+
// The thread should not match the mute
|
|
43
|
+
expect(matchMutes(mutes, thread)).toBe(false);
|
|
44
|
+
});
|
|
45
|
+
it("should match events with muted words", () => {
|
|
46
|
+
// The event with the muted word should match
|
|
47
|
+
expect(matchMutes(mutes, nonMutedUser.note("GM"))).toBe(true);
|
|
48
|
+
// Should not match other words that contain the muted word
|
|
49
|
+
expect(matchMutes(mutes, nonMutedUser.note("GMing"))).toBe(false);
|
|
50
|
+
// Should be case-insensitive
|
|
51
|
+
expect(matchMutes(mutes, nonMutedUser.note("gm"))).toBe(true);
|
|
52
|
+
// Should match if the muted word
|
|
53
|
+
expect(matchMutes(mutes, nonMutedUser.note("Hello GM world"))).toBe(true);
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -8,8 +8,13 @@ export type Bookmarks = {
|
|
|
8
8
|
hashtags: string[];
|
|
9
9
|
urls: string[];
|
|
10
10
|
};
|
|
11
|
+
/** Parses an array of tags into a {@link Bookmarks} object */
|
|
11
12
|
export declare function parseBookmarkTags(tags: string[][]): Bookmarks;
|
|
12
|
-
/**
|
|
13
|
+
/** Merges any number of {@link Bookmarks} objects */
|
|
14
|
+
export declare function mergeBookmarks(...bookmarks: (Bookmarks | undefined)[]): Bookmarks;
|
|
15
|
+
/** Returns all the bookmarks of the event */
|
|
13
16
|
export declare function getBookmarks(bookmark: NostrEvent): Bookmarks;
|
|
17
|
+
/** Returns the public bookmarks of the event */
|
|
18
|
+
export declare function getPublicBookmarks(bookmark: NostrEvent): Bookmarks;
|
|
14
19
|
/** Returns the bookmarks of the event if its unlocked */
|
|
15
20
|
export declare function getHiddenBookmarks(bookmark: NostrEvent): Bookmarks | undefined;
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { kinds } from "nostr-tools";
|
|
2
|
-
import { getAddressPointerFromATag, getEventPointerFromETag } from "./pointers.js";
|
|
3
2
|
import { getOrComputeCachedValue } from "./cache.js";
|
|
4
|
-
import { getHiddenTags } from "./index.js";
|
|
3
|
+
import { getHiddenTags, isHiddenTagsLocked } from "./index.js";
|
|
4
|
+
import { mergeAddressPointers, mergeEventPointers } from "./nip-19.js";
|
|
5
|
+
import { getAddressPointerFromATag, getCoordinateFromAddressPointer, getEventPointerFromETag } from "./pointers.js";
|
|
5
6
|
export const BookmarkPublicSymbol = Symbol.for("bookmark-public");
|
|
6
7
|
export const BookmarkHiddenSymbol = Symbol.for("bookmark-hidden");
|
|
8
|
+
/** Parses an array of tags into a {@link Bookmarks} object */
|
|
7
9
|
export function parseBookmarkTags(tags) {
|
|
8
10
|
const notes = tags.filter((t) => t[0] === "e" && t[1]).map(getEventPointerFromETag);
|
|
9
11
|
const articles = tags
|
|
@@ -14,14 +16,57 @@ export function parseBookmarkTags(tags) {
|
|
|
14
16
|
const urls = tags.filter((t) => t[0] === "r" && t[1]).map((t) => t[1]);
|
|
15
17
|
return { notes, articles, hashtags, urls };
|
|
16
18
|
}
|
|
17
|
-
/**
|
|
19
|
+
/** Merges any number of {@link Bookmarks} objects */
|
|
20
|
+
export function mergeBookmarks(...bookmarks) {
|
|
21
|
+
const notes = new Map();
|
|
22
|
+
const articles = new Map();
|
|
23
|
+
const hashtags = new Set();
|
|
24
|
+
const urls = new Set();
|
|
25
|
+
for (const bookmark of bookmarks) {
|
|
26
|
+
if (!bookmark)
|
|
27
|
+
continue;
|
|
28
|
+
for (const note of bookmark.notes) {
|
|
29
|
+
const existing = notes.get(note.id);
|
|
30
|
+
if (existing)
|
|
31
|
+
notes.set(note.id, mergeEventPointers(existing, note));
|
|
32
|
+
else
|
|
33
|
+
notes.set(note.id, note);
|
|
34
|
+
}
|
|
35
|
+
for (const article of bookmark.articles) {
|
|
36
|
+
const coord = getCoordinateFromAddressPointer(article);
|
|
37
|
+
const existing = articles.get(coord);
|
|
38
|
+
if (existing)
|
|
39
|
+
articles.set(coord, mergeAddressPointers(existing, article));
|
|
40
|
+
else
|
|
41
|
+
articles.set(coord, article);
|
|
42
|
+
}
|
|
43
|
+
for (const hashtag of bookmark.hashtags)
|
|
44
|
+
hashtags.add(hashtag);
|
|
45
|
+
for (const url of bookmark.urls)
|
|
46
|
+
urls.add(url);
|
|
47
|
+
}
|
|
48
|
+
return {
|
|
49
|
+
notes: Array.from(notes.values()),
|
|
50
|
+
articles: Array.from(articles.values()),
|
|
51
|
+
hashtags: Array.from(hashtags),
|
|
52
|
+
urls: Array.from(urls),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** Returns all the bookmarks of the event */
|
|
18
56
|
export function getBookmarks(bookmark) {
|
|
57
|
+
const hidden = getHiddenBookmarks(bookmark);
|
|
58
|
+
if (hidden)
|
|
59
|
+
return mergeBookmarks(hidden, getPublicBookmarks(bookmark));
|
|
60
|
+
else
|
|
61
|
+
return getPublicBookmarks(bookmark);
|
|
62
|
+
}
|
|
63
|
+
/** Returns the public bookmarks of the event */
|
|
64
|
+
export function getPublicBookmarks(bookmark) {
|
|
19
65
|
return getOrComputeCachedValue(bookmark, BookmarkPublicSymbol, () => parseBookmarkTags(bookmark.tags));
|
|
20
66
|
}
|
|
21
67
|
/** Returns the bookmarks of the event if its unlocked */
|
|
22
68
|
export function getHiddenBookmarks(bookmark) {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
});
|
|
69
|
+
if (isHiddenTagsLocked(bookmark))
|
|
70
|
+
return undefined;
|
|
71
|
+
return getOrComputeCachedValue(bookmark, BookmarkHiddenSymbol, () => parseBookmarkTags(getHiddenTags(bookmark)));
|
|
27
72
|
}
|
|
@@ -1,3 +1,14 @@
|
|
|
1
1
|
import { NostrEvent } from "nostr-tools";
|
|
2
|
+
import { ProfilePointer } from "nostr-tools/nip19";
|
|
2
3
|
export declare const ContactsRelaysSymbol: unique symbol;
|
|
4
|
+
export declare const PublicContactsSymbol: unique symbol;
|
|
5
|
+
export declare const HiddenContactsSymbol: unique symbol;
|
|
3
6
|
export declare function getRelaysFromContactsEvent(event: NostrEvent): Map<string, "all" | "inbox" | "outbox"> | null;
|
|
7
|
+
/** Merges any number of contact lists into a single list */
|
|
8
|
+
export declare function mergeContacts(...pointers: (ProfilePointer | undefined | (ProfilePointer | undefined)[])[]): ProfilePointer[];
|
|
9
|
+
/** Returns all public and hidden contacts from a contacts list event */
|
|
10
|
+
export declare function getContacts(event: NostrEvent): ProfilePointer[];
|
|
11
|
+
/** Returns only the public contacts from a contacts list event */
|
|
12
|
+
export declare function getPublicContacts(event: NostrEvent): ProfilePointer[];
|
|
13
|
+
/** Returns only the hidden contacts from a contacts list event */
|
|
14
|
+
export declare function getHiddenContacts(event: NostrEvent): ProfilePointer[] | undefined;
|
package/dist/helpers/contacts.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import { getOrComputeCachedValue } from "./cache.js";
|
|
2
2
|
import { isSafeRelayURL } from "./relays.js";
|
|
3
|
+
import { isPTag, processTags } from "./tags.js";
|
|
4
|
+
import { getProfilePointerFromPTag } from "./pointers.js";
|
|
5
|
+
import { getHiddenTags, isHiddenTagsLocked } from "./hidden-tags.js";
|
|
3
6
|
export const ContactsRelaysSymbol = Symbol.for("contacts-relays");
|
|
7
|
+
export const PublicContactsSymbol = Symbol.for("public-contacts");
|
|
8
|
+
export const HiddenContactsSymbol = Symbol.for("hidden-contacts");
|
|
4
9
|
export function getRelaysFromContactsEvent(event) {
|
|
5
10
|
return getOrComputeCachedValue(event, ContactsRelaysSymbol, () => {
|
|
6
11
|
try {
|
|
@@ -23,3 +28,32 @@ export function getRelaysFromContactsEvent(event) {
|
|
|
23
28
|
}
|
|
24
29
|
});
|
|
25
30
|
}
|
|
31
|
+
/** Merges any number of contact lists into a single list */
|
|
32
|
+
export function mergeContacts(...pointers) {
|
|
33
|
+
const merged = new Map();
|
|
34
|
+
for (const arr of pointers) {
|
|
35
|
+
if (Array.isArray(arr)) {
|
|
36
|
+
for (const pointer of arr)
|
|
37
|
+
if (pointer)
|
|
38
|
+
merged.set(pointer.pubkey, pointer);
|
|
39
|
+
}
|
|
40
|
+
else if (arr) {
|
|
41
|
+
merged.set(arr.pubkey, arr);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return Array.from(merged.values());
|
|
45
|
+
}
|
|
46
|
+
/** Returns all public and hidden contacts from a contacts list event */
|
|
47
|
+
export function getContacts(event) {
|
|
48
|
+
return mergeContacts(getPublicContacts(event), getHiddenContacts(event));
|
|
49
|
+
}
|
|
50
|
+
/** Returns only the public contacts from a contacts list event */
|
|
51
|
+
export function getPublicContacts(event) {
|
|
52
|
+
return getOrComputeCachedValue(event, PublicContactsSymbol, () => processTags(event.tags, (t) => (isPTag(t) ? t : undefined), getProfilePointerFromPTag));
|
|
53
|
+
}
|
|
54
|
+
/** Returns only the hidden contacts from a contacts list event */
|
|
55
|
+
export function getHiddenContacts(event) {
|
|
56
|
+
if (isHiddenTagsLocked(event))
|
|
57
|
+
return undefined;
|
|
58
|
+
return getOrComputeCachedValue(event, HiddenContactsSymbol, () => processTags(getHiddenTags(event), (t) => (isPTag(t) ? t : undefined), getProfilePointerFromPTag));
|
|
59
|
+
}
|
package/dist/helpers/event.d.ts
CHANGED
|
@@ -24,11 +24,16 @@ export declare function isReplaceable(kind: number): boolean;
|
|
|
24
24
|
/**
|
|
25
25
|
* Returns the events Unique ID
|
|
26
26
|
* For normal or ephemeral events this is ( event.id )
|
|
27
|
-
* For replaceable events this is ( event.kind + ":" + event.pubkey )
|
|
28
|
-
* For parametrized replaceable events this is ( event.kind + ":" + event.pubkey + ":" + event.tags.d
|
|
27
|
+
* For replaceable events this is ( event.kind + ":" + event.pubkey + ":" )
|
|
28
|
+
* For parametrized replaceable events this is ( event.kind + ":" + event.pubkey + ":" + event.tags.d )
|
|
29
29
|
*/
|
|
30
30
|
export declare function getEventUID(event: NostrEvent): string;
|
|
31
|
-
|
|
31
|
+
/** Returns the replaceable event address for an addressable event */
|
|
32
|
+
export declare function getReplaceableAddress(event: NostrEvent): string;
|
|
33
|
+
/** Creates a replaceable event address from a kind, pubkey, and identifier */
|
|
34
|
+
export declare function createReplaceableAddress(kind: number, pubkey: string, identifier?: string): string;
|
|
35
|
+
/** @deprecated use createReplaceableAddress instead */
|
|
36
|
+
export declare const getReplaceableUID: typeof createReplaceableAddress;
|
|
32
37
|
/** Returns a Set of tag names and values that are indexable */
|
|
33
38
|
export declare function getIndexableTags(event: NostrEvent): Set<string>;
|
|
34
39
|
/**
|
package/dist/helpers/event.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { verifiedSymbol } from "nostr-tools";
|
|
2
2
|
import { INDEXABLE_TAGS } from "../event-store/common.js";
|
|
3
3
|
import { getHiddenTags } from "./hidden-tags.js";
|
|
4
4
|
import { getOrComputeCachedValue } from "./cache.js";
|
|
5
|
-
import {
|
|
5
|
+
import { isAddressableKind, isReplaceableKind } from "nostr-tools/kinds";
|
|
6
6
|
import { EventStoreSymbol } from "../event-store/event-store.js";
|
|
7
7
|
export const EventUIDSymbol = Symbol.for("event-uid");
|
|
8
8
|
export const EventIndexableTagsSymbol = Symbol.for("indexable-tags");
|
|
@@ -29,30 +29,38 @@ export function isEvent(event) {
|
|
|
29
29
|
* or parameterized replaceable ( 30000 <= n < 40000 )
|
|
30
30
|
*/
|
|
31
31
|
export function isReplaceable(kind) {
|
|
32
|
-
return
|
|
32
|
+
return isReplaceableKind(kind) || isAddressableKind(kind);
|
|
33
33
|
}
|
|
34
34
|
/**
|
|
35
35
|
* Returns the events Unique ID
|
|
36
36
|
* For normal or ephemeral events this is ( event.id )
|
|
37
|
-
* For replaceable events this is ( event.kind + ":" + event.pubkey )
|
|
38
|
-
* For parametrized replaceable events this is ( event.kind + ":" + event.pubkey + ":" + event.tags.d
|
|
37
|
+
* For replaceable events this is ( event.kind + ":" + event.pubkey + ":" )
|
|
38
|
+
* For parametrized replaceable events this is ( event.kind + ":" + event.pubkey + ":" + event.tags.d )
|
|
39
39
|
*/
|
|
40
40
|
export function getEventUID(event) {
|
|
41
41
|
let uid = event[EventUIDSymbol];
|
|
42
42
|
if (!uid) {
|
|
43
|
-
if (isReplaceable(event.kind))
|
|
44
|
-
|
|
45
|
-
uid = getReplaceableUID(event.kind, event.pubkey, d);
|
|
46
|
-
}
|
|
43
|
+
if (isReplaceable(event.kind))
|
|
44
|
+
uid = getReplaceableAddress(event);
|
|
47
45
|
else
|
|
48
46
|
uid = event.id;
|
|
49
47
|
event[EventUIDSymbol] = uid;
|
|
50
48
|
}
|
|
51
49
|
return uid;
|
|
52
50
|
}
|
|
53
|
-
|
|
54
|
-
|
|
51
|
+
/** Returns the replaceable event address for an addressable event */
|
|
52
|
+
export function getReplaceableAddress(event) {
|
|
53
|
+
if (!isReplaceable(event.kind))
|
|
54
|
+
throw new Error("Event is not replaceable or addressable");
|
|
55
|
+
const identifier = isAddressableKind(event.kind) ? getReplaceableIdentifier(event) : undefined;
|
|
56
|
+
return createReplaceableAddress(event.kind, event.pubkey, identifier);
|
|
57
|
+
}
|
|
58
|
+
/** Creates a replaceable event address from a kind, pubkey, and identifier */
|
|
59
|
+
export function createReplaceableAddress(kind, pubkey, identifier) {
|
|
60
|
+
return kind + ":" + pubkey + ":" + (identifier ?? "");
|
|
55
61
|
}
|
|
62
|
+
/** @deprecated use createReplaceableAddress instead */
|
|
63
|
+
export const getReplaceableUID = createReplaceableAddress;
|
|
56
64
|
/** Returns a Set of tag names and values that are indexable */
|
|
57
65
|
export function getIndexableTags(event) {
|
|
58
66
|
let indexable = event[EventIndexableTagsSymbol];
|
|
@@ -100,8 +108,8 @@ export function getParentEventStore(event) {
|
|
|
100
108
|
* @throws
|
|
101
109
|
*/
|
|
102
110
|
export function getReplaceableIdentifier(event) {
|
|
103
|
-
if (!
|
|
104
|
-
throw new Error("Event is not
|
|
111
|
+
if (!isAddressableKind(event.kind))
|
|
112
|
+
throw new Error("Event is not addressable");
|
|
105
113
|
return getOrComputeCachedValue(event, ReplaceableIdentifierSymbol, () => {
|
|
106
114
|
const d = getTagValue(event, "d");
|
|
107
115
|
if (d === undefined)
|
package/dist/helpers/lists.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isAddressableKind, isReplaceableKind } from "nostr-tools/kinds";
|
|
2
2
|
import { getHiddenTags } from "./hidden-tags.js";
|
|
3
3
|
import { getAddressPointerFromATag, getCoordinateFromAddressPointer, getEventPointerFromETag, getProfilePointerFromPTag, } from "./pointers.js";
|
|
4
4
|
import { isATag, isETag, isPTag, processTags } from "./tags.js";
|
|
@@ -49,7 +49,7 @@ export function getProfilePointersFromList(list) {
|
|
|
49
49
|
/** Returns if an event is a valid list or set */
|
|
50
50
|
export function isValidList(event) {
|
|
51
51
|
try {
|
|
52
|
-
if (
|
|
52
|
+
if (isAddressableKind(event.kind)) {
|
|
53
53
|
// event is a set
|
|
54
54
|
// ensure the set has an identifier
|
|
55
55
|
getReplaceableIdentifier(event);
|
package/dist/helpers/mutes.d.ts
CHANGED
|
@@ -7,9 +7,17 @@ export type Mutes = {
|
|
|
7
7
|
hashtags: Set<string>;
|
|
8
8
|
words: Set<string>;
|
|
9
9
|
};
|
|
10
|
+
/** Merges any number of mute sets */
|
|
11
|
+
export declare function mergeMutes(...mutes: Mutes[]): Mutes;
|
|
10
12
|
/** Parses mute tags */
|
|
11
13
|
export declare function parseMutedTags(tags: string[][]): Mutes;
|
|
12
14
|
/** Returns muted things */
|
|
13
15
|
export declare function getMutedThings(mute: NostrEvent): Mutes;
|
|
16
|
+
/** Returns only the public muted things from a mute event */
|
|
17
|
+
export declare function getPublicMutedThings(mute: NostrEvent): Mutes;
|
|
14
18
|
/** Returns the hidden muted content if the event is unlocked */
|
|
15
19
|
export declare function getHiddenMutedThings(mute: NostrEvent): Mutes | undefined;
|
|
20
|
+
/** Creates a RegExp for matching muted words */
|
|
21
|
+
export declare function createMutedWordsRegExp(mutedWords: string[]): RegExp;
|
|
22
|
+
/** Returns true if the event matches the mutes */
|
|
23
|
+
export declare function matchMutes(mutes: Mutes, event: NostrEvent): boolean;
|
package/dist/helpers/mutes.js
CHANGED
|
@@ -1,8 +1,25 @@
|
|
|
1
|
+
import { kinds } from "nostr-tools";
|
|
1
2
|
import { isETag, isPTag, isTTag } from "./tags.js";
|
|
2
3
|
import { getOrComputeCachedValue } from "./cache.js";
|
|
3
|
-
import { getHiddenTags } from "./hidden-tags.js";
|
|
4
|
+
import { getHiddenTags, isHiddenTagsLocked } from "./hidden-tags.js";
|
|
5
|
+
import { getIndexableTags, getNip10References } from "./index.js";
|
|
4
6
|
export const MutePublicSymbol = Symbol.for("mute-public");
|
|
5
7
|
export const MuteHiddenSymbol = Symbol.for("mute-hidden");
|
|
8
|
+
/** Merges any number of mute sets */
|
|
9
|
+
export function mergeMutes(...mutes) {
|
|
10
|
+
const mute = { pubkeys: new Set(), threads: new Set(), hashtags: new Set(), words: new Set() };
|
|
11
|
+
for (const m of mutes) {
|
|
12
|
+
for (const pubkey of m.pubkeys)
|
|
13
|
+
mute.pubkeys.add(pubkey);
|
|
14
|
+
for (const thread of m.threads)
|
|
15
|
+
mute.threads.add(thread);
|
|
16
|
+
for (const hashtag of m.hashtags)
|
|
17
|
+
mute.hashtags.add(hashtag);
|
|
18
|
+
for (const word of m.words)
|
|
19
|
+
mute.words.add(word);
|
|
20
|
+
}
|
|
21
|
+
return mute;
|
|
22
|
+
}
|
|
6
23
|
/** Parses mute tags */
|
|
7
24
|
export function parseMutedTags(tags) {
|
|
8
25
|
const pubkeys = new Set(tags.filter(isPTag).map((t) => t[1]));
|
|
@@ -13,12 +30,56 @@ export function parseMutedTags(tags) {
|
|
|
13
30
|
}
|
|
14
31
|
/** Returns muted things */
|
|
15
32
|
export function getMutedThings(mute) {
|
|
33
|
+
const hidden = getHiddenMutedThings(mute);
|
|
34
|
+
const mutes = getPublicMutedThings(mute);
|
|
35
|
+
if (hidden)
|
|
36
|
+
return mergeMutes(hidden, mutes);
|
|
37
|
+
return mutes;
|
|
38
|
+
}
|
|
39
|
+
/** Returns only the public muted things from a mute event */
|
|
40
|
+
export function getPublicMutedThings(mute) {
|
|
16
41
|
return getOrComputeCachedValue(mute, MutePublicSymbol, () => parseMutedTags(mute.tags));
|
|
17
42
|
}
|
|
18
43
|
/** Returns the hidden muted content if the event is unlocked */
|
|
19
44
|
export function getHiddenMutedThings(mute) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
45
|
+
if (isHiddenTagsLocked(mute))
|
|
46
|
+
return undefined;
|
|
47
|
+
return getOrComputeCachedValue(mute, MuteHiddenSymbol, () => parseMutedTags(getHiddenTags(mute)));
|
|
48
|
+
}
|
|
49
|
+
/** Creates a RegExp for matching muted words */
|
|
50
|
+
export function createMutedWordsRegExp(mutedWords) {
|
|
51
|
+
// Escape special characters and join with |
|
|
52
|
+
const escapedWords = mutedWords.map((word) => word.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"));
|
|
53
|
+
// Create the RegExp with word boundaries and case insensitive flag
|
|
54
|
+
return new RegExp(`\\b(${escapedWords.join("|")})\\b`, "gi");
|
|
55
|
+
}
|
|
56
|
+
/** Returns true if the event matches the mutes */
|
|
57
|
+
export function matchMutes(mutes, event) {
|
|
58
|
+
// Filter on muted pubkeys
|
|
59
|
+
if (mutes.pubkeys) {
|
|
60
|
+
if (mutes.pubkeys.has(event.pubkey))
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
// Filter on muted hashtags`
|
|
64
|
+
if (mutes.hashtags) {
|
|
65
|
+
const tags = getIndexableTags(event);
|
|
66
|
+
for (let tag of mutes.hashtags) {
|
|
67
|
+
if (tags.has("t:" + tag))
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Filter on muted threads
|
|
72
|
+
if (mutes.threads && event.kind === kinds.ShortTextNote) {
|
|
73
|
+
const refs = getNip10References(event);
|
|
74
|
+
if (refs.root?.e && mutes.threads.has(refs.root.e.id))
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
// Filter on muted words
|
|
78
|
+
if (mutes.words) {
|
|
79
|
+
const regExp = createMutedWordsRegExp(Array.from(mutes.words));
|
|
80
|
+
if (regExp.test(event.content))
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
// Event does not match any mutes
|
|
84
|
+
return false;
|
|
24
85
|
}
|