reply_p_tags: Use uniq instead of Set to preserve order

This commit is contained in:
William Casarin
2023-04-17 14:28:24 -07:00
parent 15849e290e
commit 4821ba61a5

View File

@@ -720,11 +720,26 @@ func make_zap_request_event(keypair: FullKeypair, content: String, relays: [Rela
return ev
}
func uniq<T: Hashable>(_ xs: [T]) -> [T] {
var s = Set<T>()
var ys: [T] = []
for x in xs {
if s.contains(x) {
continue
}
s.insert(x)
ys.append(x)
}
return ys
}
func gather_reply_ids(our_pubkey: String, from: NostrEvent) -> [ReferencedId] {
var ids = get_referenced_ids(tags: from.tags, key: "e").first.map { [$0] } ?? []
ids.append(ReferencedId(ref_id: from.id, relay_id: nil, key: "e"))
ids.append(contentsOf: Set(from.referenced_pubkeys.filter { $0.ref_id != our_pubkey }))
ids.append(contentsOf: uniq(from.referenced_pubkeys.filter { $0.ref_id != our_pubkey }))
if from.pubkey != our_pubkey {
ids.append(ReferencedId(ref_id: from.pubkey, relay_id: nil, key: "p"))
}