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

Refactor channel pin #1579

Merged
merged 3 commits into from
Jan 7, 2025
Merged
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
38 changes: 14 additions & 24 deletions web/src/routes/(app)/channels/[nevent=note]/PinChannel.svelte
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
<script lang="ts">
import { createRxNostr, createRxOneshotReq, latest, now } from 'rx-nostr';
import { createRxOneshotReq, latest, now } from 'rx-nostr';
import { every, firstValueFrom, EmptyError } from 'rxjs';
import { onDestroy } from 'svelte';
import type { Kind, EventTemplate } from 'nostr-tools';
import { authorChannelsEventStore } from '$lib/cache/Events';
import { Signer } from '$lib/Signer';
import { pubkey, writeRelays } from '$lib/stores/Author';
import { verificationClient } from '$lib/timelines/MainTimeline';
import { pubkey } from '$lib/stores/Author';
import { rxNostr } from '$lib/timelines/MainTimeline';
import IconPin from '@tabler/icons-svelte/icons/pin';
import IconPinnedFilled from '@tabler/icons-svelte/icons/pinned-filled';

Expand All @@ -17,24 +16,21 @@
([tagName, id]) => tagName === 'e' && id === channelId
) ?? false;

const rxNostr = createRxNostr({ verifier: verificationClient.verifier });

async function pin() {
console.log('[channel pin]', channelId);

pinned = true;

rxNostr.setDefaultRelays($writeRelays);
const pinReq = createRxOneshotReq({
filters: { kinds: [10005], authors: [$pubkey], limit: 1 }
});
let unsignedEvent: EventTemplate;
try {
const packet = await firstValueFrom(rxNostr.use(pinReq).pipe(latest()));
console.log('[channel pin latest]', packet);
console.debug('[channel pin latest]', packet);

if (packet.event.tags.some(([tagName, id]) => tagName === 'e' && id === channelId)) {
console.log('[channel pin already]', packet.event);
console.debug('[channel pin already]', packet.event);
return;
}

Expand All @@ -46,7 +42,7 @@
};
} catch (error) {
if (error instanceof EmptyError) {
console.log('[channel pin not found]', error);
console.debug('[channel pin not found]', error);
unsignedEvent = {
kind: 10005 as Kind,
content: '',
Expand All @@ -60,10 +56,10 @@
}

const event = await Signer.signEvent(unsignedEvent);
console.log('[channel pin event]', event);
console.debug('[channel pin event]', event);
const observable = rxNostr.send(event);
observable.subscribe((packet) => {
console.log('[send]', packet);
console.debug('[channel pin send]', packet);
if (packet.ok && $authorChannelsEventStore?.id !== event.id) {
$authorChannelsEventStore = event;
}
Expand All @@ -83,17 +79,16 @@

pinned = false;

rxNostr.setDefaultRelays($writeRelays);
const pinReq = createRxOneshotReq({
filters: { kinds: [10005], authors: [$pubkey], limit: 1 }
});
let unsignedEvent: EventTemplate;
try {
const packet = await firstValueFrom(rxNostr.use(pinReq).pipe(latest()));
console.log('[channel pin latest]', packet);
console.debug('[channel pin latest]', packet);

if (!packet.event.tags.some(([tagName, id]) => tagName === 'e' && id === channelId)) {
console.log('[channel unpin already]', packet.event);
console.debug('[channel unpin already]', packet.event);
return;
}

Expand All @@ -108,7 +103,7 @@
};
} catch (error) {
if (error instanceof EmptyError) {
console.log('[channel unpin already]', error);
console.debug('[channel unpin already]', error);
return;
} else {
pinned = true;
Expand All @@ -117,28 +112,23 @@
}

const event = await Signer.signEvent(unsignedEvent);
console.log('[channel unpin event]', event);
console.debug('[channel unpin event]', event);
const observable = rxNostr.send(event);
observable.subscribe((packet) => {
console.log('[send]', packet);
console.debug('[channel unpin send]', packet);
if (packet.ok && $authorChannelsEventStore?.id !== event.id) {
$authorChannelsEventStore = event;
}
});
observable.pipe(every((packet) => !packet.ok)).subscribe((failed) => {
console.log('[channel pinned]', !failed);
console.log('[channel unpinned]', !failed);
if (failed) {
console.error('[channel unpin failed]');
alert('Failed to unpin.');
pinned = true;
}
});
}

onDestroy(async () => {
console.log('[channel pin on destroy]');
rxNostr.dispose();
});
</script>

{#if pinned}
Expand Down
Loading