Mute events in threads

Changlog-Added: Mute events in threads
This commit is contained in:
William Casarin
2023-01-27 10:18:28 -08:00
parent d559dd3a13
commit 5edb7df5c4
8 changed files with 152 additions and 38 deletions

View File

@@ -410,15 +410,8 @@ class HomeModel: ObservableObject {
return ok
}
func should_hide_event(_ ev: NostrEvent) -> Bool {
if damus_state.contacts.is_muted(ev.pubkey) {
return true
}
return !ev.should_show_event
}
func handle_text_event(sub_id: String, _ ev: NostrEvent) {
if should_hide_event(ev) {
if should_hide_event(contacts: damus_state.contacts, ev: ev) {
return
}
@@ -726,3 +719,11 @@ func event_has_our_pubkey(_ ev: NostrEvent, our_pubkey: String) -> Bool {
return false
}
func should_hide_event(contacts: Contacts, ev: NostrEvent) -> Bool {
if contacts.is_muted(ev.pubkey) {
return true
}
return !ev.should_show_event
}

View File

@@ -71,8 +71,8 @@ struct EventDetailView: View {
}
toggle_thread_view()
}
case .event(let ev, let highlight):
EventView(event: ev, has_action_bar: true, damus: damus)
case .event(let ev, let _):
EventView(damus: damus, event: ev, has_action_bar: true)
.onTapGesture {
if thread.initial_event.id == ev.id {
toggle_thread_view()

View File

@@ -35,7 +35,7 @@ struct EventView: View {
@EnvironmentObject var action_bar: ActionBarModel
init(event: NostrEvent, has_action_bar: Bool, damus: DamusState) {
init(damus: DamusState, event: NostrEvent, has_action_bar: Bool) {
self.event = event
self.has_action_bar = has_action_bar
self.damus = damus
@@ -222,9 +222,9 @@ struct EventView_Previews: PreviewProvider {
*/
EventView(
damus: test_damus_state(),
event: test_event,
has_action_bar: true,
damus: test_damus_state()
has_action_bar: true
)
}
.padding()

View File

@@ -0,0 +1,112 @@
//
// MutedEventView.swift
// damus
//
// Created by William Casarin on 2023-01-27.
//
import SwiftUI
struct MutedEventView: View {
let damus_state: DamusState
let event: NostrEvent
let scroller: ScrollViewProxy?
let selected: Bool
@Binding var nav_target: String?
@Binding var navigating: Bool
@State var shown: Bool
@Environment(\.colorScheme) var colorScheme
init(damus_state: DamusState, event: NostrEvent, scroller: ScrollViewProxy?, nav_target: Binding<String?>, navigating: Binding<Bool>, selected: Bool) {
self.damus_state = damus_state
self.event = event
self.scroller = scroller
self.selected = selected
self._nav_target = nav_target
self._navigating = navigating
self._shown = State(initialValue: !should_hide_event(contacts: damus_state.contacts, ev: event))
}
var should_mute: Bool {
return should_hide_event(contacts: damus_state.contacts, ev: event)
}
var FillColor: Color {
colorScheme == .light ? Color("DamusLightGrey") : Color("DamusDarkGrey")
}
var MutedBox: some View {
ZStack {
RoundedRectangle(cornerRadius: 20)
.foregroundColor(FillColor)
HStack {
Text("Post from a user you've blocked")
Spacer()
Button(shown ? "Hide" : "Show") {
shown.toggle()
}
}
.padding(10)
}
}
var Event: some View {
Group {
if selected {
SelectedEventView(damus: damus_state, event: event)
} else {
EventView(damus: damus_state, event: event, has_action_bar: true)
.onTapGesture {
nav_target = event.id
navigating = true
}
.onAppear {
// TODO: find another solution to prevent layout shifting and layout blocking on large responses
scroller?.scrollTo("main", anchor: .bottom)
}
}
}
}
var body: some View {
Group {
if should_mute {
MutedBox
}
if shown {
Event
}
}
.onReceive(handle_notify(.new_mutes)) { notif in
guard let mutes = notif.object as? [String] else {
return
}
if mutes.contains(event.pubkey) {
shown = false
}
}
.onReceive(handle_notify(.new_unmutes)) { notif in
guard let unmutes = notif.object as? [String] else {
return
}
if unmutes.contains(event.pubkey) {
shown = true
}
}
}
}
struct MutedEventView_Previews: PreviewProvider {
@State static var nav_target: String? = nil
@State static var navigating: Bool = false
static var previews: some View {
MutedEventView(damus_state: test_damus_state(), event: test_event, scroller: nil, nav_target: $nav_target, navigating: $navigating, selected: false)
.frame(width: .infinity, height: 50)
}
}

View File

@@ -45,7 +45,7 @@ struct ReplyView: View {
ParticipantsView(damus_state: damus, references: $references, originalReferences: $originalReferences)
}
ScrollView {
EventView(event: replying_to, has_action_bar: false, damus: damus)
EventView(damus: damus, event: replying_to, has_action_bar: false)
}
PostView(replying_to: replying_to, references: references)
}

View File

@@ -255,15 +255,13 @@ struct ThreadV2View: View {
// MARK: - Parents events view
VStack {
ForEach(thread.parentEvents, id: \.id) { event in
EventView(event: event, has_action_bar: true, damus: damus)
.onTapGesture {
nav_target = event.id
navigating = true
}
.onAppear {
// TODO: find another solution to prevent layout shifting and layout blocking on large responses
reader.scrollTo("main", anchor: .bottom)
}
MutedEventView(damus_state: damus,
event: event,
scroller: reader,
nav_target: $nav_target,
navigating: $navigating,
selected: false
)
}
}.background(GeometryReader { geometry in
// get the height and width of the EventView view
@@ -278,22 +276,25 @@ struct ThreadV2View: View {
})
// MARK: - Actual event view
SelectedEventView(
damus: damus,
event: thread.current
MutedEventView(
damus_state: damus,
event: thread.current,
scroller: reader,
nav_target: $nav_target,
navigating: $navigating,
selected: true
).id("main")
// MARK: - Responses of the actual event view
ForEach(thread.childEvents, id: \.id) { event in
EventView(
MutedEventView(
damus_state: damus,
event: event,
has_action_bar: true,
damus: damus
scroller: reader,
nav_target: $nav_target,
navigating: $navigating,
selected: false
)
.onTapGesture {
nav_target = event.id
navigating = true
}
}
}.padding()
}.navigationBarTitle(NSLocalizedString("Thread", comment: "Navigation bar title for note thread."))

View File

@@ -39,11 +39,7 @@ struct InnerTimelineView: View {
EmptyTimelineView()
} else {
ForEach(events.filter(filter), id: \.id) { (ev: NostrEvent) in
//let tm = ThreadModel(event: inner_event_or_self(ev: ev), damus_state: damus)
//let is_chatroom = should_show_chatroom(ev)
//let tv = ThreadView(thread: tm, damus: damus, is_chatroom: is_chatroom)
EventView(event: ev, has_action_bar: true, damus: damus)
EventView(damus: damus, event: ev, has_action_bar: true)
.onTapGesture {
nav_target = ev
navigating = true