Basic subscribe functionality.

This commit is contained in:
Matthew Crossman 2021-01-30 18:00:00 +11:00
parent 75bbcefbec
commit 345308a9ac
No known key found for this signature in database
GPG Key ID: C6B942B019794CC2
5 changed files with 53 additions and 11 deletions

View File

@ -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}")

View File

@ -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 <a href=\"{0}\">{0}</a>...", 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();

View File

@ -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(),
}
}

View File

@ -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);

View File

@ -125,13 +125,15 @@
<div>{{ sub.members }}</div>
<div>{{ sub.active }}</div>
</div>
<div id="sub_subscription">
{% if prefs.subs.contains(sub.name) %}
<a class="subscribe remove">Unsubscribe</a>
{% else %}
<a class="subscribe add">Subscribe</a>
{% endif %}
</div>
{% if prefs.subs.contains(sub.name) %}
<form id="sub_subscription" action="/r/{{ sub.name }}/unsubscribe" method="POST">
<input class="subscribe remove" type="submit" value="Unsubscribe">
</form>
{% else %}
<form id="sub_subscription" action="/r/{{ sub.name }}/subscribe" method="POST">
<input class="subscribe add" type="submit" value="Subscribe">
</form>
{% endif %}
</div>
</div>
<details class="panel" id="sidebar">