1
0
mirror of git://jb55.com/damus synced 2024-10-06 11:43:21 +00:00

Fix reaction notification title to be consistent with ReactionView

Changelog-Fixed: Fix reaction notification title to be consistent with ReactionView
Closes: #1137
This commit is contained in:
Terry Yiu 2023-05-16 10:54:04 -04:00 committed by William Casarin
parent 812213ff2b
commit 1769b08147
4 changed files with 48 additions and 7 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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 {

View File

@ -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), "🤙")
}
}