Skip to content

Commit

Permalink
add user blossom servers methods
Browse files Browse the repository at this point in the history
  • Loading branch information
hzrd149 committed Mar 8, 2025
1 parent f92f10c commit 06263df
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/fair-geese-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"applesauce-core": minor
---

Rename `Database.getForFilters` to `Database.getEventsForFilters`
5 changes: 5 additions & 0 deletions .changeset/selfish-llamas-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"applesauce-core": minor
---

Add `blossomServers` method to the `QueryStore`
5 changes: 5 additions & 0 deletions .changeset/swift-bees-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"applesauce-loaders": minor
---

Add `RequestLoader.blossomServers` method
5 changes: 5 additions & 0 deletions .changeset/thirty-books-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"applesauce-core": minor
---

Add `EventStore.getTimeline` method
2 changes: 1 addition & 1 deletion packages/core/src/event-store/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ export class Database {
return events;
}

getForFilters(filters: Filter[]): Set<NostrEvent> {
getEventsForFilters(filters: Filter[]): Set<NostrEvent> {
if (filters.length === 0) throw new Error("No Filters");

let events = new Set<NostrEvent>();
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/event-store/event-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export class EventStore {

/** Get all events matching a filter */
getAll(filters: Filter[]): Set<NostrEvent> {
return this.database.getForFilters(filters);
return this.database.getEventsForFilters(filters);
}

/** Check if the store has an event */
Expand All @@ -183,6 +183,11 @@ export class EventStore {
return this.database.getReplaceable(kind, pubkey, d);
}

/** Returns a timeline of events that match filters */
getTimeline(filters: Filter | Filter[]): NostrEvent[] {
return Array.from(this.database.getEventsForFilters(Array.isArray(filters) ? filters : [filters])).sort(sortDesc);
}

/**
* Creates an observable that streams all events that match the filter and remains open
* @param filters
Expand Down Expand Up @@ -366,7 +371,7 @@ export class EventStore {
const seen = new Map<string, NostrEvent>();

// get current events
return defer(() => of(Array.from(this.database.getForFilters(filters)).sort(sortDesc))).pipe(
return defer(() => of(Array.from(this.database.getEventsForFilters(filters)).sort(sortDesc))).pipe(
// claim existing events
claimEvents(this.database),
// subscribe to newer events
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/query-store/query-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@ export class QueryStore {
return this.createQuery(Queries.MailboxesQuery, pubkey);
}

/** Creates a query for a users blossom servers */
blossomServers(pubkey: string) {
return this.createQuery(Queries.UserBlossomServersQuery, pubkey);
}

/** Creates a ThreadQuery */
thread(root: string | EventPointer | AddressPointer) {
return this.createQuery(Queries.ThreadQuery, root);
Expand Down
22 changes: 21 additions & 1 deletion packages/loaders/src/loaders/request-loader.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import { kinds } from "nostr-tools";
import { MailboxesQuery, ProfileQuery, ReplaceableQuery, UserContactsQuery } from "applesauce-core/queries";
import {
MailboxesQuery,
ProfileQuery,
ReplaceableQuery,
UserBlossomServersQuery,
UserContactsQuery,
} from "applesauce-core/queries";
import { getObservableValue, simpleTimeout } from "applesauce-core/observable";
import { EventStore, QueryStore } from "applesauce-core";
import { ProfilePointer } from "nostr-tools/nip19";
import { filter, Observable } from "rxjs";

import { ReplaceableLoader } from "./replaceable-loader.js";
import { LoadableAddressPointer } from "../helpers/address-pointer.js";
import { BLOSSOM_SERVER_LIST_KIND } from "applesauce-core/helpers";

/** A special Promised based loader built on the {@link QueryStore} */
export class RequestLoader {
Expand All @@ -25,7 +32,9 @@ export class RequestLoader {
): Promise<NonNullable<T>> {
return getObservableValue(
this.store.createQuery(queryConstructor, ...args).pipe(
// ignore undefined and null values
filter((v) => v !== undefined && v !== null),
// timeout with an error is not values
simpleTimeout(this.requestTimeout),
),
);
Expand Down Expand Up @@ -59,4 +68,15 @@ export class RequestLoader {
this.checkReplaceable().next({ kind: kinds.Contacts, pubkey: pointer.pubkey, relays: pointer.relays, force });
return this.runWithTimeout(UserContactsQuery, pointer.pubkey);
}

/** Loads a pubkeys blossom servers */
blossomServers(pointer: ProfilePointer, force?: boolean) {
this.checkReplaceable().next({
kind: BLOSSOM_SERVER_LIST_KIND,
pubkey: pointer.pubkey,
relays: pointer.relays,
force,
});
return this.runWithTimeout(UserBlossomServersQuery, pointer.pubkey);
}
}

0 comments on commit 06263df

Please sign in to comment.