ReportView.swift (5293B)
1 // 2 // ReportView.swift 3 // damus 4 // 5 // Created by William Casarin on 2023-01-25. 6 // 7 8 import SwiftUI 9 10 fileprivate extension ReportTarget { 11 func reportTags(type: ReportType) -> [[String]] { 12 switch self { 13 case .user(let pubkey): 14 return [["p", pubkey.hex(), type.rawValue]] 15 case .note(let notet): 16 return [["e", notet.note_id.hex(), type.rawValue], 17 ["p", notet.pubkey.hex()]] 18 } 19 } 20 } 21 22 struct ReportView: View { 23 let postbox: PostBox 24 let target: ReportTarget 25 let keypair: FullKeypair 26 27 @State var report_sent: Bool = false 28 @State var report_id: String = "" 29 @State var report_message: String = "" 30 @State var selected_report_type: ReportType? 31 32 var body: some View { 33 if report_sent { 34 Success 35 } else { 36 MainForm 37 } 38 } 39 40 var Success: some View { 41 VStack(alignment: .center, spacing: 20) { 42 Text("Report sent!", comment: "Message indicating that a report was successfully sent to relay servers.") 43 .font(.headline) 44 45 Text("Relays have been notified and clients will be able to use this information to filter content. Thank you!", comment: "Description of what was done as a result of sending a report to relay servers.") 46 47 Text("Report ID:", comment: "Label indicating that the text underneath is the identifier of the report that was sent to relay servers.") 48 49 Text(report_id) 50 51 Button(NSLocalizedString("Copy Report ID", comment: "Button to copy report ID.")) { 52 UIPasteboard.general.string = report_id 53 let g = UIImpactFeedbackGenerator(style: .medium) 54 g.impactOccurred() 55 } 56 } 57 .padding() 58 } 59 60 func do_send_report() { 61 guard let selected_report_type, 62 let ev = NostrEvent(content: report_message, keypair: keypair.to_keypair(), kind: 1984, tags: target.reportTags(type: selected_report_type)) else { 63 return 64 } 65 66 postbox.send(ev) 67 68 report_sent = true 69 report_id = bech32_note_id(ev.id) 70 } 71 72 var send_report_button_text: String { 73 switch target { 74 case .note: 75 return NSLocalizedString("Report Note", comment: "Button to report a note.") 76 case .user: 77 return NSLocalizedString("Report User", comment: "Button to report a user.") 78 } 79 } 80 81 var MainForm: some View { 82 VStack { 83 84 Text("Report", comment: "Label indicating that the current view is for the user to report content.") 85 .font(.headline) 86 .padding() 87 88 Form { 89 Section(content: { 90 Picker("", selection: $selected_report_type) { 91 ForEach(ReportType.allCases, id: \.self) { report_type in 92 // Impersonation type is not supported when reporting notes. 93 switch target { 94 case .note: 95 if report_type != .impersonation { 96 Text(verbatim: String(describing: report_type)) 97 .tag(Optional(report_type)) 98 } 99 case .user: 100 Text(verbatim: String(describing: report_type)) 101 .tag(Optional(report_type)) 102 } 103 } 104 } 105 .labelsHidden() 106 .pickerStyle(.inline) 107 }, header: { 108 Text("What do you want to report?", comment: "Header text to prompt user what issue they want to report.") 109 }) 110 111 Section(content: { 112 TextField(NSLocalizedString("Optional", comment: "Prompt to enter optional additional information when reporting an account or content."), text: $report_message, axis: .vertical) 113 }, header: { 114 Text("Additional information", comment: "Header text to prompt user to optionally provide additional information when reporting a user or note.") 115 }) 116 117 Section(content: { 118 Button(send_report_button_text) { 119 do_send_report() 120 } 121 .disabled(selected_report_type == nil) 122 }, footer: { 123 Text("Your report will be sent to the relays you are connected to", comment: "Footer text to inform user what will happen when the report is submitted.") 124 }) 125 } 126 } 127 } 128 } 129 130 struct ReportView_Previews: PreviewProvider { 131 static var previews: some View { 132 let ds = test_damus_state 133 VStack { 134 135 ReportView(postbox: ds.postbox, target: ReportTarget.user(test_pubkey), keypair: test_keypair.to_full()!) 136 137 ReportView(postbox: ds.postbox, target: ReportTarget.user(test_pubkey), keypair: test_keypair.to_full()!, report_sent: true, report_id: "report_id") 138 139 } 140 } 141 }