Skip to content

Commit 3301da1

Browse files
committed
feat: add support to blur spoiler previews
1 parent 213481e commit 3301da1

File tree

10 files changed

+34
-6
lines changed

10 files changed

+34
-6
lines changed

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ REDLIB_DEFAULT_WIDE=off
2424
REDLIB_DEFAULT_POST_SORT=hot
2525
# Set the default comment sort method (options: confidence, top, new, controversial, old)
2626
REDLIB_DEFAULT_COMMENT_SORT=confidence
27+
# Enable blurring Spoiler content by default
28+
REDLIB_DEFAULT_BLUR_SPOILER=off
2729
# Enable showing NSFW content by default
2830
REDLIB_DEFAULT_SHOW_NSFW=off
2931
# Enable blurring NSFW content by default

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ Assign a default value for each user-modifiable setting by passing environment v
394394
| `WIDE` | `["on", "off"]` | `off` |
395395
| `POST_SORT` | `["hot", "new", "top", "rising", "controversial"]` | `hot` |
396396
| `COMMENT_SORT` | `["confidence", "top", "new", "controversial", "old"]` | `confidence` |
397+
| `BLUR_SPOILER` | `["on", "off"]` | `off` |
397398
| `SHOW_NSFW` | `["on", "off"]` | `off` |
398399
| `BLUR_NSFW` | `["on", "off"]` | `off` |
399400
| `USE_HLS` | `["on", "off"]` | `off` |

app.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
"REDLIB_DEFAULT_POST_SORT": {
3030
"required": false
3131
},
32+
"REDLIB_DEFAULT_BLUR_SPOILER": {
33+
"required": false
34+
},
3235
"REDLIB_DEFAULT_SHOW_NSFW": {
3336
"required": false
3437
},

contrib/redlib.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ PORT=12345
66
#REDLIB_DEFAULT_WIDE=off
77
#REDLIB_DEFAULT_POST_SORT=hot
88
#REDLIB_DEFAULT_COMMENT_SORT=confidence
9+
#REDLIB_DEFAULT_BLUR_SPOILER=off
910
#REDLIB_DEFAULT_SHOW_NSFW=off
1011
#REDLIB_DEFAULT_BLUR_NSFW=off
1112
#REDLIB_DEFAULT_USE_HLS=off

