aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/messaging.ts
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-03-06 08:39:18 -0800
committerAdam Soltys <[email protected]>2026-03-06 08:39:18 -0800
commit7e45e1b48c382f29c2d3bd114a4751a0f6b89d51 (patch)
tree7cbfc937afdcccab812d7f97c8f85b681c0d4687 /src/lib/messaging.ts
parent3b18232d52957dbd1a5a0c12891020d2a6e3295e (diff)
downloadcoinos-ui-7e45e1b48c382f29c2d3bd114a4751a0f6b89d51.tar.gz
coinos-ui-7e45e1b48c382f29c2d3bd114a4751a0f6b89d51.zip
group chats
Diffstat (limited to 'src/lib/messaging.ts')
-rw-r--r--src/lib/messaging.ts153
1 files changed, 58 insertions, 95 deletions
diff --git a/src/lib/messaging.ts b/src/lib/messaging.ts
index c4d4b211..37a4c22e 100644
--- a/src/lib/messaging.ts
+++ b/src/lib/messaging.ts
@@ -1,114 +1,77 @@
/**
- * Unified messaging facade: MLS (marmot) with NIP-17 fallback.
+ * Messaging facade — MLS groups only.
*
- * Drop-in replacement for the nip17 send/subscribe/getMessageRumours API.
- * Checks if the recipient supports MLS (has kind 443 key package) and
- * uses MLS when available, otherwise falls back to NIP-17.
+ * Every conversation is an MLS group (1:1 DMs are 2-person groups).
*/
-import * as nip17 from "$lib/nip17";
-import {
- recipientSupportsMls,
- sendMlsMessage,
- subscribeToMlsMessages,
- getMlsMessageRumours,
+export {
publishKeyPackage,
+ getGroups,
+ getGroupRumours,
+ sendGroupMessage,
+ findOrCreateDmGroup,
+ subscribeToMessages,
+ fetchGroupHistory,
} from "$lib/mls-relay";
-// Re-export unchanged functions from nip17
-export {
- decrypt,
- getPreferredRelays,
- cacheRumour,
- getCachedChatList,
- setCachedChatList,
- getCachedUserInfo,
- setCachedUserInfo,
-} from "$lib/nip17";
-
-// Cache for MLS support checks
-const mlsSupportCache = new Map<string, boolean>();
+export type { GroupInfo } from "$lib/mls-relay";
-async function checkMlsSupport(pubkey: string): Promise<boolean> {
- if (mlsSupportCache.has(pubkey)) return mlsSupportCache.get(pubkey)!;
- try {
- const supported = await recipientSupportsMls(pubkey);
- console.log("[messaging] MLS support for", pubkey.slice(0, 12) + "…:", supported);
- mlsSupportCache.set(pubkey, supported);
- return supported;
- } catch (e) {
- console.warn("[messaging] MLS support check failed:", e);
- return false;
- }
-}
+import { getNostrUserInfo } from "$lib/nip01";
+import { isValid } from "nostr-tools/nip05";
-/** Send a message — MLS if both parties support it, NIP-17 otherwise. */
-export const send = async (message: string, user: any, recipient: any, expiryDays?: number) => {
- console.log("[messaging] send to", recipient.pubkey?.slice(0, 12) + "…", { self: user.pubkey === recipient.pubkey, expiry: !!expiryDays });
- if (user.pubkey !== recipient.pubkey && !expiryDays) {
- try {
- const peerSupports = await checkMlsSupport(recipient.pubkey);
- if (peerSupports) {
- console.log("[messaging] → MLS path");
- await sendMlsMessage(user, recipient.pubkey, message);
- return;
- }
- console.log("[messaging] → NIP-17 fallback (no MLS support)");
- } catch (e) {
- console.warn("[messaging] MLS send failed, falling back to NIP-17:", e);
- }
- }
+// ---------------------------------------------------------------------------
+// User info cache (shared across pages)
+// ---------------------------------------------------------------------------
- return nip17.send(message, user, recipient, expiryDays);
-};
+const userInfoCache = new Map<string, any>();
-/** Get all message rumours — combines NIP-17 and MLS sources. */
-export const getMessageRumours = async (user: any): Promise<any[]> => {
- const [nip17Rumours, mlsRumours] = await Promise.allSettled([
- nip17.getMessageRumours(user),
- getMlsMessageRumours(user),
- ]);
+export function getCachedUserInfo(pubkey: string): any | null {
+ return userInfoCache.get(pubkey) ?? null;
+}
- const rumours: any[] = [];
- if (nip17Rumours.status === "fulfilled") rumours.push(...nip17Rumours.value);
- if (mlsRumours.status === "fulfilled") rumours.push(...mlsRumours.value);
+export function setCachedUserInfo(pubkey: string, info: any): void {
+ userInfoCache.set(pubkey, info);
+}
- // Dedupe by id
- const seen = new Set<string>();
- return rumours.filter((r) => {
- if (seen.has(r.id)) return false;
- seen.add(r.id);
- return true;
- });
-};
+/** Resolve a pubkey to user info (coinos user, nostr profile, or anonymous). */
+export async function resolveUser(pubkey: string): Promise<any> {
+ const cached = userInfoCache.get(pubkey);
+ if (cached) return cached;
-/** Subscribe to both NIP-17 and MLS incoming messages. Returns a close function. */
-export const subscribeToMessages = async (
- user: any,
- onNewRumour: (rumour: any) => void,
-): Promise<() => void> => {
- const closers: (() => void)[] = [];
+ const nostrUserInfo = (await getNostrUserInfo(pubkey)) || {};
+ const valid = "nip05" in nostrUserInfo && (await isValid(pubkey, nostrUserInfo.nip05));
- // NIP-17 subscription
+ let coinosUsername = null;
+ let picture = nostrUserInfo?.picture || null;
try {
- const closeNip17 = await nip17.subscribeToMessages(user, onNewRumour);
- closers.push(closeNip17);
- } catch (e) {
- console.error("[messaging] NIP-17 subscription failed:", e);
- }
+ const response = await fetch(`/api/users/${pubkey}`);
+ if (response.ok) {
+ const info = await response.json();
+ if (!info.anon) {
+ coinosUsername = info.username || null;
+ picture = info.picture || picture;
+ }
+ }
+ } catch {}
- // MLS subscription
- try {
- const closeMls = await subscribeToMlsMessages(user, onNewRumour);
- closers.push(closeMls);
- } catch (e) {
- console.error("[messaging] MLS subscription failed:", e);
- }
+ const info = {
+ coinosUsername,
+ picture,
+ nostrName: nostrUserInfo?.name || null,
+ nip05: nostrUserInfo?.nip05 || null,
+ nip05Valid: valid,
+ pubkey,
+ };
+ userInfoCache.set(pubkey, info);
+ return info;
+}
- // Publish our key package so others can start MLS conversations with us
- publishKeyPackage(user).catch((e) =>
- console.warn("[messaging] Failed to publish MLS key package:", e),
- );
+/** Get a display name for a user info object. */
+export function displayName(info: any): string {
+ return info?.coinosUsername || info?.nostrName || shortPubkey(info?.pubkey || "");
+}
- return () => closers.forEach((c) => c());
-};
+function shortPubkey(pk: string): string {
+ if (pk.length <= 16) return pk;
+ return pk.slice(0, 8) + "…" + pk.slice(-4);
+}