From 1a2e9464afa00167385652d0f4bca3c87d688400 Mon Sep 17 00:00:00 2001 From: radixrat Date: Sat, 24 Dec 2022 09:56:23 -0500 Subject: [PATCH] add friends of friends, apply to all images --- damus/Components/TranslateView.swift | 3 ++- damus/Components/UserView.swift | 2 +- damus/Views/ConfigView.swift | 16 ++++++++++++---- damus/Views/EditMetadataView.swift | 2 +- damus/Views/EventView.swift | 10 ++++++++-- damus/Views/Events/EventProfile.swift | 2 +- damus/Views/NoteContentView.swift | 12 ++++++------ damus/Views/ParicipantsView.swift | 2 +- damus/Views/ProfilePicView.swift | 15 +++++++++------ damus/Views/ProfilePictureSelector.swift | 2 +- damus/Views/ProfileView.swift | 4 ++-- damus/Views/ProfileZoomView.swift | 7 +++++-- damus/Views/SideMenuView.swift | 2 +- 13 files changed, 50 insertions(+), 29 deletions(-) diff --git a/damus/Components/TranslateView.swift b/damus/Components/TranslateView.swift index 9a104868..4465f97c 100644 --- a/damus/Components/TranslateView.swift +++ b/damus/Components/TranslateView.swift @@ -121,7 +121,8 @@ struct TranslateView: View { if let translated = translated_note { // Render translated note. let blocks = event.get_blocks(content: translated) - translated_artifacts = render_blocks(blocks: blocks, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey) + let show_images = should_show_images(contacts: damus_state.contacts, ev: event, our_pubkey: damus_state.pubkey) + translated_artifacts = render_blocks(blocks: blocks, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey, show_images: show_images) } checkingTranslationStatus = false diff --git a/damus/Components/UserView.swift b/damus/Components/UserView.swift index 721a5f40..a6ec8b4a 100644 --- a/damus/Components/UserView.swift +++ b/damus/Components/UserView.swift @@ -17,7 +17,7 @@ struct UserView: View { let pv = ProfileView(damus_state: damus_state, profile: pmodel, followers: followers) NavigationLink(destination: pv) { - ProfilePicView(pubkey: pubkey, size: PFP_SIZE, highlight: .none, profiles: damus_state.profiles) + ProfilePicView(pubkey: pubkey, size: PFP_SIZE, highlight: .none, profiles: damus_state.profiles, contacts: damus_state.contacts) VStack(alignment: .leading) { let profile = damus_state.profiles.lookup(id: pubkey) diff --git a/damus/Views/ConfigView.swift b/damus/Views/ConfigView.swift index 3ddd6f0d..586a9f8b 100644 --- a/damus/Views/ConfigView.swift +++ b/damus/Views/ConfigView.swift @@ -11,6 +11,7 @@ import SwiftUI enum RemoteImagePolicy: String, CaseIterable { case everyone case friendsOnly + case friendsOfFriends case restricted } @@ -20,8 +21,10 @@ func remoteImagePolicyText(_ fs: RemoteImagePolicy) -> String { return "Everyone" case .friendsOnly: return "Friends Only" + case .friendsOfFriends: + return "Friends of Friends" case .restricted: - return "Restricted (no remote image)" + return "Block Images" } } @@ -36,8 +39,8 @@ struct ConfigView: View { @State var privkey_copied: Bool = false @State var pubkey_copied: Bool = false @State var delete_text: String = "" - @EnvironmentObject var user_settings: UserSettingsStore - @AppStorage("remote_image_policy") var remote_image_policy: RemoteImagePolicy = .everyone + @ObservedObject var settings: UserSettingsStore + @AppStorage("remote_image_policy") var remote_image_policy: RemoteImagePolicy = .friendsOfFriends let generator = UIImpactFeedbackGenerator(style: .light) @@ -147,13 +150,18 @@ struct ConfigView: View { } } - Section("Profile Image Loading Policy") { + Section(NSLocalizedString("Remote Image Loading Policy", comment: "Section title for remote image loading policy")) { Menu { Button { self.remote_image_policy = .everyone } label: { Text(remoteImagePolicyText(.everyone)) } + Button { + self.remote_image_policy = .friendsOfFriends + } label: { + Text(remoteImagePolicyText(.friendsOfFriends)) + } Button { self.remote_image_policy = .friendsOnly } label: { diff --git a/damus/Views/EditMetadataView.swift b/damus/Views/EditMetadataView.swift index a12ef199..bf6ae2bc 100644 --- a/damus/Views/EditMetadataView.swift +++ b/damus/Views/EditMetadataView.swift @@ -120,7 +120,7 @@ struct EditMetadataView: View { let pfp_size: CGFloat = 90.0 HStack(alignment: .center) { - ProfilePicView(pubkey: damus_state.pubkey, size: pfp_size, highlight: .custom(imageBorderColor(), 4.0), profiles: damus_state.profiles) + ProfilePicView(pubkey: damus_state.pubkey, size: pfp_size, highlight: .custom(imageBorderColor(), 4.0), profiles: damus_state.profiles, contacts: damus_state.contacts) .offset(y: -(pfp_size/2.0)) // Increase if set a frame Spacer() diff --git a/damus/Views/EventView.swift b/damus/Views/EventView.swift index a63e7974..bbc6e08d 100644 --- a/damus/Views/EventView.swift +++ b/damus/Views/EventView.swift @@ -151,12 +151,18 @@ func should_show_images(contacts: Contacts, ev: NostrEvent, our_pubkey: String, if ev.pubkey == our_pubkey { return true } - if contacts.is_in_friendosphere(ev.pubkey) { + + let remote_image_policy: RemoteImagePolicy = RemoteImagePolicy(rawValue: UserDefaults.standard.string(forKey: "remote_image_policy") ?? "") ?? .friendsOfFriends + if remote_image_policy == .everyone || + remote_image_policy == .friendsOnly && contacts.is_friend(ev.pubkey) || + remote_image_policy == .friendsOfFriends && contacts.is_in_friendosphere(ev.pubkey) { return true } - if let boost_key = booster_pubkey, contacts.is_in_friendosphere(boost_key) { + + if let boost_key = booster_pubkey, contacts.is_in_friendosphere(boost_key) && remote_image_policy != .restricted { return true } + return false } diff --git a/damus/Views/Events/EventProfile.swift b/damus/Views/Events/EventProfile.swift index 17ef625e..cf7be340 100644 --- a/damus/Views/Events/EventProfile.swift +++ b/damus/Views/Events/EventProfile.swift @@ -35,7 +35,7 @@ struct EventProfile: View { let pv = ProfileView(damus_state: damus_state, profile: pmodel, followers: FollowersModel(damus_state: damus_state, target: pubkey)) NavigationLink(destination: pv) { - ProfilePicView(pubkey: pubkey, size: pfp_size, highlight: .none, profiles: damus_state.profiles) + ProfilePicView(pubkey: pubkey, size: pfp_size, highlight: .none, profiles: damus_state.profiles, contacts: damus_state.contacts) } } diff --git a/damus/Views/NoteContentView.swift b/damus/Views/NoteContentView.swift index bb492c91..10beded2 100644 --- a/damus/Views/NoteContentView.swift +++ b/damus/Views/NoteContentView.swift @@ -65,7 +65,7 @@ struct NoteContentView: View { var body: some View { MainContent() .onAppear() { - self.artifacts = render_note_content(ev: event, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey) + self.artifacts = render_note_content(ev: event, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey, show_images: show_images) } .onReceive(handle_notify(.profile_updated)) { notif in let profile = notif.object as! ProfileUpdate @@ -74,7 +74,7 @@ struct NoteContentView: View { switch block { case .mention(let m): if m.type == .pubkey && m.ref.ref_id == profile.pubkey { - self.artifacts = render_note_content(ev: event, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey) + self.artifacts = render_note_content(ev: event, profiles: damus_state.profiles, privkey: damus_state.keypair.privkey, show_images: show_images) } case .text: return case .hashtag: return @@ -261,12 +261,12 @@ struct NoteArtifacts { } } -func render_note_content(ev: NostrEvent, profiles: Profiles, privkey: String?) -> NoteArtifacts { +func render_note_content(ev: NostrEvent, profiles: Profiles, privkey: String?, show_images: Bool) -> NoteArtifacts { let blocks = ev.blocks(privkey) - return render_blocks(blocks: blocks, profiles: profiles, privkey: privkey) + return render_blocks(blocks: blocks, profiles: profiles, privkey: privkey, show_images: show_images) } -func render_blocks(blocks: [Block], profiles: Profiles, privkey: String?) -> NoteArtifacts { +func render_blocks(blocks: [Block], profiles: Profiles, privkey: String?, show_images: Bool) -> NoteArtifacts { var invoices: [Invoice] = [] var img_urls: [URL] = [] var link_urls: [URL] = [] @@ -283,7 +283,7 @@ func render_blocks(blocks: [Block], profiles: Profiles, privkey: String?) -> Not return str case .url(let url): // Handle Image URLs - if is_image_url(url) { + if show_images && is_image_url(url) { // Append Image img_urls.append(url) return str diff --git a/damus/Views/ParicipantsView.swift b/damus/Views/ParicipantsView.swift index 720a3e84..75ec693d 100644 --- a/damus/Views/ParicipantsView.swift +++ b/damus/Views/ParicipantsView.swift @@ -39,7 +39,7 @@ struct ParticipantsView: View { ForEach(originalReferences.pRefs) { participant in let pubkey = participant.id HStack { - ProfilePicView(pubkey: pubkey, size: PFP_SIZE, highlight: .none, profiles: damus_state.profiles) + ProfilePicView(pubkey: pubkey, size: PFP_SIZE, highlight: .none, profiles: damus_state.profiles, contacts: damus_state.contacts) VStack(alignment: .leading) { let profile = damus_state.profiles.lookup(id: pubkey) diff --git a/damus/Views/ProfilePicView.swift b/damus/Views/ProfilePicView.swift index 1ce2a21d..d6822d78 100644 --- a/damus/Views/ProfilePicView.swift +++ b/damus/Views/ProfilePicView.swift @@ -127,14 +127,17 @@ struct ProfilePicView: View { } } -func get_profile_url(picture: String?, pubkey: String, profiles: Profiles) -> URL { +func get_profile_url(picture: String?, pubkey: String, profiles: Profiles, contacts: Contacts) -> URL { var pic: String - let remote_image_policy: RemoteImagePolicy = RemoteImagePolicy(rawValue: UserDefaults.standard.string(forKey: "remote_image_policy")!) ?? .everyone + let remote_image_policy: RemoteImagePolicy = RemoteImagePolicy(rawValue: UserDefaults.standard.string(forKey: "remote_image_policy") ?? "") ?? .friendsOfFriends - if remote_image_policy == .restricted || (remote_image_policy == .friendsOnly && !contacts.is_friend(pubkey)) { - pic = robohash(pubkey) - } else { + if pubkey == contacts.our_pubkey || + remote_image_policy == .everyone || + remote_image_policy == .friendsOnly && contacts.is_friend(pubkey) || + remote_image_policy == .friendsOfFriends && contacts.is_in_friendosphere(pubkey) { pic = picture ?? profiles.lookup(id: pubkey)?.picture ?? robohash(pubkey) + } else { + pic = robohash(pubkey) } if let url = URL(string: pic) { @@ -161,7 +164,7 @@ struct ProfilePicView_Previews: PreviewProvider { size: 100, highlight: .none, profiles: make_preview_profiles(pubkey), - contacts: Contacts()) + contacts: Contacts(our_pubkey: pubkey)) } } diff --git a/damus/Views/ProfilePictureSelector.swift b/damus/Views/ProfilePictureSelector.swift index 4abba4a3..7ef4fc63 100644 --- a/damus/Views/ProfilePictureSelector.swift +++ b/damus/Views/ProfilePictureSelector.swift @@ -13,7 +13,7 @@ struct ProfilePictureSelector: View { var body: some View { let highlight: Highlight = .custom(Color.white, 2.0) ZStack { - ProfilePicView(pubkey: pubkey, size: 80.0, highlight: highlight, profiles: Profiles(), contacts: Contacts()) + ProfilePicView(pubkey: pubkey, size: 80.0, highlight: highlight, profiles: Profiles(), contacts: Contacts(our_pubkey: pubkey)) } } } diff --git a/damus/Views/ProfileView.swift b/damus/Views/ProfileView.swift index 48082a92..f0fd7512 100644 --- a/damus/Views/ProfileView.swift +++ b/damus/Views/ProfileView.swift @@ -254,12 +254,12 @@ struct ProfileView: View { let pfp_size: CGFloat = 90.0 HStack(alignment: .center) { - ProfilePicView(pubkey: profile.pubkey, size: pfp_size, highlight: .custom(imageBorderColor(), 4.0), profiles: damus_state.profiles) + ProfilePicView(pubkey: profile.pubkey, size: pfp_size, highlight: .custom(imageBorderColor(), 4.0), profiles: damus_state.profiles, contacts: damus_state.contacts) .onTapGesture { is_zoomed.toggle() } .fullScreenCover(isPresented: $is_zoomed) { - ProfileZoomView(pubkey: profile.pubkey, profiles: damus_state.profiles) } + ProfileZoomView(pubkey: profile.pubkey, profiles: damus_state.profiles, contacts: damus_state.contacts)} .offset(y: -(pfp_size/2.0)) // Increase if set a frame Spacer() diff --git a/damus/Views/ProfileZoomView.swift b/damus/Views/ProfileZoomView.swift index 16d8eed0..14d6308b 100644 --- a/damus/Views/ProfileZoomView.swift +++ b/damus/Views/ProfileZoomView.swift @@ -11,6 +11,7 @@ struct ProfileZoomView: View { @Environment(\.presentationMode) var presentationMode let pubkey: String let profiles: Profiles + let contacts: Contacts @GestureState private var scaleState: CGFloat = 1 @GestureState private var offsetState = CGSize.zero @@ -68,7 +69,7 @@ struct ProfileZoomView: View { Spacer() - ProfilePicView(pubkey: pubkey, size: 200.0, highlight: .none, profiles: profiles) + ProfilePicView(pubkey: pubkey, size: 200.0, highlight: .none, profiles: profiles, contacts: contacts) .padding(100) .scaledToFit() .scaleEffect(self.scale * scaleState) @@ -92,6 +93,8 @@ struct ProfileZoomView_Previews: PreviewProvider { static var previews: some View { ProfileZoomView( pubkey: pubkey, - profiles: make_preview_profiles(pubkey)) + profiles: make_preview_profiles(pubkey), + contacts: Contacts(our_pubkey: pubkey) + ) } } diff --git a/damus/Views/SideMenuView.swift b/damus/Views/SideMenuView.swift index 5e3c48ce..03275954 100644 --- a/damus/Views/SideMenuView.swift +++ b/damus/Views/SideMenuView.swift @@ -55,7 +55,7 @@ struct SideMenuView: View { NavigationLink(destination: ProfileView(damus_state: damus_state, profile: profile_model, followers: followers)) { if let picture = damus_state.profiles.lookup(id: damus_state.pubkey)?.picture { - ProfilePicView(pubkey: damus_state.pubkey, size: 60, highlight: .none, profiles: damus_state.profiles, picture: picture) + ProfilePicView(pubkey: damus_state.pubkey, size: 60, highlight: .none, profiles: damus_state.profiles, contacts: damus_state.contacts, picture: picture) } else { Image(systemName: "person.fill") }