From 3e02cc6889190f73711d41f3d18e76ada2db90fb Mon Sep 17 00:00:00 2001 From: Terry Yiu <963907+tyiu@users.noreply.github.com> Date: Thu, 26 Jan 2023 23:34:21 -0500 Subject: [PATCH] Prevent blocking or reporting yourself --- damus/Views/ChatroomView.swift | 2 +- damus/Views/DMChatView.swift | 2 +- damus/Views/EventView.swift | 6 ++-- damus/Views/Events/EmbeddedEventView.swift | 2 +- damus/Views/Events/EventMenu.swift | 36 ++++++++++++---------- damus/Views/Events/SelectedEventView.swift | 2 +- damus/Views/ProfileView.swift | 19 +++++++----- 7 files changed, 37 insertions(+), 32 deletions(-) diff --git a/damus/Views/ChatroomView.swift b/damus/Views/ChatroomView.swift index 0ce67276..ed38f7a0 100644 --- a/damus/Views/ChatroomView.swift +++ b/damus/Views/ChatroomView.swift @@ -24,7 +24,7 @@ struct ChatroomView: View { next_ev: ind == count-1 ? nil : thread.events[ind+1], damus_state: damus ) - .event_context_menu(ev, pubkey: ev.pubkey, privkey: damus.keypair.privkey) + .event_context_menu(ev, keypair: damus.keypair) .onTapGesture { if thread.initial_event.id == ev.id { //dismiss() diff --git a/damus/Views/DMChatView.swift b/damus/Views/DMChatView.swift index 0c2f6ba6..f963ced6 100644 --- a/damus/Views/DMChatView.swift +++ b/damus/Views/DMChatView.swift @@ -19,7 +19,7 @@ struct DMChatView: View { VStack(alignment: .leading) { ForEach(Array(zip(dms.events, dms.events.indices)), id: \.0.id) { (ev, ind) in DMView(event: dms.events[ind], damus_state: damus_state) - .event_context_menu(ev, pubkey: ev.pubkey, privkey: damus_state.keypair.privkey) + .event_context_menu(ev, keypair: damus_state.keypair) } EndBlock(height: 80) } diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift index a3051895..fab2e1df 100644 --- a/damus/Views/EventView.swift +++ b/damus/Views/EventView.swift @@ -129,7 +129,7 @@ struct EventView: View { .id(event.id) .frame(maxWidth: .infinity, minHeight: PFP_SIZE) .padding([.bottom], 2) - .event_context_menu(event, pubkey: pubkey, privkey: damus.keypair.privkey) + .event_context_menu(event, keypair: damus.keypair) } } @@ -171,9 +171,9 @@ extension View { } } - func event_context_menu(_ event: NostrEvent, pubkey: String, privkey: String?) -> some View { + func event_context_menu(_ event: NostrEvent, keypair: Keypair) -> some View { return self.contextMenu { - EventMenuContext(event: event, privkey: privkey, pubkey: pubkey) + EventMenuContext(event: event, keypair: keypair) } } diff --git a/damus/Views/Events/EmbeddedEventView.swift b/damus/Views/Events/EmbeddedEventView.swift index beaa6ee6..4d753687 100644 --- a/damus/Views/Events/EmbeddedEventView.swift +++ b/damus/Views/Events/EmbeddedEventView.swift @@ -23,7 +23,7 @@ struct EmbeddedEventView: View { EventBody(damus_state: damus_state, event: event, size: .small) } - .event_context_menu(event, pubkey: pubkey, privkey: damus_state.keypair.privkey) + .event_context_menu(event, keypair: damus_state.keypair) } } diff --git a/damus/Views/Events/EventMenu.swift b/damus/Views/Events/EventMenu.swift index dc8ec18e..de0f8da5 100644 --- a/damus/Views/Events/EventMenu.swift +++ b/damus/Views/Events/EventMenu.swift @@ -9,19 +9,18 @@ import SwiftUI struct EventMenuContext: View { let event: NostrEvent - let privkey: String? - let pubkey: String + let keypair: Keypair var body: some View { Button { - UIPasteboard.general.string = event.get_content(privkey) + UIPasteboard.general.string = event.get_content(keypair.privkey) } label: { Label(NSLocalizedString("Copy Text", comment: "Context menu option for copying the text from an note."), systemImage: "doc.on.doc") } Button { - UIPasteboard.general.string = bech32_pubkey(pubkey) ?? pubkey + UIPasteboard.general.string = keypair.pubkey_bech32 } label: { Label(NSLocalizedString("Copy User Pubkey", comment: "Context menu option for copying the ID of the user who created the note."), systemImage: "person") } @@ -37,25 +36,28 @@ struct EventMenuContext: View { } label: { Label(NSLocalizedString("Copy Note JSON", comment: "Context menu option for copying the JSON text from the note."), systemImage: "square.on.square") } - - Button(role: .destructive) { - let target: ReportTarget = .note(ReportNoteTarget(pubkey: event.pubkey, note_id: event.id)) - notify(.report, target) - } label: { - Label(NSLocalizedString("Report", comment: "Context menu option for reporting content."), systemImage: "exclamationmark.bubble") - } - - Button(role: .destructive) { - notify(.block, event.pubkey) - } label: { - Label(NSLocalizedString("Block", comment: "Context menu option for blocking users."), systemImage: "exclamationmark.octagon") - } Button { NotificationCenter.default.post(name: .broadcast_event, object: event) } label: { Label(NSLocalizedString("Broadcast", comment: "Context menu option for broadcasting the user's note to all of the user's connected relay servers."), systemImage: "globe") } + + // Only allow reporting if logged in with private key and the currently viewed profile is not the logged in profile. + if keypair.pubkey != event.pubkey && keypair.privkey != nil { + Button(role: .destructive) { + let target: ReportTarget = .note(ReportNoteTarget(pubkey: event.pubkey, note_id: event.id)) + notify(.report, target) + } label: { + Label(NSLocalizedString("Report", comment: "Context menu option for reporting content."), systemImage: "exclamationmark.bubble") + } + + Button(role: .destructive) { + notify(.block, event.pubkey) + } label: { + Label(NSLocalizedString("Block", comment: "Context menu option for blocking users."), systemImage: "exclamationmark.octagon") + } + } } } diff --git a/damus/Views/Events/SelectedEventView.swift b/damus/Views/Events/SelectedEventView.swift index f9c71861..f8f3c83d 100644 --- a/damus/Views/Events/SelectedEventView.swift +++ b/damus/Views/Events/SelectedEventView.swift @@ -49,7 +49,7 @@ struct SelectedEventView: View { .padding([.top], 4) } .padding([.leading], 2) - .event_context_menu(event, pubkey: pubkey, privkey: damus.keypair.privkey) + .event_context_menu(event, keypair: damus.keypair) } } } diff --git a/damus/Views/ProfileView.swift b/damus/Views/ProfileView.swift index a48412ed..4be870a1 100644 --- a/damus/Views/ProfileView.swift +++ b/damus/Views/ProfileView.swift @@ -378,14 +378,17 @@ struct ProfileView: View { Button(NSLocalizedString("Share", comment: "Button to share the link to a profile.")) { show_share_sheet = true } - - Button(NSLocalizedString("Report", comment: "Button to report a profile."), role: .destructive) { - let target: ReportTarget = .user(profile.pubkey) - notify(.report, target) - } - - Button(NSLocalizedString("Block", comment: "Button to block a profile."), role: .destructive) { - notify(.block, profile.pubkey) + + // Only allow reporting if logged in with private key and the currently viewed profile is not the logged in profile. + if profile.pubkey != damus_state.pubkey && damus_state.is_privkey_user { + Button(NSLocalizedString("Report", comment: "Button to report a profile."), role: .destructive) { + let target: ReportTarget = .user(profile.pubkey) + notify(.report, target) + } + + Button(NSLocalizedString("Block", comment: "Button to block a profile."), role: .destructive) { + notify(.block, profile.pubkey) + } } } .ignoresSafeArea()