Files
damus/damus/Views/DirectMessagesView.swift
Daniel D’Aquino 06eb9d4a0e dm: Do not show DMs from muted users
This commits causes DMs from muted users to be filtered out. It also fixes an issue where the DM list would appear completely blank in certain scenarios.

Testing
-------

CONDITIONAL PASS

Device: iPhone 14 Pro simulator
iOS: 17.0
Damus: This commit
Setup:
- Three test accounts (A, B, and C). "A" will be the account running on the device under test.
- Account "A" should start with no DMs

1. Send a direct message from "B" to "A", and reply from "A".
2. Go to DMs -> DMs tab. Conversation with "B" should appear. PASS
3. Mute user "B" (I did it via profiles page).
4. Go back to DMs view via back button. DMs from "B" should not appear. PASS
5. Since there are no DMs, the screen should display "Nothing to see here". PASS
5. Close Damus app via iOS app switcher and reopen Damus
6. Check DMs list. Should only show "Nothing to see here". PASS
7. Send a DM from account "C" to "A" and reply.
8. Go back to DMs -> DMs tab. Only the message from account "C" should appear.
9. Unmute user "B"
10. Go back to DMs. Messages from "B" and "C" should appear. PASS

Notes:
- There was one instance when the first DM from account "C" appeared in the "DMs" tab (Not "requests") momentarily. After a bit it went into requests as expected.
- When unmuting user "B", I had to refresh the DM list by switching tabs, meaning that the view did not immediately update.

Upon inspection, the two behaviors above are not caused by this change, so this is a conditional pass.

Closes: https://github.com/damus-io/damus/issues/1350
Changelog-Fixed: Do not show DMs from muted users
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
Reviewed-by: William Casarin <jb55@jb55.com>
Signed-off-by: William Casarin <jb55@jb55.com>
2023-10-18 07:04:17 +08:00

120 lines
4.0 KiB
Swift

//
// DirectMessagesView.swift
// damus
//
// Created by William Casarin on 2022-06-29.
//
import SwiftUI
enum DMType: Hashable {
case rando
case friend
}
struct DirectMessagesView: View {
let damus_state: DamusState
@State var dm_type: DMType = .friend
@ObservedObject var model: DirectMessagesModel
@ObservedObject var settings: UserSettingsStore
func MainContent(requests: Bool) -> some View {
ScrollView {
LazyVStack(spacing: 0) {
let dms = requests ? model.message_requests : model.friend_dms
let filtered_dms = filter_dms(dms: dms)
if filtered_dms.isEmpty, !model.loading {
EmptyTimelineView()
} else {
ForEach(filtered_dms, id: \.pubkey) { dm in
MaybeEvent(dm)
.padding(.top, 10)
}
}
}
.padding(.horizontal)
}
}
func filter_dms(dms: [DirectMessageModel]) -> [DirectMessageModel] {
return dms.filter({ dm in
return damus_state.settings.friend_filter.filter(contacts: damus_state.contacts, pubkey: dm.pubkey) && !damus_state.contacts.is_muted(dm.pubkey)
})
}
var options: EventViewOptions {
if self.damus_state.settings.translate_dms {
return [.truncate_content, .no_action_bar]
}
return [.truncate_content, .no_action_bar, .no_translate]
}
func MaybeEvent(_ model: DirectMessageModel) -> some View {
Group {
if let ev = model.events.last {
EventView(damus: damus_state, event: ev, pubkey: model.pubkey, options: options)
.onTapGesture {
self.model.set_active_dm_model(model)
damus_state.nav.push(route: Route.DMChat(dms: self.model.active_model))
}
Divider()
.padding([.top], 10)
} else {
EmptyView()
}
}
}
var body: some View {
VStack(spacing: 0) {
CustomPicker(selection: $dm_type, content: {
Text("DMs", comment: "Picker option for DM selector for seeing only DMs that have been responded to. DM is the English abbreviation for Direct Message.")
.tag(DMType.friend)
Text("Requests", comment: "Picker option for DM selector for seeing only message requests (DMs that someone else sent the user which has not been responded to yet). DM is the English abbreviation for Direct Message.")
.tag(DMType.rando)
})
Divider()
.frame(height: 1)
TabView(selection: $dm_type) {
MainContent(requests: false)
.tag(DMType.friend)
MainContent(requests: true)
.tag(DMType.rando)
}
.tabViewStyle(.page(indexDisplayMode: .never))
}
.toolbar {
ToolbarItem(placement: .navigationBarTrailing) {
if would_filter_non_friends_from_dms(contacts: damus_state.contacts, dms: self.model.dms) {
FriendsButton(filter: $settings.friend_filter)
}
}
}
.navigationTitle(NSLocalizedString("DMs", comment: "Navigation title for view of DMs, where DM is an English abbreviation for Direct Message."))
}
}
func would_filter_non_friends_from_dms(contacts: Contacts, dms: [DirectMessageModel]) -> Bool {
for dm in dms {
if !FriendFilter.friends.filter(contacts: contacts, pubkey: dm.pubkey) {
return true
}
}
return false
}
struct DirectMessagesView_Previews: PreviewProvider {
static var previews: some View {
let ds = test_damus_state
DirectMessagesView(damus_state: ds, model: ds.dms, settings: ds.settings)
}
}