Refactor subscriptions

This commit is contained in:
spikecodes 2021-01-30 18:10:38 -08:00
parent d62a3ab86b
commit 4ae2191392
No known key found for this signature in database
GPG Key ID: 004CECFF9B463BCB

View File

@ -28,7 +28,7 @@ struct WikiTemplate {
pub async fn page(req: HttpRequest) -> HttpResponse { pub async fn page(req: HttpRequest) -> HttpResponse {
let path = format!("{}.json?{}", req.path(), req.query_string()); let path = format!("{}.json?{}", req.path(), req.query_string());
let default = cookie(&req, "front_page"); let default = cookie(&req, "front_page");
let sub_name = req let sub = req
.match_info() .match_info()
.get("sub") .get("sub")
.unwrap_or(if default.is_empty() { "popular" } else { default.as_str() }) .unwrap_or(if default.is_empty() { "popular" } else { default.as_str() })
@ -38,11 +38,11 @@ pub async fn page(req: HttpRequest) -> HttpResponse {
match fetch_posts(&path, String::new()).await { match fetch_posts(&path, String::new()).await {
Ok((posts, after)) => { Ok((posts, after)) => {
// If you can get subreddit posts, also request subreddit metadata // If you can get subreddit posts, also request subreddit metadata
let sub = if !sub_name.contains('+') && sub_name != "popular" && sub_name != "all" { let sub = if !sub.contains('+') && sub != "popular" && sub != "all" {
subreddit(&sub_name).await.unwrap_or_default() subreddit(&sub).await.unwrap_or_default()
} else if sub_name.contains('+') { } else if sub.contains('+') {
Subreddit { Subreddit {
name: sub_name, name: sub,
..Subreddit::default() ..Subreddit::default()
} }
} else { } else {
@ -67,23 +67,17 @@ pub async fn page(req: HttpRequest) -> HttpResponse {
// Sub or unsub by setting subscription cookie using response "Set-Cookie" header // Sub or unsub by setting subscription cookie using response "Set-Cookie" header
pub async fn subscriptions(req: HttpRequest) -> HttpResponse { pub async fn subscriptions(req: HttpRequest) -> HttpResponse {
let mut res = HttpResponse::Found(); let mut res = HttpResponse::Found();
let default = cookie(&req, "front_page");
let sub = req
.match_info()
.get("sub")
.unwrap_or(if default.is_empty() { "popular" } else { default.as_str() });
let sub_name = sub.to_string();
let action = req.match_info().get("action").unwrap().to_string();
let sub = req.match_info().get("sub").unwrap_or_default().to_string();
let action = req.match_info().get("action").unwrap_or_default().to_string();
let mut sub_list = prefs(req.to_owned()).subs; let mut sub_list = prefs(req.to_owned()).subs;
// Modify sub list based on action // Modify sub list based on action
if action == "subscribe" && !sub_list.contains(&sub_name) { if action == "subscribe" && !sub_list.contains(&sub) {
sub_list.push(sub_name); sub_list.push(sub.to_owned());
sub_list.sort(); sub_list.sort();
} else if action == "unsubscribe" { } else if action == "unsubscribe" {
sub_list.retain(|s| s != &sub_name); sub_list.retain(|s| s != &sub);
} }
// Delete cookie if empty, else set // Delete cookie if empty, else set