Skip to content

Commit d7ec07c

Browse files
authored
Implement a serializer for user preferences (redlib-org#336)
1 parent e4fc22c commit d7ec07c

4 files changed

Lines changed: 70 additions & 5 deletions

File tree

Cargo.lock

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ async-recursion = "1.1.1"
5050
common-words-all = { version = "0.0.2", default-features = false, features = ["english", "one"] }
5151
hyper-rustls = { version = "0.24.2", features = [ "http2" ] }
5252
tegen = "0.1.4"
53+
serde_urlencoded = "0.7.1"
5354

5455

5556
[dev-dependencies]

src/utils.rs

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use once_cell::sync::Lazy;
1313
use regex::Regex;
1414
use rinja::Template;
1515
use rust_embed::RustEmbed;
16-
use serde::Serialize;
16+
use serde::{Serialize, Serializer};
1717
use serde_json::Value;
1818
use serde_json_path::{JsonPath, JsonPathExt};
1919
use std::collections::{HashMap, HashSet};
@@ -601,8 +601,9 @@ pub struct Params {
601601
pub before: Option<String>,
602602
}
603603

604-
#[derive(Default)]
604+
#[derive(Default, Serialize)]
605605
pub struct Preferences {
606+
#[serde(skip)]
606607
pub available_themes: Vec<String>,
607608
pub theme: String,
608609
pub front_page: String,
@@ -620,12 +621,21 @@ pub struct Preferences {
620621
pub disable_visit_reddit_confirmation: String,
621622
pub comment_sort: String,
622623
pub post_sort: String,
624+
#[serde(serialize_with = "serialize_vec_with_plus")]
623625
pub subscriptions: Vec<String>,
626+
#[serde(serialize_with = "serialize_vec_with_plus")]
624627
pub filters: Vec<String>,
625628
pub hide_awards: String,
626629
pub hide_score: String,
627630
}
628631

632+
fn serialize_vec_with_plus<S>(vec: &Vec<String>, serializer: S) -> Result<S::Ok, S::Error>
633+
where
634+
S: Serializer,
635+
{
636+
serializer.serialize_str(&vec.join("+"))
637+
}
638+
629639
#[derive(RustEmbed)]
630640
#[folder = "static/themes/"]
631641
#[include = "*.css"]
@@ -665,6 +675,10 @@ impl Preferences {
665675
hide_score: setting(req, "hide_score"),
666676
}
667677
}
678+
679+
pub fn to_urlencoded(&self) -> Result<String, String> {
680+
serde_urlencoded::to_string(self).map_err(|e| e.to_string())
681+
}
668682
}
669683

670684
/// Gets a `HashSet` of filters from the cookie in the given `Request`.
@@ -1277,7 +1291,7 @@ pub fn get_post_url(post: &Post) -> String {
12771291

12781292
#[cfg(test)]
12791293
mod tests {
1280-
use super::{format_num, format_url, rewrite_urls};
1294+
use super::{format_num, format_url, rewrite_urls, Preferences};
12811295

12821296
#[test]
12831297
fn format_num_works() {
@@ -1344,6 +1358,35 @@ mod tests {
13441358
assert_eq!(format_url("nsfw"), "");
13451359
assert_eq!(format_url("spoiler"), "");
13461360
}
1361+
#[test]
1362+
fn serialize_prefs() {
1363+
let prefs = Preferences {
1364+
available_themes: vec![],
1365+
theme: "laserwave".to_owned(),
1366+
front_page: "default".to_owned(),
1367+
layout: "compact".to_owned(),
1368+
wide: "on".to_owned(),
1369+
blur_spoiler: "on".to_owned(),
1370+
show_nsfw: "off".to_owned(),
1371+
blur_nsfw: "on".to_owned(),
1372+
hide_hls_notification: "off".to_owned(),
1373+
video_quality: "best".to_owned(),
1374+
hide_sidebar_and_summary: "off".to_owned(),
1375+
use_hls: "on".to_owned(),
1376+
autoplay_videos: "on".to_owned(),
1377+
fixed_navbar: "on".to_owned(),
1378+
disable_visit_reddit_confirmation: "on".to_owned(),
1379+
comment_sort: "confidence".to_owned(),
1380+
post_sort: "top".to_owned(),
1381+
subscriptions: vec!["memes".to_owned(), "mildlyinteresting".to_owned()],
1382+
filters: vec![],
1383+
hide_awards: "off".to_owned(),
1384+
hide_score: "off".to_owned(),
1385+
};
1386+
let urlencoded = serde_urlencoded::to_string(prefs).expect("Failed to serialize Prefs");
1387+
1388+
assert_eq!(urlencoded, "theme=laserwave&front_page=default&layout=compact&wide=on&blur_spoiler=on&show_nsfw=off&blur_nsfw=on&hide_hls_notification=off&video_quality=best&hide_sidebar_and_summary=off&use_hls=on&autoplay_videos=on&fixed_navbar=on&disable_visit_reddit_confirmation=on&comment_sort=confidence&post_sort=top&subscriptions=memes%2Bmildlyinteresting&filters=&hide_awards=off&hide_score=off")
1389+
}
13471390
}
13481391

13491392
#[test]

templates/settings.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,16 @@
161161
{% endif %}
162162

163163
<div id="settings_note">
164-
<p><b>Note:</b> settings and subscriptions are saved in browser cookies. Clearing your cookies will reset them.</p><br>
165-
<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 }}&post_sort={{ prefs.post_sort }}&comment_sort={{ prefs.comment_sort }}&show_nsfw={{ prefs.show_nsfw }}&use_hls={{ prefs.use_hls }}&hide_hls_notification={{ prefs.hide_hls_notification }}&hide_awards={{ prefs.hide_awards }}&fixed_navbar={{ prefs.fixed_navbar }}&subscriptions={{ prefs.subscriptions.join("%2B") }}&filters={{ prefs.filters.join("%2B") }}">this link</a>.</p>
164+
<p><b>Note:</b> settings and subscriptions are saved in browser cookies. Clearing your cookies will reset them.</p>
165+
<br>
166+
{% match prefs.to_urlencoded() %}
167+
{% when Ok with (encoded_prefs) %}
168+
<p>You can restore your current settings and subscriptions after clearing your cookies using <a
169+
href="/settings/restore/?{{ encoded_prefs }}">this link</a>.</p>
170+
{% when Err with (err) %}
171+
<p>There was an error creating your restore link: {{ err }}</p>
172+
<p>Please report this issue</p>
173+
{% endmatch %}
166174
</div>
167175
</div>
168176

0 commit comments

Comments
 (0)