-
Notifications
You must be signed in to change notification settings - Fork 290
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds highlights (NIP-84) to Damus. Kind 9802 are handled by all the necessary models. We show highlighted events, longform events, and url references. Url references also leverage text fragments to take the user to the highlighted text. Testing —— iPhone 15 Pro Max (17.0) Dark Mode: https://v.nostr.build/oM6DW.mp4 iPhone 15 Pro Max (17.0) Light Mode: https://v.nostr.build/BRrmP.mp4 iPhone SE (3rd generation) (16.4) Light Mode: https://v.nostr.build/6GzKa.mp4 —— Closes: #2172 Closes: #1772 Closes: #1773 Closes: #2173 Closes: #2175 Changelog-Added: Highlights (NIP-84) PATCH CHANGELOG: V1 -> V2: addressed review comments highlights are now truncated and highlight label shown in Thread view V2 -> V3: handle case where highlight context is smaller than the highlight content Signed-off-by: ericholguin <[email protected]>
- Loading branch information
1 parent
23c3130
commit 6376c61
Showing
18 changed files
with
573 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
damus/Assets.xcassets/Colors/DamusHighlight.colorset/Contents.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"colors" : [ | ||
{ | ||
"color" : { | ||
"color-space" : "srgb", | ||
"components" : { | ||
"alpha" : "1.000", | ||
"blue" : "0xF2", | ||
"green" : "0xD8", | ||
"red" : "0xF4" | ||
} | ||
}, | ||
"idiom" : "universal" | ||
}, | ||
{ | ||
"appearances" : [ | ||
{ | ||
"appearance" : "luminosity", | ||
"value" : "dark" | ||
} | ||
], | ||
"color" : { | ||
"color-space" : "srgb", | ||
"components" : { | ||
"alpha" : "1.000", | ||
"blue" : "0x45", | ||
"green" : "0x17", | ||
"red" : "0x47" | ||
} | ||
}, | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// HighlightEvent.swift | ||
// damus | ||
// | ||
// Created by eric on 4/22/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct HighlightEvent { | ||
let event: NostrEvent | ||
|
||
var event_ref: String? = nil | ||
var url_ref: URL? = nil | ||
var context: String? = nil | ||
|
||
static func parse(from ev: NostrEvent) -> HighlightEvent { | ||
var highlight = HighlightEvent(event: ev) | ||
|
||
for tag in ev.tags { | ||
guard tag.count >= 2 else { continue } | ||
switch tag[0].string() { | ||
case "e": highlight.event_ref = tag[1].string() | ||
case "a": highlight.event_ref = tag[1].string() | ||
case "r": highlight.url_ref = URL(string: tag[1].string()) | ||
case "context": highlight.context = tag[1].string() | ||
default: | ||
break | ||
} | ||
} | ||
|
||
return highlight | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// HighlightDescription.swift | ||
// damus | ||
// | ||
// Created by eric on 4/28/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// Modified from Reply Description | ||
struct HighlightDescription: View { | ||
let event: NostrEvent | ||
let highlighted_event: NostrEvent? | ||
let ndb: Ndb | ||
|
||
var body: some View { | ||
(Text(Image(systemName: "highlighter")) + Text(verbatim: " \(highlight_desc(ndb: ndb, event: event, highlighted_event: highlighted_event))")) | ||
.font(.footnote) | ||
.foregroundColor(.gray) | ||
.frame(maxWidth: .infinity, alignment: .leading) | ||
|
||
} | ||
} | ||
|
||
struct HighlightDescription_Previews: PreviewProvider { | ||
static var previews: some View { | ||
HighlightDescription(event: test_note, highlighted_event: test_note, ndb: test_damus_state.ndb) | ||
} | ||
} | ||
|
||
func highlight_desc(ndb: Ndb, event: NostrEvent, highlighted_event: NostrEvent?, locale: Locale = Locale.current) -> String { | ||
let desc = make_reply_description(event, replying_to: highlighted_event) | ||
let pubkeys = desc.pubkeys | ||
|
||
let bundle = bundleForLocale(locale: locale) | ||
|
||
if desc.pubkeys.count == 0 { | ||
return NSLocalizedString("Highlighted", bundle: bundle, comment: "Label to indicate that the user is highlighting their own post.") | ||
} | ||
|
||
guard let profile_txn = NdbTxn(ndb: ndb) else { | ||
return "" | ||
} | ||
|
||
let names: [String] = pubkeys.map { pk in | ||
let prof = ndb.lookup_profile_with_txn(pk, txn: profile_txn) | ||
|
||
return Profile.displayName(profile: prof?.profile, pubkey: pk).username.truncate(maxLength: 50) | ||
} | ||
|
||
let uniqueNames = NSOrderedSet(array: names).array as! [String] | ||
|
||
return String(format: NSLocalizedString("Highlighted %@", bundle: bundle, comment: "Label to indicate that the user is highlighting 1 user."), locale: locale, uniqueNames[0]) | ||
} |
Oops, something went wrong.