Skip to content

Commit

Permalink
REQ content pubkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
SnowCait committed Oct 16, 2023
1 parent c72d63b commit 6ede3a9
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 31 deletions.
6 changes: 3 additions & 3 deletions web/src/lib/Api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { Signer } from './Signer';
import { channelMetadataEvents } from './cache/Events';
import { cachedEvents as newCachedEvents } from './cache/Events';
import { chronological, reverseChronological } from './Constants';
import { metadataReqEmit } from './timelines/MainTimeline';
import { referencesReqEmit } from './timelines/MainTimeline';

export class Api {
public static readonly replaceableKinds = [
Expand Down Expand Up @@ -205,7 +205,7 @@ export class Api {
return undefined;
}

metadataReqEmit(event);
referencesReqEmit(event);

// // Return
// const nostrEvent = event as NostrEvent;
Expand Down Expand Up @@ -259,7 +259,7 @@ export class Api {
const referencedEvents = await this.fetchEventsByIds([...referencedEventIds]);

for (const event of [...events, ...referencedEvents]) {
metadataReqEmit(event);
referencesReqEmit(event);
}

events.sort(reverseChronological);
Expand Down
22 changes: 21 additions & 1 deletion web/src/lib/Content.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { nip19 } from 'nostr-tools';
import type { EventPointer } from 'nostr-tools/lib/nip19';
import type { EventPointer, ProfilePointer } from 'nostr-tools/lib/nip19';

export class Token {
constructor(
Expand Down Expand Up @@ -103,6 +103,26 @@ export class Content {
.filter((x): x is string => x !== undefined);
}

static findNpubsAndNprofilesToPubkeys(content: string): string[] {
return this.findNpubsAndNprofiles(content)
.map((x) => {
try {
const { type, data } = nip19.decode(x);
switch (type) {
case 'npub':
return data as string;
case 'nprofile':
return (data as ProfilePointer).pubkey;
default:
return undefined;
}
} catch (error) {
return undefined;
}
})
.filter((x): x is string => x !== undefined);
}

static findNotesAndNevents(content: string): string[] {
const matches = content.matchAll(/(?<=^|\s)(nostr:)?(?<note>(note|nevent)1\w{6,})\b/g);
return [...matches]
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/Search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { EventItem } from './Items';
import { pool } from '../stores/Pool';
import { Api } from './Api';
import { readRelays } from '../stores/Author';
import { metadataReqEmit } from './timelines/MainTimeline';
import { referencesReqEmit } from './timelines/MainTimeline';

export class Search {
parseQuery(query: string): Filter {
Expand Down Expand Up @@ -70,7 +70,7 @@ export class Search {
const events = await api.fetchEvents([filter]);
console.log('[search events]', events);
for (const event of events) {
metadataReqEmit(event);
referencesReqEmit(event);
}
events.sort(reverseChronological);
return events.map((event) => new EventItem(event));
Expand Down
4 changes: 2 additions & 2 deletions web/src/lib/Timeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { userTimelineEvents } from '../stores/Events';
import { chunk } from './Array';
import { filterLimitItems } from './Constants';
import { Content } from './Content';
import { metadataReqEmit } from './timelines/MainTimeline';
import { referencesReqEmit } from './timelines/MainTimeline';

export class Timeline {
private readonly $pool: SimplePool;
Expand Down Expand Up @@ -99,7 +99,7 @@ export class Timeline {
return;
}

metadataReqEmit(event);
referencesReqEmit(event);

// Cache note events
const eventIds = new Set([
Expand Down
23 changes: 14 additions & 9 deletions web/src/lib/timelines/MainTimeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ import { Content } from '$lib/Content';
export const rxNostr = createRxNostr({ timeout }); // Based on NIP-65

const metadataReq = createRxBackwardReq();
const referencesReq = createRxBackwardReq();
const eventsReq = createRxBackwardReq();

export function metadataReqEmit(event: Event): void {
for (const pubkey of [event.pubkey, ...filterTags('p', event.tags)]) {
export function referencesReqEmit(event: Event): void {
console.debug('[rx-nostr references REQ emit]', event);
for (const pubkey of [
...new Set([
event.pubkey,
...filterTags('p', event.tags),
...Content.findNpubsAndNprofilesToPubkeys(event.content)
])
]) {
console.debug('[rx-nostr metadata REQ emit]', pubkey);
metadataReq.emit({
kinds: [0],
Expand All @@ -23,10 +30,8 @@ export function metadataReqEmit(event: Event): void {
});
}

const ids = Content.findNotesAndNeventsToIds(event.content);

const $eventItemStore = get(eventItemStore);
referencesReq.emit({
eventsReq.emit({
ids: [
...new Set([
...event.tags
Expand All @@ -35,7 +40,7 @@ export function metadataReqEmit(event: Event): void {
tagName === 'e' && id !== undefined && !$eventItemStore.has(id)
)
.map(([, id]) => id),
...ids
...Content.findNotesAndNeventsToIds(event.content)
])
]
});
Expand All @@ -56,10 +61,10 @@ rxNostr
});

rxNostr
.use(referencesReq.pipe(bufferTime(1000, null, 10), batch()))
.use(eventsReq.pipe(bufferTime(1000, null, 10), batch()))
.pipe(uniq())
.subscribe(async (packet) => {
console.log('[rx-nostr id]', packet);
console.log('[rx-nostr event]', packet);
const eventItem = new EventItem(packet.event);
const $eventItemStore = get(eventItemStore);
$eventItemStore.set(eventItem.event.id, eventItem);
Expand Down
4 changes: 2 additions & 2 deletions web/src/routes/[npub=npub]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { nip05, nip19, SimplePool, type Event } from 'nostr-tools';
import { createRxOneshotReq, now, uniq } from 'rx-nostr';
import { tap, bufferTime } from 'rxjs';
import { metadataReqEmit, rxNostr } from '$lib/timelines/MainTimeline';
import { referencesReqEmit, rxNostr } from '$lib/timelines/MainTimeline';
import type { User } from '../types';
import { pool } from '../../stores/Pool';
import TimelineView from '../TimelineView.svelte';
Expand Down Expand Up @@ -132,7 +132,7 @@
.use(pastEventsReq)
.pipe(
uniq(),
tap(({ event }: { event: Event }) => metadataReqEmit(event)),
tap(({ event }: { event: Event }) => referencesReqEmit(event)),
bufferTime(timelineBufferMs)
)
.subscribe({
Expand Down
4 changes: 2 additions & 2 deletions web/src/routes/[npub=npub]/bookmark/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import TimelineView from '../../TimelineView.svelte';
import { Signer } from '$lib/Signer';
import { EventItem } from '$lib/Items';
import { metadataReqEmit } from '$lib/timelines/MainTimeline';
import { referencesReqEmit } from '$lib/timelines/MainTimeline';
let publicBookmarkEventItems: EventItem[] = [];
let privateBookmarkEventItems: EventItem[] = [];
Expand Down Expand Up @@ -56,7 +56,7 @@
console.log('[bookmarks]', originalPublicBookmarkEvents, originalPrivateBookmarkEvents);
for (const event of [...originalPublicBookmarkEvents, ...originalPrivateBookmarkEvents]) {
metadataReqEmit(event);
referencesReqEmit(event);
}
publicBookmarkEventItems = originalPublicBookmarkEvents.map(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import { userTimelineEvents as items } from '../../../stores/Events';
import { Kind, SimplePool, type Event, nip19 } from 'nostr-tools';
import { minTimelineLength, reverseChronologicalItem, timelineBufferMs } from '$lib/Constants';
import { rxNostr, metadataReqEmit } from '$lib/timelines/MainTimeline';
import { rxNostr, referencesReqEmit } from '$lib/timelines/MainTimeline';
import { EventItem } from '$lib/Items';
export let pubkey: string;
Expand Down Expand Up @@ -77,7 +77,7 @@
.use(pastEventsReq)
.pipe(
uniq(),
tap(({ event }: { event: Event }) => metadataReqEmit(event)),
tap(({ event }: { event: Event }) => referencesReqEmit(event)),
bufferTime(timelineBufferMs)
)
.subscribe({
Expand Down
4 changes: 2 additions & 2 deletions web/src/routes/[slug=note]/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { createRxOneshotReq, filterKind, uniq, type LazyFilter } from 'rx-nostr';
import { tap } from 'rxjs';
import { rxNostr, metadataReqEmit } from '$lib/timelines/MainTimeline';
import { rxNostr, referencesReqEmit } from '$lib/timelines/MainTimeline';
import { cachedEvents, metadataStore } from '$lib/cache/Events';
import type { PageData } from './$types';
import { author, readRelays } from '../../stores/Author';
Expand Down Expand Up @@ -90,7 +90,7 @@
}
console.log('[thread REQ]', filters)
const eventReq = createRxOneshotReq({filters});
const observable = rxNostr.use(eventReq).pipe(uniq(), tap(({event}) => metadataReqEmit(event)));
const observable = rxNostr.use(eventReq).pipe(uniq(), tap(({event}) => referencesReqEmit(event)));
// Replies
observable.pipe(filterKind(1)).subscribe(packet => {
Expand Down
6 changes: 3 additions & 3 deletions web/src/routes/channels/[nevent=note]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import { Channel, channelIdStore } from '$lib/Channel';
import { timeout } from '$lib/Constants';
import type { ChannelMetadata } from '$lib/Types';
import { metadataReqEmit } from '$lib/timelines/MainTimeline';
import { referencesReqEmit } from '$lib/timelines/MainTimeline';
import { author, readRelays } from '../../../stores/Author';
import Content from '../../content/Content.svelte';
import TimelineView from '../../TimelineView.svelte';
Expand Down Expand Up @@ -117,7 +117,7 @@
.use(channelMessageReq)
.pipe(
uniq(),
tap(({ event }: { event: Event }) => metadataReqEmit(event))
tap(({ event }: { event: Event }) => referencesReqEmit(event))
)
.subscribe(async (packet) => {
console.debug('[channel message event]', packet);
Expand Down Expand Up @@ -167,7 +167,7 @@
.use(pastChannelMessageReq)
.pipe(
uniq(),
tap(({ event }: { event: Event }) => metadataReqEmit(event)),
tap(({ event }: { event: Event }) => referencesReqEmit(event)),
bufferTime(timelineBufferMs)
)
.subscribe({
Expand Down
6 changes: 3 additions & 3 deletions web/src/routes/home/FollowingTimeline.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import { batch, createRxForwardReq, createRxOneshotReq, latestEach, uniq } from 'rx-nostr';
import { tap, bufferTime } from 'rxjs';
import { userStatusesGeneral, userStatusesMusic } from '../../stores/UserStatuses';
import { metadataReqEmit, rxNostr } from '$lib/timelines/MainTimeline';
import { referencesReqEmit, rxNostr } from '$lib/timelines/MainTimeline';
import { WebStorage } from '$lib/WebStorage';
import { findIdentifier } from '$lib/EventHelper';
import { authorReplaceableKinds } from '$lib/Author';
Expand Down Expand Up @@ -171,7 +171,7 @@
return;
}
metadataReqEmit(event);
referencesReqEmit(event);
// Cache note events
const eventIds = new Set([
Expand Down Expand Up @@ -455,7 +455,7 @@
.use(pastChannelMessageReq)
.pipe(
uniq(),
tap(({ event }: { event: Event }) => metadataReqEmit(event)),
tap(({ event }: { event: Event }) => referencesReqEmit(event)),
bufferTime(timelineBufferMs)
)
.subscribe({
Expand Down

0 comments on commit 6ede3a9

Please sign in to comment.