Skip to content

Commit 4d56877

Browse files
committed
Adds active filter
1 parent e7ebdff commit 4d56877

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

api/src/services/search.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use actix_web::web::{Data, Query};
22

33
#[derive(serde::Deserialize)]
44
struct Request {
5+
active: Option<bool>,
56
q: Option<String>,
67
tag: Option<String>,
78
source: Option<String>,
@@ -53,7 +54,7 @@ fn search(
5354
clause: &elephantry::Where,
5455
query: &Request,
5556
) -> oxfeed::Result<actix_web::HttpResponse> {
56-
use std::fmt::Write;
57+
use std::fmt::Write as _;
5758

5859
let mut clause = clause.clone();
5960

@@ -64,6 +65,10 @@ fn search(
6465
include_str!("../../sql/search_items.sql").to_string()
6566
};
6667

68+
if let Some(active) = &query.active {
69+
clause.and_where("s.active = $*", vec![active]);
70+
}
71+
6772
if let Some(tag) = &query.tag {
6873
clause.and_where("$* = any(tags)", vec![tag]);
6974
}
@@ -118,6 +123,10 @@ async fn sources(
118123
.and_where("source.title ~* $*", vec![&q])
119124
.build();
120125

126+
if let Some(active) = &query.active {
127+
clause.and_where("source.active = $*", vec![active]);
128+
}
129+
121130
if let Some(source) = &query.source {
122131
clause.and_where("source.url ~* $*", vec![source])
123132
}

front/src/filter.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#[derive(Clone, Default, Eq, PartialEq)]
22
pub(crate) struct Filter {
3+
active: Option<bool>,
34
q: String,
45
source: String,
56
tag: String,
@@ -14,6 +15,7 @@ impl Filter {
1415
#[must_use]
1516
pub fn from(location: &crate::Location) -> Self {
1617
Self {
18+
active: location.active(),
1719
q: location.q(),
1820
source: location.param("source"),
1921
tag: location.param("tag"),
@@ -22,13 +24,17 @@ impl Filter {
2224

2325
#[must_use]
2426
pub fn is_empty(&self) -> bool {
25-
self.q.is_empty() && self.tag.is_empty() && self.source.is_empty()
27+
self.active.is_none() && self.q.is_empty() && self.tag.is_empty() && self.source.is_empty()
2628
}
2729

2830
#[must_use]
2931
pub fn to_url_param(&self) -> String {
3032
let mut params = Vec::new();
3133

34+
if let Some(active) = self.active {
35+
params.push(format!("active={active}"));
36+
}
37+
3238
if !self.q.is_empty() {
3339
params.push(format!("q={}", urlencoding::encode(&self.q)));
3440
}
@@ -47,15 +53,19 @@ impl Filter {
4753

4854
impl From<String> for Filter {
4955
fn from(query: String) -> Self {
56+
let mut active = None;
5057
let mut q = query.clone();
5158
let mut source = String::new();
5259
let mut tag = String::new();
5360

5461
let regex =
55-
regex::Regex::new(r#"(:?source=(?P<source>[^ ]+) )?(tag=(?P<tag>[^ ]+) )?(?P<q>.*)"#)
62+
regex::Regex::new(r#"(:?active=(?P<active>[^ ]+) )?(:?source=(?P<source>[^ ]+) )?(tag=(?P<tag>[^ ]+) )?(?P<q>.*)"#)
5663
.unwrap();
5764

5865
if let Some(captures) = regex.captures(&query) {
66+
active = captures
67+
.name("active")
68+
.and_then(|x| x.as_str().parse().ok());
5969
q = captures
6070
.name("q")
6171
.map_or_else(|| query.clone(), |x| x.as_str().to_string());
@@ -69,12 +79,21 @@ impl From<String> for Filter {
6979
.unwrap_or_default();
7080
}
7181

72-
Self { q, source, tag }
82+
Self {
83+
active,
84+
q,
85+
source,
86+
tag,
87+
}
7388
}
7489
}
7590

7691
impl std::fmt::Display for Filter {
7792
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
93+
if let Some(active) = self.active {
94+
write!(f, "active={active} ")?;
95+
}
96+
7897
if !self.source.is_empty() {
7998
write!(f, "source={} ", self.source)?;
8099
}

front/src/location.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ impl Location {
2424
self.history.location().path().to_string()
2525
}
2626

27+
#[must_use]
28+
pub fn active(&self) -> Option<bool> {
29+
self.param("active").parse().ok()
30+
}
31+
2732
#[must_use]
2833
pub fn q(&self) -> String {
2934
self.param("q")

0 commit comments

Comments
 (0)