Generate URL to restore settings, including subscriptions. Closes #89 (#116)

* Start recursive comments

* Update comment.html

* Fix move error

* Comment improvements

* Fix merge

* Remove extra endif from post.html

* Fix post.html

* Restore setting from link

* Tweak settings page

Co-authored-by: spikecodes <19519553+spikecodes@users.noreply.github.com>
This commit is contained in:
robrobinbin 2021-02-13 21:55:23 +01:00 committed by GitHub
parent ff8685ae4c
commit 93cfc713c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 76 additions and 46 deletions

View File

@ -166,6 +166,7 @@ async fn main() -> tide::Result<()> {
// Configure settings
app.at("/settings/").get(settings::get).post(settings::set);
app.at("/settings/restore/").get(settings::restore);
// Subreddit services
// See posts and info about subreddit

View File

@ -12,6 +12,7 @@ struct SettingsTemplate {
}
#[derive(serde::Deserialize, Default)]
#[serde(default)]
pub struct SettingsForm {
theme: Option<String>,
front_page: Option<String>,
@ -19,6 +20,8 @@ pub struct SettingsForm {
wide: Option<String>,
comment_sort: Option<String>,
show_nsfw: Option<String>,
redirect: Option<String>,
subscriptions: Option<String>,
}
// FUNCTIONS
@ -56,3 +59,39 @@ pub async fn set(mut req: Request<()>) -> tide::Result {
Ok(res)
}
// Set cookies using response "Set-Cookie" header
pub async fn restore(req: Request<()>) -> tide::Result {
let form: SettingsForm = req.query()?;
let path = match form.redirect {
Some(value) => format!("/{}/", value),
None => "/".to_string()
};
let mut res = Response::builder(302)
.content_type("text/html")
.header("Location", path.to_owned())
.body(format!("Redirecting to <a href=\"{0}\">{0}</a>...", path))
.build();
let names = vec!["theme", "front_page", "layout", "wide", "comment_sort", "show_nsfw", "subscriptions"];
let values = vec![form.theme, form.front_page, form.layout, form.wide, form.comment_sort, form.show_nsfw, form.subscriptions];
for (i, name) in names.iter().enumerate() {
match values.get(i) {
Some(value) => res.insert_cookie(
Cookie::build(name.to_owned(), value.to_owned().unwrap_or_default())
.path("/")
.http_only(true)
.expires(OffsetDateTime::now_utc() + Duration::weeks(52))
.finish(),
),
None => res.remove_cookie(Cookie::named(name.to_owned())),
};
}
Ok(res)
}

View File

@ -87,7 +87,7 @@ pub async fn subscriptions(req: Request<()>) -> tide::Result {
let query = req.url().query().unwrap_or_default().to_string();
let action: Vec<String> = req.url().path().split('/').map(String::from).collect();
let mut sub_list = prefs(req).subs;
let mut sub_list = prefs(req).subscriptions;
// Find each subreddit name (separated by '+') in sub parameter
for part in sub.split('+') {

View File

@ -146,7 +146,7 @@ pub struct Preferences {
pub wide: String,
pub show_nsfw: String,
pub comment_sort: String,
pub subs: Vec<String>,
pub subscriptions: Vec<String>,
}
//
@ -162,7 +162,7 @@ pub fn prefs(req: Request<()>) -> Preferences {
wide: cookie(&req, "wide"),
show_nsfw: cookie(&req, "show_nsfw"),
comment_sort: cookie(&req, "comment_sort"),
subs: cookie(&req, "subscriptions")
subscriptions: cookie(&req, "subscriptions")
.split('+')
.map(String::from)
.filter(|s| !s.is_empty())

View File

@ -958,19 +958,21 @@ a.search_subreddit:hover {
/* Settings */
#settings, #settings > form {
display: flex;
flex-direction: column;
align-items: center;
#settings {
max-width: 450px;
}
#settings_note {
font-size: 14px;
max-width: 300px;
margin-top: 10px;
opacity: 0.75;
}
#settings_note a {
color: var(--accent);
}
.prefs {
display: flex;
flex-direction: column;
@ -979,6 +981,7 @@ a.search_subreddit:hover {
padding: 20px;
background: var(--post);
border-radius: 5px;
margin-bottom: 20px;
}
.prefs > div {
@ -987,10 +990,7 @@ a.search_subreddit:hover {
width: 100%;
height: 35px;
align-items: center;
}
.prefs > div:not(:last-of-type) {
margin-bottom: 10px;
margin-top: 10px;
}
.prefs select {
@ -1017,22 +1017,6 @@ input[type="submit"] {
-moz-appearance: none;
}
#settings_subs {
list-style: none;
padding: 0;
}
#settings_subs > li {
display: flex;
margin: 10px 0;
}
#settings_subs > li:last-of-type { margin-bottom: 0; }
#settings_subs > li > span {
padding: 10px 0;
margin-right: auto;
}
#settings_subs .unsubscribe {
margin-left: 30px;
}
@ -1176,6 +1160,10 @@ td, th {
right: 10px;
min-width: auto;
}
#settings {
max-width: unset;
}
aside, #subreddit, #user {
margin: 0;

View File

@ -46,25 +46,27 @@
<label for="show_nsfw">Show NSFW posts:</label>
<input type="checkbox" name="show_nsfw" {% if prefs.show_nsfw == "on" %}checked{% endif %}>
</div>
<input id="save" type="submit" value="Save">
</div>
<p id="settings_note"><b>Note:</b> settings are saved in browser cookies. Clearing your cookie data will reset them.</p>
<input id="save" type="submit" value="Save">
</form>
{% if prefs.subs.len() > 0 %}
<aside class="prefs">
{% if prefs.subscriptions.len() > 0 %}
<div class="prefs" id="settings_subs">
<p>Subscribed Subreddits</p>
<ul id="settings_subs">
{% for sub in prefs.subs %}
<li>
<span>{{ sub }}</span>
<form action="/r/{{ sub }}/unsubscribe/?redirect=settings" method="POST">
<button class="unsubscribe">Unsubscribe</button>
</form>
</li>
{% endfor %}
</ul>
</aside>
{% for sub in prefs.subscriptions %}
<div>
<span>{{ sub }}</span>
<form action="/r/{{ sub }}/unsubscribe/?redirect=settings" method="POST">
<button class="unsubscribe">Unsubscribe</button>
</form>
</div>
{% endfor %}
</div>
{% 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 }}&subscriptions={{ prefs.subscriptions.join("%2B") }}">this link</a>.</p>
</div>
</div>
{% endblock %}

View File

@ -85,7 +85,7 @@
<div>{{ sub.active }}</div>
</div>
<div id="sub_subscription">
{% if prefs.subs.contains(sub.name) %}
{% if prefs.subscriptions.contains(sub.name) %}
<form action="/r/{{ sub.name }}/unsubscribe/" method="POST">
<button class="unsubscribe">Unsubscribe</button>
</form>

View File

@ -41,7 +41,7 @@
{%- endmacro %}
{% macro sub_list(current) -%}
{% if prefs.subs.len() > 0 %}
{% if prefs.subscriptions.len() > 0 %}
<details id="feeds">
<summary>Feeds</summary>
<div id="feed_list">
@ -50,7 +50,7 @@
<a href="/r/popular">Popular</a>
<a href="/r/all">All</a>
<p>REDDIT FEEDS</p>
{% for sub in prefs.subs %}
{% for sub in prefs.subscriptions %}
<a href="/r/{{ sub }}" {% if sub == current %}class="selected"{% endif %}>{{ sub }}</a>
{% endfor %}
</div>