Basic subscribe functionality.
This commit is contained in:
parent
75bbcefbec
commit
345308a9ac
@ -99,6 +99,9 @@ async fn main() -> std::io::Result<()> {
|
|||||||
// See posts and info about subreddit
|
// See posts and info about subreddit
|
||||||
.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
|
||||||
|
.route("/subscribe/", web::post().to(subreddit::subscribe))
|
||||||
|
//.route("/unsubscribe/", web::post().to(subreddit::unsubscribe))
|
||||||
// View post on subreddit
|
// View post on subreddit
|
||||||
.service(
|
.service(
|
||||||
web::scope("/comments/{id}/{title}")
|
web::scope("/comments/{id}/{title}")
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
// CRATES
|
// CRATES
|
||||||
use crate::utils::*;
|
use crate::utils::*;
|
||||||
use actix_web::{HttpRequest, HttpResponse, Result};
|
use actix_web::{cookie::Cookie, HttpRequest, HttpResponse, Result};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
|
use time::{Duration, OffsetDateTime};
|
||||||
|
|
||||||
// STRUCTS
|
// STRUCTS
|
||||||
#[derive(Template)]
|
#[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 {
|
pub async fn wiki(req: HttpRequest) -> HttpResponse {
|
||||||
let sub = req.match_info().get("sub").unwrap_or("reddit.com").to_string();
|
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();
|
let page = req.match_info().get("page").unwrap_or("index").to_string();
|
||||||
|
@ -145,7 +145,7 @@ pub fn prefs(req: HttpRequest) -> Preferences {
|
|||||||
wide: cookie(&req, "wide"),
|
wide: cookie(&req, "wide"),
|
||||||
hide_nsfw: cookie(&req, "hide_nsfw"),
|
hide_nsfw: cookie(&req, "hide_nsfw"),
|
||||||
comment_sort: cookie(&req, "comment_sort"),
|
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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -236,10 +236,10 @@ aside {
|
|||||||
/* Subscriptions/Favorites */
|
/* Subscriptions/Favorites */
|
||||||
|
|
||||||
#sub_subscription {
|
#sub_subscription {
|
||||||
margin-top: 30px;
|
margin-top: 20px;
|
||||||
}
|
}
|
||||||
|
|
||||||
#sub_subscription > a {
|
#sub_subscription > input {
|
||||||
padding: 10px 20px;
|
padding: 10px 20px;
|
||||||
border-radius: 5px;
|
border-radius: 5px;
|
||||||
color: var(--foreground);
|
color: var(--foreground);
|
||||||
|
@ -125,15 +125,17 @@
|
|||||||
<div>{{ sub.members }}</div>
|
<div>{{ sub.members }}</div>
|
||||||
<div>{{ sub.active }}</div>
|
<div>{{ sub.active }}</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="sub_subscription">
|
|
||||||
{% if prefs.subs.contains(sub.name) %}
|
{% if prefs.subs.contains(sub.name) %}
|
||||||
<a class="subscribe remove">Unsubscribe</a>
|
<form id="sub_subscription" action="/r/{{ sub.name }}/unsubscribe" method="POST">
|
||||||
|
<input class="subscribe remove" type="submit" value="Unsubscribe">
|
||||||
|
</form>
|
||||||
{% else %}
|
{% else %}
|
||||||
<a class="subscribe add">Subscribe</a>
|
<form id="sub_subscription" action="/r/{{ sub.name }}/subscribe" method="POST">
|
||||||
|
<input class="subscribe add" type="submit" value="Subscribe">
|
||||||
|
</form>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
<details class="panel" id="sidebar">
|
<details class="panel" id="sidebar">
|
||||||
<summary id="sidebar_label">Sidebar</summary>
|
<summary id="sidebar_label">Sidebar</summary>
|
||||||
<div id="sidebar_contents">{{ sub.info }}</div>
|
<div id="sidebar_contents">{{ sub.info }}</div>
|
||||||
|
Loading…
Reference in New Issue
Block a user