Skip to content

Commit

Permalink
use event tags to find events in SQLite cache
Browse files Browse the repository at this point in the history
  • Loading branch information
pablof7z committed Jan 6, 2025
1 parent 17735c2 commit 238d4a5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
22 changes: 15 additions & 7 deletions ndk-mobile/src/cache-adapter/sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ export class NDKCacheAdapterSqlite implements NDKCacheAdapter {
}

async query(subscription: NDKSubscription): Promise<void> {
this.ifReady(() => {
// if (subscription.filters.some((filter) => !this.cachableKinds.includes(filter.kinds?.[0]))) return;

this.onReady(() => {

// Process filters from the subscription
for (const filter of subscription.filters) {
if (filter.authors) {
Expand All @@ -135,15 +134,23 @@ export class NDKCacheAdapterSqlite implements NDKCacheAdapter {
) as EventRecord[];
if (events.length > 0) foundEvents(subscription, events, filter);
}

for (const key in filter) {
if (key.startsWith('#') && key.length === 2) {
const tag = key[1];
const events = this.db.getAllSync(
`SELECT * FROM events INNER JOIN event_tags ON events.id = event_tags.event_id WHERE event_tags.tag = ? AND event_tags.value IN (${filter[key].map(() => '?').join(',')})`,
[tag, ...filter[key]]
) as EventRecord[];
console.log(`SQLITE ${key} filter found`, events.length);
if (events.length > 0) foundEvents(subscription, events, filter);
}
}
}
});
}

// public cachableKinds = [3, 10002, NDKKind.MuteList, NDKKind.BlossomList, NDKKind.Image, NDKKind.Text ];

async setEvent(event: NDKEvent, filters: NDKFilter[], relay?: NDKRelay): Promise<void> {
// if (!this.cachableKinds.includes(event.kind!)) return;

this.onReady(async () => {
// is the event replaceable?
if (event.isReplaceable()) {
Expand Down Expand Up @@ -269,6 +276,7 @@ export class NDKCacheAdapterSqlite implements NDKCacheAdapter {
}

addUnpublishedEvent(event: NDKEvent, relayUrls: WebSocket['url'][]): void {
this.setEvent(event, []);
this.onReady(async () => {
const relayStatus: { [key: string]: boolean } = {};
relayUrls.forEach(url => relayStatus[url] = false);
Expand Down
6 changes: 5 additions & 1 deletion ndk-mobile/src/stores/session/actions/mutePubkey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export const mutePubkey = (pubkey: Hexpubkey, set) => {

muteList.add(pubkey);
muteListEvent.tags.push(['p', pubkey]);
muteListEvent.publishReplaceable();

muteListEvent.publishReplaceable()
.then((res) => console.log('mute list', res, JSON.stringify(muteListEvent.rawEvent(), null, 4)))
.catch(console.error)


return { muteList };
});
Expand Down

0 comments on commit 238d4a5

Please sign in to comment.