Add basic unsubscribe.
This commit is contained in:
parent
345308a9ac
commit
c71df35b22
@ -100,7 +100,7 @@ async fn main() -> std::io::Result<()> {
|
|||||||
.route("/", web::get().to(subreddit::page))
|
.route("/", web::get().to(subreddit::page))
|
||||||
.route("/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
|
.route("/{sort:hot|new|top|rising|controversial}/", web::get().to(subreddit::page))
|
||||||
// Handle subscribe/unsubscribe
|
// Handle subscribe/unsubscribe
|
||||||
.route("/subscribe/", web::post().to(subreddit::subscribe))
|
.route("/{action:subscribe|unsubscribe}/", web::post().to(subreddit::set))
|
||||||
//.route("/unsubscribe/", web::post().to(subreddit::unsubscribe))
|
//.route("/unsubscribe/", web::post().to(subreddit::unsubscribe))
|
||||||
// View post on subreddit
|
// View post on subreddit
|
||||||
.service(
|
.service(
|
||||||
|
@ -64,8 +64,8 @@ pub async fn page(req: HttpRequest) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Subscribe by setting subscription cookie using response "Set-Cookie" header
|
// Sub or unsub by setting subscription cookie using response "Set-Cookie" header
|
||||||
pub async fn subscribe(req: HttpRequest) -> HttpResponse {
|
pub async fn set(req: HttpRequest) -> HttpResponse {
|
||||||
let mut res = HttpResponse::Found();
|
let mut res = HttpResponse::Found();
|
||||||
let default = cookie(&req, "front_page");
|
let default = cookie(&req, "front_page");
|
||||||
let sub = req
|
let sub = req
|
||||||
@ -74,26 +74,36 @@ pub async fn subscribe(req: HttpRequest) -> HttpResponse {
|
|||||||
.unwrap_or(if default.is_empty() { "popular" } else { default.as_str() });
|
.unwrap_or(if default.is_empty() { "popular" } else { default.as_str() });
|
||||||
let sub_name = sub.to_string();
|
let sub_name = sub.to_string();
|
||||||
|
|
||||||
|
let action = req.match_info().get("action").unwrap().to_string();
|
||||||
|
|
||||||
let mut sub_list = prefs(req.to_owned()).subs;
|
let mut sub_list = prefs(req.to_owned()).subs;
|
||||||
|
|
||||||
println!("sub_list len: {}", sub_list.join(","));
|
// Modify sub list based on action
|
||||||
|
if action == "subscribe" {
|
||||||
if sub_list.len() == 0 {
|
if sub_list.is_empty() {
|
||||||
sub_list = Vec::new();
|
sub_list = Vec::new();
|
||||||
sub_list.push(sub_name);
|
sub_list.push(sub_name);
|
||||||
} else if !sub_list.contains(&sub_name) {
|
} else if !sub_list.contains(&sub_name) {
|
||||||
sub_list.push(sub_name);
|
sub_list.push(sub_name);
|
||||||
sub_list.sort();
|
sub_list.sort();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sub_list.retain(|s| s != &sub_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
res.cookie(Cookie::build("subscriptions", sub_list.join(","))
|
// Delete cookie if empty, else set
|
||||||
.path("/")
|
if sub_list.is_empty() {
|
||||||
.http_only(true)
|
res.del_cookie(&Cookie::named("subscriptions"));
|
||||||
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
} else {
|
||||||
.finish(),);
|
res.cookie(Cookie::build("subscriptions", sub_list.join(","))
|
||||||
|
.path("/")
|
||||||
|
.http_only(true)
|
||||||
|
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
||||||
|
.finish(),);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Redirect back to subreddit
|
||||||
let path = format!("/r/{}", sub);
|
let path = format!("/r/{}", sub);
|
||||||
|
|
||||||
res
|
res
|
||||||
.content_type("text/html")
|
.content_type("text/html")
|
||||||
.set_header("Location", path.to_string())
|
.set_header("Location", path.to_string())
|
||||||
|
Loading…
Reference in New Issue
Block a user