fix: clear content when reposting protected events

This commit is contained in:
codytseng
2025-02-16 20:33:33 +08:00
parent c4665fdc04
commit f00ea38e17
2 changed files with 46 additions and 20 deletions

View File

@@ -1,7 +1,8 @@
import { tagNameEquals } from '@/lib/tag'
import { useMuteList } from '@/providers/MuteListProvider' import { useMuteList } from '@/providers/MuteListProvider'
import client from '@/services/client.service' import client from '@/services/client.service'
import { Event, kinds, verifyEvent } from 'nostr-tools' import { Event, kinds, nip19, verifyEvent } from 'nostr-tools'
import { useMemo } from 'react' import { useEffect, useState } from 'react'
import GenericNoteCard from './GenericNoteCard' import GenericNoteCard from './GenericNoteCard'
export default function RepostNoteCard({ export default function RepostNoteCard({
@@ -14,25 +15,47 @@ export default function RepostNoteCard({
filterMutedNotes?: boolean filterMutedNotes?: boolean
}) { }) {
const { mutePubkeys } = useMuteList() const { mutePubkeys } = useMuteList()
const targetEvent = useMemo(() => { const [targetEvent, setTargetEvent] = useState<Event | null>(null)
try { useEffect(() => {
const targetEvent = event.content ? (JSON.parse(event.content) as Event) : null const fetch = async () => {
if (!targetEvent || !verifyEvent(targetEvent) || targetEvent.kind === kinds.Repost) { try {
return null const eventFromContent = event.content ? (JSON.parse(event.content) as Event) : null
} if (eventFromContent && verifyEvent(eventFromContent)) {
client.addEventToCache(targetEvent) if (eventFromContent.kind === kinds.Repost) {
const targetSeenOn = client.getSeenEventRelays(targetEvent.id) return
if (targetSeenOn.length === 0) { }
const seenOn = client.getSeenEventRelays(event.id) client.addEventToCache(eventFromContent)
seenOn.forEach((relay) => { const targetSeenOn = client.getSeenEventRelays(eventFromContent.id)
client.trackEventSeenOn(targetEvent.id, relay) if (targetSeenOn.length === 0) {
const seenOn = client.getSeenEventRelays(event.id)
seenOn.forEach((relay) => {
client.trackEventSeenOn(eventFromContent.id, relay)
})
}
setTargetEvent(eventFromContent)
return
}
const [, id, relay, , pubkey] = event.tags.find(tagNameEquals('e')) ?? []
if (!id) {
return
}
const targetEventId = nip19.neventEncode({
id,
relays: relay ? [relay] : [],
author: pubkey
}) })
const targetEvent = await client.fetchEvent(targetEventId)
if (targetEvent) {
setTargetEvent(targetEvent)
}
} catch {
// ignore
} }
return targetEvent
} catch {
return null
} }
fetch()
}, [event]) }, [event])
if (!targetEvent) return null if (!targetEvent) return null
if (filterMutedNotes && mutePubkeys.includes(targetEvent.pubkey)) { if (filterMutedNotes && mutePubkeys.includes(targetEvent.pubkey)) {
return null return null

View File

@@ -9,6 +9,7 @@ import {
extractImagesFromContent, extractImagesFromContent,
extractMentions, extractMentions,
getEventCoordinate, getEventCoordinate,
isProtectedEvent,
isReplaceable isReplaceable
} from './event' } from './event'
@@ -35,14 +36,15 @@ export function createReactionDraftEvent(event: Event): TDraftEvent {
// https://github.com/nostr-protocol/nips/blob/master/18.md // https://github.com/nostr-protocol/nips/blob/master/18.md
export function createRepostDraftEvent(event: Event): TDraftEvent { export function createRepostDraftEvent(event: Event): TDraftEvent {
const isProtected = isProtectedEvent(event)
const tags = [ const tags = [
['e', event.id, client.getEventHint(event.id), event.pubkey], ['e', event.id, client.getEventHint(event.id), 'mentions', event.pubkey],
['p', event.pubkey] ['p', event.pubkey]
] ]
return { return {
kind: kinds.Repost, kind: kinds.Repost,
content: JSON.stringify(event), content: isProtected ? '' : JSON.stringify(event),
tags, tags,
created_at: dayjs().unix() created_at: dayjs().unix()
} }
@@ -63,7 +65,6 @@ export async function createShortTextNoteDraftEvent(
const tags = pubkeys const tags = pubkeys
.map((pubkey) => ['p', pubkey]) .map((pubkey) => ['p', pubkey])
.concat(otherRelatedEventIds.map((eventId) => ['e', eventId, client.getEventHint(eventId)]))
.concat(quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)])) .concat(quoteEventIds.map((eventId) => ['q', eventId, client.getEventHint(eventId)]))
.concat(hashtags.map((hashtag) => ['t', hashtag])) .concat(hashtags.map((hashtag) => ['t', hashtag]))
@@ -71,6 +72,8 @@ export async function createShortTextNoteDraftEvent(
tags.push(['e', rootEventId, client.getEventHint(rootEventId), 'root']) tags.push(['e', rootEventId, client.getEventHint(rootEventId), 'root'])
} }
tags.push(...otherRelatedEventIds.map((eventId) => ['e', eventId, client.getEventHint(eventId)]))
if (parentEventId) { if (parentEventId) {
tags.push(['e', parentEventId, client.getEventHint(parentEventId), 'reply']) tags.push(['e', parentEventId, client.getEventHint(parentEventId), 'reply'])
} }