reply: ensure the person you're replying to is the first entry in the reply description

Suggested-by: Tanel
Changelog-Fixed: Ensure the person you're replying to is the first entry in the reply description
This commit is contained in:
William Casarin
2023-08-06 15:37:32 -07:00
parent e642913944
commit 25e022d933
5 changed files with 40 additions and 14 deletions

View File

@@ -12,16 +12,24 @@ struct ReplyDesc {
let others: Int let others: Int
} }
func make_reply_description(_ tags: Tags) -> ReplyDesc { func make_reply_description(_ event: NostrEvent, replying_to: NostrEvent?) -> ReplyDesc {
var c = 0 var c = 0
var ns: [Pubkey] = [] var ns: [Pubkey] = []
var i = tags.count var i = event.tags.count
for tag in tags { if let replying_to {
ns.append(replying_to.pubkey)
}
for tag in event.tags {
if let pk = Pubkey.from_tag(tag: tag) { if let pk = Pubkey.from_tag(tag: tag) {
c += 1 c += 1
if ns.count < 2 { if ns.count < 2 {
ns.append(pk) if let replying_to, pk == replying_to.pubkey {
continue
} else {
ns.append(pk)
}
} }
} }
i -= 1 i -= 1

View File

@@ -10,10 +10,11 @@ import SwiftUI
// jb55 - TODO: this could be a lot better // jb55 - TODO: this could be a lot better
struct ReplyDescription: View { struct ReplyDescription: View {
let event: NostrEvent let event: NostrEvent
let replying_to: NostrEvent?
let profiles: Profiles let profiles: Profiles
var body: some View { var body: some View {
Text(verbatim: "\(reply_desc(profiles: profiles, event: event))") Text(verbatim: "\(reply_desc(profiles: profiles, event: event, replying_to: replying_to))")
.font(.footnote) .font(.footnote)
.foregroundColor(.gray) .foregroundColor(.gray)
.frame(maxWidth: .infinity, alignment: .leading) .frame(maxWidth: .infinity, alignment: .leading)
@@ -22,12 +23,12 @@ struct ReplyDescription: View {
struct ReplyDescription_Previews: PreviewProvider { struct ReplyDescription_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ReplyDescription(event: test_note, profiles: test_damus_state().profiles) ReplyDescription(event: test_note, replying_to: test_note, profiles: test_damus_state().profiles)
} }
} }
func reply_desc(profiles: Profiles, event: NostrEvent, locale: Locale = Locale.current) -> String { func reply_desc(profiles: Profiles, event: NostrEvent, replying_to: NostrEvent?, locale: Locale = Locale.current) -> String {
let desc = make_reply_description(event.tags) let desc = make_reply_description(event, replying_to: replying_to)
let pubkeys = desc.pubkeys let pubkeys = desc.pubkeys
let n = desc.others let n = desc.others

View File

@@ -8,14 +8,23 @@
import SwiftUI import SwiftUI
struct ReplyPart: View { struct ReplyPart: View {
let events: EventCache
let event: NostrEvent let event: NostrEvent
let privkey: Privkey? let privkey: Privkey?
let profiles: Profiles let profiles: Profiles
var replying_to: NostrEvent? {
guard let note_ref = event.event_refs(privkey).first(where: { evref in evref.is_direct_reply != nil })?.is_direct_reply else {
return nil
}
return events.lookup(note_ref.note_id)
}
var body: some View { var body: some View {
Group { Group {
if event_is_reply(event.event_refs(privkey)) { if event_is_reply(event.event_refs(privkey)) {
ReplyDescription(event: event, profiles: profiles) ReplyDescription(event: event, replying_to: replying_to, profiles: profiles)
} else { } else {
EmptyView() EmptyView()
} }
@@ -25,6 +34,6 @@ struct ReplyPart: View {
struct ReplyPart_Previews: PreviewProvider { struct ReplyPart_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ReplyPart(event: test_note, privkey: nil, profiles: test_damus_state().profiles) ReplyPart(events: test_damus_state().events, event: test_note, privkey: nil, profiles: test_damus_state().profiles)
} }
} }

View File

@@ -69,7 +69,7 @@ struct EventShell<Content: View>: View {
EventTop(state: state, event: event, pubkey: pubkey, is_anon: is_anon) EventTop(state: state, event: event, pubkey: pubkey, is_anon: is_anon)
if !options.contains(.no_replying_to) { if !options.contains(.no_replying_to) {
ReplyPart(event: event, privkey: state.keypair.privkey, profiles: state.profiles) ReplyPart(events: state.events, event: event, privkey: state.keypair.privkey, profiles: state.profiles)
} }
content content
@@ -95,7 +95,7 @@ struct EventShell<Content: View>: View {
VStack { VStack {
EventTop(state: state, event: event, pubkey: pubkey, is_anon: is_anon) EventTop(state: state, event: event, pubkey: pubkey, is_anon: is_anon)
ReplyPart(event: event, privkey: state.keypair.privkey, profiles: state.profiles) ReplyPart(events: state.events, event: event, privkey: state.keypair.privkey, profiles: state.profiles)
} }
} }
.padding(.horizontal) .padding(.horizontal)

View File

@@ -18,6 +18,14 @@ struct SelectedEventView: View {
@StateObject var bar: ActionBarModel @StateObject var bar: ActionBarModel
var replying_to: NostrEvent? {
guard let note_ref = event.event_refs(damus.keypair.privkey).first(where: { evref in evref.is_direct_reply != nil })?.is_direct_reply else {
return nil
}
return damus.events.lookup(note_ref.note_id)
}
init(damus: DamusState, event: NostrEvent, size: EventViewKind) { init(damus: DamusState, event: NostrEvent, size: EventViewKind) {
self.damus = damus self.damus = damus
self.event = event self.event = event
@@ -43,7 +51,7 @@ struct SelectedEventView: View {
.lineLimit(1) .lineLimit(1)
if event_is_reply(event.event_refs(damus.keypair.privkey)) { if event_is_reply(event.event_refs(damus.keypair.privkey)) {
ReplyDescription(event: event, profiles: damus.profiles) ReplyDescription(event: event, replying_to: replying_to, profiles: damus.profiles)
.padding(.horizontal) .padding(.horizontal)
} }