Split subscription names by +

This commit is contained in:
spikecodes 2021-02-09 21:56:38 -08:00
parent 8785bc95f5
commit fee2cb1b56
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB
2 changed files with 11 additions and 5 deletions

View File

@ -89,11 +89,17 @@ pub async fn subscriptions(req: Request<()>) -> tide::Result {
let mut sub_list = prefs(req).subs;
// Find each subreddit name (separated by '+') in sub parameter
let split = sub.split('+');
// Modify sub list based on action
if action.contains(&"subscribe".to_string()) && !sub_list.contains(&sub) {
sub_list.push(sub.to_owned());
// Add each sub name to the subscribed list
split.map(|part| sub_list.push(part.to_owned())).min();
// Reorder sub names alphabettically
sub_list.sort_by_key(|a| a.to_lowercase())
} else if action.contains(&"unsubscribe".to_string()) {
// Remove sub name from subscribed list
sub_list.retain(|s| s != &sub);
}
@ -137,7 +143,7 @@ pub async fn wiki(req: Request<()>) -> tide::Result {
match request(path).await {
Ok(res) => template(WikiTemplate {
sub,
wiki: rewrite_url(res["data"]["content_html"].as_str().unwrap_or_default()),
wiki: rewrite_urls(res["data"]["content_html"].as_str().unwrap_or_default()),
page,
prefs: prefs(req),
}),
@ -172,7 +178,7 @@ async fn subreddit(sub: &str) -> Result<Subreddit, String> {
name: val(&res, "display_name"),
title: val(&res, "title"),
description: val(&res, "public_description"),
info: rewrite_url(&val(&res, "description_html").replace("\\", "")),
info: rewrite_urls(&val(&res, "description_html").replace("\\", "")),
icon: format_url(icon.as_str()),
members: format_num(members),
active: format_num(active),

View File

@ -192,7 +192,7 @@ pub fn format_url(url: &str) -> String {
}
// Rewrite Reddit links to Libreddit in body of text
pub fn rewrite_url(text: &str) -> String {
pub fn rewrite_urls(text: &str) -> String {
let re = Regex::new(r#"href="(https://|http://|)(www.|old.|np.|)(reddit).(com)/"#).unwrap();
re.replace_all(text, r#"href="/"#).to_string()
}
@ -369,7 +369,7 @@ pub async fn fetch_posts(path: &str, fallback_title: String) -> Result<(Vec<Post
id: val(post, "id"),
title: if title.is_empty() { fallback_title.to_owned() } else { title },
community: val(post, "subreddit"),
body: rewrite_url(&val(post, "body_html")),
body: rewrite_urls(&val(post, "body_html")),
author: Author {
name: val(post, "author"),
flair: Flair {