mirror of
https://github.com/purrgrammer/grimoire.git
synced 2026-04-10 15:36:53 +02:00
feat: implement unified chat system with NIP-C7 and NIP-29 support
Core Architecture: - Protocol adapter pattern for chat implementations - Base adapter interface with protocol-specific implementations - Auto-detection of protocol from identifier format - Reactive message loading via EventStore observables Protocol Implementations: - NIP-C7 adapter: Simple chat (kind 9) with npub/nprofile support - NIP-29 adapter: Relay-based groups with member roles and moderation - Protocol-aware reply message loading with relay hints - Proper NIP-29 members/admins fetching using #d tags UI Components: - ChatViewer: Main chat interface with virtualized message timeline - ChatMessage: Message rendering with reply preview - ReplyPreview: Auto-loading replied-to messages from relays - MembersDropdown: Virtualized member list with role labels - RelaysDropdown: Connection status for chat relays - ChatComposer: Message input with send functionality Command System: - chat command with identifier parsing and auto-detection - Support for npub, nprofile, NIP-05, and relay'group-id formats - Integration with window system and dynamic titles NIP-29 Specific: - Fetch kind:39000 (metadata), kind:39001 (admins), kind:39002 (members) - Extract roles from p tags: ["p", "<pubkey>", "<role1>", "<role2>"] - Role normalization (admin, moderator, host, member) - Single group relay connection management Testing: - Comprehensive chat parser tests - Protocol adapter test structure - All tests passing (704 tests) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
255
src/components/ChatViewer.tsx
Normal file
255
src/components/ChatViewer.tsx
Normal file
@@ -0,0 +1,255 @@
|
||||
import { useMemo, useState, memo, useCallback } from "react";
|
||||
import { use$ } from "applesauce-react/hooks";
|
||||
import { from } from "rxjs";
|
||||
import { Virtuoso } from "react-virtuoso";
|
||||
import type {
|
||||
ChatProtocol,
|
||||
ProtocolIdentifier,
|
||||
Conversation,
|
||||
} from "@/types/chat";
|
||||
import { NipC7Adapter } from "@/lib/chat/adapters/nip-c7-adapter";
|
||||
import { Nip29Adapter } from "@/lib/chat/adapters/nip-29-adapter";
|
||||
import type { ChatProtocolAdapter } from "@/lib/chat/adapters/base-adapter";
|
||||
import type { Message } from "@/types/chat";
|
||||
import { UserName } from "./nostr/UserName";
|
||||
import { RichText } from "./nostr/RichText";
|
||||
import Timestamp from "./Timestamp";
|
||||
import { ReplyPreview } from "./chat/ReplyPreview";
|
||||
import { MembersDropdown } from "./chat/MembersDropdown";
|
||||
import { RelaysDropdown } from "./chat/RelaysDropdown";
|
||||
import { useGrimoire } from "@/core/state";
|
||||
import { Button } from "./ui/button";
|
||||
|
||||
interface ChatViewerProps {
|
||||
protocol: ChatProtocol;
|
||||
identifier: ProtocolIdentifier;
|
||||
customTitle?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* MessageItem - Memoized message component for performance
|
||||
*/
|
||||
const MessageItem = memo(function MessageItem({
|
||||
message,
|
||||
adapter,
|
||||
conversation,
|
||||
}: {
|
||||
message: Message;
|
||||
adapter: ChatProtocolAdapter;
|
||||
conversation: Conversation;
|
||||
}) {
|
||||
return (
|
||||
<div className="group flex items-start hover:bg-muted/50">
|
||||
<div className="">
|
||||
<div className="flex items-center gap-2">
|
||||
<UserName pubkey={message.author} className="font-semibold text-sm" />
|
||||
<span className="text-xs text-muted-foreground">
|
||||
<Timestamp timestamp={message.timestamp} />
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-sm leading-relaxed">
|
||||
{message.event ? (
|
||||
<RichText event={message.event}>
|
||||
{message.replyTo && (
|
||||
<ReplyPreview
|
||||
replyToId={message.replyTo}
|
||||
adapter={adapter}
|
||||
conversation={conversation}
|
||||
/>
|
||||
)}
|
||||
</RichText>
|
||||
) : (
|
||||
<span className="whitespace-pre-wrap">{message.content}</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
/**
|
||||
* ChatViewer - Main chat interface component
|
||||
*
|
||||
* Provides protocol-agnostic chat UI that works across all Nostr messaging protocols.
|
||||
* Uses adapter pattern to handle protocol-specific logic while providing consistent UX.
|
||||
*/
|
||||
export function ChatViewer({
|
||||
protocol,
|
||||
identifier,
|
||||
customTitle,
|
||||
}: ChatViewerProps) {
|
||||
const { addWindow } = useGrimoire();
|
||||
|
||||
// Get the appropriate adapter for this protocol
|
||||
const adapter = useMemo(() => getAdapter(protocol), [protocol]);
|
||||
|
||||
// Resolve conversation from identifier (async operation)
|
||||
const conversation = use$(
|
||||
() => from(adapter.resolveConversation(identifier)),
|
||||
[adapter, identifier],
|
||||
);
|
||||
|
||||
// Load messages for this conversation (reactive)
|
||||
const messages = use$(
|
||||
() => (conversation ? adapter.loadMessages(conversation) : undefined),
|
||||
[adapter, conversation],
|
||||
);
|
||||
|
||||
// Track reply context (which message is being replied to)
|
||||
const [replyTo, setReplyTo] = useState<string | undefined>();
|
||||
|
||||
// Handle sending messages
|
||||
const handleSend = async (content: string, replyToId?: string) => {
|
||||
if (!conversation) return;
|
||||
await adapter.sendMessage(conversation, content, replyToId);
|
||||
setReplyTo(undefined); // Clear reply context after sending
|
||||
};
|
||||
|
||||
// Handle NIP badge click
|
||||
const handleNipClick = useCallback(() => {
|
||||
if (conversation?.protocol === "nip-29") {
|
||||
addWindow("nip", { number: 29 });
|
||||
}
|
||||
}, [conversation?.protocol, addWindow]);
|
||||
|
||||
if (!conversation) {
|
||||
return (
|
||||
<div className="flex h-full items-center justify-center text-muted-foreground">
|
||||
Loading conversation...
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex h-full flex-col">
|
||||
{/* Header with conversation info and controls */}
|
||||
<div className="px-1 border-b w-full">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="flex flex-1 min-w-0 items-center gap-2">
|
||||
{conversation.metadata?.icon && (
|
||||
<img
|
||||
src={conversation.metadata.icon}
|
||||
alt={conversation.title}
|
||||
className="h-4 w-4 object-cover flex-shrink-0"
|
||||
/>
|
||||
)}
|
||||
<div className="flex-1 flex flex-row gap-2 items-baseline min-w-0">
|
||||
<h2 className="truncate text-base font-semibold">
|
||||
{customTitle || conversation.title}
|
||||
</h2>
|
||||
{conversation.metadata?.description && (
|
||||
<p className="text-xs text-muted-foreground line-clamp-1">
|
||||
{conversation.metadata.description}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-2 text-xs text-muted-foreground p-1">
|
||||
<MembersDropdown participants={conversation.participants} />
|
||||
<RelaysDropdown conversation={conversation} />
|
||||
{conversation.type === "group" && (
|
||||
<button
|
||||
onClick={handleNipClick}
|
||||
className="rounded bg-muted px-1.5 py-0.5 font-mono hover:bg-muted/80 transition-colors cursor-pointer"
|
||||
>
|
||||
{conversation.protocol.toUpperCase()}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Message timeline with virtualization */}
|
||||
<div className="flex-1 overflow-hidden">
|
||||
{messages && messages.length > 0 ? (
|
||||
<Virtuoso
|
||||
data={messages}
|
||||
initialTopMostItemIndex={messages.length - 1}
|
||||
followOutput="smooth"
|
||||
itemContent={(_index, message) => (
|
||||
<MessageItem
|
||||
key={message.id}
|
||||
message={message}
|
||||
adapter={adapter}
|
||||
conversation={conversation}
|
||||
/>
|
||||
)}
|
||||
style={{ height: "100%" }}
|
||||
/>
|
||||
) : (
|
||||
<div className="flex h-full items-center justify-center text-sm text-muted-foreground">
|
||||
No messages yet. Start the conversation!
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Message composer */}
|
||||
<div className="">
|
||||
{replyTo && (
|
||||
<div className="flex items-center gap-2 rounded bg-muted px-2 py-1 text-xs">
|
||||
<span>Replying to {replyTo.slice(0, 8)}...</span>
|
||||
<button
|
||||
onClick={() => setReplyTo(undefined)}
|
||||
className="ml-auto text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
const form = e.currentTarget;
|
||||
const input = form.elements.namedItem(
|
||||
"message",
|
||||
) as HTMLTextAreaElement;
|
||||
if (input.value.trim()) {
|
||||
handleSend(input.value, replyTo);
|
||||
input.value = "";
|
||||
}
|
||||
}}
|
||||
className="flex gap-2"
|
||||
>
|
||||
<textarea
|
||||
name="message"
|
||||
autoFocus
|
||||
placeholder="Type a message..."
|
||||
className="flex-1 resize-none bg-background px-2 py-1.5 text-sm"
|
||||
rows={1}
|
||||
onKeyDown={(e) => {
|
||||
// Submit on Enter (without Shift)
|
||||
if (e.key === "Enter" && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
e.currentTarget.form?.requestSubmit();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<Button type="submit" variant="secondary">
|
||||
Send
|
||||
</Button>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the appropriate adapter for a protocol
|
||||
* TODO: Add other adapters as they're implemented
|
||||
*/
|
||||
function getAdapter(protocol: ChatProtocol): ChatProtocolAdapter {
|
||||
switch (protocol) {
|
||||
case "nip-c7":
|
||||
return new NipC7Adapter();
|
||||
case "nip-29":
|
||||
return new Nip29Adapter();
|
||||
// case "nip-17":
|
||||
// return new Nip17Adapter();
|
||||
// case "nip-28":
|
||||
// return new Nip28Adapter();
|
||||
// case "nip-53":
|
||||
// return new Nip53Adapter();
|
||||
default:
|
||||
throw new Error(`Unsupported protocol: ${protocol}`);
|
||||
}
|
||||
}
|
||||
@@ -26,6 +26,10 @@ import { getTagValues } from "@/lib/nostr-utils";
|
||||
import { getLiveHost } from "@/lib/live-activity";
|
||||
import type { NostrEvent } from "@/types/nostr";
|
||||
import { getZapSender } from "applesauce-common/helpers/zap";
|
||||
import { NipC7Adapter } from "@/lib/chat/adapters/nip-c7-adapter";
|
||||
import { Nip29Adapter } from "@/lib/chat/adapters/nip-29-adapter";
|
||||
import type { ChatProtocol, ProtocolIdentifier } from "@/types/chat";
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
export interface WindowTitleData {
|
||||
title: string | ReactElement;
|
||||
@@ -546,6 +550,46 @@ function useDynamicTitle(window: WindowInstance): WindowTitleData {
|
||||
return `Relay Pool (${connectedCount}/${relayList.length})`;
|
||||
}, [appId, relays]);
|
||||
|
||||
// Chat viewer title - resolve conversation to get partner name
|
||||
const [chatTitle, setChatTitle] = useState<string | null>(null);
|
||||
useEffect(() => {
|
||||
if (appId !== "chat") {
|
||||
setChatTitle(null);
|
||||
return;
|
||||
}
|
||||
|
||||
const protocol = props.protocol as ChatProtocol;
|
||||
const identifier = props.identifier as ProtocolIdentifier;
|
||||
|
||||
// Get adapter and resolve conversation
|
||||
const getAdapter = () => {
|
||||
switch (protocol) {
|
||||
case "nip-c7":
|
||||
return new NipC7Adapter();
|
||||
case "nip-29":
|
||||
return new Nip29Adapter();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
};
|
||||
|
||||
const adapter = getAdapter();
|
||||
if (!adapter) {
|
||||
setChatTitle("Chat");
|
||||
return;
|
||||
}
|
||||
|
||||
// Resolve conversation asynchronously
|
||||
adapter
|
||||
.resolveConversation(identifier)
|
||||
.then((conversation) => {
|
||||
setChatTitle(conversation.title);
|
||||
})
|
||||
.catch(() => {
|
||||
setChatTitle("Chat");
|
||||
});
|
||||
}, [appId, props]);
|
||||
|
||||
// Generate final title data with icon and tooltip
|
||||
return useMemo(() => {
|
||||
let title: ReactElement | string;
|
||||
@@ -619,6 +663,10 @@ function useDynamicTitle(window: WindowInstance): WindowTitleData {
|
||||
title = connTitle;
|
||||
icon = getCommandIcon("conn");
|
||||
tooltip = rawCommand;
|
||||
} else if (chatTitle && appId === "chat") {
|
||||
title = chatTitle;
|
||||
icon = getCommandIcon("chat");
|
||||
tooltip = rawCommand;
|
||||
} else {
|
||||
title = staticTitle || appId.toUpperCase();
|
||||
tooltip = rawCommand;
|
||||
@@ -642,6 +690,7 @@ function useDynamicTitle(window: WindowInstance): WindowTitleData {
|
||||
kindsTitle,
|
||||
debugTitle,
|
||||
connTitle,
|
||||
chatTitle,
|
||||
staticTitle,
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ const DebugViewer = lazy(() =>
|
||||
import("./DebugViewer").then((m) => ({ default: m.DebugViewer })),
|
||||
);
|
||||
const ConnViewer = lazy(() => import("./ConnViewer"));
|
||||
const ChatViewer = lazy(() =>
|
||||
import("./ChatViewer").then((m) => ({ default: m.ChatViewer })),
|
||||
);
|
||||
const SpellsViewer = lazy(() =>
|
||||
import("./SpellsViewer").then((m) => ({ default: m.SpellsViewer })),
|
||||
);
|
||||
@@ -169,6 +172,15 @@ export function WindowRenderer({ window, onClose }: WindowRendererProps) {
|
||||
case "conn":
|
||||
content = <ConnViewer />;
|
||||
break;
|
||||
case "chat":
|
||||
content = (
|
||||
<ChatViewer
|
||||
protocol={window.props.protocol}
|
||||
identifier={window.props.identifier}
|
||||
customTitle={window.customTitle}
|
||||
/>
|
||||
);
|
||||
break;
|
||||
case "spells":
|
||||
content = <SpellsViewer />;
|
||||
break;
|
||||
|
||||
58
src/components/chat/MembersDropdown.tsx
Normal file
58
src/components/chat/MembersDropdown.tsx
Normal file
@@ -0,0 +1,58 @@
|
||||
import { Users2 } from "lucide-react";
|
||||
import { Virtuoso } from "react-virtuoso";
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { UserName } from "@/components/nostr/UserName";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import type { Participant } from "@/types/chat";
|
||||
|
||||
interface MembersDropdownProps {
|
||||
participants: Participant[];
|
||||
}
|
||||
|
||||
/**
|
||||
* MembersDropdown - Shows member count and list with roles
|
||||
* Similar to relay indicators in ReqViewer
|
||||
*/
|
||||
export function MembersDropdown({ participants }: MembersDropdownProps) {
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button className="flex items-center gap-1 text-muted-foreground hover:text-foreground transition-colors">
|
||||
<Users2 className="size-3" />
|
||||
<span>{participants.length}</span>
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end" className="w-64">
|
||||
<div className="px-2 py-1.5 text-xs font-medium text-muted-foreground">
|
||||
Members ({participants.length})
|
||||
</div>
|
||||
<div style={{ height: "300px" }}>
|
||||
<Virtuoso
|
||||
data={participants}
|
||||
itemContent={(_index, participant) => (
|
||||
<div
|
||||
key={participant.pubkey}
|
||||
className="flex items-center justify-between gap-2 px-2 py-1.5 rounded hover:bg-muted/50 transition-colors"
|
||||
>
|
||||
<UserName
|
||||
pubkey={participant.pubkey}
|
||||
className="text-sm truncate flex-1 min-w-0"
|
||||
/>
|
||||
{participant.role && participant.role !== "member" && (
|
||||
<Label size="sm" className="flex-shrink-0">
|
||||
{participant.role}
|
||||
</Label>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
style={{ height: "100%" }}
|
||||
/>
|
||||
</div>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
}
|
||||
92
src/components/chat/RelaysDropdown.tsx
Normal file
92
src/components/chat/RelaysDropdown.tsx
Normal file
@@ -0,0 +1,92 @@
|
||||
import { Wifi } from "lucide-react";
|
||||
import {
|
||||
DropdownMenu,
|
||||
DropdownMenuContent,
|
||||
DropdownMenuTrigger,
|
||||
} from "@/components/ui/dropdown-menu";
|
||||
import { RelayLink } from "@/components/nostr/RelayLink";
|
||||
import { useRelayState } from "@/hooks/useRelayState";
|
||||
import { getConnectionIcon, getAuthIcon } from "@/lib/relay-status-utils";
|
||||
import { normalizeRelayURL } from "@/lib/relay-url";
|
||||
import type { Conversation } from "@/types/chat";
|
||||
|
||||
interface RelaysDropdownProps {
|
||||
conversation: Conversation;
|
||||
}
|
||||
|
||||
/**
|
||||
* RelaysDropdown - Shows relay count and list with connection status
|
||||
* Similar to relay indicators in ReqViewer
|
||||
*/
|
||||
export function RelaysDropdown({ conversation }: RelaysDropdownProps) {
|
||||
const { relays: relayStates } = useRelayState();
|
||||
|
||||
// Get relays for this conversation
|
||||
const relays: string[] = [];
|
||||
|
||||
// NIP-29: Single group relay
|
||||
if (conversation.metadata?.relayUrl) {
|
||||
relays.push(conversation.metadata.relayUrl);
|
||||
}
|
||||
|
||||
// Normalize URLs for state lookup
|
||||
const normalizedRelays = relays.map((url) => {
|
||||
try {
|
||||
return normalizeRelayURL(url);
|
||||
} catch {
|
||||
return url;
|
||||
}
|
||||
});
|
||||
|
||||
// Count connected relays
|
||||
const connectedCount = normalizedRelays.filter((url) => {
|
||||
const state = relayStates[url];
|
||||
return state?.connectionState === "connected";
|
||||
}).length;
|
||||
|
||||
if (relays.length === 0) {
|
||||
return null; // Don't show if no relays
|
||||
}
|
||||
|
||||
return (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
<button className="flex items-center gap-1 text-muted-foreground hover:text-foreground transition-colors">
|
||||
<Wifi className="size-3" />
|
||||
<span>
|
||||
{connectedCount}/{relays.length}
|
||||
</span>
|
||||
</button>
|
||||
</DropdownMenuTrigger>
|
||||
<DropdownMenuContent align="end" className="w-64">
|
||||
<div className="px-2 py-1.5 text-xs font-medium text-muted-foreground">
|
||||
Relays ({relays.length})
|
||||
</div>
|
||||
<div className="space-y-1 p-1">
|
||||
{relays.map((url) => {
|
||||
const normalizedUrl = normalizedRelays[relays.indexOf(url)];
|
||||
const state = relayStates[normalizedUrl];
|
||||
const connIcon = getConnectionIcon(state);
|
||||
const authIcon = getAuthIcon(state);
|
||||
|
||||
return (
|
||||
<div
|
||||
key={url}
|
||||
className="flex items-center gap-2 px-2 py-1.5 rounded hover:bg-muted/50 transition-colors"
|
||||
>
|
||||
<div className="flex items-center gap-1 flex-shrink-0">
|
||||
{connIcon.icon}
|
||||
{authIcon.icon}
|
||||
</div>
|
||||
<RelayLink
|
||||
url={url}
|
||||
className="text-sm truncate flex-1 min-w-0"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</DropdownMenuContent>
|
||||
</DropdownMenu>
|
||||
);
|
||||
}
|
||||
59
src/components/chat/ReplyPreview.tsx
Normal file
59
src/components/chat/ReplyPreview.tsx
Normal file
@@ -0,0 +1,59 @@
|
||||
import { memo, useEffect } from "react";
|
||||
import { use$ } from "applesauce-react/hooks";
|
||||
import eventStore from "@/services/event-store";
|
||||
import { UserName } from "../nostr/UserName";
|
||||
import { RichText } from "../nostr/RichText";
|
||||
import type { ChatProtocolAdapter } from "@/lib/chat/adapters/base-adapter";
|
||||
import type { Conversation } from "@/types/chat";
|
||||
|
||||
interface ReplyPreviewProps {
|
||||
replyToId: string;
|
||||
adapter: ChatProtocolAdapter;
|
||||
conversation: Conversation;
|
||||
}
|
||||
|
||||
/**
|
||||
* ReplyPreview - Shows who is being replied to with truncated message content
|
||||
* Automatically fetches missing events from protocol-specific relays
|
||||
*/
|
||||
export const ReplyPreview = memo(function ReplyPreview({
|
||||
replyToId,
|
||||
adapter,
|
||||
conversation,
|
||||
}: ReplyPreviewProps) {
|
||||
// Load the event being replied to (reactive - updates when event arrives)
|
||||
const replyEvent = use$(() => eventStore.event(replyToId), [replyToId]);
|
||||
|
||||
// Fetch event from relays if not in store
|
||||
useEffect(() => {
|
||||
if (!replyEvent) {
|
||||
adapter.loadReplyMessage(conversation, replyToId).catch((err) => {
|
||||
console.error(
|
||||
`[ReplyPreview] Failed to load reply ${replyToId.slice(0, 8)}:`,
|
||||
err,
|
||||
);
|
||||
});
|
||||
}
|
||||
}, [replyEvent, adapter, conversation, replyToId]);
|
||||
|
||||
if (!replyEvent) {
|
||||
return (
|
||||
<div className="text-xs text-muted-foreground mb-0.5">
|
||||
↳ Replying to {replyToId.slice(0, 8)}...
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="text-xs text-muted-foreground flex items-baseline gap-1 mb-0.5 overflow-hidden">
|
||||
<span className="flex-shrink-0">↳</span>
|
||||
<UserName
|
||||
pubkey={replyEvent.pubkey}
|
||||
className="font-medium flex-shrink-0"
|
||||
/>
|
||||
<div className="line-clamp-1 overflow-hidden flex-1 min-w-0">
|
||||
<RichText event={replyEvent} />
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
@@ -9,7 +9,7 @@ const Textarea = React.forwardRef<
|
||||
return (
|
||||
<textarea
|
||||
className={cn(
|
||||
"flex min-h-[60px] w-full rounded-md border border-input bg-transparent px-3 py-2 text-base shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
|
||||
"flex bg-transparent p-2 text-base transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 md:text-sm",
|
||||
className,
|
||||
)}
|
||||
ref={ref}
|
||||
|
||||
109
src/lib/chat-parser.test.ts
Normal file
109
src/lib/chat-parser.test.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { parseChatCommand } from "./chat-parser";
|
||||
|
||||
describe("parseChatCommand", () => {
|
||||
describe("NIP-29 relay groups", () => {
|
||||
it("should parse NIP-29 group ID without protocol (single arg)", () => {
|
||||
const result = parseChatCommand(["groups.0xchat.com'chachi"]);
|
||||
|
||||
expect(result.protocol).toBe("nip-29");
|
||||
expect(result.identifier).toEqual({
|
||||
type: "group",
|
||||
value: "chachi",
|
||||
relays: ["wss://groups.0xchat.com"],
|
||||
});
|
||||
expect(result.adapter.protocol).toBe("nip-29");
|
||||
});
|
||||
|
||||
it("should parse NIP-29 group ID when split by shell-quote", () => {
|
||||
// shell-quote splits on ' so "groups.0xchat.com'chachi" becomes ["groups.0xchat.com", "chachi"]
|
||||
const result = parseChatCommand(["groups.0xchat.com", "chachi"]);
|
||||
|
||||
expect(result.protocol).toBe("nip-29");
|
||||
expect(result.identifier).toEqual({
|
||||
type: "group",
|
||||
value: "chachi",
|
||||
relays: ["wss://groups.0xchat.com"],
|
||||
});
|
||||
expect(result.adapter.protocol).toBe("nip-29");
|
||||
});
|
||||
|
||||
it("should parse NIP-29 group ID with wss:// protocol (single arg)", () => {
|
||||
const result = parseChatCommand(["wss://groups.0xchat.com'chachi"]);
|
||||
|
||||
expect(result.protocol).toBe("nip-29");
|
||||
expect(result.identifier).toEqual({
|
||||
type: "group",
|
||||
value: "chachi",
|
||||
relays: ["wss://groups.0xchat.com"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should parse NIP-29 group ID with wss:// when split by shell-quote", () => {
|
||||
const result = parseChatCommand(["wss://groups.0xchat.com", "chachi"]);
|
||||
|
||||
expect(result.protocol).toBe("nip-29");
|
||||
expect(result.identifier).toEqual({
|
||||
type: "group",
|
||||
value: "chachi",
|
||||
relays: ["wss://groups.0xchat.com"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should parse NIP-29 group with different relay and group-id (single arg)", () => {
|
||||
const result = parseChatCommand(["relay.example.com'bitcoin-dev"]);
|
||||
|
||||
expect(result.protocol).toBe("nip-29");
|
||||
expect(result.identifier.value).toBe("bitcoin-dev");
|
||||
expect(result.identifier.relays).toEqual(["wss://relay.example.com"]);
|
||||
});
|
||||
|
||||
it("should parse NIP-29 group with different relay when split", () => {
|
||||
const result = parseChatCommand(["relay.example.com", "bitcoin-dev"]);
|
||||
|
||||
expect(result.protocol).toBe("nip-29");
|
||||
expect(result.identifier.value).toBe("bitcoin-dev");
|
||||
expect(result.identifier.relays).toEqual(["wss://relay.example.com"]);
|
||||
});
|
||||
|
||||
it("should parse NIP-29 group from nos.lol", () => {
|
||||
const result = parseChatCommand(["nos.lol'welcome"]);
|
||||
|
||||
expect(result.protocol).toBe("nip-29");
|
||||
expect(result.identifier.value).toBe("welcome");
|
||||
expect(result.identifier.relays).toEqual(["wss://nos.lol"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("error handling", () => {
|
||||
it("should throw error when no identifier provided", () => {
|
||||
expect(() => parseChatCommand([])).toThrow(
|
||||
"Chat identifier required. Usage: chat <identifier>",
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error for unsupported identifier format", () => {
|
||||
expect(() => parseChatCommand(["unsupported-format"])).toThrow(
|
||||
/Unable to determine chat protocol/,
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error for npub (NIP-C7 disabled)", () => {
|
||||
expect(() => parseChatCommand(["npub1xyz"])).toThrow(
|
||||
/Unable to determine chat protocol/,
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error for note/nevent (NIP-28 not implemented)", () => {
|
||||
expect(() => parseChatCommand(["note1xyz"])).toThrow(
|
||||
/Unable to determine chat protocol/,
|
||||
);
|
||||
});
|
||||
|
||||
it("should throw error for naddr (NIP-53 not implemented)", () => {
|
||||
expect(() => parseChatCommand(["naddr1xyz"])).toThrow(
|
||||
/Unable to determine chat protocol/,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
71
src/lib/chat-parser.ts
Normal file
71
src/lib/chat-parser.ts
Normal file
@@ -0,0 +1,71 @@
|
||||
import type { ChatCommandResult } from "@/types/chat";
|
||||
// import { NipC7Adapter } from "./chat/adapters/nip-c7-adapter";
|
||||
import { Nip29Adapter } from "./chat/adapters/nip-29-adapter";
|
||||
// Import other adapters as they're implemented
|
||||
// import { Nip17Adapter } from "./chat/adapters/nip-17-adapter";
|
||||
// import { Nip28Adapter } from "./chat/adapters/nip-28-adapter";
|
||||
// import { Nip53Adapter } from "./chat/adapters/nip-53-adapter";
|
||||
|
||||
/**
|
||||
* Parse a chat command identifier and auto-detect the protocol
|
||||
*
|
||||
* Tries each adapter's parseIdentifier() in priority order:
|
||||
* 1. NIP-17 (encrypted DMs) - prioritized for privacy
|
||||
* 2. NIP-28 (channels) - specific event format (kind 40)
|
||||
* 3. NIP-29 (groups) - specific group ID format
|
||||
* 4. NIP-53 (live chat) - specific addressable format (kind 30311)
|
||||
* 5. NIP-C7 (simple chat) - fallback for generic pubkeys
|
||||
*
|
||||
* @param args - Command arguments (first arg is the identifier)
|
||||
* @returns Parsed result with protocol and identifier
|
||||
* @throws Error if no adapter can parse the identifier
|
||||
*/
|
||||
export function parseChatCommand(args: string[]): ChatCommandResult {
|
||||
if (args.length === 0) {
|
||||
throw new Error("Chat identifier required. Usage: chat <identifier>");
|
||||
}
|
||||
|
||||
// Handle NIP-29 format that may be split by shell-quote
|
||||
// If we have 2 args and they look like relay + group-id, join them with '
|
||||
let identifier = args[0];
|
||||
if (args.length === 2 && args[0].includes(".") && !args[0].includes("'")) {
|
||||
// Looks like "relay.com" "group-id" split by shell-quote
|
||||
// Rejoin with apostrophe for NIP-29 format
|
||||
identifier = `${args[0]}'${args[1]}`;
|
||||
}
|
||||
|
||||
// Try each adapter in priority order
|
||||
const adapters = [
|
||||
// new Nip17Adapter(), // Phase 2
|
||||
// new Nip28Adapter(), // Phase 3
|
||||
new Nip29Adapter(), // Phase 4 - Relay groups (currently only enabled)
|
||||
// new Nip53Adapter(), // Phase 5
|
||||
// new NipC7Adapter(), // Phase 1 - Simple chat (disabled for now)
|
||||
];
|
||||
|
||||
for (const adapter of adapters) {
|
||||
const parsed = adapter.parseIdentifier(identifier);
|
||||
if (parsed) {
|
||||
return {
|
||||
protocol: adapter.protocol,
|
||||
identifier: parsed,
|
||||
adapter,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
`Unable to determine chat protocol from identifier: ${identifier}
|
||||
|
||||
Currently supported format:
|
||||
- relay.com'group-id (NIP-29 relay group, wss:// prefix optional)
|
||||
Examples:
|
||||
chat relay.example.com'bitcoin-dev
|
||||
chat wss://relay.example.com'nostr-dev
|
||||
|
||||
More formats coming soon:
|
||||
- npub/nprofile/hex pubkey (NIP-C7/NIP-17 direct messages)
|
||||
- note/nevent (NIP-28 public channels)
|
||||
- naddr (NIP-53 live activity chat)`,
|
||||
);
|
||||
}
|
||||
107
src/lib/chat/adapters/base-adapter.ts
Normal file
107
src/lib/chat/adapters/base-adapter.ts
Normal file
@@ -0,0 +1,107 @@
|
||||
import type { Observable } from "rxjs";
|
||||
import type {
|
||||
Conversation,
|
||||
Message,
|
||||
ProtocolIdentifier,
|
||||
ChatCapabilities,
|
||||
ChatProtocol,
|
||||
ConversationType,
|
||||
LoadMessagesOptions,
|
||||
CreateConversationParams,
|
||||
} from "@/types/chat";
|
||||
import type { NostrEvent } from "@/types/nostr";
|
||||
|
||||
/**
|
||||
* Abstract base class for all chat protocol adapters
|
||||
*
|
||||
* Each adapter implements protocol-specific logic for:
|
||||
* - Identifier parsing and resolution
|
||||
* - Message loading and sending
|
||||
* - Conversation management
|
||||
* - Protocol capabilities
|
||||
*/
|
||||
export abstract class ChatProtocolAdapter {
|
||||
abstract readonly protocol: ChatProtocol;
|
||||
abstract readonly type: ConversationType;
|
||||
|
||||
/**
|
||||
* Parse an identifier string to determine if this adapter can handle it
|
||||
* Returns null if the identifier doesn't match this protocol
|
||||
*/
|
||||
abstract parseIdentifier(input: string): ProtocolIdentifier | null;
|
||||
|
||||
/**
|
||||
* Resolve a protocol identifier into a full Conversation object
|
||||
* May involve fetching metadata from relays
|
||||
*/
|
||||
abstract resolveConversation(
|
||||
identifier: ProtocolIdentifier,
|
||||
): Promise<Conversation>;
|
||||
|
||||
/**
|
||||
* Load messages for a conversation
|
||||
* Returns an Observable that emits message arrays as they arrive
|
||||
*/
|
||||
abstract loadMessages(
|
||||
conversation: Conversation,
|
||||
options?: LoadMessagesOptions,
|
||||
): Observable<Message[]>;
|
||||
|
||||
/**
|
||||
* Load more historical messages (pagination)
|
||||
*/
|
||||
abstract loadMoreMessages(
|
||||
conversation: Conversation,
|
||||
before: number,
|
||||
): Promise<Message[]>;
|
||||
|
||||
/**
|
||||
* Send a message to a conversation
|
||||
* Returns when the message has been published
|
||||
*/
|
||||
abstract sendMessage(
|
||||
conversation: Conversation,
|
||||
content: string,
|
||||
replyTo?: string,
|
||||
): Promise<void>;
|
||||
|
||||
/**
|
||||
* Get the capabilities of this protocol
|
||||
* Used to determine which UI features to show
|
||||
*/
|
||||
abstract getCapabilities(): ChatCapabilities;
|
||||
|
||||
/**
|
||||
* Load a replied-to message by ID
|
||||
* First checks EventStore, then fetches from protocol-specific relays if needed
|
||||
* Returns null if event cannot be loaded
|
||||
*/
|
||||
abstract loadReplyMessage(
|
||||
conversation: Conversation,
|
||||
eventId: string,
|
||||
): Promise<NostrEvent | null>;
|
||||
|
||||
/**
|
||||
* Load list of all conversations for this protocol
|
||||
* Optional - not all protocols support conversation lists
|
||||
*/
|
||||
loadConversationList?(): Observable<Conversation[]>;
|
||||
|
||||
/**
|
||||
* Create a new conversation
|
||||
* Optional - not all protocols support creation
|
||||
*/
|
||||
createConversation?(params: CreateConversationParams): Promise<Conversation>;
|
||||
|
||||
/**
|
||||
* Join an existing conversation
|
||||
* Optional - only for protocols with join semantics (groups)
|
||||
*/
|
||||
joinConversation?(conversation: Conversation): Promise<void>;
|
||||
|
||||
/**
|
||||
* Leave a conversation
|
||||
* Optional - only for protocols with leave semantics (groups)
|
||||
*/
|
||||
leaveConversation?(conversation: Conversation): Promise<void>;
|
||||
}
|
||||
98
src/lib/chat/adapters/nip-29-adapter.test.ts
Normal file
98
src/lib/chat/adapters/nip-29-adapter.test.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { Nip29Adapter } from "./nip-29-adapter";
|
||||
|
||||
describe("Nip29Adapter", () => {
|
||||
const adapter = new Nip29Adapter();
|
||||
|
||||
describe("parseIdentifier", () => {
|
||||
it("should parse group ID with relay domain (no protocol)", () => {
|
||||
const result = adapter.parseIdentifier("groups.0xchat.com'chachi");
|
||||
expect(result).toEqual({
|
||||
type: "group",
|
||||
value: "chachi",
|
||||
relays: ["wss://groups.0xchat.com"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should parse group ID with wss:// protocol", () => {
|
||||
const result = adapter.parseIdentifier("wss://groups.0xchat.com'chachi");
|
||||
expect(result).toEqual({
|
||||
type: "group",
|
||||
value: "chachi",
|
||||
relays: ["wss://groups.0xchat.com"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should parse group ID with ws:// protocol", () => {
|
||||
const result = adapter.parseIdentifier("ws://relay.example.com'test");
|
||||
expect(result).toEqual({
|
||||
type: "group",
|
||||
value: "test",
|
||||
relays: ["ws://relay.example.com"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should parse various group-id formats", () => {
|
||||
const result1 = adapter.parseIdentifier("relay.example.com'bitcoin-dev");
|
||||
expect(result1?.value).toBe("bitcoin-dev");
|
||||
expect(result1?.relays).toEqual(["wss://relay.example.com"]);
|
||||
|
||||
const result2 = adapter.parseIdentifier("nos.lol'welcome");
|
||||
expect(result2?.value).toBe("welcome");
|
||||
expect(result2?.relays).toEqual(["wss://nos.lol"]);
|
||||
|
||||
const result3 = adapter.parseIdentifier("relay.test.com'my_group_123");
|
||||
expect(result3?.value).toBe("my_group_123");
|
||||
expect(result3?.relays).toEqual(["wss://relay.test.com"]);
|
||||
});
|
||||
|
||||
it("should handle relay URLs with ports", () => {
|
||||
const result = adapter.parseIdentifier(
|
||||
"relay.example.com:7777'testgroup",
|
||||
);
|
||||
expect(result).toEqual({
|
||||
type: "group",
|
||||
value: "testgroup",
|
||||
relays: ["wss://relay.example.com:7777"],
|
||||
});
|
||||
});
|
||||
|
||||
it("should return null for invalid formats", () => {
|
||||
expect(adapter.parseIdentifier("")).toBeNull();
|
||||
expect(adapter.parseIdentifier("just-a-string")).toBeNull();
|
||||
expect(adapter.parseIdentifier("no-apostrophe")).toBeNull();
|
||||
expect(adapter.parseIdentifier("'missing-relay")).toBeNull();
|
||||
expect(adapter.parseIdentifier("missing-groupid'")).toBeNull();
|
||||
expect(adapter.parseIdentifier("multiple'apostrophes'here")).toBeNull();
|
||||
});
|
||||
|
||||
it("should return null for non-NIP-29 identifiers", () => {
|
||||
// These should not match NIP-29 format
|
||||
expect(adapter.parseIdentifier("npub1...")).toBeNull();
|
||||
expect(adapter.parseIdentifier("note1...")).toBeNull();
|
||||
expect(adapter.parseIdentifier("naddr1...")).toBeNull();
|
||||
expect(adapter.parseIdentifier("alice@example.com")).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("protocol properties", () => {
|
||||
it("should have correct protocol and type", () => {
|
||||
expect(adapter.protocol).toBe("nip-29");
|
||||
expect(adapter.type).toBe("group");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getCapabilities", () => {
|
||||
it("should return correct capabilities", () => {
|
||||
const capabilities = adapter.getCapabilities();
|
||||
|
||||
expect(capabilities.supportsEncryption).toBe(false);
|
||||
expect(capabilities.supportsThreading).toBe(true);
|
||||
expect(capabilities.supportsModeration).toBe(true);
|
||||
expect(capabilities.supportsRoles).toBe(true);
|
||||
expect(capabilities.supportsGroupManagement).toBe(true);
|
||||
expect(capabilities.canCreateConversations).toBe(false);
|
||||
expect(capabilities.requiresRelay).toBe(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
553
src/lib/chat/adapters/nip-29-adapter.ts
Normal file
553
src/lib/chat/adapters/nip-29-adapter.ts
Normal file
@@ -0,0 +1,553 @@
|
||||
import { Observable } from "rxjs";
|
||||
import { map, first } from "rxjs/operators";
|
||||
import type { Filter } from "nostr-tools";
|
||||
import { ChatProtocolAdapter } from "./base-adapter";
|
||||
import type {
|
||||
Conversation,
|
||||
Message,
|
||||
ProtocolIdentifier,
|
||||
ChatCapabilities,
|
||||
LoadMessagesOptions,
|
||||
} from "@/types/chat";
|
||||
import type { NostrEvent } from "@/types/nostr";
|
||||
import eventStore from "@/services/event-store";
|
||||
import pool from "@/services/relay-pool";
|
||||
import { publishEventToRelays } from "@/services/hub";
|
||||
import accountManager from "@/services/accounts";
|
||||
import { getTagValues } from "@/lib/nostr-utils";
|
||||
import { EventFactory } from "applesauce-core/event-factory";
|
||||
|
||||
/**
|
||||
* NIP-29 Adapter - Relay-Based Groups
|
||||
*
|
||||
* Features:
|
||||
* - Relay-enforced group membership and moderation
|
||||
* - Admin, moderator, and member roles
|
||||
* - Single relay enforces all group rules
|
||||
* - Group chat messages (kind 9)
|
||||
*
|
||||
* Group ID format: wss://relay.url'group-id
|
||||
* Events use "h" tag with group-id
|
||||
*/
|
||||
export class Nip29Adapter extends ChatProtocolAdapter {
|
||||
readonly protocol = "nip-29" as const;
|
||||
readonly type = "group" as const;
|
||||
|
||||
/**
|
||||
* Parse identifier - accepts group ID format: relay'group-id
|
||||
* Examples:
|
||||
* - wss://relay.example.com'bitcoin-dev
|
||||
* - relay.example.com'bitcoin-dev (wss:// prefix is optional)
|
||||
*/
|
||||
parseIdentifier(input: string): ProtocolIdentifier | null {
|
||||
// NIP-29 format: [wss://]relay'group-id
|
||||
const match = input.match(/^((?:wss?:\/\/)?[^']+)'([^']+)$/);
|
||||
if (!match) return null;
|
||||
|
||||
let [, relayUrl] = match;
|
||||
const groupId = match[2];
|
||||
|
||||
// Add wss:// prefix if not present
|
||||
if (!relayUrl.startsWith("ws://") && !relayUrl.startsWith("wss://")) {
|
||||
relayUrl = `wss://${relayUrl}`;
|
||||
}
|
||||
|
||||
return {
|
||||
type: "group",
|
||||
value: groupId,
|
||||
relays: [relayUrl],
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve conversation from group identifier
|
||||
*/
|
||||
async resolveConversation(
|
||||
identifier: ProtocolIdentifier,
|
||||
): Promise<Conversation> {
|
||||
const groupId = identifier.value;
|
||||
const relayUrl = identifier.relays?.[0];
|
||||
|
||||
if (!relayUrl) {
|
||||
throw new Error("NIP-29 groups require a relay URL");
|
||||
}
|
||||
|
||||
const activePubkey = accountManager.active$.value?.pubkey;
|
||||
if (!activePubkey) {
|
||||
throw new Error("No active account");
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[NIP-29] Fetching group metadata for ${groupId} from ${relayUrl}`,
|
||||
);
|
||||
|
||||
// Fetch group metadata from the specific relay (kind 39000)
|
||||
const metadataFilter: Filter = {
|
||||
kinds: [39000],
|
||||
"#d": [groupId],
|
||||
limit: 1,
|
||||
};
|
||||
|
||||
// Use pool.subscription to fetch from the relay
|
||||
const metadataEvents: NostrEvent[] = [];
|
||||
const metadataObs = pool.subscription([relayUrl], [metadataFilter], {
|
||||
eventStore, // Automatically add to store
|
||||
});
|
||||
|
||||
// Subscribe and wait for EOSE
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
console.log("[NIP-29] Metadata fetch timeout");
|
||||
resolve();
|
||||
}, 5000);
|
||||
|
||||
const sub = metadataObs.subscribe({
|
||||
next: (response) => {
|
||||
if (typeof response === "string") {
|
||||
// EOSE received
|
||||
clearTimeout(timeout);
|
||||
console.log(
|
||||
`[NIP-29] Got ${metadataEvents.length} metadata events`,
|
||||
);
|
||||
sub.unsubscribe();
|
||||
resolve();
|
||||
} else {
|
||||
// Event received
|
||||
metadataEvents.push(response);
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
clearTimeout(timeout);
|
||||
console.error("[NIP-29] Metadata fetch error:", err);
|
||||
sub.unsubscribe();
|
||||
reject(err);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
const metadataEvent = metadataEvents[0];
|
||||
|
||||
// Debug: Log metadata event tags
|
||||
if (metadataEvent) {
|
||||
console.log(`[NIP-29] Metadata event tags:`, metadataEvent.tags);
|
||||
}
|
||||
|
||||
// Extract group info from metadata event
|
||||
const title = metadataEvent
|
||||
? getTagValues(metadataEvent, "name")[0] || groupId
|
||||
: groupId;
|
||||
const description = metadataEvent
|
||||
? getTagValues(metadataEvent, "about")[0]
|
||||
: undefined;
|
||||
const icon = metadataEvent
|
||||
? getTagValues(metadataEvent, "picture")[0]
|
||||
: undefined;
|
||||
|
||||
console.log(`[NIP-29] Group title: ${title}`);
|
||||
|
||||
// Fetch admins (kind 39001) and members (kind 39002)
|
||||
// Both use d tag (addressable events signed by relay)
|
||||
const participantsFilter: Filter = {
|
||||
kinds: [39001, 39002],
|
||||
"#d": [groupId],
|
||||
limit: 10, // Should be 1 of each kind, but allow for duplicates
|
||||
};
|
||||
|
||||
const participantEvents: NostrEvent[] = [];
|
||||
const participantsObs = pool.subscription(
|
||||
[relayUrl],
|
||||
[participantsFilter],
|
||||
{
|
||||
eventStore,
|
||||
},
|
||||
);
|
||||
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
const timeout = setTimeout(() => {
|
||||
console.log("[NIP-29] Participants fetch timeout");
|
||||
resolve();
|
||||
}, 5000);
|
||||
|
||||
const sub = participantsObs.subscribe({
|
||||
next: (response) => {
|
||||
if (typeof response === "string") {
|
||||
// EOSE received
|
||||
clearTimeout(timeout);
|
||||
console.log(
|
||||
`[NIP-29] Got ${participantEvents.length} participant events`,
|
||||
);
|
||||
sub.unsubscribe();
|
||||
resolve();
|
||||
} else {
|
||||
// Event received
|
||||
participantEvents.push(response);
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
clearTimeout(timeout);
|
||||
console.error("[NIP-29] Participants fetch error:", err);
|
||||
sub.unsubscribe();
|
||||
reject(err);
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
// Helper to validate and normalize role names
|
||||
const normalizeRole = (role: string | undefined): ParticipantRole => {
|
||||
if (!role) return "member";
|
||||
const lower = role.toLowerCase();
|
||||
if (lower === "admin") return "admin";
|
||||
if (lower === "moderator") return "moderator";
|
||||
if (lower === "host") return "host";
|
||||
// Default to member for unknown roles
|
||||
return "member";
|
||||
};
|
||||
|
||||
// Extract participants from both admins and members events
|
||||
const participantsMap = new Map<string, Participant>();
|
||||
|
||||
// Process kind:39001 (admins with roles)
|
||||
const adminEvents = participantEvents.filter((e) => e.kind === 39001);
|
||||
for (const event of adminEvents) {
|
||||
// Each p tag: ["p", "<pubkey>", "<role1>", "<role2>", ...]
|
||||
for (const tag of event.tags) {
|
||||
if (tag[0] === "p" && tag[1]) {
|
||||
const pubkey = tag[1];
|
||||
const roles = tag.slice(2).filter((r) => r); // Get all roles after pubkey
|
||||
const primaryRole = normalizeRole(roles[0]); // Use first role as primary
|
||||
participantsMap.set(pubkey, { pubkey, role: primaryRole });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Process kind:39002 (members without roles)
|
||||
const memberEvents = participantEvents.filter((e) => e.kind === 39002);
|
||||
for (const event of memberEvents) {
|
||||
// Each p tag: ["p", "<pubkey>"]
|
||||
for (const tag of event.tags) {
|
||||
if (tag[0] === "p" && tag[1]) {
|
||||
const pubkey = tag[1];
|
||||
// Only add if not already in map (admins take precedence)
|
||||
if (!participantsMap.has(pubkey)) {
|
||||
participantsMap.set(pubkey, { pubkey, role: "member" });
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const participants = Array.from(participantsMap.values());
|
||||
|
||||
console.log(
|
||||
`[NIP-29] Found ${participants.length} participants (${adminEvents.length} admin events, ${memberEvents.length} member events)`,
|
||||
);
|
||||
console.log(
|
||||
`[NIP-29] Metadata - title: ${title}, icon: ${icon}, description: ${description}`,
|
||||
);
|
||||
|
||||
return {
|
||||
id: `nip-29:${relayUrl}'${groupId}`,
|
||||
type: "group",
|
||||
protocol: "nip-29",
|
||||
title,
|
||||
participants,
|
||||
metadata: {
|
||||
groupId,
|
||||
relayUrl,
|
||||
...(description && { description }),
|
||||
...(icon && { icon }),
|
||||
},
|
||||
unreadCount: 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Load messages for a group
|
||||
*/
|
||||
loadMessages(
|
||||
conversation: Conversation,
|
||||
options?: LoadMessagesOptions,
|
||||
): Observable<Message[]> {
|
||||
const groupId = conversation.metadata?.groupId;
|
||||
const relayUrl = conversation.metadata?.relayUrl;
|
||||
|
||||
if (!groupId || !relayUrl) {
|
||||
throw new Error("Group ID and relay URL required");
|
||||
}
|
||||
|
||||
console.log(`[NIP-29] Loading messages for ${groupId} from ${relayUrl}`);
|
||||
|
||||
// Subscribe to group messages (kind 9)
|
||||
const filter: Filter = {
|
||||
kinds: [9],
|
||||
"#h": [groupId],
|
||||
limit: options?.limit || 200,
|
||||
};
|
||||
|
||||
if (options?.before) {
|
||||
filter.until = options.before;
|
||||
}
|
||||
if (options?.after) {
|
||||
filter.since = options.after;
|
||||
}
|
||||
|
||||
// Start a persistent subscription to the group relay
|
||||
// This will feed new messages into the EventStore in real-time
|
||||
pool
|
||||
.subscription([relayUrl], [filter], {
|
||||
eventStore, // Automatically add to store
|
||||
})
|
||||
.subscribe({
|
||||
next: (response) => {
|
||||
if (typeof response === "string") {
|
||||
// EOSE received
|
||||
console.log("[NIP-29] EOSE received for messages");
|
||||
} else {
|
||||
// Event received
|
||||
console.log(
|
||||
`[NIP-29] Received message: ${response.id.slice(0, 8)}...`,
|
||||
);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
// Return observable from EventStore which will update automatically
|
||||
return eventStore.timeline(filter).pipe(
|
||||
map((events) => {
|
||||
console.log(`[NIP-29] Timeline has ${events.length} messages`);
|
||||
return events
|
||||
.map((event) => this.eventToMessage(event, conversation.id))
|
||||
.sort((a, b) => a.timestamp - b.timestamp); // Oldest first for flex-col-reverse
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load more historical messages (pagination)
|
||||
*/
|
||||
async loadMoreMessages(
|
||||
_conversation: Conversation,
|
||||
_before: number,
|
||||
): Promise<Message[]> {
|
||||
// For now, return empty - pagination to be implemented in Phase 6
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message to the group
|
||||
*/
|
||||
async sendMessage(
|
||||
conversation: Conversation,
|
||||
content: string,
|
||||
replyTo?: string,
|
||||
): Promise<void> {
|
||||
const activePubkey = accountManager.active$.value?.pubkey;
|
||||
const activeSigner = accountManager.active$.value?.signer;
|
||||
|
||||
if (!activePubkey || !activeSigner) {
|
||||
throw new Error("No active account or signer");
|
||||
}
|
||||
|
||||
const groupId = conversation.metadata?.groupId;
|
||||
const relayUrl = conversation.metadata?.relayUrl;
|
||||
|
||||
if (!groupId || !relayUrl) {
|
||||
throw new Error("Group ID and relay URL required");
|
||||
}
|
||||
|
||||
// Create event factory and sign event
|
||||
const factory = new EventFactory();
|
||||
factory.setSigner(activeSigner);
|
||||
|
||||
const tags: string[][] = [["h", groupId]];
|
||||
|
||||
if (replyTo) {
|
||||
// NIP-29 uses q-tag for replies (same as NIP-C7)
|
||||
tags.push(["q", replyTo]);
|
||||
}
|
||||
|
||||
// Use kind 9 for group chat messages
|
||||
const draft = await factory.build({ kind: 9, content, tags });
|
||||
const event = await factory.sign(draft);
|
||||
|
||||
// Publish only to the group relay
|
||||
await publishEventToRelays(event, [conversation?.metadata?.relayUrl]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get protocol capabilities
|
||||
*/
|
||||
getCapabilities(): ChatCapabilities {
|
||||
return {
|
||||
supportsEncryption: false, // kind 9 messages are public
|
||||
supportsThreading: true, // q-tag replies (NIP-C7 style)
|
||||
supportsModeration: true, // kind 9005/9006 for delete/ban
|
||||
supportsRoles: true, // admin, moderator, member
|
||||
supportsGroupManagement: true, // join/leave via kind 9021
|
||||
canCreateConversations: false, // Groups created by admins (kind 9007)
|
||||
requiresRelay: true, // Single relay enforces rules
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a replied-to message
|
||||
* First checks EventStore, then fetches from group relay if needed
|
||||
*/
|
||||
async loadReplyMessage(
|
||||
conversation: Conversation,
|
||||
eventId: string,
|
||||
): Promise<NostrEvent | null> {
|
||||
// First check EventStore - might already be loaded
|
||||
const cachedEvent = await eventStore
|
||||
.event(eventId)
|
||||
.pipe(first())
|
||||
.toPromise();
|
||||
if (cachedEvent) {
|
||||
return cachedEvent;
|
||||
}
|
||||
|
||||
// Not in store, fetch from group relay
|
||||
const relayUrl = conversation.metadata?.relayUrl;
|
||||
if (!relayUrl) {
|
||||
console.warn("[NIP-29] No relay URL for loading reply message");
|
||||
return null;
|
||||
}
|
||||
|
||||
console.log(
|
||||
`[NIP-29] Fetching reply message ${eventId.slice(0, 8)}... from ${relayUrl}`,
|
||||
);
|
||||
|
||||
const filter: Filter = {
|
||||
ids: [eventId],
|
||||
limit: 1,
|
||||
};
|
||||
|
||||
const events: NostrEvent[] = [];
|
||||
const obs = pool.subscription([relayUrl], [filter], { eventStore });
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
const timeout = setTimeout(() => {
|
||||
console.log(
|
||||
`[NIP-29] Reply message fetch timeout for ${eventId.slice(0, 8)}...`,
|
||||
);
|
||||
resolve();
|
||||
}, 3000);
|
||||
|
||||
const sub = obs.subscribe({
|
||||
next: (response) => {
|
||||
if (typeof response === "string") {
|
||||
// EOSE received
|
||||
clearTimeout(timeout);
|
||||
sub.unsubscribe();
|
||||
resolve();
|
||||
} else {
|
||||
// Event received
|
||||
events.push(response);
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
clearTimeout(timeout);
|
||||
console.error(`[NIP-29] Reply message fetch error:`, err);
|
||||
sub.unsubscribe();
|
||||
resolve();
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return events[0] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Join an existing group
|
||||
*/
|
||||
async joinConversation(conversation: Conversation): Promise<void> {
|
||||
const activePubkey = accountManager.active$.value?.pubkey;
|
||||
const activeSigner = accountManager.active$.value?.signer;
|
||||
|
||||
if (!activePubkey || !activeSigner) {
|
||||
throw new Error("No active account or signer");
|
||||
}
|
||||
|
||||
const groupId = conversation.metadata?.groupId;
|
||||
const relayUrl = conversation.metadata?.relayUrl;
|
||||
|
||||
if (!groupId || !relayUrl) {
|
||||
throw new Error("Group ID and relay URL required");
|
||||
}
|
||||
|
||||
// Create join request (kind 9021)
|
||||
const factory = new EventFactory();
|
||||
factory.setSigner(activeSigner);
|
||||
|
||||
const tags: string[][] = [
|
||||
["h", groupId],
|
||||
["relay", relayUrl],
|
||||
];
|
||||
|
||||
const draft = await factory.build({
|
||||
kind: 9021,
|
||||
content: "",
|
||||
tags,
|
||||
});
|
||||
const event = await factory.sign(draft);
|
||||
await publishEventToRelays(event, [relayUrl]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Leave a group
|
||||
*/
|
||||
async leaveConversation(conversation: Conversation): Promise<void> {
|
||||
const activePubkey = accountManager.active$.value?.pubkey;
|
||||
const activeSigner = accountManager.active$.value?.signer;
|
||||
|
||||
if (!activePubkey || !activeSigner) {
|
||||
throw new Error("No active account or signer");
|
||||
}
|
||||
|
||||
const groupId = conversation.metadata?.groupId;
|
||||
const relayUrl = conversation.metadata?.relayUrl;
|
||||
|
||||
if (!groupId || !relayUrl) {
|
||||
throw new Error("Group ID and relay URL required");
|
||||
}
|
||||
|
||||
// Create leave request (kind 9022)
|
||||
const factory = new EventFactory();
|
||||
factory.setSigner(activeSigner);
|
||||
|
||||
const tags: string[][] = [
|
||||
["h", groupId],
|
||||
["relay", relayUrl],
|
||||
];
|
||||
|
||||
const draft = await factory.build({
|
||||
kind: 9022,
|
||||
content: "",
|
||||
tags,
|
||||
});
|
||||
const event = await factory.sign(draft);
|
||||
await publishEventToRelays(event, [relayUrl]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Convert Nostr event to Message
|
||||
*/
|
||||
private eventToMessage(event: NostrEvent, conversationId: string): Message {
|
||||
// Look for reply q-tags (NIP-29 uses q-tags like NIP-C7)
|
||||
const qTags = getTagValues(event, "q");
|
||||
const replyTo = qTags[0]; // First q-tag is the reply target
|
||||
|
||||
return {
|
||||
id: event.id,
|
||||
conversationId,
|
||||
author: event.pubkey,
|
||||
content: event.content,
|
||||
timestamp: event.created_at,
|
||||
replyTo,
|
||||
protocol: "nip-29",
|
||||
metadata: {
|
||||
encrypted: false, // kind 9 messages are always public
|
||||
},
|
||||
event,
|
||||
};
|
||||
}
|
||||
}
|
||||
318
src/lib/chat/adapters/nip-c7-adapter.ts
Normal file
318
src/lib/chat/adapters/nip-c7-adapter.ts
Normal file
@@ -0,0 +1,318 @@
|
||||
import { Observable, firstValueFrom } from "rxjs";
|
||||
import { map, first } from "rxjs/operators";
|
||||
import { nip19 } from "nostr-tools";
|
||||
import type { Filter } from "nostr-tools";
|
||||
import { ChatProtocolAdapter } from "./base-adapter";
|
||||
import type {
|
||||
Conversation,
|
||||
Message,
|
||||
ProtocolIdentifier,
|
||||
ChatCapabilities,
|
||||
LoadMessagesOptions,
|
||||
} from "@/types/chat";
|
||||
import type { NostrEvent } from "@/types/nostr";
|
||||
import eventStore from "@/services/event-store";
|
||||
import pool from "@/services/relay-pool";
|
||||
import { publishEvent } from "@/services/hub";
|
||||
import accountManager from "@/services/accounts";
|
||||
import { isNip05, resolveNip05 } from "@/lib/nip05";
|
||||
import { getDisplayName } from "@/lib/nostr-utils";
|
||||
import { getTagValues } from "@/lib/nostr-utils";
|
||||
import { isValidHexPubkey } from "@/lib/nostr-validation";
|
||||
import { getProfileContent } from "applesauce-core/helpers";
|
||||
import { EventFactory } from "applesauce-core/event-factory";
|
||||
|
||||
/**
|
||||
* NIP-C7 Adapter - Simple Chat (Kind 9)
|
||||
*
|
||||
* Features:
|
||||
* - Direct messaging between users
|
||||
* - Quote-based threading (q-tag)
|
||||
* - No encryption
|
||||
* - Uses outbox relays
|
||||
*/
|
||||
export class NipC7Adapter extends ChatProtocolAdapter {
|
||||
readonly protocol = "nip-c7" as const;
|
||||
readonly type = "dm" as const;
|
||||
|
||||
/**
|
||||
* Parse identifier - accepts npub, nprofile, hex pubkey, or NIP-05
|
||||
*/
|
||||
parseIdentifier(input: string): ProtocolIdentifier | null {
|
||||
// Try bech32 decoding (npub/nprofile)
|
||||
try {
|
||||
const decoded = nip19.decode(input);
|
||||
if (decoded.type === "npub") {
|
||||
return {
|
||||
type: "chat-partner",
|
||||
value: decoded.data,
|
||||
};
|
||||
}
|
||||
if (decoded.type === "nprofile") {
|
||||
return {
|
||||
type: "chat-partner",
|
||||
value: decoded.data.pubkey,
|
||||
relays: decoded.data.relays,
|
||||
};
|
||||
}
|
||||
} catch {
|
||||
// Not bech32, try other formats
|
||||
}
|
||||
|
||||
// Try hex pubkey
|
||||
if (isValidHexPubkey(input)) {
|
||||
return {
|
||||
type: "chat-partner",
|
||||
value: input,
|
||||
};
|
||||
}
|
||||
|
||||
// Try NIP-05
|
||||
if (isNip05(input)) {
|
||||
return {
|
||||
type: "chat-partner-nip05",
|
||||
value: input,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve conversation from identifier
|
||||
*/
|
||||
async resolveConversation(
|
||||
identifier: ProtocolIdentifier,
|
||||
): Promise<Conversation> {
|
||||
let pubkey: string;
|
||||
|
||||
// Resolve NIP-05 if needed
|
||||
if (identifier.type === "chat-partner-nip05") {
|
||||
const resolved = await resolveNip05(identifier.value);
|
||||
if (!resolved) {
|
||||
throw new Error(`Failed to resolve NIP-05: ${identifier.value}`);
|
||||
}
|
||||
pubkey = resolved;
|
||||
} else {
|
||||
pubkey = identifier.value;
|
||||
}
|
||||
|
||||
const activePubkey = accountManager.active$.value?.pubkey;
|
||||
if (!activePubkey) {
|
||||
throw new Error("No active account");
|
||||
}
|
||||
|
||||
// Get display name for partner
|
||||
const metadataEvent = await this.getMetadata(pubkey);
|
||||
const metadata = metadataEvent
|
||||
? getProfileContent(metadataEvent)
|
||||
: undefined;
|
||||
const title = getDisplayName(pubkey, metadata);
|
||||
|
||||
return {
|
||||
id: `nip-c7:${pubkey}`,
|
||||
type: "dm",
|
||||
protocol: "nip-c7",
|
||||
title,
|
||||
participants: [
|
||||
{ pubkey: activePubkey, role: "member" },
|
||||
{ pubkey, role: "member" },
|
||||
],
|
||||
unreadCount: 0,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Load messages between active user and conversation partner
|
||||
*/
|
||||
loadMessages(
|
||||
conversation: Conversation,
|
||||
options?: LoadMessagesOptions,
|
||||
): Observable<Message[]> {
|
||||
const activePubkey = accountManager.active$.value?.pubkey;
|
||||
if (!activePubkey) {
|
||||
throw new Error("No active account");
|
||||
}
|
||||
|
||||
const partner = conversation.participants.find(
|
||||
(p) => p.pubkey !== activePubkey,
|
||||
);
|
||||
if (!partner) {
|
||||
throw new Error("No conversation partner found");
|
||||
}
|
||||
|
||||
// Subscribe to kind 9 messages between users
|
||||
const filter: Filter = {
|
||||
kinds: [9],
|
||||
authors: [activePubkey, partner.pubkey],
|
||||
"#p": [activePubkey, partner.pubkey],
|
||||
limit: options?.limit || 50,
|
||||
};
|
||||
|
||||
if (options?.before) {
|
||||
filter.until = options.before;
|
||||
}
|
||||
if (options?.after) {
|
||||
filter.since = options.after;
|
||||
}
|
||||
|
||||
return eventStore
|
||||
.timeline(filter)
|
||||
.pipe(
|
||||
map((events) =>
|
||||
events
|
||||
.map((event) => this.eventToMessage(event, conversation.id))
|
||||
.sort((a, b) => a.timestamp - b.timestamp),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load more historical messages (pagination)
|
||||
*/
|
||||
async loadMoreMessages(
|
||||
_conversation: Conversation,
|
||||
_before: number,
|
||||
): Promise<Message[]> {
|
||||
// For now, return empty - pagination to be implemented in Phase 6
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a message
|
||||
*/
|
||||
async sendMessage(
|
||||
conversation: Conversation,
|
||||
content: string,
|
||||
replyTo?: string,
|
||||
): Promise<void> {
|
||||
const activePubkey = accountManager.active$.value?.pubkey;
|
||||
const activeSigner = accountManager.active$.value?.signer;
|
||||
|
||||
if (!activePubkey || !activeSigner) {
|
||||
throw new Error("No active account or signer");
|
||||
}
|
||||
|
||||
const partner = conversation.participants.find(
|
||||
(p) => p.pubkey !== activePubkey,
|
||||
);
|
||||
if (!partner) {
|
||||
throw new Error("No conversation partner found");
|
||||
}
|
||||
|
||||
// Create event factory and sign event
|
||||
const factory = new EventFactory();
|
||||
factory.setSigner(activeSigner);
|
||||
|
||||
const tags: string[][] = [["p", partner.pubkey]];
|
||||
if (replyTo) {
|
||||
tags.push(["q", replyTo]); // NIP-C7 quote tag for threading
|
||||
}
|
||||
|
||||
const draft = await factory.build({ kind: 9, content, tags });
|
||||
const event = await factory.sign(draft);
|
||||
await publishEvent(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get protocol capabilities
|
||||
*/
|
||||
getCapabilities(): ChatCapabilities {
|
||||
return {
|
||||
supportsEncryption: false,
|
||||
supportsThreading: true, // q-tag quotes
|
||||
supportsModeration: false,
|
||||
supportsRoles: false,
|
||||
supportsGroupManagement: false,
|
||||
canCreateConversations: true,
|
||||
requiresRelay: false,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Load a replied-to message
|
||||
* First checks EventStore, then fetches from relays if needed
|
||||
*/
|
||||
async loadReplyMessage(
|
||||
_conversation: Conversation,
|
||||
eventId: string,
|
||||
): Promise<NostrEvent | null> {
|
||||
// First check EventStore - might already be loaded
|
||||
const cachedEvent = await eventStore
|
||||
.event(eventId)
|
||||
.pipe(first())
|
||||
.toPromise();
|
||||
if (cachedEvent) {
|
||||
return cachedEvent;
|
||||
}
|
||||
|
||||
// Not in store, fetch from relay pool
|
||||
console.log(`[NIP-C7] Fetching reply message ${eventId.slice(0, 8)}...`);
|
||||
|
||||
const filter: Filter = {
|
||||
ids: [eventId],
|
||||
limit: 1,
|
||||
};
|
||||
|
||||
const events: NostrEvent[] = [];
|
||||
const obs = pool.subscription([], [filter], { eventStore }); // Empty relay list = use global pool
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
const timeout = setTimeout(() => {
|
||||
console.log(
|
||||
`[NIP-C7] Reply message fetch timeout for ${eventId.slice(0, 8)}...`,
|
||||
);
|
||||
resolve();
|
||||
}, 3000);
|
||||
|
||||
const sub = obs.subscribe({
|
||||
next: (response) => {
|
||||
if (typeof response === "string") {
|
||||
// EOSE received
|
||||
clearTimeout(timeout);
|
||||
sub.unsubscribe();
|
||||
resolve();
|
||||
} else {
|
||||
// Event received
|
||||
events.push(response);
|
||||
}
|
||||
},
|
||||
error: (err) => {
|
||||
clearTimeout(timeout);
|
||||
console.error(`[NIP-C7] Reply message fetch error:`, err);
|
||||
sub.unsubscribe();
|
||||
resolve();
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
return events[0] || null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Convert Nostr event to Message
|
||||
*/
|
||||
private eventToMessage(event: NostrEvent, conversationId: string): Message {
|
||||
const quotedEventIds = getTagValues(event, "q");
|
||||
|
||||
return {
|
||||
id: event.id,
|
||||
conversationId,
|
||||
author: event.pubkey,
|
||||
content: event.content,
|
||||
timestamp: event.created_at,
|
||||
replyTo: quotedEventIds[0], // First q tag
|
||||
protocol: "nip-c7",
|
||||
event,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper: Get user metadata
|
||||
*/
|
||||
private async getMetadata(pubkey: string): Promise<NostrEvent | undefined> {
|
||||
return firstValueFrom(eventStore.replaceable(0, pubkey), {
|
||||
defaultValue: undefined,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -55,3 +55,21 @@ export const hub = new ActionRunner(eventStore, factory, publishEvent);
|
||||
accountManager.active$.subscribe((account) => {
|
||||
factory.setSigner(account?.signer || undefined);
|
||||
});
|
||||
|
||||
export async function publishEventToRelays(
|
||||
event: NostrEvent,
|
||||
relays: string[],
|
||||
): Promise<void> {
|
||||
// If no relays, throw error
|
||||
if (relays.length === 0) {
|
||||
throw new Error(
|
||||
"No relays found for publishing. Please configure relay list (kind 10002) or ensure event has relay hints.",
|
||||
);
|
||||
}
|
||||
|
||||
// Publish to relay pool
|
||||
await pool.publish(relays, event);
|
||||
|
||||
// Add to EventStore for immediate local availability
|
||||
eventStore.add(event);
|
||||
}
|
||||
|
||||
@@ -10,3 +10,21 @@ import "fake-indexeddb/auto";
|
||||
// Polyfill WebSocket - required by nostr-tools relay code
|
||||
import { WebSocket } from "ws";
|
||||
globalThis.WebSocket = WebSocket as unknown as typeof globalThis.WebSocket;
|
||||
|
||||
// Polyfill localStorage - required by state management and accounts
|
||||
const localStorageMock = (() => {
|
||||
let store: Record<string, string> = {};
|
||||
return {
|
||||
getItem: (key: string) => store[key] || null,
|
||||
setItem: (key: string, value: string) => {
|
||||
store[key] = value.toString();
|
||||
},
|
||||
removeItem: (key: string) => {
|
||||
delete store[key];
|
||||
},
|
||||
clear: () => {
|
||||
store = {};
|
||||
},
|
||||
};
|
||||
})();
|
||||
globalThis.localStorage = localStorageMock as Storage;
|
||||
|
||||
@@ -16,6 +16,7 @@ export type AppId =
|
||||
| "relay"
|
||||
| "debug"
|
||||
| "conn"
|
||||
| "chat"
|
||||
| "spells"
|
||||
| "spellbooks"
|
||||
| "win";
|
||||
|
||||
143
src/types/chat.ts
Normal file
143
src/types/chat.ts
Normal file
@@ -0,0 +1,143 @@
|
||||
import type { NostrEvent } from "./nostr";
|
||||
|
||||
/**
|
||||
* Chat protocol identifier
|
||||
*/
|
||||
export type ChatProtocol = "nip-c7" | "nip-17" | "nip-28" | "nip-29" | "nip-53";
|
||||
|
||||
/**
|
||||
* Conversation type
|
||||
*/
|
||||
export type ConversationType = "dm" | "channel" | "group" | "live-chat";
|
||||
|
||||
/**
|
||||
* Participant role in a conversation
|
||||
*/
|
||||
export type ParticipantRole = "admin" | "moderator" | "member" | "host";
|
||||
|
||||
/**
|
||||
* Participant in a conversation
|
||||
*/
|
||||
export interface Participant {
|
||||
pubkey: string;
|
||||
role?: ParticipantRole;
|
||||
permissions?: string[];
|
||||
}
|
||||
|
||||
/**
|
||||
* Protocol-specific conversation metadata
|
||||
*/
|
||||
export interface ConversationMetadata {
|
||||
// NIP-28 channel
|
||||
channelEvent?: NostrEvent; // kind 40 creation event
|
||||
|
||||
// NIP-29 group
|
||||
groupId?: string; // host'group-id format
|
||||
relayUrl?: string; // Relay enforcing group rules
|
||||
description?: string; // Group description
|
||||
icon?: string; // Group icon/picture URL
|
||||
|
||||
// NIP-53 live chat
|
||||
activityAddress?: {
|
||||
kind: number;
|
||||
pubkey: string;
|
||||
identifier: string;
|
||||
};
|
||||
|
||||
// NIP-17 DM
|
||||
encrypted?: boolean;
|
||||
giftWrapped?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic conversation abstraction
|
||||
* Works across all messaging protocols
|
||||
*/
|
||||
export interface Conversation {
|
||||
id: string; // Protocol-specific identifier
|
||||
type: ConversationType;
|
||||
protocol: ChatProtocol;
|
||||
title: string;
|
||||
participants: Participant[];
|
||||
metadata?: ConversationMetadata;
|
||||
lastMessage?: Message;
|
||||
unreadCount: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* Message metadata (reactions, zaps, encryption status, etc.)
|
||||
*/
|
||||
export interface MessageMetadata {
|
||||
encrypted?: boolean;
|
||||
reactions?: NostrEvent[];
|
||||
zaps?: NostrEvent[];
|
||||
deleted?: boolean;
|
||||
hidden?: boolean; // NIP-28 channel hide
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic message abstraction
|
||||
* Works across all messaging protocols
|
||||
*/
|
||||
export interface Message {
|
||||
id: string;
|
||||
conversationId: string;
|
||||
author: string; // pubkey
|
||||
content: string;
|
||||
timestamp: number;
|
||||
replyTo?: string; // Parent message ID
|
||||
metadata?: MessageMetadata;
|
||||
protocol: ChatProtocol;
|
||||
event: NostrEvent; // Original Nostr event for verification
|
||||
}
|
||||
|
||||
/**
|
||||
* Protocol-specific identifier
|
||||
* Returned by adapter parseIdentifier()
|
||||
*/
|
||||
export interface ProtocolIdentifier {
|
||||
type: string; // e.g., 'dm-recipient', 'channel-event', 'group-id'
|
||||
value: any; // Protocol-specific value
|
||||
relays?: string[]; // Relay hints from bech32 encoding
|
||||
}
|
||||
|
||||
/**
|
||||
* Chat command parsing result
|
||||
*/
|
||||
export interface ChatCommandResult {
|
||||
protocol: ChatProtocol;
|
||||
identifier: ProtocolIdentifier;
|
||||
adapter: any; // Will be ChatProtocolAdapter but avoiding circular dependency
|
||||
}
|
||||
|
||||
/**
|
||||
* Message loading options
|
||||
*/
|
||||
export interface LoadMessagesOptions {
|
||||
limit?: number;
|
||||
before?: number; // Unix timestamp
|
||||
after?: number; // Unix timestamp
|
||||
}
|
||||
|
||||
/**
|
||||
* Conversation creation parameters
|
||||
*/
|
||||
export interface CreateConversationParams {
|
||||
type: ConversationType;
|
||||
title?: string;
|
||||
participants: string[]; // pubkeys
|
||||
metadata?: Record<string, any>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Chat capabilities - what features a protocol supports
|
||||
*/
|
||||
export interface ChatCapabilities {
|
||||
supportsEncryption: boolean;
|
||||
supportsThreading: boolean;
|
||||
supportsModeration: boolean;
|
||||
supportsRoles: boolean;
|
||||
supportsGroupManagement: boolean;
|
||||
canCreateConversations: boolean;
|
||||
requiresRelay: boolean;
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import { parseOpenCommand } from "@/lib/open-parser";
|
||||
import { parseProfileCommand } from "@/lib/profile-parser";
|
||||
import { parseRelayCommand } from "@/lib/relay-parser";
|
||||
import { resolveNip05Batch } from "@/lib/nip05";
|
||||
import { parseChatCommand } from "@/lib/chat-parser";
|
||||
|
||||
export interface ManPageEntry {
|
||||
name: string;
|
||||
@@ -344,6 +345,35 @@ export const manPages: Record<string, ManPageEntry> = {
|
||||
return parsed;
|
||||
},
|
||||
},
|
||||
chat: {
|
||||
name: "chat",
|
||||
section: "1",
|
||||
synopsis: "chat <group-identifier>",
|
||||
description:
|
||||
"Join and participate in NIP-29 relay-based group chats. Groups are hosted on a single relay that enforces membership and moderation rules. Use the format 'relay'group-id' where relay is the WebSocket URL (wss:// prefix optional) and group-id is the group identifier.",
|
||||
options: [
|
||||
{
|
||||
flag: "<group-identifier>",
|
||||
description:
|
||||
"NIP-29 group identifier in format: relay'group-id (wss:// prefix optional)",
|
||||
},
|
||||
],
|
||||
examples: [
|
||||
"chat relay.example.com'bitcoin-dev Join relay group (wss:// prefix optional)",
|
||||
"chat wss://relay.example.com'nostr-dev Join relay group with explicit protocol",
|
||||
"chat nos.lol'welcome Join welcome group on nos.lol",
|
||||
],
|
||||
seeAlso: ["profile", "open", "req"],
|
||||
appId: "chat",
|
||||
category: "Nostr",
|
||||
argParser: async (args: string[]) => {
|
||||
const result = parseChatCommand(args);
|
||||
return {
|
||||
protocol: result.protocol,
|
||||
identifier: result.identifier,
|
||||
};
|
||||
},
|
||||
},
|
||||
profile: {
|
||||
name: "profile",
|
||||
section: "1",
|
||||
|
||||
Reference in New Issue
Block a user