damus

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

commit c5d8e4a4a14e71113002e04f60f7ceb1a0550945
parent 8b600a977400519ad7f01788f3bb36b66788e122
Author: Grimless <kyle@kyleroucis.com>
Date:   Tue, 22 Aug 2023 00:00:46 -0400

Simplify and inline Report event logic.

Closes: https://github.com/damus-io/damus/pull/1498
Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
Mdamus/Models/Report.swift | 26--------------------------
Mdamus/Views/Events/EventMenu.swift | 3+--
Mdamus/Views/ReportView.swift | 25+++++++++++++++----------
3 files changed, 16 insertions(+), 38 deletions(-)

diff --git a/damus/Models/Report.swift b/damus/Models/Report.swift @@ -38,31 +38,5 @@ struct ReportNoteTarget { enum ReportTarget { case user(Pubkey) case note(ReportNoteTarget) - - static func note(pubkey: Pubkey, note_id: NoteId) -> ReportTarget { - return .note(ReportNoteTarget(pubkey: pubkey, note_id: note_id)) - } -} - -struct Report { - let type: ReportType - let target: ReportTarget - let message: String - } -func create_report_tags(target: ReportTarget, type: ReportType) -> [[String]] { - switch target { - case .user(let pubkey): - return [["p", pubkey.hex(), type.rawValue]] - case .note(let notet): - return [["e", notet.note_id.hex(), type.rawValue], - ["p", notet.pubkey.hex()]] - } -} - -func create_report_event(keypair: FullKeypair, report: Report) -> NostrEvent? { - let kind: UInt32 = 1984 - let tags = create_report_tags(target: report.target, type: report.type) - return NostrEvent(content: report.message, keypair: keypair.to_keypair(), kind: kind, tags: tags) -} diff --git a/damus/Views/Events/EventMenu.swift b/damus/Views/Events/EventMenu.swift @@ -66,7 +66,6 @@ struct MenuItems: View { } var body: some View { - Group { Button { UIPasteboard.general.string = event.get_content(keypair.privkey) @@ -126,7 +125,7 @@ struct MenuItems: View { // Only allow reporting if logged in with private key and the currently viewed profile is not the logged in profile. if keypair.pubkey != target_pubkey && keypair.privkey != nil { Button(role: .destructive) { - notify(.report(.note(pubkey: target_pubkey, note_id: event.id))) + notify(.report(.note(ReportNoteTarget(pubkey: target_pubkey, note_id: event.id)))) } label: { Label(NSLocalizedString("Report", comment: "Context menu option for reporting content."), image: "raising-hand") } diff --git a/damus/Views/ReportView.swift b/damus/Views/ReportView.swift @@ -7,6 +7,18 @@ import SwiftUI +fileprivate extension ReportTarget { + func reportTags(type: ReportType) -> [[String]] { + switch self { + case .user(let pubkey): + return [["p", pubkey.hex(), type.rawValue]] + case .note(let notet): + return [["e", notet.note_id.hex(), type.rawValue], + ["p", notet.pubkey.hex()]] + } + } +} + struct ReportView: View { let postbox: PostBox let target: ReportTarget @@ -47,10 +59,12 @@ struct ReportView: View { func do_send_report() { guard let selected_report_type, - let ev = send_report(keypair: keypair, postbox: postbox, target: target, type: selected_report_type, message: report_message) else { + let ev = NostrEvent(content: report_message, keypair: keypair.to_keypair(), kind: 1984, tags: target.reportTags(type: selected_report_type)) else { return } + postbox.send(ev) + report_sent = true report_id = bech32_note_id(ev.id) } @@ -113,15 +127,6 @@ struct ReportView: View { } } -func send_report(keypair: FullKeypair, postbox: PostBox, target: ReportTarget, type: ReportType, message: String) -> NostrEvent? { - let report = Report(type: type, target: target, message: message) - guard let ev = create_report_event(keypair: keypair, report: report) else { - return nil - } - postbox.send(ev) - return ev -} - struct ReportView_Previews: PreviewProvider { static var previews: some View { let ds = test_damus_state()