mirror of
https://gitlab.com/soapbox-pub/mkstack.git
synced 2026-12-03 08:07:21 +00:00
Support NIP-73 hashtag identifiers
This commit is contained in:
5 files changed
+65
-60
No files matched your search
@@ -16,7 +16,7 @@ import { formatDistanceToNow } from 'date-fns';
|
||||
import { genUserName } from '@/lib/genUserName';
|
||||
|
||||
interface CommentProps {
|
||||
root: NostrEvent | URL;
|
||||
root: NostrEvent | URL | `#${string}`;
|
||||
comment: NostrEvent;
|
||||
depth?: number;
|
||||
maxDepth?: number;
|
||||
|
||||
@@ -9,8 +9,8 @@ import { NostrEvent } from '@nostrify/nostrify';
|
||||
import { MessageSquare, Send } from 'lucide-react';
|
||||
|
||||
interface CommentFormProps {
|
||||
root: NostrEvent | URL;
|
||||
reply?: NostrEvent | URL;
|
||||
root: NostrEvent | URL | `#${string}`;
|
||||
reply?: NostrEvent | URL | `#${string}`;
|
||||
onSuccess?: () => void;
|
||||
placeholder?: string;
|
||||
compact?: boolean;
|
||||
|
||||
@@ -8,7 +8,7 @@ import { CommentForm } from './CommentForm';
|
||||
import { Comment } from './Comment';
|
||||
|
||||
interface CommentsSectionProps {
|
||||
root: NostrEvent | URL;
|
||||
root: NostrEvent | URL | `#${string}`;
|
||||
title?: string;
|
||||
emptyStateMessage?: string;
|
||||
emptyStateSubtitle?: string;
|
||||
|
||||
@@ -2,15 +2,17 @@ import { NKinds, NostrEvent, NostrFilter } from '@nostrify/nostrify';
|
||||
import { useNostr } from '@nostrify/react';
|
||||
import { useQuery } from '@tanstack/react-query';
|
||||
|
||||
export function useComments(root: NostrEvent | URL, limit?: number) {
|
||||
export function useComments(root: NostrEvent | URL | `#${string}`, limit?: number) {
|
||||
const { nostr } = useNostr();
|
||||
|
||||
return useQuery({
|
||||
queryKey: ['nostr', 'comments', root instanceof URL ? root.toString() : root.id, limit],
|
||||
queryKey: ['nostr', 'comments', root instanceof URL ? root.toString() : typeof root === 'string' ? root : root.id, limit],
|
||||
queryFn: async () => {
|
||||
const filter: NostrFilter = { kinds: [1111] };
|
||||
|
||||
if (root instanceof URL) {
|
||||
if (typeof root === 'string') {
|
||||
filter['#I'] = [root];
|
||||
} else if (root instanceof URL) {
|
||||
filter['#I'] = [root.toString()];
|
||||
} else if (NKinds.addressable(root.kind)) {
|
||||
const d = root.tags.find(([name]) => name === 'd')?.[1] ?? '';
|
||||
@@ -37,7 +39,9 @@ export function useComments(root: NostrEvent | URL, limit?: number) {
|
||||
|
||||
// Filter top-level comments (those with lowercase tag matching the root)
|
||||
const topLevelComments = events.filter(comment => {
|
||||
if (root instanceof URL) {
|
||||
if (typeof root === 'string') {
|
||||
return getTagValue(comment, 'i') === root;
|
||||
} else if (root instanceof URL) {
|
||||
return getTagValue(comment, 'i') === root.toString();
|
||||
} else if (NKinds.addressable(root.kind)) {
|
||||
const d = getTagValue(root, 'd') ?? '';
|
||||
|
||||
+53
-52
@@ -3,8 +3,8 @@ import { useNostrPublish } from '@/hooks/useNostrPublish';
|
||||
import { NKinds, type NostrEvent } from '@nostrify/nostrify';
|
||||
|
||||
interface PostCommentParams {
|
||||
root: NostrEvent | URL; // The root event to comment on
|
||||
reply?: NostrEvent | URL; // Optional reply to another comment
|
||||
root: NostrEvent | URL | `#${string}`; // The root event to comment on
|
||||
reply?: NostrEvent | URL | `#${string}`; // Optional reply to another comment
|
||||
content: string;
|
||||
}
|
||||
|
||||
@@ -17,61 +17,15 @@ export function usePostComment() {
|
||||
mutationFn: async ({ root, reply, content }: PostCommentParams) => {
|
||||
const tags: string[][] = [];
|
||||
|
||||
// d-tag identifiers
|
||||
const dRoot = root instanceof URL ? '' : root.tags.find(([name]) => name === 'd')?.[1] ?? '';
|
||||
const dReply = reply instanceof URL ? '' : reply?.tags.find(([name]) => name === 'd')?.[1] ?? '';
|
||||
|
||||
// Root event tags
|
||||
if (root instanceof URL) {
|
||||
tags.push(['I', root.toString()]);
|
||||
} else if (NKinds.addressable(root.kind)) {
|
||||
tags.push(['A', `${root.kind}:${root.pubkey}:${dRoot}`]);
|
||||
} else if (NKinds.replaceable(root.kind)) {
|
||||
tags.push(['A', `${root.kind}:${root.pubkey}:`]);
|
||||
} else {
|
||||
tags.push(['E', root.id]);
|
||||
}
|
||||
if (root instanceof URL) {
|
||||
tags.push(['K', root.hostname]);
|
||||
} else {
|
||||
tags.push(['K', root.kind.toString()]);
|
||||
tags.push(['P', root.pubkey]);
|
||||
}
|
||||
tags.push(...makeCommentTags('root', root));
|
||||
|
||||
// Reply event tags
|
||||
if (reply) {
|
||||
if (reply instanceof URL) {
|
||||
tags.push(['i', reply.toString()]);
|
||||
} else if (NKinds.addressable(reply.kind)) {
|
||||
tags.push(['a', `${reply.kind}:${reply.pubkey}:${dReply}`]);
|
||||
} else if (NKinds.replaceable(reply.kind)) {
|
||||
tags.push(['a', `${reply.kind}:${reply.pubkey}:`]);
|
||||
} else {
|
||||
tags.push(['e', reply.id]);
|
||||
}
|
||||
if (reply instanceof URL) {
|
||||
tags.push(['k', reply.hostname]);
|
||||
} else {
|
||||
tags.push(['k', reply.kind.toString()]);
|
||||
tags.push(['p', reply.pubkey]);
|
||||
}
|
||||
tags.push(...makeCommentTags('reply', reply));
|
||||
} else {
|
||||
// If this is a top-level comment, use the root event's tags
|
||||
if (root instanceof URL) {
|
||||
tags.push(['i', root.toString()]);
|
||||
} else if (NKinds.addressable(root.kind)) {
|
||||
tags.push(['a', `${root.kind}:${root.pubkey}:${dRoot}`]);
|
||||
} else if (NKinds.replaceable(root.kind)) {
|
||||
tags.push(['a', `${root.kind}:${root.pubkey}:`]);
|
||||
} else {
|
||||
tags.push(['e', root.id]);
|
||||
}
|
||||
if (root instanceof URL) {
|
||||
tags.push(['k', root.hostname]);
|
||||
} else {
|
||||
tags.push(['k', root.kind.toString()]);
|
||||
tags.push(['p', root.pubkey]);
|
||||
}
|
||||
tags.push(...makeCommentTags('reply', root));
|
||||
}
|
||||
|
||||
const event = await publishEvent({
|
||||
@@ -83,10 +37,57 @@ export function usePostComment() {
|
||||
return event;
|
||||
},
|
||||
onSuccess: (_, { root }) => {
|
||||
const rootKey = root instanceof URL ? root.toString() : typeof root === 'string' ? root : root.id;
|
||||
|
||||
// Invalidate and refetch comments
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['nostr', 'comments', root instanceof URL ? root.toString() : root.id]
|
||||
queryKey: ['nostr', 'comments', rootKey]
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Build NIP-22 comment tags for a given scope and target */
|
||||
function makeCommentTags(scope: 'root' | 'reply', target: NostrEvent | URL | `#${string}`): string[][] {
|
||||
const tags: string[][] = [];
|
||||
|
||||
const d = (typeof target === 'string' || target instanceof URL)
|
||||
? ''
|
||||
: target.tags.find(([name]) => name === 'd')?.[1] ?? '';
|
||||
|
||||
if (typeof target === 'string') {
|
||||
tags.push(['I', target]);
|
||||
} else if (target instanceof URL) {
|
||||
tags.push(['I', target.toString()]);
|
||||
} else if (NKinds.addressable(target.kind)) {
|
||||
tags.push(['A', `${target.kind}:${target.pubkey}:${d}`]);
|
||||
} else if (NKinds.replaceable(target.kind)) {
|
||||
tags.push(['A', `${target.kind}:${target.pubkey}:`]);
|
||||
} else {
|
||||
tags.push(['E', target.id]);
|
||||
}
|
||||
if (typeof target === 'string') {
|
||||
tags.push(['K', '#']);
|
||||
} else if (target instanceof URL) {
|
||||
switch (target.protocol) {
|
||||
case 'http:':
|
||||
case 'https:':
|
||||
tags.push(['K', 'web']);
|
||||
break;
|
||||
default:
|
||||
tags.push(['K', target.protocol.replace(/:$/, '')]);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
tags.push(['K', target.kind.toString()]);
|
||||
tags.push(['P', target.pubkey]);
|
||||
}
|
||||
|
||||
// Lowercase all tag names for reply scope
|
||||
if (scope === 'reply') {
|
||||
return tags.map(([name, ...values]) => [name.toLowerCase(), ...values]);
|
||||
}
|
||||
|
||||
// Root scope: uppercase tags
|
||||
return tags;
|
||||
}
|
||||
Reference in new issue
Block a user