Simplify and inline Report event logic.

Closes: https://github.com/damus-io/damus/pull/1498
Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
Grimless
2023-08-22 00:00:46 -04:00
committed by William Casarin
parent 8b600a9774
commit c5d8e4a4a1
3 changed files with 16 additions and 38 deletions

View File

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

View File

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

View File

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