add: post warnings

This commit is contained in:
trisua 2025-05-06 15:48:03 -04:00
parent 24cf61c783
commit 71d017693c
8 changed files with 151 additions and 12 deletions

View file

@ -65,6 +65,15 @@
{{ components::emoji_picker(element_id="content",
render_dialog=true) }}
<button
class="small square quaternary"
title="More options"
onclick="document.getElementById('post_options_dialog').showModal()"
type="button"
>
{{ icon "ellipsis" }}
</button>
<button class="primary">
{{ text "communities:action.create" }}
</button>
@ -89,13 +98,35 @@
}),
})
.then((res) => res.json())
.then((res) => {
.then(async (res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
if (res.ok) {
// update settings
if (
JSON.stringify(
window.POST_INITIAL_SETTINGS,
) !== window.BLANK_INITIAL_SETTINGS
) {
await fetch(
`/api/v1/posts/${res.payload}/context`,
{
method: "POST",
headers: {
"Content-Type":
"application/json",
},
body: JSON.stringify({
context:
window.POST_INITIAL_SETTINGS,
}),
},
);
}
setTimeout(() => {
window.location.href = `/post/${res.payload}`;
}, 100);
@ -137,4 +168,85 @@
window.history.back();
}
</script>
<dialog id="post_options_dialog">
<div class="inner flex flex-col gap-2">
<div id="post_options" class="flex flex-col gap-2"></div>
<hr />
<div class="flex justify-between">
<div></div>
<div class="flex gap-2">
<button
class="bold red quaternary"
onclick="document.getElementById('post_options_dialog').close()"
type="button"
>
{{ icon "x" }} {{ text "dialog:action.close" }}
</button>
</div>
</div>
<script>
setTimeout(() => {
window.POST_INITIAL_SETTINGS = {
comments_enabled: true,
reposts_enabled: true,
reactions_enabled: true,
is_nsfw: false,
content_warning: "",
};
window.BLANK_INITIAL_SETTINGS = JSON.stringify(
window.POST_INITIAL_SETTINGS,
);
const settings_fields = [
[
[
"comments_enabled",
"Allow people to comment on your post",
],
window.POST_INITIAL_SETTINGS.comments_enabled.toString(),
"checkbox",
],
[
[
"reposts_enabled",
"Allow people to repost/quote your post",
],
window.POST_INITIAL_SETTINGS.reposts_enabled.toString(),
"checkbox",
],
[
[
"reactions_enabled",
"Allow people to like/dislike your post",
],
window.POST_INITIAL_SETTINGS.reactions_enabled.toString(),
"checkbox",
],
[
["is_nsfw", "Hide from public timelines"],
window.POST_INITIAL_SETTINGS.is_nsfw.toString(),
"checkbox",
],
[
["content_warning", "Content warning"],
window.POST_INITIAL_SETTINGS.content_warning,
"textarea",
],
];
trigger("ui::generate_settings_ui", [
document.getElementById("post_options"),
settings_fields,
window.POST_INITIAL_SETTINGS,
]);
}, 250);
</script>
</div>
</dialog>
{% endblock %}

View file

@ -227,9 +227,24 @@ and show_community and community.id != config.town_square or question %}
{% endif %}
</div>
{% if not post.context.content_warning %}
<span id="post-content:{{ post.id }}" class="no_p_margin"
>{{ post.content|markdown|safe }}</span
>
{% else %}
<details>
<summary
class="card flex gap-2 items-center secondary red w-full"
>
{{ icon "triangle-alert" }}
<b>{{ post.context.content_warning }}</b>
</summary>
<span id="post-content:{{ post.id }}" class="no_p_margin"
>{{ post.content|markdown|safe }}</span
>
</details>
{% endif %}
</div>
</div>

View file

@ -159,10 +159,15 @@
"checkbox",
],
[
["is_nsfw", "Mark as NSFW"],
["is_nsfw", "Hide from public timelines"],
"{{ community.context.is_nsfw }}",
"checkbox",
],
[
["content_warning", "Content warning"],
settings.content_warning,
"textarea",
],
];
if (can_manage_pins) {

View file

@ -654,18 +654,18 @@ impl DataManager {
auto_method!(update_user_stripe_id(&str)@get_user_by_id -> "UPDATE users SET stripe_id = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_user);
auto_method!(incr_user_notifications()@get_user_by_id -> "UPDATE users SET notification_count = notification_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --incr);
auto_method!(decr_user_notifications()@get_user_by_id -> "UPDATE users SET notification_count = notification_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr);
auto_method!(decr_user_notifications()@get_user_by_id -> "UPDATE users SET notification_count = notification_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr=notification_count);
auto_method!(incr_user_follower_count()@get_user_by_id -> "UPDATE users SET follower_count = follower_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --incr);
auto_method!(decr_user_follower_count()@get_user_by_id -> "UPDATE users SET follower_count = follower_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr);
auto_method!(decr_user_follower_count()@get_user_by_id -> "UPDATE users SET follower_count = follower_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr=follower_count);
auto_method!(incr_user_following_count()@get_user_by_id -> "UPDATE users SET following_count = following_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --incr);
auto_method!(decr_user_following_count()@get_user_by_id -> "UPDATE users SET following_count = following_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr);
auto_method!(decr_user_following_count()@get_user_by_id -> "UPDATE users SET following_count = following_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr=following_count);
auto_method!(incr_user_post_count()@get_user_by_id -> "UPDATE users SET post_count = post_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --incr);
auto_method!(decr_user_post_count()@get_user_by_id -> "UPDATE users SET post_count = post_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr);
auto_method!(decr_user_post_count()@get_user_by_id -> "UPDATE users SET post_count = post_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr=post_count);
auto_method!(update_user_request_count(i32)@get_user_by_id -> "UPDATE users SET request_count = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_user);
auto_method!(incr_user_request_count()@get_user_by_id -> "UPDATE users SET request_count = request_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --incr);
auto_method!(decr_user_request_count()@get_user_by_id -> "UPDATE users SET request_count = request_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr);
auto_method!(decr_user_request_count()@get_user_by_id -> "UPDATE users SET request_count = request_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_user --decr=request_count);
}

View file

@ -667,10 +667,14 @@ macro_rules! auto_method {
}
};
($name:ident()@$select_fn:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:ident --decr) => {
($name:ident()@$select_fn:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:ident --decr=$field:ident) => {
pub async fn $name(&self, id: usize) -> Result<()> {
let y = self.$select_fn(id).await?;
if (y.$field as isize) - 1 < 0 {
return Ok(());
}
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),

View file

@ -471,9 +471,9 @@ impl DataManager {
auto_method!(incr_community_likes()@get_community_by_id_no_void -> "UPDATE communities SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --incr);
auto_method!(incr_community_dislikes()@get_community_by_id_no_void -> "UPDATE communities SET dislikes = dislikes + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --incr);
auto_method!(decr_community_likes()@get_community_by_id_no_void -> "UPDATE communities SET likes = likes - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr);
auto_method!(decr_community_dislikes()@get_community_by_id_no_void -> "UPDATE communities SET dislikes = dislikes - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr);
auto_method!(decr_community_likes()@get_community_by_id_no_void -> "UPDATE communities SET likes = likes - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr=likes);
auto_method!(decr_community_dislikes()@get_community_by_id_no_void -> "UPDATE communities SET dislikes = dislikes - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr=dislikes);
auto_method!(incr_community_member_count()@get_community_by_id_no_void -> "UPDATE communities SET member_count = member_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --incr);
auto_method!(decr_community_member_count()@get_community_by_id_no_void -> "UPDATE communities SET member_count = member_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr);
auto_method!(decr_community_member_count()@get_community_by_id_no_void -> "UPDATE communities SET member_count = member_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr=member_count);
}

View file

@ -584,7 +584,7 @@ impl DataManager {
let res = query_rows!(
&conn,
&format!(
"SELECT * FROM posts WHERE (community = {} {query_string}) AND replying_to = 0 ORDER BY created DESC LIMIT $1 OFFSET $2",
"SELECT * FROM posts WHERE (community = {} {query_string}) AND NOT context LIKE '%\"is_nsfw\":true%' AND replying_to = 0 ORDER BY created DESC LIMIT $1 OFFSET $2",
first.community
),
&[&(batch as i64), &((page * batch) as i64)],

View file

@ -178,6 +178,8 @@ pub struct PostContext {
pub answering: usize,
#[serde(default = "default_reactions_enabled")]
pub reactions_enabled: bool,
#[serde(default)]
pub content_warning: String,
}
fn default_comments_enabled() -> bool {
@ -204,6 +206,7 @@ impl Default for PostContext {
repost: None,
answering: 0,
reactions_enabled: default_reactions_enabled(),
content_warning: String::new(),
}
}
}