damus

nostr ios client
git clone git://jb55.com/damus
Log | Files | Refs | README | LICENSE

commit 1769b081474287396a14580f2522a785b180c644
parent 812213ff2bb3bb7aafaa68964218775ce34bded4
Author: Terry Yiu <963907+tyiu@users.noreply.github.com>
Date:   Tue, 16 May 2023 10:54:04 -0400

Fix reaction notification title to be consistent with ReactionView

Changelog-Fixed: Fix reaction notification title to be consistent with ReactionView
Closes: #1137

Diffstat:
Mdamus/Models/HomeModel.swift | 2+-
Mdamus/Nostr/NostrEvent.swift | 26++++++++++++++++++++++++--
Mdamus/Views/Reactions/ReactionView.swift | 5+----
MdamusTests/LikeTests.swift | 22++++++++++++++++++++++
4 files changed, 48 insertions(+), 7 deletions(-)

diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -1202,7 +1202,7 @@ func create_local_notification(profiles: Profiles, notify: LocalNotification) { title = String(format: NSLocalizedString("Reposted by %@", comment: "Reposted by heading in local notification"), displayName) identifier = "myBoostNotification" case .like: - title = String(format: NSLocalizedString("%@ reacted with %@", comment: "Reacted by heading in local notification"), displayName, notify.event.content) + title = String(format: NSLocalizedString("%@ reacted with %@", comment: "Reacted by heading in local notification"), displayName, to_reaction_emoji(ev: notify.event) ?? "") identifier = "myLikeNotification" case .dm: title = displayName diff --git a/damus/Nostr/NostrEvent.swift b/damus/Nostr/NostrEvent.swift @@ -492,11 +492,11 @@ func make_boost_event(pubkey: String, privkey: String, boosted: NostrEvent) -> N return ev } -func make_like_event(pubkey: String, privkey: String, liked: NostrEvent) -> NostrEvent { +func make_like_event(pubkey: String, privkey: String, liked: NostrEvent, content: String = "🤙") -> NostrEvent { var tags: [[String]] = liked.tags.filter { tag in tag.count >= 2 && (tag[0] == "e" || tag[0] == "p") } tags.append(["e", liked.id]) tags.append(["p", liked.pubkey]) - let ev = NostrEvent(content: "🤙", pubkey: pubkey, kind: 7, tags: tags) + let ev = NostrEvent(content: content, pubkey: pubkey, kind: 7, tags: tags) ev.calculate_id() ev.sign(privkey: privkey) @@ -966,6 +966,28 @@ func first_eref_mention(ev: NostrEvent, privkey: String?) -> Mention? { return nil } +/** + Transforms a `NostrEvent` of known kind `NostrKind.like`to a human-readable emoji. + If the known kind is not a `NostrKind.like`, it will return `nil`. + If the event content is an empty string or `+`, it will map that to a heart ❤️ emoji. + If the event content is a "-", it will map that to a dislike 👎 emoji. + Otherwise, it will return the event content at face value without transforming it. + */ +func to_reaction_emoji(ev: NostrEvent) -> String? { + guard ev.known_kind == NostrKind.like else { + return nil + } + + switch ev.content { + case "", "+": + return "❤️" + case "-": + return "👎" + default: + return ev.content + } +} + extension [ReferencedId] { var pRefs: [ReferencedId] { get { diff --git a/damus/Views/Reactions/ReactionView.swift b/damus/Views/Reactions/ReactionView.swift @@ -12,10 +12,7 @@ struct ReactionView: View { let reaction: NostrEvent var content: String { - if reaction.content == "" || reaction.content == "+" { - return "❤️" - } - return reaction.content + return to_reaction_emoji(ev: reaction) ?? "" } var body: some View { diff --git a/damusTests/LikeTests.swift b/damusTests/LikeTests.swift @@ -32,4 +32,26 @@ class LikeTests: XCTestCase { XCTAssertEqual(like_ev.last_refid()!.ref_id, id) } + func testToReactionEmoji() { + let privkey = "0fc2092231f958f8d57d66f5e238bb45b6a2571f44c0ce024bbc6f3a9c8a15fe" + let pubkey = "30c6d1dc7f7c156794fa15055e651b758a61b99f50fcf759de59386050bf6ae2" + let liked = NostrEvent(content: "awesome #[0] post", pubkey: "orig_pk", tags: [["p", "cindy"], ["e", "bob"]]) + liked.calculate_id() + let id = liked.id + + let emptyReaction = make_like_event(pubkey: pubkey, privkey: privkey, liked: liked, content: "") + let plusReaction = make_like_event(pubkey: pubkey, privkey: privkey, liked: liked, content: "+") + let minusReaction = make_like_event(pubkey: pubkey, privkey: privkey, liked: liked, content: "-") + let heartReaction = make_like_event(pubkey: pubkey, privkey: privkey, liked: liked, content: "❤️") + let thumbsUpReaction = make_like_event(pubkey: pubkey, privkey: privkey, liked: liked, content: "👍") + let shakaReaction = make_like_event(pubkey: pubkey, privkey: privkey, liked: liked, content: "🤙") + + XCTAssertEqual(to_reaction_emoji(ev: emptyReaction), "❤️") + XCTAssertEqual(to_reaction_emoji(ev: plusReaction), "❤️") + XCTAssertEqual(to_reaction_emoji(ev: minusReaction), "👎") + XCTAssertEqual(to_reaction_emoji(ev: heartReaction), "❤️") + XCTAssertEqual(to_reaction_emoji(ev: thumbsUpReaction), "👍") + XCTAssertEqual(to_reaction_emoji(ev: shakaReaction), "🤙") + } + }