fix: 🐛

This commit is contained in:
codytseng
2025-08-07 00:24:03 +08:00
parent 83052a2ba8
commit 2f4f4fffcf
5 changed files with 75 additions and 13 deletions

View File

@@ -12,7 +12,7 @@ import {
import dayjs from 'dayjs'
import { Event, kinds, nip19 } from 'nostr-tools'
import {
getReplaceableEventCoordinate,
getReplaceableCoordinateFromEvent,
getRootETag,
isProtectedEvent,
isReplaceableEvent
@@ -552,7 +552,7 @@ function extractImagesFromContent(content: string) {
}
function buildATag(event: Event, upperCase: boolean = false) {
const coordinate = getReplaceableEventCoordinate(event)
const coordinate = getReplaceableCoordinateFromEvent(event)
const hint = client.getEventHint(event.id)
return trimTagEnd([upperCase ? 'A' : 'a', coordinate, hint])
}

View File

@@ -149,9 +149,13 @@ export function getRootBech32Id(event?: Event) {
return generateBech32IdFromETag(eTag)
}
export function getReplaceableEventCoordinate(event: Event) {
export function getReplaceableCoordinate(kind: number, pubkey: string, d: string = '') {
return `${kind}:${pubkey}:${d}`
}
export function getReplaceableCoordinateFromEvent(event: Event) {
const d = event.tags.find(tagNameEquals('d'))?.[1]
return `${event.kind}:${event.pubkey}:${d ?? ''}`
return getReplaceableCoordinate(event.kind, event.pubkey, d)
}
export function getNoteBech32Id(event: Event) {
@@ -242,3 +246,26 @@ export function createFakeEvent(event: Partial<Event>): Event {
...event
}
}
// Legacy compare function for sorting compatibility
// If return 0, it means the two events are equal.
// If return a negative number, it means `b` should be retained, and `a` should be discarded.
// If return a positive number, it means `a` should be retained, and `b` should be discarded.
export function compareEvents(a: Event, b: Event): number {
if (a.created_at !== b.created_at) {
return a.created_at - b.created_at
}
// In case of replaceable events with the same timestamp, the event with the lowest id (first in lexical order) should be retained, and the other discarded.
if (a.id !== b.id) {
return a.id < b.id ? 1 : -1
}
return 0
}
// Returns the event that should be retained when comparing two events
export function getRetainedEvent(a: Event, b: Event): Event {
if (compareEvents(a, b) > 0) {
return a
}
return b
}