mirror of https://github.com/spikecodes/libreddit
Add saved posts to restore settings link
This commit is contained in:
parent
08e48bbfed
commit
6c551f0d33
|
@ -57,7 +57,7 @@ pub async fn save(req: Request<Body>) -> Result<Response<Body>, String> {
|
||||||
Some(id) => {
|
Some(id) => {
|
||||||
saved_posts.push(id);
|
saved_posts.push(id);
|
||||||
response.insert_cookie(
|
response.insert_cookie(
|
||||||
Cookie::build(String::from("saved_posts"), saved_posts.join(","))
|
Cookie::build(String::from("saved_posts"), saved_posts.join("+"))
|
||||||
.path("/")
|
.path("/")
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
||||||
|
@ -89,7 +89,7 @@ pub async fn unsave(req: Request<Body>) -> Result<Response<Body>, String> {
|
||||||
saved_posts.remove(index);
|
saved_posts.remove(index);
|
||||||
}
|
}
|
||||||
response.insert_cookie(
|
response.insert_cookie(
|
||||||
Cookie::build(String::from("saved_posts"), saved_posts.join(","))
|
Cookie::build(String::from("saved_posts"), saved_posts.join("+"))
|
||||||
.path("/")
|
.path("/")
|
||||||
.http_only(true)
|
.http_only(true)
|
||||||
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
|
||||||
|
|
|
@ -2,7 +2,7 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
// CRATES
|
// CRATES
|
||||||
use crate::server::ResponseExt;
|
use crate::server::ResponseExt;
|
||||||
use crate::utils::{redirect, template, Preferences};
|
use crate::utils::{get_saved_posts, redirect, template, Preferences};
|
||||||
use askama::Template;
|
use askama::Template;
|
||||||
use cookie::Cookie;
|
use cookie::Cookie;
|
||||||
use futures_lite::StreamExt;
|
use futures_lite::StreamExt;
|
||||||
|
@ -14,6 +14,7 @@ use time::{Duration, OffsetDateTime};
|
||||||
#[template(path = "settings.html")]
|
#[template(path = "settings.html")]
|
||||||
struct SettingsTemplate {
|
struct SettingsTemplate {
|
||||||
prefs: Preferences,
|
prefs: Preferences,
|
||||||
|
saved_posts: Vec<String>,
|
||||||
url: String,
|
url: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,9 +37,11 @@ const PREFS: [&str; 10] = [
|
||||||
|
|
||||||
// Retrieve cookies from request "Cookie" header
|
// Retrieve cookies from request "Cookie" header
|
||||||
pub async fn get(req: Request<Body>) -> Result<Response<Body>, String> {
|
pub async fn get(req: Request<Body>) -> Result<Response<Body>, String> {
|
||||||
|
let saved_posts = get_saved_posts(&req);
|
||||||
let url = req.uri().to_string();
|
let url = req.uri().to_string();
|
||||||
template(SettingsTemplate {
|
template(SettingsTemplate {
|
||||||
prefs: Preferences::new(req),
|
prefs: Preferences::new(req),
|
||||||
|
saved_posts,
|
||||||
url,
|
url,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -109,7 +112,7 @@ fn set_cookies_method(req: Request<Body>, remove_cookies: bool) -> Response<Body
|
||||||
|
|
||||||
let mut response = redirect(path);
|
let mut response = redirect(path);
|
||||||
|
|
||||||
for name in [PREFS.to_vec(), vec!["subscriptions", "filters"]].concat() {
|
for name in [PREFS.to_vec(), vec!["subscriptions", "filters", "saved_posts"]].concat() {
|
||||||
match form.get(name) {
|
match form.get(name) {
|
||||||
Some(value) => response.insert_cookie(
|
Some(value) => response.insert_cookie(
|
||||||
Cookie::build(name.to_owned(), value.clone())
|
Cookie::build(name.to_owned(), value.clone())
|
||||||
|
|
|
@ -713,7 +713,7 @@ pub async fn error(req: Request<Body>, msg: String) -> Result<Response<Body>, St
|
||||||
|
|
||||||
pub fn get_saved_posts(req: &Request<Body>) -> Vec<String> {
|
pub fn get_saved_posts(req: &Request<Body>) -> Vec<String> {
|
||||||
match req.cookie("saved_posts") {
|
match req.cookie("saved_posts") {
|
||||||
Some(cookie) => cookie.value().split(',').map(String::from).collect(),
|
Some(cookie) => cookie.value().split('+').map(String::from).collect(),
|
||||||
None => Vec::new(),
|
None => Vec::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -109,8 +109,8 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<div id="settings_note">
|
<div id="settings_note">
|
||||||
<p><b>Note:</b> settings and subscriptions are saved in browser cookies. Clearing your cookies will reset them.</p><br>
|
<p><b>Note:</b> settings, subscriptions and saved posts are saved in browser cookies. Clearing your cookies will reset them.</p><br>
|
||||||
<p>You can restore your current settings and subscriptions after clearing your cookies using <a href="/settings/restore/?theme={{ prefs.theme }}&front_page={{ prefs.front_page }}&layout={{ prefs.layout }}&wide={{ prefs.wide }}&comment_sort={{ prefs.comment_sort }}&show_nsfw={{ prefs.show_nsfw }}&use_hls={{ prefs.use_hls }}&hide_hls_notification={{ prefs.hide_hls_notification }}&subscriptions={{ prefs.subscriptions.join("%2B") }}&filters={{ prefs.filters.join("%2B") }}">this link</a>.</p>
|
<p>You can restore your current settings, subscriptions and saved posts after clearing your cookies using <a href="/settings/restore/?theme={{ prefs.theme }}&front_page={{ prefs.front_page }}&layout={{ prefs.layout }}&wide={{ prefs.wide }}&comment_sort={{ prefs.comment_sort }}&show_nsfw={{ prefs.show_nsfw }}&use_hls={{ prefs.use_hls }}&hide_hls_notification={{ prefs.hide_hls_notification }}&subscriptions={{ prefs.subscriptions.join("%2B") }}&filters={{ prefs.filters.join("%2B") }}&saved_posts={{ saved_posts.join("%2B") }}">this link</a>.</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue