Skip to content

Commit

Permalink
Merge pull request #124 from tyiu/nip-10-backwards-compatibility
Browse files Browse the repository at this point in the history
Add write support for NIP-10 deprecated positional tags in text notes to maximize backwards compatibility
  • Loading branch information
PavleKreator authored Apr 15, 2024
2 parents 0094357 + 5d99f2b commit dbefe05
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions Primal/Common/Nostr/NostrObject+Extra.swift
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,27 @@ fileprivate func createNostrRepostEvent(_ nostrContent: NostrContent) -> NostrOb
}

fileprivate func createNostrReplyEvent(_ content: String, post: PrimalFeedPost, mentionedPubkeys: [String]) -> NostrObject? {
let e = ["e", post.id, RelayHintManager.instance.getRelayHint(post.id), "reply"]
let p = ["p", post.pubkey]

var root = ["e", post.id, RelayHintManager.instance.getRelayHint(post.id), "root"]
for tag in post.tags {
if tag.last == "root" {
root = tag
}
var allTags: [[String]] = []

/// The `e` tags are ordered at best effort to support the deprecated method of positional tags to maximize backwards compatibility
/// with clients that support replies but have not been updated to understand tag markers.
///
/// https://github.com/nostr-protocol/nips/blob/master/10.md
///
/// The tag to the root of the reply chain goes first.
/// The tag to the reply event being responded to goes last.
if let root = post.tags.last(where: { tag in tag[safe: 3] == "root" }) {
allTags.append(root)
allTags.append(["e", post.id, RelayHintManager.instance.getRelayHint(post.id), "reply"])
} else {
// For top level replies (those replying directly to the root event), only the "root" marker should be used.
allTags.append(["e", post.id, RelayHintManager.instance.getRelayHint(post.id), "root"])
}

let allTags = [e, p, root] + mentionedPubkeys.map { ["p", $0, RelayHintManager.instance.getRelayHint($0), "mention"] }


allTags.append(["p", post.pubkey])

allTags += mentionedPubkeys.map { ["p", $0] }

return createNostrObject(content: content, kind: 1, tags: allTags)
}

Expand Down

0 comments on commit dbefe05

Please sign in to comment.