Files
damus/damus/Views/SearchView.swift
Daniel D’Aquino 89acde1b90 filters: Add filter to hashtag search timeline view (#1412)
Applied the content filters to the hashtag search timeline view, to filter out #nsfw-tagged posts on that view if the user has that setting enabled.

Testing of the fix
------------------

**PASS**

**iOS:** 17.0 (iPhone 14 Pro simulator)
**Damus:** This commit
**Test steps:**
1. Search for #sauna hashtag
2. Pick one post from results that contains multiple hashtags
3. Locally change nsfw filter in the code to another hashtag (I picked #homestead in this example) (This is to make testing easier)
4. Run app on simulator
5. Disable nsfw filtering
6. Search for the #sauna hashtag
7. Ensure that the post from step 2 is there
8. Turn on nsfw filtering
9. Search for the #sauna hashtag again. Ensure that post from step 2 is no longer visible
10. Switch keyword back to #nsfw in the code. Re-run app
11. Search for the #nsfw hashtag. No posts appear (timeline view is empty). (Not sure if this is the desired behavior, but seems reasonable)
12. Turn off nsfw filtering
13. Search for the #nsfw hashtag again. #nsfw posts should appear.

Closes: https://github.com/damus-io/damus/issues/1412
Changelog-Fixed: Apply filters to hashtag search timeline view
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-02 12:34:08 -07:00

95 lines
2.4 KiB
Swift

//
// SearchView.swift
// damus
//
// Created by William Casarin on 2022-05-09.
//
import SwiftUI
struct SearchView: View {
let appstate: DamusState
@ObservedObject var search: SearchModel
@Environment(\.dismiss) var dismiss
var content_filter: (NostrEvent) -> Bool {
let filters = ContentFilters.defaults(damus_state: self.appstate)
return ContentFilters(filters: filters).filter
}
let height: CGFloat = 250.0
var body: some View {
TimelineView(events: search.events, loading: $search.loading, damus: appstate, show_friend_icon: true, filter: content_filter) {
ZStack(alignment: .leading) {
DamusBackground(maxHeight: height)
.mask(LinearGradient(gradient: Gradient(colors: [.black, .black, .black, .clear]), startPoint: .top, endPoint: .bottom))
SearchHeaderView(state: appstate, described: described_search)
.padding(.leading, 30)
.padding(.top, 100)
}
}
.ignoresSafeArea()
.onReceive(handle_notify(.switched_timeline)) { obj in
dismiss()
}
.onAppear() {
search.subscribe()
}
.onDisappear() {
search.unsubscribe()
}
.onReceive(handle_notify(.new_mutes)) { notif in
search.filter_muted()
}
}
var described_search: DescribedSearch {
return describe_search(search.search)
}
}
enum DescribedSearch: CustomStringConvertible {
case hashtag(String)
case unknown
var is_hashtag: String? {
switch self {
case .hashtag(let ht):
return ht
case .unknown:
return nil
}
}
var description: String {
switch self {
case .hashtag(let s):
return "#" + s
case .unknown:
return "Search"
}
}
}
func describe_search(_ filter: NostrFilter) -> DescribedSearch {
if let hashtags = filter.hashtag {
if hashtags.count >= 1 {
return .hashtag(hashtags[0])
}
}
return .unknown
}
struct SearchView_Previews: PreviewProvider {
static var previews: some View {
let test_state = test_damus_state
let filter = NostrFilter(hashtag: ["bitcoin"])
let model = SearchModel(state: test_state, search: filter)
SearchView(appstate: test_state, search: model)
}
}