feat: improve the relays used for sending replies
This commit is contained in:
@@ -1,10 +1,11 @@
|
|||||||
import { Button } from '@/components/ui/button'
|
import { Button } from '@/components/ui/button'
|
||||||
import { useToast } from '@/hooks/use-toast'
|
import { useToast } from '@/hooks/use-toast'
|
||||||
import { createCommentDraftEvent, createShortTextNoteDraftEvent } from '@/lib/draft-event'
|
import { createCommentDraftEvent, createShortTextNoteDraftEvent } from '@/lib/draft-event'
|
||||||
|
import { getRootEventTag } from '@/lib/event.ts'
|
||||||
import { useNostr } from '@/providers/NostrProvider'
|
import { useNostr } from '@/providers/NostrProvider'
|
||||||
import client from '@/services/client.service'
|
import client from '@/services/client.service'
|
||||||
import { ChevronDown, ImageUp, LoaderCircle } from 'lucide-react'
|
import { ChevronDown, ImageUp, LoaderCircle } from 'lucide-react'
|
||||||
import { Event, kinds } from 'nostr-tools'
|
import { Event, kinds, nip19 } from 'nostr-tools'
|
||||||
import { useState } from 'react'
|
import { useState } from 'react'
|
||||||
import { useTranslation } from 'react-i18next'
|
import { useTranslation } from 'react-i18next'
|
||||||
import TextareaWithMentions from '../TextareaWithMentions.tsx'
|
import TextareaWithMentions from '../TextareaWithMentions.tsx'
|
||||||
@@ -46,9 +47,35 @@ export default function NormalPostContent({
|
|||||||
setPosting(true)
|
setPosting(true)
|
||||||
try {
|
try {
|
||||||
const additionalRelayUrls: string[] = []
|
const additionalRelayUrls: string[] = []
|
||||||
if (parentEvent) {
|
if (parentEvent && !specifiedRelayUrls) {
|
||||||
const relayList = await client.fetchRelayList(parentEvent.pubkey)
|
const relayList = await client.fetchRelayList(parentEvent.pubkey)
|
||||||
additionalRelayUrls.push(...relayList.read.slice(0, 5))
|
additionalRelayUrls.push(...relayList.read.slice(0, 3))
|
||||||
|
const rootEventTag = getRootEventTag(parentEvent)
|
||||||
|
if (rootEventTag) {
|
||||||
|
const [, rootEventId, rootEventRelay, , rootAuthor] = rootEventTag
|
||||||
|
if (rootAuthor) {
|
||||||
|
if (rootAuthor !== parentEvent.pubkey) {
|
||||||
|
const rootAuthorRelayList = await client.fetchRelayList(rootAuthor)
|
||||||
|
additionalRelayUrls.push(...rootAuthorRelayList.read.slice(0, 3))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
|
const rootEvent = await client.fetchEvent(
|
||||||
|
nip19.neventEncode(
|
||||||
|
rootEventRelay
|
||||||
|
? { id: rootEventId }
|
||||||
|
: { id: rootEventId, relays: [rootEventRelay] }
|
||||||
|
)
|
||||||
|
)
|
||||||
|
if (rootEvent && rootEvent.pubkey !== parentEvent.pubkey) {
|
||||||
|
const rootAuthorRelayList = await client.fetchRelayList(rootEvent.pubkey)
|
||||||
|
additionalRelayUrls.push(...rootAuthorRelayList.read.slice(0, 3))
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
// ignore
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const draftEvent =
|
const draftEvent =
|
||||||
parentEvent && parentEvent.kind !== kinds.ShortTextNote
|
parentEvent && parentEvent.kind !== kinds.ShortTextNote
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ export function getParentEventId(event?: Event) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getRootEventId(event?: Event) {
|
export function getRootEventTag(event?: Event) {
|
||||||
if (!event) return undefined
|
if (!event) return undefined
|
||||||
let tag = event.tags.find(isRootETag)
|
let tag = event.tags.find(isRootETag)
|
||||||
if (!tag) {
|
if (!tag) {
|
||||||
@@ -74,6 +74,11 @@ export function getRootEventId(event?: Event) {
|
|||||||
([tagName, tagValue]) => tagName === 'e' && !embeddedEventIds.includes(tagValue)
|
([tagName, tagValue]) => tagName === 'e' && !embeddedEventIds.includes(tagValue)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
return tag
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getRootEventId(event?: Event) {
|
||||||
|
const tag = getRootEventTag(event)
|
||||||
if (!tag) return undefined
|
if (!tag) return undefined
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@@ -355,15 +355,16 @@ export function NostrProvider({ children }: { children: React.ReactNode }) {
|
|||||||
}: { additionalRelayUrls?: string[]; specifiedRelayUrls?: string[] } = {}
|
}: { additionalRelayUrls?: string[]; specifiedRelayUrls?: string[] } = {}
|
||||||
) => {
|
) => {
|
||||||
const event = await signEvent(draftEvent)
|
const event = await signEvent(draftEvent)
|
||||||
await client.publishEvent(
|
const relays = specifiedRelayUrls?.length
|
||||||
specifiedRelayUrls?.length
|
? specifiedRelayUrls
|
||||||
? specifiedRelayUrls
|
: (relayList?.write.slice(0, 5) ?? [])
|
||||||
: (relayList?.write ?? [])
|
.concat(additionalRelayUrls ?? [])
|
||||||
.concat(additionalRelayUrls ?? [])
|
.concat(client.getCurrentRelayUrls())
|
||||||
.concat(client.getCurrentRelayUrls())
|
if (!relays.length) {
|
||||||
.concat(BIG_RELAY_URLS),
|
relays.push(...BIG_RELAY_URLS)
|
||||||
event
|
}
|
||||||
)
|
|
||||||
|
await client.publishEvent(relays, event)
|
||||||
return event
|
return event
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -108,8 +108,9 @@ class ClientService extends EventTarget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async publishEvent(relayUrls: string[], event: NEvent) {
|
async publishEvent(relayUrls: string[], event: NEvent) {
|
||||||
|
const uniqueRelayUrls = Array.from(new Set(relayUrls))
|
||||||
const result = await Promise.any(
|
const result = await Promise.any(
|
||||||
relayUrls.map(async (url) => {
|
uniqueRelayUrls.map(async (url) => {
|
||||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||||
const that = this
|
const that = this
|
||||||
const relay = await this.pool.ensureRelay(url)
|
const relay = await this.pool.ensureRelay(url)
|
||||||
|
|||||||
Reference in New Issue
Block a user