Skip to content

Commit 1deace1

Browse files
committed
Make clean urls an optional setting/config
1 parent 94c5911 commit 1deace1

File tree

9 files changed

+46
-15
lines changed

9 files changed

+46
-15
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,5 @@ REDLIB_DEFAULT_DISABLE_VISIT_REDDIT_CONFIRMATION=off
5050
REDLIB_DEFAULT_HIDE_SCORE=off
5151
# Enable fixed navbar by default
5252
REDLIB_DEFAULT_FIXED_NAVBAR=on
53+
# Enable tracking url removal
54+
REDLIB_DEFAULT_CLEAN_URLS=off

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
- [Docker](#docker)
3535
- [Docker Compose](#docker-compose)
3636
- [Docker CLI](#docker-cli)
37-
- Podman
37+
- Podman
3838
- Quadlets
3939

4040
- [Binary](#binary)
@@ -216,7 +216,7 @@ Stream logs from the Redlib container:
216216
```bash
217217
docker logs -f redlib
218218
```
219-
## Podman
219+
## Podman
220220

221221
[Podman](https://podman.io/) lets you run containerized applications in a rootless fashion. Containers are loosely isolated environments that are lightweight and contain everything needed to run the application, so there's no need to rely on what's installed on the host.
222222

@@ -225,8 +225,8 @@ Container images for Redlib are available at [quay.io](https://quay.io/repositor
225225
### Quadlets
226226

227227
> [!IMPORTANT]
228-
> These instructions assume that you are on a systemd based distro with [podman](https://podman.io/). If not, follow these [instructions on podman's website](https://podman.io/docs/installation) for how to do so.
229-
> It also assumes you have used `loginctl enable-linger <username>` to enable the service to start for your user without logging in.
228+
> These instructions assume that you are on a systemd based distro with [podman](https://podman.io/). If not, follow these [instructions on podman's website](https://podman.io/docs/installation) for how to do so.
229+
> It also assumes you have used `loginctl enable-linger <username>` to enable the service to start for your user without logging in.
230230
231231
Copy the `redlib.container` and `.env.example` files to `.config/containers/systemd/` and modify any relevant values (for example, the ports Redlib should listen on, renaming the .env file and editing its values, etc.).
232232

@@ -244,7 +244,7 @@ systemctl --user start redlib.service
244244
```
245245

246246
You can check the status of your container by using the following command:
247-
```bash
247+
```bash
248248
systemctl --user status redlib.service
249249
```
250250

@@ -441,4 +441,5 @@ Assign a default value for each user-modifiable setting by passing environment v
441441
| `HIDE_SCORE` | `["on", "off"]` | `off` |
442442
| `HIDE_SIDEBAR_AND_SUMMARY` | `["on", "off"]` | `off` |
443443
| `FIXED_NAVBAR` | `["on", "off"]` | `on` |
444-
| `REMOVE_DEFAULT_FEEDS` | `["on", "off"]` | `off` |
444+
| `REMOVE_DEFAULT_FEEDS` | `["on", "off"]` | `off` |
445+
| `CLEAN_URLS` | `["on", "off"]` | `off` |

app.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@
7979
},
8080
"REDLIB_DEFAULT_REMOVE_DEFAULT_FEEDS": {
8181
"required": false
82+
},
83+
"REDLIB_DEFAULT_CLEAN_URLS": {
84+
"required": false
8285
}
8386
}
8487
}

src/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ pub struct Config {
112112

113113
#[serde(rename = "REDLIB_DEFAULT_REMOVE_DEFAULT_FEEDS")]
114114
pub(crate) default_remove_default_feeds: Option<String>,
115+
116+
#[serde(rename = "REDLIB_DEFAULT_CLEAN_URLS")]
117+
pub(crate) default_clean_urls: Option<String>,
115118
}
116119

117120
impl Config {
@@ -160,6 +163,7 @@ impl Config {
160163
enable_rss: parse("REDLIB_ENABLE_RSS"),
161164
full_url: parse("REDLIB_FULL_URL"),
162165
default_remove_default_feeds: parse("REDLIB_DEFAULT_REMOVE_DEFAULT_FEEDS"),
166+
default_clean_urls: parse("REDLIB_DEFAULT_CLEAN_URLS"),
163167
}
164168
}
165169
}
@@ -190,6 +194,7 @@ fn get_setting_from_config(name: &str, config: &Config) -> Option<String> {
190194
"REDLIB_ENABLE_RSS" => config.enable_rss.clone(),
191195
"REDLIB_FULL_URL" => config.full_url.clone(),
192196
"REDLIB_DEFAULT_REMOVE_DEFAULT_FEEDS" => config.default_remove_default_feeds.clone(),
197+
"REDLIB_DEFAULT_CLEAN_URLS" => config.default_clean_urls.clone(),
193198
_ => None,
194199
}
195200
}

src/instance_info.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ impl InstanceInfo {
151151
["Hide HLS notification", &convert(&self.config.default_hide_hls_notification)],
152152
["Subscriptions", &convert(&self.config.default_subscriptions)],
153153
["Filters", &convert(&self.config.default_filters)],
154+
["Clean URLs", &convert(&self.config.default_clean_urls)],
154155
])
155156
.with_header_row(["Default preferences"]),
156157
);
@@ -186,6 +187,7 @@ impl InstanceInfo {
186187
Default blur NSFW: {:?}\n
187188
Default use HLS: {:?}\n
188189
Default hide HLS notification: {:?}\n
190+
Default clean urls: {:?}\n
189191
Default subscriptions: {:?}\n
190192
Default filters: {:?}\n",
191193
self.package_name,
@@ -213,6 +215,7 @@ impl InstanceInfo {
213215
self.config.default_blur_nsfw,
214216
self.config.default_use_hls,
215217
self.config.default_hide_hls_notification,
218+
self.config.default_clean_urls,
216219
self.config.default_subscriptions,
217220
self.config.default_filters,
218221
)

src/post.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use crate::config::get_setting;
66
use crate::server::RequestExt;
77
use crate::subreddit::{can_access_quarantine, quarantine};
88
use crate::utils::{
9-
error, format_num, get_filters, nsfw_landing, param, parse_post, rewrite_emotes, setting, template, time, val, Author, Awards, Comment, Flair, FlairPart, Post, Preferences,
9+
clean_url, error, format_num, get_filters, nsfw_landing, param, parse_post, rewrite_emotes, setting, template, time, val, Author, Awards, Comment, Flair, FlairPart, Post,
10+
Preferences,
1011
};
1112
use hyper::{Body, Request, Response};
1213

@@ -64,7 +65,12 @@ pub async fn item(req: Request<Body>) -> Result<Response<Body>, String> {
6465
// Otherwise, grab the JSON output from the request
6566
Ok(response) => {
6667
// Parse the JSON into Post and Comment structs
67-
let post = parse_post(&response[0]["data"]["children"][0]).await;
68+
let mut post = parse_post(&response[0]["data"]["children"][0]).await;
69+
70+
let clean_urls = setting(&req, "clean_urls");
71+
if clean_urls == "on".to_owned() {
72+
post.media.url = clean_url(post.media.url);
73+
}
6874

6975
let req_url = req.uri().to_string();
7076
// Return landing page if this post if this Reddit deems this post

src/settings.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct SettingsTemplate {
2424

2525
// CONSTANTS
2626

27-
const PREFS: [&str; 19] = [
27+
const PREFS: [&str; 20] = [
2828
"theme",
2929
"front_page",
3030
"layout",
@@ -44,6 +44,7 @@ const PREFS: [&str; 19] = [
4444
"disable_visit_reddit_confirmation",
4545
"video_quality",
4646
"remove_default_feeds",
47+
"clean_urls",
4748
];
4849

4950
// FUNCTIONS

src/utils.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ impl Media {
271271
(
272272
post_type.to_string(),
273273
Self {
274-
url: format_url(clean_url(url_val.as_str().unwrap_or_default()).as_str()),
274+
url: format_url(url_val.as_str().unwrap_or_default()),
275275
alt_url,
276276
// Note: in the data["is_reddit_media_domain"] path above
277277
// width and height will be 0.
@@ -672,6 +672,8 @@ pub struct Preferences {
672672
pub hide_score: String,
673673
#[revision(start = 1)]
674674
pub remove_default_feeds: String,
675+
#[revision(start = 1)]
676+
pub clean_urls: String,
675677
}
676678

677679
fn serialize_vec_with_plus<S>(vec: &[String], serializer: S) -> Result<S::Ok, S::Error>
@@ -730,6 +732,7 @@ impl Preferences {
730732
hide_awards: setting(req, "hide_awards"),
731733
hide_score: setting(req, "hide_score"),
732734
remove_default_feeds: setting(req, "remove_default_feeds"),
735+
clean_urls: setting(req, "clean_urls"),
733736
}
734737
}
735738

@@ -1080,15 +1083,15 @@ pub fn format_url(url: &str) -> String {
10801083
// Remove tracking query params
10811084
static URL_CLEANER: Lazy<Mutex<UrlCleaner>> = Lazy::new(|| Mutex::new(UrlCleaner::from_embedded_rules().expect("Failed to initialize UrlCleaner")));
10821085

1083-
pub fn clean_url(url: &str) -> String {
1084-
let is_external_url = match Url::parse(url) {
1086+
pub fn clean_url(url: String) -> String {
1087+
let is_external_url = match Url::parse(url.as_str()) {
10851088
Ok(parsed_url) => parsed_url.domain().is_some(),
10861089
_ => false,
10871090
};
1088-
let mut cleaned_url = url.to_owned();
1091+
let mut cleaned_url = url.clone();
10891092
if is_external_url {
10901093
let cleaner = URL_CLEANER.lock().unwrap();
1091-
cleaned_url = cleaner.clear_single_url_str(url).expect("Unable to clean the URL.").as_ref().to_owned();
1094+
cleaned_url = cleaner.clear_single_url_str(&cleaned_url.as_str()).expect("Unable to clean the URL.").as_ref().to_owned();
10921095
}
10931096
cleaned_url
10941097
}
@@ -1565,6 +1568,7 @@ mod tests {
15651568
hide_awards: "off".to_owned(),
15661569
hide_score: "off".to_owned(),
15671570
remove_default_feeds: "off".to_owned(),
1571+
clean_urls: "off".to_owned(),
15681572
};
15691573
let urlencoded = serde_urlencoded::to_string(prefs).expect("Failed to serialize Prefs");
15701574

templates/settings.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@
145145
<input type="checkbox" name="disable_visit_reddit_confirmation" {% if
146146
prefs.disable_visit_reddit_confirmation=="on" %}checked{% endif %}>
147147
</div>
148+
<div class="prefs-group">
149+
<label for="clean_urls">Remove tracking elements from URLs</label>
150+
<input type="hidden" value="off" name="clean_urls">
151+
<input type="checkbox" name="clean_urls" {% if
152+
prefs.clean_urls=="on" %}checked{% endif %}>
153+
</div>
148154
</fieldset>
149155
<input id="save" type="submit" value="Save">
150156
</div>
@@ -214,4 +220,4 @@
214220
</div>
215221
</div>
216222

217-
{% endblock %}
223+
{% endblock %}

0 commit comments

Comments
 (0)