|
1 | 1 | import { computed, type Ref, ref, type UnwrapRef } from 'vue' |
2 | 2 | import { useRequestURL, useRoute, useRuntimeConfig } from '#imports' |
3 | 3 | import useNuxstr from './useNuxstr' |
4 | | -import { NDKEvent, type NDKFilter, NDKKind, type NDKSignedEvent, type NDKSubscription } from '@nostr-dev-kit/ndk' |
5 | | -import type { Comment } from '~/src/runtime/types' |
| 4 | +import { useNostr } from './useNostr' |
| 5 | +import type { Event, Filter } from 'nostr-tools' |
| 6 | + |
| 7 | +import type { Comment } from '../types' |
6 | 8 |
|
7 | 9 | function useComments(customContentId?: string) { |
8 | | - const { ndk, connect, isLoggedIn, mapComment, pubkey, fetchProfile } = useNuxstr() |
| 10 | + const { isLoggedIn, pubkey, fetchProfile } = useNuxstr() |
| 11 | + const { subscribe } = useNostr() |
9 | 12 | const route = useRoute() |
10 | 13 |
|
11 | 14 | const config = useRuntimeConfig() |
@@ -34,56 +37,65 @@ function useComments(customContentId?: string) { |
34 | 37 | } |
35 | 38 |
|
36 | 39 | function siteUrl(): string { |
37 | | - const url: URL = useRequestURL() |
38 | | - return `${url.protocol}//${url.host}` |
| 40 | + if (import.meta.server) { |
| 41 | + const url: URL = useRequestURL() |
| 42 | + return `${url.protocol}//${url.host}` |
| 43 | + } |
| 44 | + return window.location.origin |
39 | 45 | } |
40 | 46 |
|
41 | 47 | function fullUrl(path: string): string { |
42 | 48 | return `${siteUrl()}${path}` |
43 | 49 | } |
44 | 50 |
|
45 | 51 | async function subscribeComments() { |
46 | | - await connect() |
47 | | - const filter: NDKFilter = { |
48 | | - kinds: [NDKKind.GenericReply], |
| 52 | + const filter: Filter = { |
| 53 | + kinds: [1111], // NDKKind.GenericReply is 22 |
49 | 54 | ['#t']: [tagValue()], |
50 | 55 | limit: 100, |
51 | | - ['#k']: ['web'], |
52 | | - ['#A']: [fullUrl(contentId.value)], |
53 | 56 | } |
54 | | - const sub: NDKSubscription = ndk.subscribe(filter) |
55 | | - sub.on('event', async (event: NDKSignedEvent) => { |
56 | | - const comment: Comment = mapComment(event) |
| 57 | + |
| 58 | + subscribe(filter, async (event: Event) => { |
| 59 | + if (commentsData.value.some(c => c.id === event.id)) return |
| 60 | + const comment: Comment = { |
| 61 | + id: event.id, |
| 62 | + pubkey: event.pubkey, |
| 63 | + created_at: event.created_at, |
| 64 | + content: event.content, |
| 65 | + profile: undefined, |
| 66 | + } |
57 | 67 | comment.profile = await fetchProfile(event.pubkey) |
58 | 68 | commentsData.value.push(comment) |
59 | 69 | }) |
60 | 70 | } |
61 | 71 |
|
62 | 72 | async function postComment(comment: string) { |
63 | | - await connect() |
64 | | - const ndkEvent: NDKEvent = await createCommentEvent(comment) |
65 | | - return await ndkEvent.publish().then(() => true).catch((err: unknown) => { |
| 73 | + const { publish } = useNostr() |
| 74 | + try { |
| 75 | + const event = await createCommentEvent(comment) |
| 76 | + const signedEvent = await window.nostr.signEvent(event) |
| 77 | + await publish(signedEvent) |
| 78 | + return true |
| 79 | + } |
| 80 | + catch (err: unknown) { |
66 | 81 | error.value = (err as Error)?.message || String(err) |
67 | 82 | return false |
68 | | - }) |
| 83 | + } |
69 | 84 | } |
70 | 85 |
|
71 | 86 | /// Create a new comment event as defined in NIP 22 |
72 | | - async function createCommentEvent(comment: string): Promise<NDKEvent> { |
73 | | - const event: NDKEvent = new NDKEvent(ndk) |
74 | | - event.kind = NDKKind.GenericReply |
75 | | - event.content = comment |
76 | | - event.tags = [ |
77 | | - ['A', fullUrl(contentId.value)], |
78 | | - ['a', fullUrl(contentId.value)], |
79 | | - ['I', fullUrl(contentId.value)], // |
80 | | - ['i', fullUrl(contentId.value)], |
81 | | - ['t', tagValue()], |
82 | | - ['k', 'web'], // Defined NIP 73 |
83 | | - ['K', 'web'], // Defined NIP 73, |
84 | | - ['p', pubkey ?? ''], |
85 | | - ] |
86 | | - return event |
| 87 | + async function createCommentEvent(comment: string) { |
| 88 | + return { |
| 89 | + kind: 1111, // GenericReply |
| 90 | + created_at: Math.floor(Date.now() / 1000), |
| 91 | + content: comment, |
| 92 | + tags: [ |
| 93 | + ['A', fullUrl(contentId.value)], |
| 94 | + ['t', tagValue()], |
| 95 | + ['k', 'web'], |
| 96 | + ['p', pubkey ?? ''], |
| 97 | + ], |
| 98 | + } |
87 | 99 | } |
88 | 100 |
|
89 | 101 | return { loading, error, comments, isLoggedIn, subscribeComments, postComment } |
|
0 commit comments