add: post editing, profile pinned posts, theme settings

TODO: nsfw profile/community options
This commit is contained in:
trisua 2025-04-07 16:07:01 -04:00
parent 31f63c90cd
commit f83cfa3756
10 changed files with 255 additions and 8 deletions

View file

@ -73,6 +73,7 @@ version = "1.0.0"
"communities:label.change_title" = "Change title"
"communities:label.new_title" = "New title"
"communities:label.pinned" = "Pinned"
"communities:label.edit_content" = "Edit content"
"notifs:action.mark_as_read" = "Mark as read"
"notifs:action.mark_as_unread" = "Mark as unread"
@ -92,6 +93,9 @@ version = "1.0.0"
"settings:label.two_factor_authentication" = "Two-factor authentication"
"settings:label.change_avatar" = "Change avatar"
"settings:label.change_banner" = "Change banner"
"settings:label.theme_hue" = "Theme hue"
"settings:label.theme_sat" = "Theme sat"
"settings:label.theme_lit" = "Theme lit"
"mod_panel:label.open_reported_content" = "Open reported content"
"mod_panel:label.manage_profile" = "Manage profile"

View file

@ -46,6 +46,13 @@
<span>{{ text "communities:label.replies" }}</span>
</a>
{% if user.id == post.owner %}
<a href="#/edit" data-tab-button="edit">
{{ icon "pen" }}
<span>{{ text "communities:label.edit_content" }}</span>
</a>
{% endif %}
<a href="#/configure" data-tab-button="configure">
{{ icon "settings" }}
<span>{{ text "communities:action.configure" }}</span>
@ -98,6 +105,8 @@
ui.refresh_container(element, []);
const can_manage_pins = "{{ can_manage_pins }}" === "true";
const is_owner = "{{ user.id == post.owner }}" === "true";
const settings_fields = [
[
[
@ -117,11 +126,74 @@
]);
}
if (is_owner) {
settings_fields.push([
["is_profile_pinned", "Pinned to your profile"],
"{{ post.context.is_profile_pinned }}",
"checkbox",
]);
}
ui.generate_settings_ui(element, settings_fields, settings);
}, 250);
</script>
</div>
{% endif %}
{% if user.id == post.owner %}
<div class="card-nest w-full hidden" data-tab="edit">
<div class="card small flex items-center gap-2">
{{ icon "pen" }}
<span>{{ text "communities:label.edit_content" }}</span>
</div>
<form
class="card flex flex-col gap-2"
onsubmit="edit_post_from_form(event)"
>
<div class="flex flex-col gap-1">
<label for="content"
>{{ text "communities:label.content" }}</label
>
<textarea
type="text"
name="content"
id="content"
placeholder="content"
required
minlength="2"
maxlength="4096"
>
{{ post.content }}</textarea
>
</div>
<button class="primary">{{ text "general:action.save" }}</button>
</form>
</div>
<script>
async function edit_post_from_form(e) {
e.preventDefault();
await trigger("atto::debounce", ["posts::edit"]);
fetch("/api/v1/posts/{{ post.id }}/content", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
content: e.target.content.value,
}),
})
.then((res) => res.json())
.then((res) => {
trigger("atto::toast", [
res.ok ? "success" : "error",
res.message,
]);
});
}
</script>
{% endif %} {% endif %}
<div class="card-nest w-full" data-tab="replies">
<div class="card small flex items-center gap-2">

View file

@ -143,9 +143,14 @@ show_community and post.community != config.town_square %}
>{{ components::full_username(user=owner) }}</span
>
{% if post.context.edited != 0 %}
<div class="flex">
<span class="fade date">{{ post.context.edited }}</span>
<sup title="Edited">*</sup>
</div>
{% else %}
<span class="fade date">{{ post.created }}</span>
{% if show_community %}
{% endif %} {% if show_community %}
<a href="/api/v1/communities/find/{{ post.community }}">
<!-- prettier-ignore -->
{% if not community %}
@ -211,6 +216,23 @@ show_community and post.community != config.town_square %}
{% endif %} {% if (user.id == post.owner) or is_helper
or can_manage_post %}
<b class="title">{{ text "general:action.manage" }}</b>
{% if user.id == post.owner %}
<a href="/post/{{ post.id }}#/edit">
{{ icon "pen" }}
<span
>{{ text "communities:label.edit_content"
}}</span
>
</a>
{% endif %}
<a href="/post/{{ post.id }}#/configure">
{{ icon "settings" }}
<span
>{{ text "communities:action.configure" }}</span
>
</a>
<button
class="red"
onclick="trigger('me::remove_post', ['{{ post.id }}'])"

View file

@ -1,5 +1,20 @@
{% import "macros.html" as macros %} {% extends "profile/base.html" %} {% block
content %}
content %} {% if pinned|length != 0 %}
<div class="card-nest">
<div class="card small flex gap-2 items-center">
{{ icon "pin" }}
<span>{{ text "communities:label.pinned" }}</span>
</div>
<div class="card flex flex-col gap-4">
<!-- prettier-ignore -->
{% for post in pinned %}
{{ components::post(post=post[0], owner=post[1], secondary=true, community=post[2], show_community=true, can_manage_post=is_self) }}
{% endfor %}
</div>
</div>
{% endif %}
<div class="card-nest">
<div class="card small flex gap-2 items-center">
{{ icon "clock" }}
@ -9,7 +24,7 @@ content %}
<div class="card flex flex-col gap-4">
<!-- prettier-ignore -->
{% for post in posts %}
{{ components::post(post=post[0], owner=post[1], secondary=true, community=post[2], show_community=true) }}
{{ components::post(post=post[0], owner=post[1], secondary=true, community=post[2], show_community=true, can_manage_post=is_self) }}
{% endfor %}
{{ components::pagination(page=page, items=posts|length) }}