src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ pub struct Config {
4848
#[serde(alias = "LIBREDDIT_DEFAULT_POST_SORT")]
4949
pub(crate) default_post_sort: Option<String>,
5050

51+
#[serde(rename = "REDLIB_DEFAULT_BLUR_SPOILER")]
52+
#[serde(alias = "LIBREDDIT_DEFAULT_BLUR_SPOILER")]
53+
pub(crate) default_blur_spoiler: Option<String>,
54+
5155
#[serde(rename = "REDLIB_DEFAULT_SHOW_NSFW")]
5256
#[serde(alias = "LIBREDDIT_DEFAULT_SHOW_NSFW")]
5357
pub(crate) default_show_nsfw: Option<String>,
@@ -130,6 +134,7 @@ impl Config {
130134
default_post_sort: parse("REDLIB_DEFAULT_POST_SORT"),
131135
default_wide: parse("REDLIB_DEFAULT_WIDE"),
132136
default_comment_sort: parse("REDLIB_DEFAULT_COMMENT_SORT"),
137+
default_blur_spoiler: parse("REDLIB_DEFAULT_BLUR_SPOILER"),
133138
default_show_nsfw: parse("REDLIB_DEFAULT_SHOW_NSFW"),
134139
default_blur_nsfw: parse("REDLIB_DEFAULT_BLUR_NSFW"),
135140
default_use_hls: parse("REDLIB_DEFAULT_USE_HLS"),
@@ -155,6 +160,7 @@ fn get_setting_from_config(name: &str, config: &Config) -> Option<String> {
155160
"REDLIB_DEFAULT_LAYOUT" => config.default_layout.clone(),
156161
"REDLIB_DEFAULT_COMMENT_SORT" => config.default_comment_sort.clone(),
157162
"REDLIB_DEFAULT_POST_SORT" => config.default_post_sort.clone(),
163+
"REDLIB_DEFAULT_BLUR_SPOILER" => config.default_blur_spoiler.clone(),
158164
"REDLIB_DEFAULT_SHOW_NSFW" => config.default_show_nsfw.clone(),
159165
"REDLIB_DEFAULT_BLUR_NSFW" => config.default_blur_nsfw.clone(),
160166
"REDLIB_DEFAULT_USE_HLS" => config.default_use_hls.clone(),

src/instance_info.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ impl InstanceInfo {
141141
["Wide", &convert(&self.config.default_wide)],
142142
["Comment sort", &convert(&self.config.default_comment_sort)],
143143
["Post sort", &convert(&self.config.default_post_sort)],
144+
["Blur Spoiler", &convert(&self.config.default_blur_spoiler)],
144145
["Show NSFW", &convert(&self.config.default_show_nsfw)],
145146
["Blur NSFW", &convert(&self.config.default_blur_nsfw)],
146147
["Use HLS", &convert(&self.config.default_use_hls)],
@@ -174,6 +175,7 @@ impl InstanceInfo {
174175
Default wide: {:?}\n
175176
Default comment sort: {:?}\n
176177
Default post sort: {:?}\n
178+
Default blur Spoiler: {:?}\n
177179
Default show NSFW: {:?}\n
178180
Default blur NSFW: {:?}\n
179181
Default use HLS: {:?}\n
@@ -197,6 +199,7 @@ impl InstanceInfo {
197199
self.config.default_wide,
198200
self.config.default_comment_sort,
199201
self.config.default_post_sort,
202+
self.config.default_blur_spoiler,
200203
self.config.default_show_nsfw,
201204
self.config.default_blur_nsfw,
202205
self.config.default_use_hls,

src/settings.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ struct SettingsTemplate {
1919

2020
// CONSTANTS
2121

22-
const PREFS: [&str; 16] = [
22+
const PREFS: [&str; 17] = [
2323
"theme",
2424
"front_page",
2525
"layout",
2626
"wide",
2727
"comment_sort",
2828
"post_sort",
29+
"blur_spoiler",
2930
"show_nsfw",
3031
"blur_nsfw",
3132
"use_hls",

src/utils.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ impl PollOption {
157157

158158
// Post flags with nsfw and stickied
159159
pub struct Flags {
160+
pub spoiler: bool,
160161
pub nsfw: bool,
161162
pub stickied: bool,
162163
}
@@ -403,6 +404,7 @@ impl Post {
403404
},
404405
},
405406
flags: Flags {
407+
spoiler: data["spoiler"].as_bool().unwrap_or_default(),
406408
nsfw: data["over_18"].as_bool().unwrap_or_default(),
407409
stickied: data["stickied"].as_bool().unwrap_or_default() || data["pinned"].as_bool().unwrap_or_default(),
408410
},
@@ -575,6 +577,7 @@ pub struct Preferences {
575577
pub front_page: String,
576578
pub layout: String,
577579
pub wide: String,
580+
pub blur_spoiler: String,
578581
pub show_nsfw: String,
579582
pub blur_nsfw: String,
580583
pub hide_hls_notification: String,
@@ -612,6 +615,7 @@ impl Preferences {
612615
front_page: setting(req, "front_page"),
613616
layout: setting(req, "layout"),
614617
wide: setting(req, "wide"),
618+
blur_spoiler: setting(req, "blur_spoiler"),
615619
show_nsfw: setting(req, "show_nsfw"),
616620
hide_sidebar_and_summary: setting(req, "hide_sidebar_and_summary"),
617621
blur_nsfw: setting(req, "blur_nsfw"),
@@ -732,6 +736,7 @@ pub async fn parse_post(post: &Value) -> Post {
732736
},
733737
},
734738
flags: Flags {
739+
spoiler: post["data"]["spoiler"].as_bool().unwrap_or_default(),
735740
nsfw: post["data"]["over_18"].as_bool().unwrap_or_default(),
736741
stickied: post["data"]["stickied"].as_bool().unwrap_or_default() || post["data"]["pinned"].as_bool().unwrap_or(false),
737742
},

templates/settings.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
{% call utils::options(prefs.comment_sort, ["confidence", "top", "new", "controversial", "old"], "confidence") %}
5555
</select>
5656
</div>
57+
<div class="prefs-group">
58+
<label for="blur_spoiler">Blur spoiler previews:</label>
59+
<input type="hidden" value="off" name="blur_spoiler">
60+
<input type="checkbox" name="blur_spoiler" id="blur_spoiler" {% if prefs.blur_spoiler == "on" %}checked{% endif %}>
61+
</div>
5762
{% if !crate::utils::sfw_only() %}
5863
<div class="prefs-group">
5964
<label for="show_nsfw">Show NSFW posts:</label>

templates/utils.html

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ <h1 class="post_title">
194194
{% endmacro %}
195195

196196
{% macro post_in_list(post) -%}
197+
{% set post_should_be_blurred = (post.flags.nsfw && prefs.blur_nsfw=="on") || (post.flags.spoiler && prefs.blur_spoiler=="on") -%}
197198
<div class="post {% if post.flags.stickied %}stickied{% endif %}" id="{{ post.id }}">
198199
<p class="post_header">
199200
{% let community -%}
@@ -233,7 +234,7 @@ <h2 class="post_title">
233234
<img width="100%" height="100%" loading="lazy" alt="Post image" src="{{ post.media.url }}"/>
234235
{% else %}
235236
<svg
236-
{%if post.flags.nsfw && prefs.blur_nsfw=="on" %}class="post_nsfw_blur"{% endif %}
237+
{%if post_should_be_blurred %}class="post_nsfw_blur"{% endif %}
237238
width="{{ post.media.width }}px"
238239
height="{{ post.media.height }}px"
239240
xmlns="http://www.w3.org/2000/svg">
@@ -247,19 +248,19 @@ <h2 class="post_title">
247248
</div>
248249
{% else if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "gif" %}
249250
<div class="post_media_content">
250-
<video class="post_media_video short {%if post.flags.nsfw && prefs.blur_nsfw=="on" %}post_nsfw_blur{% endif %}" src="{{ post.media.url }}" {% if post.media.width > 0 && post.media.height > 0 %}width="{{ post.media.width }}" height="{{ post.media.height }}"{% endif %} poster="{{ post.media.poster }}" preload="none" controls loop {% if prefs.autoplay_videos == "on" %}autoplay{% endif %}><a href={{ post.media.url }}>Video</a></video>
251+
<video class="post_media_video short {%if post_should_be_blurred %}post_nsfw_blur{% endif %}" src="{{ post.media.url }}" {% if post.media.width > 0 && post.media.height > 0 %}width="{{ post.media.width }}" height="{{ post.media.height }}"{% endif %} poster="{{ post.media.poster }}" preload="none" controls loop {% if prefs.autoplay_videos == "on" %}autoplay{% endif %}><a href={{ post.media.url }}>Video</a></video>
251252
</div>
252253
{% else if (prefs.layout.is_empty() || prefs.layout == "card") && post.post_type == "video" %}
253254
{% if prefs.use_hls == "on" && !post.media.alt_url.is_empty() %}
254255
<div class="post_media_content">
255-
<video class="post_media_video short {%if post.flags.nsfw && prefs.blur_nsfw=="on" %}post_nsfw_blur{% endif %} {% if prefs.autoplay_videos == "on" %}hls_autoplay{% endif %}" {% if post.media.width > 0 && post.media.height > 0 %}width="{{ post.media.width }}" height="{{ post.media.height }}"{% endif %} poster="{{ post.media.poster }}" controls preload="none">
256+
<video class="post_media_video short {%if post_should_be_blurred %}post_nsfw_blur{% endif %} {% if prefs.autoplay_videos == "on" %}hls_autoplay{% endif %}" {% if post.media.width > 0 && post.media.height > 0 %}width="{{ post.media.width }}" height="{{ post.media.height }}"{% endif %} poster="{{ post.media.poster }}" controls preload="none">
256257
<source src="{{ post.media.alt_url }}" type="application/vnd.apple.mpegurl" />
257258
<source src="{{ post.media.url }}" type="video/mp4" />
258259
</video>
259260
</div>
260261
{% else %}
261262
<div class="post_media_content">
262-
<video class="post_media_video short {%if post.flags.nsfw && prefs.blur_nsfw=="on" %}post_nsfw_blur{% endif %}" src="{{ post.media.url }}" {% if post.media.width > 0 && post.media.height > 0 %}width="{{ post.media.width }}" height="{{ post.media.height }}"{% endif %} poster="{{ post.media.poster }}" preload="none" controls {% if prefs.autoplay_videos == "on" %}autoplay{% endif %}><a href={{ post.media.url }}>Video</a></video>
263+
<video class="post_media_video short {%if post_should_be_blurred %}post_nsfw_blur{% endif %}" src="{{ post.media.url }}" {% if post.media.width > 0 && post.media.height > 0 %}width="{{ post.media.width }}" height="{{ post.media.height }}"{% endif %} poster="{{ post.media.poster }}" preload="none" controls {% if prefs.autoplay_videos == "on" %}autoplay{% endif %}><a href={{ post.media.url }}>Video</a></video>
263264
</div>
264265
{% call render_hls_notification(format!("{}%23{}", &self.url[1..].replace("&", "%26").replace("+", "%2B"), post.id)) %}
265266
{% endif %}
@@ -272,7 +273,7 @@ <h2 class="post_title">
272273
</svg>
273274
{% else %}
274275
<div style="max-width:{{ post.thumbnail.width }}px;max-height:{{ post.thumbnail.height }}px;">
275-
<svg {% if post.flags.nsfw && prefs.blur_nsfw=="on" %} class="thumb_nsfw_blur" {% endif %} width="{{ post.thumbnail.width }}px" height="{{ post.thumbnail.height }}px" xmlns="http://www.w3.org/2000/svg">
276+
<svg {% if post_should_be_blurred %} class="thumb_nsfw_blur" {% endif %} width="{{ post.thumbnail.width }}px" height="{{ post.thumbnail.height }}px" xmlns="http://www.w3.org/2000/svg">
276277
<image width="100%" height="100%" href="{{ post.thumbnail.url }}"/>
277278
<desc>
278279
<img loading="lazy" alt="Thumbnail" src="{{ post.thumbnail.url }}"/>

0 commit comments

Comments
 (0)