Add saved posts to restore settings link

This commit is contained in:
David Hamilton 2022-03-21 16:34:15 -07:00
parent 08e48bbfed
commit 6c551f0d33
4 changed files with 10 additions and 7 deletions

View File

@ -57,7 +57,7 @@ pub async fn save(req: Request<Body>) -> Result<Response<Body>, String> {
Some(id) => {
saved_posts.push(id);
response.insert_cookie(
Cookie::build(String::from("saved_posts"), saved_posts.join(","))
Cookie::build(String::from("saved_posts"), saved_posts.join("+"))
.path("/")
.http_only(true)
.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);
}
response.insert_cookie(
Cookie::build(String::from("saved_posts"), saved_posts.join(","))
Cookie::build(String::from("saved_posts"), saved_posts.join("+"))
.path("/")
.http_only(true)
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))

View File

@ -2,7 +2,7 @@ use std::collections::HashMap;
// CRATES
use crate::server::ResponseExt;
use crate::utils::{redirect, template, Preferences};
use crate::utils::{get_saved_posts, redirect, template, Preferences};
use askama::Template;
use cookie::Cookie;
use futures_lite::StreamExt;
@ -14,6 +14,7 @@ use time::{Duration, OffsetDateTime};
#[template(path = "settings.html")]
struct SettingsTemplate {
prefs: Preferences,
saved_posts: Vec<String>,
url: String,
}
@ -36,9 +37,11 @@ const PREFS: [&str; 10] = [
// Retrieve cookies from request "Cookie" header
pub async fn get(req: Request<Body>) -> Result<Response<Body>, String> {
let saved_posts = get_saved_posts(&req);
let url = req.uri().to_string();
template(SettingsTemplate {
prefs: Preferences::new(req),
saved_posts,
url,
})
}
@ -109,7 +112,7 @@ fn set_cookies_method(req: Request<Body>, remove_cookies: bool) -> Response<Body
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) {
Some(value) => response.insert_cookie(
Cookie::build(name.to_owned(), value.clone())

View File

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

View File

@ -109,8 +109,8 @@
{% endif %}
<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>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><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, 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>