applesauce-core 0.0.0-next-20250312113207 → 0.0.0-next-20250312162115
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/event-store.d.ts +3 -2
- package/dist/event-store/event-store.js +3 -3
- package/dist/event-store/index.d.ts +1 -0
- package/dist/event-store/index.js +1 -0
- package/dist/event-store/interface.d.ts +27 -0
- package/dist/event-store/interface.js +1 -0
- package/dist/helpers/cache.d.ts +3 -4
- package/dist/helpers/cache.js +1 -1
- package/dist/helpers/event.d.ts +9 -4
- package/dist/helpers/hidden-content.d.ts +10 -10
- package/dist/helpers/hidden-content.js +7 -5
- package/dist/helpers/hidden-tags.d.ts +9 -18
- package/dist/helpers/hidden-tags.js +19 -34
- package/dist/helpers/mutes.d.ts +1 -0
- package/dist/helpers/mutes.js +2 -1
- package/dist/observable/claim-latest.d.ts +3 -2
- package/dist/observable/claim-latest.js +2 -1
- package/dist/observable/with-immediate-value.d.ts +3 -0
- package/dist/observable/with-immediate-value.js +19 -0
- package/dist/queries/mutes.js +1 -1
- package/dist/queries/simple.d.ts +1 -1
- package/dist/queries/simple.js +3 -3
- package/dist/query-store/__tests__/query-store.test.d.ts +1 -0
- package/dist/query-store/__tests__/query-store.test.js +63 -0
- package/dist/query-store/query-store.d.ts +4 -4
- package/dist/query-store/query-store.js +8 -3
- package/package.json +1 -1
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { Filter, NostrEvent } from "nostr-tools";
|
|
2
2
|
import { Observable } from "rxjs";
|
|
3
3
|
import { Database } from "./database.js";
|
|
4
|
+
import { IEventStore } from "./interface.js";
|
|
4
5
|
export declare const EventStoreSymbol: unique symbol;
|
|
5
|
-
export declare class EventStore {
|
|
6
|
+
export declare class EventStore implements IEventStore {
|
|
6
7
|
database: Database;
|
|
7
8
|
/** Enable this to keep old versions of replaceable events */
|
|
8
9
|
keepOldVersions: boolean;
|
|
@@ -50,7 +51,7 @@ export declare class EventStore {
|
|
|
50
51
|
/** Returns an observable that completes when an event is removed */
|
|
51
52
|
removed(id: string): Observable<never>;
|
|
52
53
|
/** Creates an observable that emits when event is updated */
|
|
53
|
-
updated(
|
|
54
|
+
updated(event: string | NostrEvent): Observable<NostrEvent>;
|
|
54
55
|
/** Creates an observable that subscribes to a single event */
|
|
55
56
|
event(id: string): Observable<NostrEvent | undefined>;
|
|
56
57
|
/** Creates an observable that subscribes to multiple events */
|
|
@@ -181,8 +181,8 @@ export class EventStore {
|
|
|
181
181
|
mergeMap(() => EMPTY));
|
|
182
182
|
}
|
|
183
183
|
/** Creates an observable that emits when event is updated */
|
|
184
|
-
updated(
|
|
185
|
-
return this.database.updated.pipe(filter((e) => e.id ===
|
|
184
|
+
updated(event) {
|
|
185
|
+
return this.database.updated.pipe(filter((e) => e.id === event || e === event));
|
|
186
186
|
}
|
|
187
187
|
/** Creates an observable that subscribes to a single event */
|
|
188
188
|
event(id) {
|
|
@@ -199,7 +199,7 @@ export class EventStore {
|
|
|
199
199
|
// emit undefined when deleted
|
|
200
200
|
this.removed(id).pipe(endWith(undefined))).pipe(
|
|
201
201
|
// claim all events
|
|
202
|
-
|
|
202
|
+
claimLatest(this.database));
|
|
203
203
|
}
|
|
204
204
|
/** Creates an observable that subscribes to multiple events */
|
|
205
205
|
events(ids) {
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { Filter, NostrEvent } from "nostr-tools";
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
export interface IEventStore {
|
|
4
|
+
updates: Observable<NostrEvent>;
|
|
5
|
+
add(event: NostrEvent, fromRelay?: string): NostrEvent;
|
|
6
|
+
remove(event: string | NostrEvent): boolean;
|
|
7
|
+
update(event: NostrEvent): NostrEvent;
|
|
8
|
+
hasEvent(id: string): boolean;
|
|
9
|
+
hasReplaceable(kind: number, pubkey: string, identifier?: string): boolean;
|
|
10
|
+
getEvent(id: string): NostrEvent | undefined;
|
|
11
|
+
getReplaceable(kind: number, pubkey: string, identifier?: string): NostrEvent | undefined;
|
|
12
|
+
getReplaceableHistory(kind: number, pubkey: string, identifier?: string): NostrEvent[] | undefined;
|
|
13
|
+
getAll(filters: Filter | Filter[]): Set<NostrEvent>;
|
|
14
|
+
getTimeline(filters: Filter | Filter[]): NostrEvent[];
|
|
15
|
+
filters(filters: Filter | Filter[]): Observable<NostrEvent>;
|
|
16
|
+
updated(id: string | NostrEvent): Observable<NostrEvent>;
|
|
17
|
+
removed(id: string): Observable<never>;
|
|
18
|
+
event(id: string): Observable<NostrEvent | undefined>;
|
|
19
|
+
events(ids: string[]): Observable<Record<string, NostrEvent>>;
|
|
20
|
+
replaceable(kind: number, pubkey: string, identifier?: string): Observable<NostrEvent | undefined>;
|
|
21
|
+
replaceableSet(pointers: {
|
|
22
|
+
kind: number;
|
|
23
|
+
pubkey: string;
|
|
24
|
+
identifier?: string;
|
|
25
|
+
}[]): Observable<Record<string, NostrEvent>>;
|
|
26
|
+
timeline(filters: Filter | Filter[], includeOldVersion?: boolean): Observable<NostrEvent[]>;
|
|
27
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/helpers/cache.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export declare function
|
|
3
|
-
export declare function setCachedValue<T extends unknown>(event: NostrEvent | EventTemplate, symbol: symbol, value: T): void;
|
|
1
|
+
export declare function getCachedValue<T extends unknown>(event: any, symbol: symbol): T | undefined;
|
|
2
|
+
export declare function setCachedValue<T extends unknown>(event: any, symbol: symbol, value: T): void;
|
|
4
3
|
/** Internal method used to cache computed values on events */
|
|
5
|
-
export declare function getOrComputeCachedValue<T extends unknown>(event:
|
|
4
|
+
export declare function getOrComputeCachedValue<T extends unknown>(event: any, symbol: symbol, compute: () => T): T;
|
package/dist/helpers/cache.js
CHANGED
package/dist/helpers/event.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { NostrEvent, VerifiedEvent } from "nostr-tools";
|
|
2
|
+
import { IEventStore } from "../event-store/interface.js";
|
|
3
3
|
export declare const EventUIDSymbol: unique symbol;
|
|
4
4
|
export declare const EventIndexableTagsSymbol: unique symbol;
|
|
5
5
|
export declare const FromCacheSymbol: unique symbol;
|
|
@@ -35,7 +35,12 @@ export declare function getIndexableTags(event: NostrEvent): Set<string>;
|
|
|
35
35
|
* Returns the second index ( tag[1] ) of the first tag that matches the name
|
|
36
36
|
* If the event has any hidden tags they will be searched first
|
|
37
37
|
*/
|
|
38
|
-
export declare function getTagValue
|
|
38
|
+
export declare function getTagValue<T extends {
|
|
39
|
+
kind: number;
|
|
40
|
+
tags: string[][];
|
|
41
|
+
content: string;
|
|
42
|
+
pubkey: string;
|
|
43
|
+
}>(event: T, name: string): string | undefined;
|
|
39
44
|
/** Sets events verified flag without checking anything */
|
|
40
45
|
export declare function fakeVerifyEvent(event: NostrEvent): event is VerifiedEvent;
|
|
41
46
|
/** Marks an event as being from a cache */
|
|
@@ -43,7 +48,7 @@ export declare function markFromCache(event: NostrEvent): void;
|
|
|
43
48
|
/** Returns if an event was from a cache */
|
|
44
49
|
export declare function isFromCache(event: NostrEvent): boolean;
|
|
45
50
|
/** Returns the EventStore of an event if its been added to one */
|
|
46
|
-
export declare function getParentEventStore(event:
|
|
51
|
+
export declare function getParentEventStore<T extends object>(event: T): IEventStore | undefined;
|
|
47
52
|
/**
|
|
48
53
|
* Returns the replaceable identifier for a replaceable event
|
|
49
54
|
* @throws
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { UnsignedEvent, type EventTemplate, type NostrEvent } from "nostr-tools";
|
|
2
1
|
export declare const HiddenContentSymbol: unique symbol;
|
|
3
2
|
export type HiddenContentSigner = {
|
|
4
3
|
nip04?: {
|
|
@@ -17,14 +16,14 @@ export declare function setEventContentEncryptionMethod(kind: number, method: "n
|
|
|
17
16
|
/** Checks if an event can have hidden content */
|
|
18
17
|
export declare function canHaveHiddenContent(kind: number): boolean;
|
|
19
18
|
/** Checks if an event has hidden content */
|
|
20
|
-
export declare function hasHiddenContent
|
|
19
|
+
export declare function hasHiddenContent<T extends {
|
|
21
20
|
kind: number;
|
|
22
21
|
content: string;
|
|
23
|
-
}): boolean;
|
|
22
|
+
}>(event: T): boolean;
|
|
24
23
|
/** Returns the hidden tags for an event if they are unlocked */
|
|
25
|
-
export declare function getHiddenContent(event:
|
|
24
|
+
export declare function getHiddenContent<T extends object>(event: T): string | undefined;
|
|
26
25
|
/** Checks if the hidden tags are locked */
|
|
27
|
-
export declare function isHiddenContentLocked(event:
|
|
26
|
+
export declare function isHiddenContentLocked<T extends object>(event: T): boolean;
|
|
28
27
|
/** Returns either nip04 or nip44 encryption methods depending on event kind */
|
|
29
28
|
export declare function getHiddenContentEncryptionMethods(kind: number, signer: HiddenContentSigner): {
|
|
30
29
|
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
@@ -33,14 +32,15 @@ export declare function getHiddenContentEncryptionMethods(kind: number, signer:
|
|
|
33
32
|
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
34
33
|
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
35
34
|
};
|
|
35
|
+
export type HiddenContentEvent = {
|
|
36
|
+
kind: number;
|
|
37
|
+
pubkey: string;
|
|
38
|
+
content: string;
|
|
39
|
+
};
|
|
36
40
|
/**
|
|
37
41
|
* Unlocks the encrypted content in an event
|
|
38
42
|
* @param event The event with content to decrypt
|
|
39
43
|
* @param signer A signer to use to decrypt the tags
|
|
40
44
|
* @throws
|
|
41
45
|
*/
|
|
42
|
-
export declare function unlockHiddenContent(event:
|
|
43
|
-
kind: number;
|
|
44
|
-
pubkey: string;
|
|
45
|
-
content: string;
|
|
46
|
-
}, signer: HiddenContentSigner): Promise<string>;
|
|
46
|
+
export declare function unlockHiddenContent<T extends HiddenContentEvent>(event: T, signer: HiddenContentSigner): Promise<string>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as kinds from "nostr-tools/kinds";
|
|
2
2
|
import { GROUPS_LIST_KIND } from "./groups.js";
|
|
3
|
-
import { getParentEventStore } from "./event.js";
|
|
3
|
+
import { getParentEventStore, isEvent } from "./event.js";
|
|
4
4
|
export const HiddenContentSymbol = Symbol.for("hidden-content");
|
|
5
5
|
/** Various event kinds that can have encrypted tags in their content and which encryption method they use */
|
|
6
6
|
export const EventContentEncryptionMethod = {
|
|
@@ -46,7 +46,7 @@ export function getHiddenContent(event) {
|
|
|
46
46
|
}
|
|
47
47
|
/** Checks if the hidden tags are locked */
|
|
48
48
|
export function isHiddenContentLocked(event) {
|
|
49
|
-
return
|
|
49
|
+
return Reflect.has(event, HiddenContentSymbol) === false;
|
|
50
50
|
}
|
|
51
51
|
/** Returns either nip04 or nip44 encryption methods depending on event kind */
|
|
52
52
|
export function getHiddenContentEncryptionMethods(kind, signer) {
|
|
@@ -69,8 +69,10 @@ export async function unlockHiddenContent(event, signer) {
|
|
|
69
69
|
const plaintext = await encryption.decrypt(event.pubkey, event.content);
|
|
70
70
|
Reflect.set(event, HiddenContentSymbol, plaintext);
|
|
71
71
|
// if the event has been added to an event store, notify it
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
eventStore
|
|
72
|
+
if (isEvent(event)) {
|
|
73
|
+
const eventStore = getParentEventStore(event);
|
|
74
|
+
if (eventStore)
|
|
75
|
+
eventStore.update(event);
|
|
76
|
+
}
|
|
75
77
|
return plaintext;
|
|
76
78
|
}
|
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { EventStore } from "../event-store/event-store.js";
|
|
3
|
-
import { HiddenContentSigner } from "./hidden-content.js";
|
|
1
|
+
import { HiddenContentEvent, HiddenContentSigner } from "./hidden-content.js";
|
|
4
2
|
export declare const HiddenTagsSymbol: unique symbol;
|
|
5
3
|
/** Checks if an event can have hidden tags */
|
|
6
4
|
export declare function canHaveHiddenTags(kind: number): boolean;
|
|
7
5
|
/** Checks if an event has hidden tags */
|
|
8
|
-
export declare function hasHiddenTags
|
|
6
|
+
export declare function hasHiddenTags<T extends {
|
|
7
|
+
content: string;
|
|
8
|
+
kind: number;
|
|
9
|
+
}>(event: T): boolean;
|
|
9
10
|
/** Returns the hidden tags for an event if they are unlocked */
|
|
10
|
-
export declare function getHiddenTags(event:
|
|
11
|
+
export declare function getHiddenTags<T extends object>(event: T): string[][] | undefined;
|
|
11
12
|
/** Checks if the hidden tags are locked */
|
|
12
|
-
export declare function isHiddenTagsLocked(event:
|
|
13
|
+
export declare function isHiddenTagsLocked<T extends object>(event: T): boolean;
|
|
13
14
|
/** Returns either nip04 or nip44 encryption method depending on list kind */
|
|
14
|
-
export declare function
|
|
15
|
+
export declare function getHiddenTagsEncryptionMethods(kind: number, signer: HiddenContentSigner): {
|
|
15
16
|
encrypt: (pubkey: string, plaintext: string) => Promise<string> | string;
|
|
16
17
|
decrypt: (pubkey: string, ciphertext: string) => Promise<string> | string;
|
|
17
18
|
} | {
|
|
@@ -25,14 +26,4 @@ export declare function getListEncryptionMethods(kind: number, signer: HiddenCon
|
|
|
25
26
|
* @param store An optional EventStore to notify about the update
|
|
26
27
|
* @throws
|
|
27
28
|
*/
|
|
28
|
-
export declare function unlockHiddenTags<T extends
|
|
29
|
-
kind: number;
|
|
30
|
-
pubkey: string;
|
|
31
|
-
content: string;
|
|
32
|
-
}>(event: T, signer: HiddenContentSigner, store?: EventStore): Promise<string[][]>;
|
|
33
|
-
/**
|
|
34
|
-
* Override the hidden tags in an event
|
|
35
|
-
* @deprecated use EventFactory to create draft events
|
|
36
|
-
* @throws
|
|
37
|
-
*/
|
|
38
|
-
export declare function overrideHiddenTags(event: NostrEvent, hidden: string[][], signer: HiddenContentSigner): Promise<EventTemplate>;
|
|
29
|
+
export declare function unlockHiddenTags<T extends HiddenContentEvent>(event: T, signer: HiddenContentSigner): Promise<string[][]>;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { canHaveHiddenContent, getHiddenContentEncryptionMethods, unlockHiddenContent, } from "./hidden-content.js";
|
|
1
|
+
import { canHaveHiddenContent, getHiddenContent, getHiddenContentEncryptionMethods, isHiddenContentLocked, unlockHiddenContent, } from "./hidden-content.js";
|
|
2
|
+
import { getOrComputeCachedValue } from "./cache.js";
|
|
4
3
|
export const HiddenTagsSymbol = Symbol.for("hidden-tags");
|
|
5
4
|
/** Checks if an event can have hidden tags */
|
|
6
5
|
export function canHaveHiddenTags(kind) {
|
|
@@ -12,14 +11,23 @@ export function hasHiddenTags(event) {
|
|
|
12
11
|
}
|
|
13
12
|
/** Returns the hidden tags for an event if they are unlocked */
|
|
14
13
|
export function getHiddenTags(event) {
|
|
15
|
-
|
|
14
|
+
if (isHiddenTagsLocked(event))
|
|
15
|
+
return undefined;
|
|
16
|
+
return getOrComputeCachedValue(event, HiddenTagsSymbol, () => {
|
|
17
|
+
const plaintext = getHiddenContent(event);
|
|
18
|
+
const parsed = JSON.parse(plaintext);
|
|
19
|
+
if (!Array.isArray(parsed))
|
|
20
|
+
throw new Error("Content is not an array of tags");
|
|
21
|
+
// Convert array to tags array string[][]
|
|
22
|
+
return parsed.filter((t) => Array.isArray(t)).map((t) => t.map((v) => String(v)));
|
|
23
|
+
});
|
|
16
24
|
}
|
|
17
25
|
/** Checks if the hidden tags are locked */
|
|
18
26
|
export function isHiddenTagsLocked(event) {
|
|
19
|
-
return
|
|
27
|
+
return isHiddenContentLocked(event);
|
|
20
28
|
}
|
|
21
29
|
/** Returns either nip04 or nip44 encryption method depending on list kind */
|
|
22
|
-
export function
|
|
30
|
+
export function getHiddenTagsEncryptionMethods(kind, signer) {
|
|
23
31
|
return getHiddenContentEncryptionMethods(kind, signer);
|
|
24
32
|
}
|
|
25
33
|
/**
|
|
@@ -29,34 +37,11 @@ export function getListEncryptionMethods(kind, signer) {
|
|
|
29
37
|
* @param store An optional EventStore to notify about the update
|
|
30
38
|
* @throws
|
|
31
39
|
*/
|
|
32
|
-
export async function unlockHiddenTags(event, signer
|
|
40
|
+
export async function unlockHiddenTags(event, signer) {
|
|
33
41
|
if (!canHaveHiddenTags(event.kind))
|
|
34
42
|
throw new Error("Event kind does not support hidden tags");
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
// Convert array to tags array string[][]
|
|
40
|
-
const tags = parsed.filter((t) => Array.isArray(t)).map((t) => t.map((v) => String(v)));
|
|
41
|
-
Reflect.set(event, HiddenTagsSymbol, tags);
|
|
42
|
-
if (store && isEvent(event))
|
|
43
|
-
store.update(event);
|
|
44
|
-
return tags;
|
|
45
|
-
}
|
|
46
|
-
/**
|
|
47
|
-
* Override the hidden tags in an event
|
|
48
|
-
* @deprecated use EventFactory to create draft events
|
|
49
|
-
* @throws
|
|
50
|
-
*/
|
|
51
|
-
export async function overrideHiddenTags(event, hidden, signer) {
|
|
52
|
-
if (!canHaveHiddenTags(event.kind))
|
|
53
|
-
throw new Error("Event kind does not support hidden tags");
|
|
54
|
-
const encryption = getListEncryptionMethods(event.kind, signer);
|
|
55
|
-
const ciphertext = await encryption.encrypt(event.pubkey, JSON.stringify(hidden));
|
|
56
|
-
return {
|
|
57
|
-
kind: event.kind,
|
|
58
|
-
content: ciphertext,
|
|
59
|
-
created_at: unixNow(),
|
|
60
|
-
tags: event.tags,
|
|
61
|
-
};
|
|
43
|
+
// unlock hidden content is needed
|
|
44
|
+
if (isHiddenContentLocked(event))
|
|
45
|
+
await unlockHiddenContent(event, signer);
|
|
46
|
+
return getHiddenTags(event);
|
|
62
47
|
}
|
package/dist/helpers/mutes.d.ts
CHANGED
|
@@ -7,6 +7,7 @@ export type Mutes = {
|
|
|
7
7
|
hashtags: Set<string>;
|
|
8
8
|
words: Set<string>;
|
|
9
9
|
};
|
|
10
|
+
/** Parses mute tags */
|
|
10
11
|
export declare function parseMutedTags(tags: string[][]): Mutes;
|
|
11
12
|
/** Returns muted things */
|
|
12
13
|
export declare function getMutedThings(mute: NostrEvent): Mutes;
|
package/dist/helpers/mutes.js
CHANGED
|
@@ -3,6 +3,7 @@ import { getOrComputeCachedValue } from "./cache.js";
|
|
|
3
3
|
import { getHiddenTags } from "./hidden-tags.js";
|
|
4
4
|
export const MutePublicSymbol = Symbol.for("mute-public");
|
|
5
5
|
export const MuteHiddenSymbol = Symbol.for("mute-hidden");
|
|
6
|
+
/** Parses mute tags */
|
|
6
7
|
export function parseMutedTags(tags) {
|
|
7
8
|
const pubkeys = new Set(tags.filter(isPTag).map((t) => t[1]));
|
|
8
9
|
const threads = new Set(tags.filter(isETag).map((t) => t[1]));
|
|
@@ -12,7 +13,7 @@ export function parseMutedTags(tags) {
|
|
|
12
13
|
}
|
|
13
14
|
/** Returns muted things */
|
|
14
15
|
export function getMutedThings(mute) {
|
|
15
|
-
return getOrComputeCachedValue(mute, MutePublicSymbol, (
|
|
16
|
+
return getOrComputeCachedValue(mute, MutePublicSymbol, () => parseMutedTags(mute.tags));
|
|
16
17
|
}
|
|
17
18
|
/** Returns the hidden muted content if the event is unlocked */
|
|
18
19
|
export function getHiddenMutedThings(mute) {
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { MonoTypeOperatorFunction } from "rxjs";
|
|
2
2
|
import { NostrEvent } from "nostr-tools";
|
|
3
|
-
import { Database } from "../event-store/database.js";
|
|
4
|
-
|
|
3
|
+
import { type Database } from "../event-store/database.js";
|
|
4
|
+
/** An operator that claims the latest event with the database */
|
|
5
|
+
export declare function claimLatest<T extends NostrEvent | undefined>(database: Database): MonoTypeOperatorFunction<T>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { finalize, tap } from "rxjs";
|
|
2
|
+
/** An operator that claims the latest event with the database */
|
|
2
3
|
export function claimLatest(database) {
|
|
3
4
|
return (source) => {
|
|
4
5
|
let latest = undefined;
|
|
@@ -12,7 +13,7 @@ export function claimLatest(database) {
|
|
|
12
13
|
// update state
|
|
13
14
|
latest = event;
|
|
14
15
|
}), finalize(() => {
|
|
15
|
-
//
|
|
16
|
+
// remove latest claim
|
|
16
17
|
if (latest)
|
|
17
18
|
database.removeClaim(latest, source);
|
|
18
19
|
}));
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Observable } from "rxjs";
|
|
2
|
+
/** if a synchronous value is not emitted, default is emitted */
|
|
3
|
+
export function withImmediateValueOrDefault(defaultValue) {
|
|
4
|
+
return (source) => new Observable((observer) => {
|
|
5
|
+
let seen = false;
|
|
6
|
+
const sub = source.subscribe({
|
|
7
|
+
next: (v) => {
|
|
8
|
+
seen = true;
|
|
9
|
+
observer.next(v);
|
|
10
|
+
},
|
|
11
|
+
error: (err) => observer.error(err),
|
|
12
|
+
complete: () => observer.complete(),
|
|
13
|
+
});
|
|
14
|
+
// if a value is not emitted sync, emit default
|
|
15
|
+
if (!seen)
|
|
16
|
+
observer.next(defaultValue);
|
|
17
|
+
return sub;
|
|
18
|
+
});
|
|
19
|
+
}
|
package/dist/queries/mutes.js
CHANGED
|
@@ -5,7 +5,7 @@ import { isHiddenTagsLocked } from "../helpers/hidden-tags.js";
|
|
|
5
5
|
export function UserMuteQuery(pubkey) {
|
|
6
6
|
return {
|
|
7
7
|
key: pubkey,
|
|
8
|
-
run: (
|
|
8
|
+
run: (event) => event.replaceable(kinds.Mutelist, pubkey).pipe(map((event) => event && getMutedThings(event))),
|
|
9
9
|
};
|
|
10
10
|
}
|
|
11
11
|
export function UserHiddenMuteQuery(pubkey) {
|
package/dist/queries/simple.d.ts
CHANGED
|
@@ -7,7 +7,7 @@ export declare function MultipleEventsQuery(ids: string[]): Query<Record<string,
|
|
|
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[],
|
|
10
|
+
export declare function TimelineQuery(filters: Filter | Filter[], includeOldVersion?: 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;
|
package/dist/queries/simple.js
CHANGED
|
@@ -22,11 +22,11 @@ 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, includeOldVersion) {
|
|
26
26
|
filters = Array.isArray(filters) ? filters : [filters];
|
|
27
27
|
return {
|
|
28
|
-
key: hash_sum(filters) + (
|
|
29
|
-
run: (events) => events.timeline(filters,
|
|
28
|
+
key: hash_sum(filters) + (includeOldVersion ? "-history" : ""),
|
|
29
|
+
run: (events) => events.timeline(filters, includeOldVersion),
|
|
30
30
|
};
|
|
31
31
|
}
|
|
32
32
|
/** Creates a Query that returns a directory of events by their UID */
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach } from "vitest";
|
|
2
|
+
import { subscribeSpyTo } from "@hirez_io/observer-spy";
|
|
3
|
+
import { EventStore } from "../../event-store/event-store.js";
|
|
4
|
+
import { QueryStore } from "../query-store.js";
|
|
5
|
+
import { ProfileQuery } from "../../queries/profile.js";
|
|
6
|
+
import { SingleEventQuery } from "../../queries/simple.js";
|
|
7
|
+
let eventStore;
|
|
8
|
+
let queryStore;
|
|
9
|
+
const event = {
|
|
10
|
+
content: '{"name":"hzrd149","picture":"https://cdn.hzrd149.com/5ed3fe5df09a74e8c126831eac999364f9eb7624e2b86d521521b8021de20bdc.png","about":"JavaScript developer working on some nostr stuff\\n- noStrudel https://nostrudel.ninja/ \\n- Blossom https://github.com/hzrd149/blossom \\n- Applesauce https://hzrd149.github.io/applesauce/","website":"https://hzrd149.com","nip05":"_@hzrd149.com","lud16":"hzrd1499@minibits.cash","pubkey":"266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5","display_name":"hzrd149","displayName":"hzrd149","banner":""}',
|
|
11
|
+
created_at: 1738362529,
|
|
12
|
+
id: "e9df8d5898c4ccfbd21fcd59f3f48abb3ff0ab7259b19570e2f1756de1e9306b",
|
|
13
|
+
kind: 0,
|
|
14
|
+
pubkey: "266815e0c9210dfa324c6cba3573b14bee49da4209a9456f9484e5106cd408a5",
|
|
15
|
+
sig: "465a47b93626a587bf81dadc2b306b8f713a62db31d6ce1533198e9ae1e665a6eaf376a03250bf9ffbb02eb9059c8eafbd37ae1092d05d215757575bd8357586",
|
|
16
|
+
tags: [],
|
|
17
|
+
};
|
|
18
|
+
beforeEach(() => {
|
|
19
|
+
eventStore = new EventStore();
|
|
20
|
+
queryStore = new QueryStore(eventStore);
|
|
21
|
+
});
|
|
22
|
+
describe("createQuery", () => {
|
|
23
|
+
it("should emit synchronous value if it exists", () => {
|
|
24
|
+
let value = undefined;
|
|
25
|
+
eventStore.add(event);
|
|
26
|
+
queryStore.createQuery(ProfileQuery, event.pubkey).subscribe((v) => (value = v));
|
|
27
|
+
expect(value).not.toBe(undefined);
|
|
28
|
+
});
|
|
29
|
+
it("should not emit undefined if value exists", () => {
|
|
30
|
+
eventStore.add(event);
|
|
31
|
+
const spy = subscribeSpyTo(queryStore.createQuery(SingleEventQuery, event.id));
|
|
32
|
+
expect(spy.getValues()).toEqual([event]);
|
|
33
|
+
});
|
|
34
|
+
it("should emit synchronous undefined if value does not exists", () => {
|
|
35
|
+
let value = 0;
|
|
36
|
+
queryStore.createQuery(ProfileQuery, event.pubkey).subscribe((v) => {
|
|
37
|
+
value = v;
|
|
38
|
+
});
|
|
39
|
+
expect(value).not.toBe(0);
|
|
40
|
+
expect(value).toBe(undefined);
|
|
41
|
+
});
|
|
42
|
+
it("should share latest value", () => {
|
|
43
|
+
eventStore.add(event);
|
|
44
|
+
const spy = subscribeSpyTo(queryStore.createQuery(SingleEventQuery, event.id));
|
|
45
|
+
const spy2 = subscribeSpyTo(queryStore.createQuery(SingleEventQuery, event.id));
|
|
46
|
+
expect(spy.getValues()).toEqual([event]);
|
|
47
|
+
expect(spy2.getValues()).toEqual([event]);
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe("executeQuery", () => {
|
|
51
|
+
it("should resolve when value is already present", async () => {
|
|
52
|
+
eventStore.add(event);
|
|
53
|
+
expect(await queryStore.executeQuery(ProfileQuery, event.pubkey)).toEqual(expect.objectContaining({ name: "hzrd149" }));
|
|
54
|
+
});
|
|
55
|
+
it("should resolve when value is added to event store", async () => {
|
|
56
|
+
const p = queryStore.executeQuery(ProfileQuery, event.pubkey);
|
|
57
|
+
// delay adding the event
|
|
58
|
+
setTimeout(() => {
|
|
59
|
+
eventStore.add(event);
|
|
60
|
+
}, 10);
|
|
61
|
+
await expect(p).resolves.toEqual(expect.objectContaining({ name: "hzrd149" }));
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
import { Observable } from "rxjs";
|
|
2
2
|
import { Filter, NostrEvent } from "nostr-tools";
|
|
3
3
|
import type { AddressPointer, EventPointer } from "nostr-tools/nip19";
|
|
4
|
-
import {
|
|
4
|
+
import { IEventStore } from "../event-store/interface.js";
|
|
5
5
|
import * as Queries from "../queries/index.js";
|
|
6
6
|
export type Query<T extends unknown> = {
|
|
7
7
|
/** A unique key for this query. this is used to detect duplicate queries */
|
|
8
8
|
key: string;
|
|
9
9
|
/** The meat of the query, this should return an Observables that subscribes to the eventStore in some way */
|
|
10
|
-
run: (events:
|
|
10
|
+
run: (events: IEventStore, store: QueryStore) => Observable<T>;
|
|
11
11
|
};
|
|
12
12
|
export type QueryConstructor<T extends unknown, Args extends Array<any>> = (...args: Args) => Query<T>;
|
|
13
13
|
export declare class QueryStore {
|
|
14
14
|
static Queries: typeof Queries;
|
|
15
|
-
store:
|
|
16
|
-
constructor(store:
|
|
15
|
+
store: IEventStore;
|
|
16
|
+
constructor(store: IEventStore);
|
|
17
17
|
/** A directory of all active queries */
|
|
18
18
|
queries: Map<QueryConstructor<any, any[]>, Map<string, Observable<any>>>;
|
|
19
19
|
/** How long a query should be kept "warm" while nothing is subscribed to it */
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { filter, finalize, ReplaySubject, share,
|
|
1
|
+
import { filter, finalize, ReplaySubject, share, timer } from "rxjs";
|
|
2
2
|
import hash_sum from "hash-sum";
|
|
3
3
|
import * as Queries from "../queries/index.js";
|
|
4
4
|
import { getObservableValue } from "../observable/get-observable-value.js";
|
|
5
|
+
import { withImmediateValueOrDefault } from "../observable/with-immediate-value.js";
|
|
5
6
|
export class QueryStore {
|
|
6
7
|
static Queries = Queries;
|
|
7
8
|
store;
|
|
@@ -32,11 +33,15 @@ export class QueryStore {
|
|
|
32
33
|
.run(this.store, this)
|
|
33
34
|
.pipe(
|
|
34
35
|
// always emit undefined so the observable is sync
|
|
35
|
-
|
|
36
|
+
withImmediateValueOrDefault(undefined),
|
|
36
37
|
// remove the observable when its subscribed
|
|
37
38
|
finalize(cleanup),
|
|
38
39
|
// only create a single observable for all components
|
|
39
|
-
share({
|
|
40
|
+
share({
|
|
41
|
+
connector: () => new ReplaySubject(1),
|
|
42
|
+
resetOnComplete: () => timer(this.queryKeepWarmTimeout),
|
|
43
|
+
resetOnRefCountZero: () => timer(this.queryKeepWarmTimeout),
|
|
44
|
+
}));
|
|
40
45
|
// set debug fields
|
|
41
46
|
Reflect.set(observable, "queryArgs", args);
|
|
42
47
|
observables.set(key, observable);
|