View file

@ -675,6 +675,21 @@
"{{ profile.settings.private_last_seen }}",
"checkbox",
],
[
["theme_hue", "Theme hue (integer 0-255)"],
"{{ profile.settings.theme_hue }}",
"input",
],
[
["theme_sat", "Theme sat (percentage 0%-100%)"],
"{{ profile.settings.theme_sat }}",
"input",
],
[
["theme_lit", "Theme lit (percentage 0%-100%)"],
"{{ profile.settings.theme_lit }}",
"input",
],
],
settings,
);

View file

@ -315,6 +315,26 @@
</div>
</div>
</dialog>
{% endif %}
{% endif %} {% if user %}
<!-- theming -->
{% if user.settings.theme_hue %}
<style>
:root, * {
--hue: {{ user.settings.theme_hue }} !important;
}
</style>
{% endif %} {% if user.settings.theme_sat %}
<style>
:root, * {
--sat: {{ user.settings.theme_sat }} !important;
}
</style>
{% endif %} {% if user.settings.theme_lit %}
<style>
:root, * {
--lit: {{ user.settings.theme_lit }} !important;
}
</style>
{% endif %} {% endif %}
</body>
</html>

View file

@ -156,6 +156,14 @@ pub async fn posts_request(
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
let pinned = match data.0.get_pinned_posts_by_user(other_user.id).await {
Ok(p) => match data.0.fill_posts_with_community(p).await {
Ok(p) => p,
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
},
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
};
let communities = match data.0.get_memberships_by_owner(other_user.id).await {
Ok(m) => match data.0.fill_communities(m).await {
Ok(m) => m,
@ -203,6 +211,7 @@ pub async fn posts_request(
context.insert("posts", &posts);
context.insert("page", &props.page);
context.insert("pinned", &pinned);
profile_context(
&mut context,
&other_user,

View file

@ -16,6 +16,7 @@ use crate::{auto_method, execute, get, query_row, query_rows, params};
#[cfg(feature = "sqlite")]
use rusqlite::Row;
use tetratto_shared::unix_epoch_timestamp;
#[cfg(feature = "postgres")]
use tokio_postgres::Row;
@ -143,7 +144,7 @@ impl DataManager {
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
"SELECT * FROM posts WHERE owner = $1 AND NOT context LIKE '%\"is_profile_pinned\":true%' ORDER BY created DESC LIMIT $2 OFFSET $3",
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
@ -210,6 +211,30 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get all pinned posts from the given user (from most recent).
///
/// # Arguments
/// * `id` - the ID of the user the requested posts belong to
pub async fn get_pinned_posts_by_user(&self, id: usize) -> Result<Vec<Post>> {
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE owner = $1 AND context LIKE '%\"is_profile_pinned\":true%' ORDER BY created DESC",
&[&(id as i64),],
|x| { Self::get_post_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("post".to_string()));
}
Ok(res.unwrap())
}
/// Get posts from all communities, sorted by likes.
///
/// # Arguments
@ -529,6 +554,19 @@ impl DataManager {
}
}
// check if we can manage profile pins
if (x.is_profile_pinned != y.context.is_profile_pinned) && (user.id != y.owner) {
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(AuditLogEntry::new(
user.id,
format!("invoked `update_post_context(profile_pinned)` with x value `{id}`"),
))
.await?
}
}
// ...
let conn = match self.connect().await {
Ok(c) => c,
@ -551,7 +589,44 @@ impl DataManager {
Ok(())
}
auto_method!(update_post_content(String)@get_post_by_id:MANAGE_POSTS -> "UPDATE posts SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.post:{}");
pub async fn update_post_content(&self, id: usize, user: User, x: String) -> Result<()> {
let mut y = self.get_post_by_id(id).await?;
if user.id != y.owner {
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(AuditLogEntry::new(
user.id,
format!("invoked `update_post_content` with x value `{id}`"),
))
.await?
}
}
// ...
let conn = match self.connect().await {
Ok(c) => c,
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
};
let res = execute!(
&conn,
"UPDATE posts SET content = $1 WHERE id = $2",
params![&x, &(id as i64)]
);
if let Err(e) = res {
return Err(Error::DatabaseError(e.to_string()));
}
// update context
y.context.edited = unix_epoch_timestamp() as usize;
self.update_post_context(id, user, y.context).await?;
// return
Ok(())
}
auto_method!(incr_post_likes() -> "UPDATE posts SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);
auto_method!(incr_post_dislikes() -> "UPDATE posts SET dislikes = dislikes + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);

View file

@ -62,6 +62,12 @@ pub struct UserSettings {
pub theme_preference: ThemePreference,
#[serde(default)]
pub private_last_seen: bool,
#[serde(default)]
pub theme_hue: String,
#[serde(default)]
pub theme_sat: String,
#[serde(default)]
pub theme_lit: String,
}
impl Default for UserSettings {
@ -74,6 +80,9 @@ impl Default for UserSettings {
private_communities: false,
theme_preference: ThemePreference::default(),
private_last_seen: false,
theme_hue: String::new(),
theme_sat: String::new(),
theme_lit: String::new(),
}
}
}

View file

@ -167,6 +167,10 @@ pub struct PostContext {
pub comments_enabled: bool,
#[serde(default)]
pub is_pinned: bool,
#[serde(default)]
pub is_profile_pinned: bool,
#[serde(default)]
pub edited: usize,
}
fn default_comments_enabled() -> bool {
@ -178,6 +182,8 @@ impl Default for PostContext {
Self {
comments_enabled: default_comments_enabled(),
is_pinned: false,
is_profile_pinned: false,
edited: 0,
}
}
}