diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index cfc2501e8..e2dfd8ffd 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -1,4 +1,4 @@ -use lemmy_db_schema::source::tagline::Tagline; +use lemmy_db_schema::{newtypes::TaglineId, source::tagline::Tagline}; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] @@ -12,6 +12,15 @@ pub struct CreateTagline { pub content: String, } +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +/// Update a tagline +pub struct UpdateTagline { + pub id: TaglineId, + pub content: String, +} + #[derive(Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", ts(export))] diff --git a/crates/api_crud/src/tagline/mod.rs b/crates/api_crud/src/tagline/mod.rs index fa6613e51..9ab1b43f8 100644 --- a/crates/api_crud/src/tagline/mod.rs +++ b/crates/api_crud/src/tagline/mod.rs @@ -1,2 +1,3 @@ pub mod create; pub mod list; +pub mod update; diff --git a/crates/api_crud/src/tagline/update.rs b/crates/api_crud/src/tagline/update.rs new file mode 100644 index 000000000..fe5709375 --- /dev/null +++ b/crates/api_crud/src/tagline/update.rs @@ -0,0 +1,34 @@ +use activitypub_federation::config::Data; +use actix_web::web::Json; +use lemmy_api_common::{ + context::LemmyContext, + tagline::{UpdateTagline, TaglineResponse}, + utils::is_admin, +}; +use lemmy_db_schema::{source::{ + local_site::LocalSite, + tagline::{Tagline, TaglineUpdateForm}, +}, utils::naive_now}; +use lemmy_db_views::structs::LocalUserView; +use lemmy_utils::error::LemmyError; + +#[tracing::instrument(skip(context))] +pub async fn update_tagline( + data: Json, + context: Data, + local_user_view: LocalUserView, +) -> Result, LemmyError> { + let local_site = LocalSite::read(&mut context.pool()).await?; + // Make sure user is an admin + is_admin(&local_user_view)?; + + let tagline_form = TaglineUpdateForm { + local_site_id: local_site.id, + content: data.content.to_string(), + updated: Some(naive_now()), + }; + + let tagline = Tagline::update(&mut context.pool(), data.id, &tagline_form).await?; + + Ok(Json(TaglineResponse { tagline })) +} diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index 9b3dffc13..d70d2fcd8 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -1,7 +1,7 @@ use crate::{ - newtypes::LocalSiteId, + newtypes::{LocalSiteId, TaglineId}, schema::tagline::dsl::{local_site_id, published, tagline}, - source::tagline::{Tagline, TaglineInsertForm}, + source::tagline::{Tagline, TaglineInsertForm, TaglineUpdateForm}, utils::{get_conn, limit_and_offset, DbPool}, }; use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; @@ -52,6 +52,18 @@ impl Tagline { .await } + pub async fn update( + pool: &mut DbPool<'_>, + tagline_id: TaglineId, + form: &TaglineUpdateForm, + ) -> Result { + let conn = &mut get_conn(pool).await?; + diesel::update(tagline.find(tagline_id)) + .set(form) + .get_result::(conn) + .await + } + async fn clear(conn: &mut AsyncPgConnection) -> Result { diesel::delete(tagline).execute(conn).await } diff --git a/crates/db_schema/src/newtypes.rs b/crates/db_schema/src/newtypes.rs index 96fc23ac6..ceadb2b54 100644 --- a/crates/db_schema/src/newtypes.rs +++ b/crates/db_schema/src/newtypes.rs @@ -152,6 +152,12 @@ pub struct LocalSiteId(i32); /// The custom emoji id. pub struct CustomEmojiId(i32); +#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, Default)] +#[cfg_attr(feature = "full", derive(DieselNewType, TS))] +#[cfg_attr(feature = "full", ts(export))] +/// The tagline id. +pub struct TaglineId(i32); + #[cfg(feature = "full")] #[derive(Serialize, Deserialize)] #[serde(remote = "Ltree")] diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index c08d9f22c..6194d25c4 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -37,3 +37,12 @@ pub struct TaglineInsertForm { pub content: String, pub updated: Option>, } + +#[derive(Clone, Default)] +#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] +#[cfg_attr(feature = "full", diesel(table_name = tagline))] +pub struct TaglineUpdateForm { + pub local_site_id: LocalSiteId, + pub content: String, + pub updated: Option>, +} \ No newline at end of file diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index 80b7ce630..305d66663 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -123,7 +123,11 @@ use lemmy_api_crud::{ update::update_private_message, }, site::{create::create_site, read::get_site, update::update_site}, - tagline::{create::create_tagline, list::list_taglines}, + tagline::{ + create::create_tagline, + list::list_taglines, + update::update_tagline + }, user::{create::register, delete::delete_account}, }; use lemmy_apub::api::{ @@ -370,6 +374,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { web::scope("/tagline") .wrap(rate_limit.message()) .route("/", web::post().to(create_tagline)) + .route("/", web::put().to(update_tagline)) .route("/list", web::get().to(list_taglines)), ), );