Show full subreddit results in search

This commit is contained in:
spikecodes 2021-11-23 22:24:23 -08:00
parent 85ae7c1f60
commit 8e332b0630
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
5 changed files with 52 additions and 32 deletions

2
Cargo.lock generated
View File

@ -608,7 +608,7 @@ checksum = "8521a1b57e76b1ec69af7599e75e38e7b7fad6610f037db8c79b127201b5d119"
[[package]]
name = "libreddit"
version = "0.18.2"
version = "0.19.0"
dependencies = [
"askama",
"async-recursion",

View File

@ -3,7 +3,7 @@ name = "libreddit"
description = " Alternative private front-end to Reddit"
license = "AGPL-3.0"
repository = "https://github.com/spikecodes/libreddit"
version = "0.18.2"
version = "0.19.0"
authors = ["spikecodes <19519553+spikecodes@users.noreply.github.com>"]
edition = "2018"

View File

@ -16,6 +16,7 @@ struct SearchParams {
before: String,
after: String,
restrict_sr: String,
typed: String,
}
// STRUCTS
@ -55,10 +56,12 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
return Ok(random);
}
let typed = param(&path, "type").unwrap_or_default();
let sort = param(&path, "sort").unwrap_or_else(|| "relevance".to_string());
// If search is not restricted to this subreddit, show other subreddits in search results
let subreddits = param(&path, "restrict_sr").map_or(search_subreddits(&query).await, |_| Vec::new());
let subreddits = param(&path, "restrict_sr").map_or(search_subreddits(&query, &typed).await, |_| Vec::new());
let url = String::from(req.uri().path_and_query().map_or("", |val| val.as_str()));
@ -74,6 +77,7 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
before: param(&path, "after").unwrap_or_default(),
after,
restrict_sr: param(&path, "restrict_sr").unwrap_or_default(),
typed,
},
prefs: Preferences::new(req),
url,
@ -89,8 +93,9 @@ pub async fn find(req: Request<Body>) -> Result<Response<Body>, String> {
}
}
async fn search_subreddits(q: &str) -> Vec<Subreddit> {
let subreddit_search_path = format!("/subreddits/search.json?q={}&limit=3", q.replace(' ', "+"));
async fn search_subreddits(q: &str, typed: &str) -> Vec<Subreddit> {
let limit = if typed == "sr_user" { "50" } else { "3" };
let subreddit_search_path = format!("/subreddits/search.json?q={}&limit={}", q.replace(' ', "+"), limit);
// Send a request to the url
json(subreddit_search_path, false).await.unwrap_or_default()["data"]["children"]
@ -101,9 +106,7 @@ async fn search_subreddits(q: &str) -> Vec<Subreddit> {
.map(|subreddit| {
// For each subreddit from subreddit list
// Fetch subreddit icon either from the community_icon or icon_img value
let icon = subreddit["data"]["community_icon"]
.as_str()
.map_or_else(|| val(subreddit, "icon_img"), ToString::to_string);
let icon = subreddit["data"]["community_icon"].as_str().map_or_else(|| val(subreddit, "icon_img"), ToString::to_string);
Subreddit {
name: val(subreddit, "display_name_prefixed"),

View File

@ -658,6 +658,13 @@ a.search_subreddit:hover {
opacity: 0.5;
}
#more_subreddits {
justify-content: center;
color: var(--accent);
font-weight: 600;
text-align: center;
}
/* Post */
.sep {

View File

@ -17,6 +17,7 @@
<label for="restrict_sr" class="search_label">in r/{{ sub }}</label>
</div>
{% endif %}
{% if params.typed == "sr_user" %}<input type="hidden" name="type" value="sr_user">{% endif %}
<select id="sort_options" name="sort" title="Sort results by">
{% call utils::options(params.sort, ["relevance", "hot", "top", "new", "comments"], "") %}
</select>{% if params.sort != "new" %}<select id="timeframe" name="t" title="Timeframe">
@ -30,8 +31,11 @@
</button>
</form>
{% if subreddits.len() > 0 %}
{% if subreddits.len() > 0 || params.typed == "sr_user" %}
<div id="search_subreddits">
{% if params.typed == "sr_user" %}
<a href="?q={{ params.q }}&sort={{ params.sort }}&t={{ params.t }}" class="search_subreddit" id="more_subreddits">← Back to post/comment results</a>
{% endif %}
{% for subreddit in subreddits %}
<a href="{{ subreddit.url }}" class="search_subreddit">
<div class="search_subreddit_left">{% if subreddit.icon != "" %}<img loading="lazy" src="{{ subreddit.icon }}" alt="r/{{ subreddit.name }} icon">{% endif %}</div>
@ -45,10 +49,13 @@
</div>
</a>
{% endfor %}
{% if params.typed != "sr_user" %}
<a href="?q={{ params.q }}&sort={{ params.sort }}&t={{ params.t }}&type=sr_user" class="search_subreddit" id="more_subreddits">More subreddit results →</a>
{% endif %}
</div>
{% endif %}
{% if params.typed != "sr_user" %}
{% for post in posts %}
{% if post.flags.nsfw && prefs.show_nsfw != "on" %}
{% else if post.title != "Comment" %}
{% call utils::post_in_list(post) %}
@ -68,11 +75,13 @@
</div>
{% endif %}
{% endfor %}
{% endif %}
{% if prefs.use_hls == "on" %}
<script src="/hls.min.js"></script>
<script src="/playHLSVideo.js"></script>
{% endif %}
{% if params.typed != "sr_user" %}
<footer>
{% if params.before != "" %}
<a href="?q={{ params.q }}&restrict_sr={{ params.restrict_sr }}
@ -86,5 +95,6 @@
&after={{ params.after }}">NEXT</a>
{% endif %}
</footer>
{% endif %}
</div>
{% endblock %}