From eab5f7a1617fd5614f2922ace6b6aa0795c51399 Mon Sep 17 00:00:00 2001
From: MK- Turiz <143194249+turizspace@users.noreply.github.com>
Date: Fri, 18 Oct 2024 17:05:55 +0300
Subject: [PATCH] Problem: can't zap notes
---
src/lib/components/UserLud16.svelte | 265 ++++++++++++++++++++++
src/lib/views/messages/RenderKind1.svelte | 16 +-
src/lib/zapUtils.ts | 93 ++++++++
3 files changed, 368 insertions(+), 6 deletions(-)
create mode 100644 src/lib/components/UserLud16.svelte
create mode 100644 src/lib/zapUtils.ts
diff --git a/src/lib/components/UserLud16.svelte b/src/lib/components/UserLud16.svelte
new file mode 100644
index 0000000..7bacda7
--- /dev/null
+++ b/src/lib/components/UserLud16.svelte
@@ -0,0 +1,265 @@
+
+
+
+
+ {#if $showModal}
+
+
+
Send Zap
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ {/if}
+
+
+
diff --git a/src/lib/views/messages/RenderKind1.svelte b/src/lib/views/messages/RenderKind1.svelte
index 061cf10..c800a2b 100644
--- a/src/lib/views/messages/RenderKind1.svelte
+++ b/src/lib/views/messages/RenderKind1.svelte
@@ -2,6 +2,7 @@
import RenderNoteContent from '@/components/RenderNoteContent.svelte';
import UserDisplayName from '@/components/UserDisplayName.svelte';
import UserProfilePic from '@/components/UserProfilePic.svelte';
+ import UserLud16 from '@/components/UserLud16.svelte';
import { viewed } from '@/snort_workers/main';
import type { FrontendData } from '@/snort_workers/types';
import { formatTimeAgo } from '@/utils';
@@ -23,6 +24,9 @@
let top: HTMLDivElement;
let messageViewController: HTMLDivElement;
+ // Declare showLud16 to toggle visibility of UserLud16
+ let showLud16 = false;
+
function updateDistance() {
const rect = messageViewController.getBoundingClientRect();
if (isTop && rect.top < 120 && $threadParentID === 'root') {
@@ -60,11 +64,6 @@
-
@@ -89,10 +88,14 @@
{
- alert('implement me!');
+ showLud16 = !showLud16; // Toggle visibility of UserLud16 on click
}}
{note}
/>
+
+ {#if showLud16}
+
+ {/if}
@@ -111,6 +114,7 @@
+
{
+ try {
+ let lnurl = '';
+ const { lud06, lud16 } = JSON.parse(metadata.content || '{}'); // Add fallback for missing content
+
+ if (lud06) {
+ const { words } = bech32.decode(lud06, 1000);
+ const data = bech32.fromWords(words);
+ lnurl = utf8Decoder.decode(data);
+ } else if (lud16) {
+ if (!lud16.includes('@')) {
+ throw new Error('Invalid LUD16 format');
+ }
+ // Extract domain and name from the LUD16 address
+ const [name, domain] = lud16.split('@');
+ // Build the LNURL service URL dynamically based on the domain
+ lnurl = new URL(`/.well-known/lnurlp/${name}`, `https://${domain}`).toString();
+ } else {
+ return null; // Handle missing LNURL case
+ }
+
+
+ const res = await _fetch(lnurl);
+
+ if (!res.ok) { // Handle non-200 response codes
+ console.error(`Error fetching LNURL: ${res.statusText}`);
+ return null;
+ }
+
+ const body = await res.json();
+
+ if (body.allowsNostr && body.nostrPubkey) {
+ return body.callback;
+ }
+ } catch (err) {
+ console.error("Error in getZapEndpoint:", err);
+ }
+
+ return null;
+}
+
+
+export function makeZapRequest({
+ profile,
+ event,
+ amount,
+ relays = [],
+ comment = ''
+}: {
+ profile: string;
+ event?: string;
+ amount: number;
+ relays?: string[];
+ comment?: string;
+}): {
+ created_at: number;
+ content: string;
+ tags: string[][]
+} {
+ if (!amount) throw new Error('Amount not given');
+ if (amount <= 0) throw new Error('Amount must be a positive number');
+ if (!profile) throw new Error('Profile not given');
+
+ const zapRequest = {
+ created_at: Math.round(Date.now() / 1000),
+ content: comment,
+ tags: [
+ ['amount', amount.toString()],
+ ['p', profile], // Include profile pubkey
+ ] as string[][] // Explicitly typing the array as string[][]
+ };
+
+ if (event) {
+ zapRequest.tags.push(['e', event]);
+ }
+
+ if (relays.length > 0) {
+ for (const relay of relays) {
+ zapRequest.tags.push(['relays', relay]);
+ } }
+
+ return zapRequest;
+}