diff --git a/src/main.rs b/src/main.rs index 40f828c..6831563 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,6 +99,9 @@ async fn main() -> std::io::Result<()> { // See posts and info about subreddit .route("/", web::get().to(subreddit::page)) .route("/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page)) + // Handle subscribe/unsubscribe + .route("/subscribe/", web::post().to(subreddit::subscribe)) + //.route("/unsubscribe/", web::post().to(subreddit::unsubscribe)) // View post on subreddit .service( web::scope("/comments/{id}/{title}") diff --git a/src/subreddit.rs b/src/subreddit.rs index 8f4880a..3272a83 100644 --- a/src/subreddit.rs +++ b/src/subreddit.rs @@ -1,7 +1,8 @@ // CRATES use crate::utils::*; -use actix_web::{HttpRequest, HttpResponse, Result}; +use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result}; use askama::Template; +use time::{Duration, OffsetDateTime}; // STRUCTS #[derive(Template)] @@ -63,6 +64,42 @@ pub async fn page(req: HttpRequest) -> HttpResponse { } } +// Subscribe by setting subscription cookie using response "Set-Cookie" header +pub async fn subscribe(req: HttpRequest) -> HttpResponse { + 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 mut sub_list = prefs(req.to_owned()).subs; + + println!("sub_list len: {}", sub_list.join(",")); + + if sub_list.len() == 0 { + sub_list = Vec::new(); + sub_list.push(sub_name); + } else if !sub_list.contains(&sub_name) { + sub_list.push(sub_name); + sub_list.sort(); + } + + res.cookie(Cookie::build("subscriptions", sub_list.join(",")) + .path("/") + .http_only(true) + .expires(OffsetDateTime::now_utc() + Duration::weeks(52)) + .finish(),); + + let path = format!("/r/{}", sub); + + res + .content_type("text/html") + .set_header("Location", path.to_string()) + .body(format!("Redirecting to {0}...", path.to_string())) +} + pub async fn wiki(req: HttpRequest) -> HttpResponse { let sub = req.match_info().get("sub").unwrap_or("reddit.com").to_string(); let page = req.match_info().get("page").unwrap_or("index").to_string(); diff --git a/src/utils.rs b/src/utils.rs index 8b1df82..a8c8373 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -145,7 +145,7 @@ pub fn prefs(req: HttpRequest) -> Preferences { wide: cookie(&req, "wide"), hide_nsfw: cookie(&req, "hide_nsfw"), comment_sort: cookie(&req, "comment_sort"), - subs: cookie(&req, "subreddits").split(",").map(|s| s.to_string()).collect(), + subs: cookie(&req, "subscriptions").split(",").map(|s| s.to_string()).filter(|s| s != "").collect(), } } diff --git a/static/style.css b/static/style.css index 7452fce..228a33b 100644 --- a/static/style.css +++ b/static/style.css @@ -236,10 +236,10 @@ aside { /* Subscriptions/Favorites */ #sub_subscription { - margin-top: 30px; + margin-top: 20px; } -#sub_subscription > a { +#sub_subscription > input { padding: 10px 20px; border-radius: 5px; color: var(--foreground); diff --git a/templates/subreddit.html b/templates/subreddit.html index eb71961..2617b43 100644 --- a/templates/subreddit.html +++ b/templates/subreddit.html @@ -125,13 +125,15 @@
{{ sub.members }}
{{ sub.active }}
-
- {% if prefs.subs.contains(sub.name) %} - Unsubscribe - {% else %} - Subscribe - {% endif %} -
+ {% if prefs.subs.contains(sub.name) %} +
+ +
+ {% else %} +
+ +
+ {% endif %}