damus

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

commit d658d1d987b6c3808286df69e46f735ee73de45c
parent e14cd99c85172be62660f38250506ac68020c03e
Author: William Casarin <jb55@jb55.com>
Date:   Sat, 21 Jan 2023 11:13:44 -0800

DM Message Requests

Put strangers in a different tab

Changelog-Added: Add DM Message Requests

Diffstat:
Mdamus/Models/DamusState.swift | 2+-
Mdamus/Models/DirectMessageModel.swift | 27++++++++++++++++++++++++---
Mdamus/Models/DirectMessagesModel.swift | 15++++++++++++++-
Mdamus/Models/HomeModel.swift | 6++++--
Mdamus/Views/DMChatView.swift | 2+-
Mdamus/Views/DirectMessagesView.swift | 50+++++++++++++++++++++++++++++++++++++++++---------
Mdamus/Views/ProfileView.swift | 2+-
7 files changed, 86 insertions(+), 18 deletions(-)

diff --git a/damus/Models/DamusState.swift b/damus/Models/DamusState.swift @@ -29,6 +29,6 @@ struct DamusState { static var empty: DamusState { - return DamusState.init(pool: RelayPool(), keypair: Keypair(pubkey: "", privkey: ""), likes: EventCounter(our_pubkey: ""), boosts: EventCounter(our_pubkey: ""), contacts: Contacts(our_pubkey: ""), tips: TipCounter(our_pubkey: ""), profiles: Profiles(), dms: DirectMessagesModel(), previews: PreviewCache()) + return DamusState.init(pool: RelayPool(), keypair: Keypair(pubkey: "", privkey: ""), likes: EventCounter(our_pubkey: ""), boosts: EventCounter(our_pubkey: ""), contacts: Contacts(our_pubkey: ""), tips: TipCounter(our_pubkey: ""), profiles: Profiles(), dms: DirectMessagesModel(our_pubkey: ""), previews: PreviewCache()) } } diff --git a/damus/Models/DirectMessageModel.swift b/damus/Models/DirectMessageModel.swift @@ -8,13 +8,34 @@ import Foundation class DirectMessageModel: ObservableObject { - @Published var events: [NostrEvent] + @Published var events: [NostrEvent] { + didSet { + is_request = determine_is_request() + } + } + + var is_request: Bool + var our_pubkey: String + + func determine_is_request() -> Bool { + for event in events { + if event.pubkey == our_pubkey { + return false + } + } + + return true + } - init(events: [NostrEvent]) { + init(events: [NostrEvent], our_pubkey: String) { self.events = events + self.is_request = false + self.our_pubkey = our_pubkey } - init() { + init(our_pubkey: String) { self.events = [] + self.is_request = false + self.our_pubkey = our_pubkey } } diff --git a/damus/Models/DirectMessagesModel.swift b/damus/Models/DirectMessagesModel.swift @@ -10,13 +10,26 @@ import Foundation class DirectMessagesModel: ObservableObject { @Published var dms: [(String, DirectMessageModel)] = [] @Published var loading: Bool = false + let our_pubkey: String + + init(our_pubkey: String) { + self.our_pubkey = our_pubkey + } + + var message_requests: [(String, DirectMessageModel)] { + return dms.filter { dm in dm.1.is_request } + } + + var friend_dms: [(String, DirectMessageModel)] { + return dms.filter { dm in !dm.1.is_request } + } func lookup_or_create(_ pubkey: String) -> DirectMessageModel { if let dm = lookup(pubkey) { return dm } - let new = DirectMessageModel() + let new = DirectMessageModel(our_pubkey: our_pubkey) dms.append((pubkey, new)) return new } diff --git a/damus/Models/HomeModel.swift b/damus/Models/HomeModel.swift @@ -48,17 +48,19 @@ class HomeModel: ObservableObject { @Published var new_events: NewEventsBits = NewEventsBits() @Published var notifications: [NostrEvent] = [] - @Published var dms: DirectMessagesModel = DirectMessagesModel() + @Published var dms: DirectMessagesModel @Published var events: [NostrEvent] = [] @Published var loading: Bool = false @Published var signal: SignalModel = SignalModel() init() { self.damus_state = DamusState.empty + self.dms = DirectMessagesModel(our_pubkey: damus_state.pubkey) } init(damus_state: DamusState) { self.damus_state = damus_state + self.dms = DirectMessagesModel(our_pubkey: damus_state.pubkey) } var pool: RelayPool { @@ -644,7 +646,7 @@ func handle_incoming_dm(prev_events: NewEventsBits, dms: DirectMessagesModel, ou if !found { inserted = true - let model = DirectMessageModel(events: [ev]) + let model = DirectMessageModel(events: [ev], our_pubkey: our_pubkey) dms.dms.append((the_pk, model)) } diff --git a/damus/Views/DMChatView.swift b/damus/Views/DMChatView.swift @@ -158,7 +158,7 @@ struct DMChatView_Previews: PreviewProvider { static var previews: some View { let ev = NostrEvent(content: "hi", pubkey: "pubkey", kind: 1, tags: []) - let model = DirectMessageModel(events: [ev]) + let model = DirectMessageModel(events: [ev], our_pubkey: "pubkey") DMChatView(damus_state: test_damus_state(), pubkey: "pubkey") .environmentObject(model) diff --git a/damus/Views/DirectMessagesView.swift b/damus/Views/DirectMessagesView.swift @@ -7,15 +7,26 @@ import SwiftUI +enum DMType: Hashable { + case rando + case friend +} + struct DirectMessagesView: View { let damus_state: DamusState + @State var dm_type: DMType = .friend @State var open_dm: Bool = false @State var pubkey: String = "" - @State var active_model: DirectMessageModel = DirectMessageModel() @EnvironmentObject var model: DirectMessagesModel + @State var active_model: DirectMessageModel + + init(damus_state: DamusState) { + self.damus_state = damus_state + self._active_model = State(initialValue: DirectMessageModel(our_pubkey: damus_state.pubkey)) + } - var MainContent: some View { + func MainContent(requests: Bool) -> some View { ScrollView { let chat = DMChatView(damus_state: damus_state, pubkey: pubkey) .environmentObject(active_model) @@ -26,13 +37,12 @@ struct DirectMessagesView: View { if model.dms.isEmpty, !model.loading { EmptyTimelineView() } else { - ForEach(model.dms, id: \.0) { tup in + let dms = requests ? model.message_requests : model.friend_dms + ForEach(dms, id: \.0) { tup in MaybeEvent(tup) } } } - .padding(.horizontal) - .padding(.top) } } @@ -52,8 +62,29 @@ struct DirectMessagesView: View { } var body: some View { - MainContent - .navigationTitle(NSLocalizedString("Encrypted DMs", comment: "Navigation title for view of encrypted DMs, where DM is an English abbreviation for Direct Message.")) + VStack { + Picker(NSLocalizedString("DM Type", comment: "DM selector for seeing either DMs or message requests, which are messages that have not been responded to yet."), selection: $dm_type) { + Text("DMs") + .tag(DMType.friend) + + Text("Requests") + .tag(DMType.rando) + + } + .pickerStyle(.segmented) + + TabView(selection: $dm_type) { + MainContent(requests: false) + .tag(DMType.friend) + + MainContent(requests: true) + .tag(DMType.rando) + } + .tabViewStyle(.page(indexDisplayMode: .never)) + } + .padding(.horizontal) + .padding(.top) + .navigationTitle(NSLocalizedString("Encrypted DMs", comment: "Navigation title for view of encrypted DMs, where DM is an English abbreviation for Direct Message.")) } } @@ -63,8 +94,9 @@ struct DirectMessagesView_Previews: PreviewProvider { pubkey: "pubkey", kind: 4, tags: []) - let model = DirectMessageModel(events: [ev]) - DirectMessagesView(damus_state: test_damus_state()) + let ds = test_damus_state() + let model = DirectMessageModel(events: [ev], our_pubkey: ds.pubkey) + DirectMessagesView(damus_state: ds) .environmentObject(model) } } diff --git a/damus/Views/ProfileView.swift b/damus/Views/ProfileView.swift @@ -355,7 +355,7 @@ struct ProfileView_Previews: PreviewProvider { func test_damus_state() -> DamusState { let pubkey = "3efdaebb1d8923ebd99c9e7ace3b4194ab45512e2be79c1b7d68d9243e0d2681" - let damus = DamusState(pool: RelayPool(), keypair: Keypair(pubkey: pubkey, privkey: "privkey"), likes: EventCounter(our_pubkey: pubkey), boosts: EventCounter(our_pubkey: pubkey), contacts: Contacts(our_pubkey: pubkey), tips: TipCounter(our_pubkey: pubkey), profiles: Profiles(), dms: DirectMessagesModel(), previews: PreviewCache()) + let damus = DamusState(pool: RelayPool(), keypair: Keypair(pubkey: pubkey, privkey: "privkey"), likes: EventCounter(our_pubkey: pubkey), boosts: EventCounter(our_pubkey: pubkey), contacts: Contacts(our_pubkey: pubkey), tips: TipCounter(our_pubkey: pubkey), profiles: Profiles(), dms: DirectMessagesModel(our_pubkey: pubkey), previews: PreviewCache()) let prof = Profile(name: "damus", display_name: "damus", about: "iOS app!", picture: "https://damus.io/img/logo.png", banner: "", website: "https://damus.io", lud06: nil, lud16: "jb55@sendsats.lol", nip05: "damus.io") let tsprof = TimestampedProfile(profile: prof, timestamp: 0)