From 0b4a1ac2d006204e8e79c157e083b21b6fcaedab Mon Sep 17 00:00:00 2001 From: Adrien Hopkins Date: Mon, 5 Jan 2026 11:22:11 -0500 Subject: Allow creation of new chats from dm page --- src/routes/(app)/dm/+page.svelte | 96 +++++++++++++++++++++++++++++++++------- 1 file changed, 81 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/routes/(app)/dm/+page.svelte b/src/routes/(app)/dm/+page.svelte index 54d0bd76..c172e604 100644 --- a/src/routes/(app)/dm/+page.svelte +++ b/src/routes/(app)/dm/+page.svelte @@ -2,13 +2,14 @@ import { tick } from "svelte"; import Icon from "$comp/Icon.svelte"; - import { isValid } from "nostr-tools/nip05"; + import { isValid, queryProfile } from "nostr-tools/nip05"; import * as toolsnip17 from 'nostr-tools/nip17'; import { getNostrUserInfo } from '$lib/nip01'; import { relaysSupporting } from '$lib/nip11'; import { getMessageRumours } from '$lib/nip17'; import * as libnip17 from '$lib/nip17'; + import { decode } from 'nostr-tools/nip19'; import { sign, getPrivateKey, pTagKeys } from '$lib/nostr'; import { t } from "$lib/translations"; import { theme } from "$lib/store"; @@ -28,7 +29,11 @@ } const userInfo = async (pubkey: string) => { - const nostrUserInfo = await getNostrUserInfo(pubkey); + if (pubkey.slice(0, 4) === "npub") { + return userInfo(decode(pubkey).data); + } + + const nostrUserInfo = await getNostrUserInfo(pubkey) || {}; const valid = 'nip05' in nostrUserInfo && await isValid(pubkey, nostrUserInfo.nip05); const username = await usernameFromPubkey(pubkey); return { @@ -89,10 +94,11 @@ let messageSenders = $state([]); let messageRecipients = $state([]); let chats = $state([]); - $inspect(chats); getMessageRumours(user).then(updateSendersRecipients); let selectedChat = $state(null); + let creatingNewChat = $state(false); + let searchQuery = $state(""); /// individual chat let text = $state(""); @@ -108,6 +114,9 @@ const selectChat = (c: object) => { selectedChat = c; + if (!chats.includes(c)) { + chats.unshift(c); + } userInfo(selectedChat.pubkey).then(info => nostrUserInfo = info); libnip17.getPreferredRelays(selectedChat.pubkey).then((relays) => { canSend = relays && relays.length > 0; @@ -117,6 +126,30 @@ updateEvents(); } + const newChatUsername = async (username: string) => { + const response = await fetch(`/api/users/${username}`); + if (!response.ok) { + alert("Invalid username"); + return; + } + const coinosUserInfo = await response.json(); + newChatPubkey(coinosUserInfo.pubkey); + } + + const newChatNip05 = async (nip05: string) => { + const response = await queryProfile(nip05); + if (response == null) { + alert("Invalid nip-05"); + return; + } + newChatPubkey(response.pubkey); + } + + const newChatPubkey = async (pubkey: string) => { + selectChat(await userInfo(pubkey)); + creatingNewChat = false; + } + const appendMultimap = (map: Map, key: string, value: object) => { if (map.has(key)) { map.get(key).push(value); @@ -206,8 +239,12 @@ } .short { - width: 6em; height: 1.5em; + margin-bottom: 10px; + } + + .small-width { + width: 6em; } .tiny { @@ -239,10 +276,20 @@ } .chat-btn { - min-height: 3em; margin: 2px 0px; - width: 100%; + padding: 0 25px; border-radius: 10px; + min-width: 30px; + text-align: left; + } + + .tall-btn { + min-height: 3em; + width: 100%; + } + + .push-right { + margin-left: auto; } .chat-header { @@ -310,12 +357,31 @@
-

Chats

- {#each chats as c} - + {#if creatingNewChat} + + {/if} + {#if !creatingNewChat} + {#each chats as c} + + {/each} + {:else if searchQuery !== ""} + - {/each} + {#if searchQuery.includes(".")} + + {/if} + {#if searchQuery.length === 64 || searchQuery.slice(0, 4) === "npub"} + + {/if} + {/if}

{$t("dm.header")}

{#if selectedChat} -

{selectedChat.username || nostrUserInfo.name} +

{selectedChat.username || nostrUserInfo.name || "Anonymous"} {nostrUserInfo.nip05 || selectedChat.pubkey.substring(0, 16)}

@@ -343,16 +409,16 @@
- sendMessage(text)}> + sendMessage(text)}> {#if relayWarningShown}

{$t("dm.noRelaysSet")} {$t("dm.nostrSettingsLink")}

{/if} - : . + : .

{$t("dm.fadedWarning")}

{#if canSend && !canSendExpiring} -

{$t("dm.cantSendExpiring").replace("[R]", selectedChat.username)}

+

{$t("dm.cantSendExpiring").replace("[R]", selectedChat.username || selectedChat.name)}

{/if} {/if}
-- cgit v1.2.3