Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reposted naddr #298

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion web/src/lib/Api.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { nip19, type Event, type SimplePool, Kind, type Filter } from 'nostr-tools';
import { get } from 'svelte/store';
import type { Event as NostrEvent, UserEvent } from '../routes/types';
import { cachedEvents, events as timelineEvents } from '../stores/Events';
import { cachedEvents, cachedReplaceableEvents, events as timelineEvents } from '../stores/Events';
import { saveMetadataEvent, userEvents } from '../stores/UserEvents';
import { isMuteEvent } from '../stores/Author';

Expand Down Expand Up @@ -131,6 +131,36 @@ export class Api {
return nostrEvent;
}

async fetchEventByAddress(
kind: Kind,
pubkey: string,
identifier: string
): Promise<Event | undefined> {
const address = [kind, pubkey, identifier].join(':');

// Load cache
const $cachedEvents = get(cachedReplaceableEvents);
const cachedEvent = $cachedEvents.get(address);
if (cachedEvent !== undefined) {
return cachedEvent;
}

const event = await this.fetchEvent([
{
kinds: [kind],
authors: [pubkey],
'#d': [identifier]
}
]);

// Save cache
if (event !== undefined) {
$cachedEvents.set(address, event);
}

return event;
}

async fetchContactListEvent(pubkey: string): Promise<Event | undefined> {
const events = await this.pool.list(this.relays, [
{
Expand Down
8 changes: 1 addition & 7 deletions web/src/routes/[naddr=naddr]/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,7 @@
$pool,
relays !== undefined && relays.length > 0 ? relays : $defaultRelays
);
event = await api.fetchEvent([
{
kinds: [kind],
authors: [pubkey],
'#d': [identifier]
}
]);
event = await api.fetchEventByAddress(kind, pubkey, identifier);
console.log('[event]', event);

if (event === undefined) {
Expand Down
8 changes: 1 addition & 7 deletions web/src/routes/content/Naddr.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,7 @@
$pool,
relays !== undefined && relays.length > 0 ? relays : $defaultRelays
);
event = await api.fetchEvent([
{
kinds: [kind],
authors: [pubkey],
'#d': [identifier]
}
]);
event = await api.fetchEventByAddress(kind, pubkey, identifier);
console.log('[event]', event);
});
</script>
Expand Down
16 changes: 16 additions & 0 deletions web/src/routes/timeline/NaddrLink.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<script lang="ts">
import { Kind, nip19 } from 'nostr-tools';

export let kind: Kind;
export let pubkey: string;
export let identifier: string;
export let relays: string[] | undefined = undefined;

const naddr = nip19.naddrEncode({ kind, pubkey, identifier, relays });
</script>

<article class="timeline-item">
<a href="/{naddr}">
{naddr.substring(0, 'naddr1'.length + 7)}
</a>
</article>
60 changes: 55 additions & 5 deletions web/src/routes/timeline/RepostedNote.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { Api } from '$lib/Api';
import NoteLink from './NoteLink.svelte';
import EventComponent from './EventComponent.svelte';
import NaddrLink from './NaddrLink.svelte';

export let event: NostrEvent;
export let readonly: boolean;
Expand All @@ -27,19 +28,66 @@
originalTag = event.tags.findLast(([t]) => t === 'e');
}

let originalAddressTag: string[] | undefined;
let kindString: string | undefined;
let pubkey: string | undefined;
let identifier: string | undefined;

if (originalTag === undefined) {
originalAddressTag = event.tags.find(
([tagName, tagContent, , marker]) =>
tagName === 'a' &&
tagContent !== undefined &&
(marker === 'mention' || marker === undefined)
);
}

onMount(async () => {
const api = new Api($pool, $readRelays);
api.fetchUserEvent(event.pubkey).then((userEvent) => {
user = userEvent?.user;
});

if (originalTag === undefined) {
console.warn('[repost not found]', event);
return;
if (originalTag !== undefined) {
const eventId = originalTag[1];
originalEvent = await api.fetchEventById(eventId);
} else if (originalAddressTag !== undefined) {
const relay = originalAddressTag.at(2);
const addressApi = new Api(
$pool,
relay === undefined ? $readRelays : [...$readRelays, relay]
);
[kindString, pubkey, identifier] = originalAddressTag[1].split(':');
const original = await addressApi.fetchEventByAddress(
Number(kindString),
pubkey,
identifier
);
if (original !== undefined) {
const userEvent = await api.fetchUserEvent(original.pubkey);
if (userEvent === undefined) {
originalEvent = original as NostrEvent;
} else {
let u: User;
try {
u = JSON.parse(userEvent.content);
originalEvent = {
...original,
user: u
};
} catch (error) {
console.warn('[invalid metadata]', error, userEvent.content);
originalEvent = original as NostrEvent;
}
}
}
} else {
console.warn('[original tag not found]', event);
}

const eventId = originalTag[1];
originalEvent = await api.fetchEventById(eventId);
if (originalEvent === undefined) {
console.warn('[original event not found]', event);
}
});

const toggleJsonDisplay = () => {
Expand Down Expand Up @@ -78,6 +126,8 @@
<EventComponent event={originalEvent} {readonly} {createdAtFormat} />
{:else if originalTag !== undefined}
<NoteLink eventId={originalTag[1]} />
{:else if originalAddressTag !== undefined && kindString !== undefined && pubkey !== undefined && identifier !== undefined}
<NaddrLink kind={Number(kindString)} {pubkey} {identifier} />
{/if}

<style>
Expand Down
2 changes: 2 additions & 0 deletions web/src/stores/Events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { writable, type Writable } from 'svelte/store';
import type { Event } from '../routes/types';
import type { Event as NostrEvent } from 'nostr-tools';

export const events: Writable<Event[]> = writable([]);
export const searchEvents: Writable<Event[]> = writable([]);
export const cachedEvents: Writable<Map<string, Event>> = writable(new Map());
export const cachedReplaceableEvents: Writable<Map<string, NostrEvent>> = writable(new Map());