diff --git a/crates/app/src/langs/en-US.toml b/crates/app/src/langs/en-US.toml index 079bcc1..1ef63e2 100644 --- a/crates/app/src/langs/en-US.toml +++ b/crates/app/src/langs/en-US.toml @@ -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" diff --git a/crates/app/src/public/html/communities/post.html b/crates/app/src/public/html/communities/post.html index c046f6b..b9b8314 100644 --- a/crates/app/src/public/html/communities/post.html +++ b/crates/app/src/public/html/communities/post.html @@ -46,6 +46,13 @@ {{ text "communities:label.replies" }} + {% if user.id == post.owner %} + + {{ icon "pen" }} + {{ text "communities:label.edit_content" }} + + {% endif %} + {{ icon "settings" }} {{ text "communities:action.configure" }} @@ -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); - {% endif %} + + {% if user.id == post.owner %} + + + + {% endif %} {% endif %}
diff --git a/crates/app/src/public/html/components.html b/crates/app/src/public/html/components.html index 6d1ee56..4356ccd 100644 --- a/crates/app/src/public/html/components.html +++ b/crates/app/src/public/html/components.html @@ -143,9 +143,14 @@ show_community and post.community != config.town_square %} >{{ components::full_username(user=owner) }} + {% if post.context.edited != 0 %} +
+ {{ post.context.edited }} + * +
+ {% else %} {{ post.created }} - - {% if show_community %} + {% endif %} {% if show_community %}
{% 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 %} {{ text "general:action.manage" }} + {% if user.id == post.owner %} + + {{ icon "pen" }} + {{ text "communities:label.edit_content" + }} + + {% endif %} + + + {{ icon "settings" }} + {{ text "communities:action.configure" }} + +
+{% endif %} +
{{ icon "clock" }} @@ -9,7 +24,7 @@ content %}
{% 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) }} diff --git a/crates/app/src/public/html/profile/settings.html b/crates/app/src/public/html/profile/settings.html index 67e183d..2171405 100644 --- a/crates/app/src/public/html/profile/settings.html +++ b/crates/app/src/public/html/profile/settings.html @@ -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, ); diff --git a/crates/app/src/public/html/root.html b/crates/app/src/public/html/root.html index a1438ab..eb0b1ae 100644 --- a/crates/app/src/public/html/root.html +++ b/crates/app/src/public/html/root.html @@ -315,6 +315,26 @@
- {% endif %} + {% endif %} {% if user %} + + {% if user.settings.theme_hue %} + + {% endif %} {% if user.settings.theme_sat %} + + {% endif %} {% if user.settings.theme_lit %} + + {% endif %} {% endif %} diff --git a/crates/app/src/routes/pages/profile.rs b/crates/app/src/routes/pages/profile.rs index 4a35bf7..a6a8759 100644 --- a/crates/app/src/routes/pages/profile.rs +++ b/crates/app/src/routes/pages/profile.rs @@ -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, diff --git a/crates/core/src/database/posts.rs b/crates/core/src/database/posts.rs index 63c6112..b5d39fd 100644 --- a/crates/core/src/database/posts.rs +++ b/crates/core/src/database/posts.rs @@ -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> { + 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); diff --git a/crates/core/src/model/auth.rs b/crates/core/src/model/auth.rs index 061f9cb..2ceff17 100644 --- a/crates/core/src/model/auth.rs +++ b/crates/core/src/model/auth.rs @@ -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(), } } } diff --git a/crates/core/src/model/communities.rs b/crates/core/src/model/communities.rs index 27684c9..348a262 100644 --- a/crates/core/src/model/communities.rs +++ b/crates/core/src/model/communities.rs @@ -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, } } }