From fd275231bc3adb04f832d3d47171939fc9f225cc Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Sun, 31 Mar 2024 16:49:32 +0200 Subject: [PATCH 01/26] Add custom_emoji list route --- crates/api_common/src/custom_emoji.rs | 19 ++++++++++++++++ crates/api_crud/src/custom_emoji/list.rs | 27 ++++++++++++++++++++++ crates/api_crud/src/custom_emoji/mod.rs | 1 + crates/db_views/src/custom_emoji_view.rs | 29 +++++++++++++++++++++++- src/api_routes_http.rs | 10 ++++---- 5 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 crates/api_crud/src/custom_emoji/list.rs diff --git a/crates/api_common/src/custom_emoji.rs b/crates/api_common/src/custom_emoji.rs index 468d2128d..eab09f2a9 100644 --- a/crates/api_common/src/custom_emoji.rs +++ b/crates/api_common/src/custom_emoji.rs @@ -1,6 +1,7 @@ use lemmy_db_schema::newtypes::CustomEmojiId; use lemmy_db_views::structs::CustomEmojiView; use serde::{Deserialize, Serialize}; +use serde_with::skip_serializing_none; #[cfg(feature = "full")] use ts_rs::TS; use url::Url; @@ -46,3 +47,21 @@ pub struct DeleteCustomEmoji { pub struct CustomEmojiResponse { pub custom_emoji: CustomEmojiView, } + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +/// A response for custom emojis. +pub struct ListCustomEmojisResponse { + pub custom_emojis: Vec, +} + +#[skip_serializing_none] +#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +/// Fetches a list of registration applications. +pub struct ListCustomEmojis { + pub page: Option, + pub limit: Option, +} diff --git a/crates/api_crud/src/custom_emoji/list.rs b/crates/api_crud/src/custom_emoji/list.rs new file mode 100644 index 000000000..86ec74bad --- /dev/null +++ b/crates/api_crud/src/custom_emoji/list.rs @@ -0,0 +1,27 @@ +use actix_web::web::{Data, Json, Query}; +use lemmy_api_common::{ + context::LemmyContext, + custom_emoji::{ListCustomEmojis, ListCustomEmojisResponse}, +}; +use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView}; +use lemmy_utils::error::LemmyError; + +#[tracing::instrument(skip(context))] +pub async fn list_custom_emojis( + data: Query, + local_user_view: Option, + context: Data, +) -> Result, LemmyError> { + + let local_site = SiteView::read_local(&mut context.pool()).await?; + let custom_emojis = CustomEmojiView::list( + &mut context.pool(), + local_site.local_site.id, + data.page, + data.limit, + ) + .await + .map_err(|e| anyhow::anyhow!("Failed to construct custom emojis response: {e}"))?; + + Ok(Json(ListCustomEmojisResponse { custom_emojis })) +} diff --git a/crates/api_crud/src/custom_emoji/mod.rs b/crates/api_crud/src/custom_emoji/mod.rs index fdb2f5561..ffd48daf6 100644 --- a/crates/api_crud/src/custom_emoji/mod.rs +++ b/crates/api_crud/src/custom_emoji/mod.rs @@ -1,3 +1,4 @@ pub mod create; pub mod delete; +pub mod list; pub mod update; diff --git a/crates/db_views/src/custom_emoji_view.rs b/crates/db_views/src/custom_emoji_view.rs index 4d2f1fd85..ef391954b 100644 --- a/crates/db_views/src/custom_emoji_view.rs +++ b/crates/db_views/src/custom_emoji_view.rs @@ -5,7 +5,7 @@ use lemmy_db_schema::{ newtypes::{CustomEmojiId, LocalSiteId}, schema::{custom_emoji, custom_emoji_keyword}, source::{custom_emoji::CustomEmoji, custom_emoji_keyword::CustomEmojiKeyword}, - utils::{get_conn, DbPool}, + utils::{get_conn, limit_and_offset, DbPool}, }; use std::collections::HashMap; @@ -57,6 +57,33 @@ impl CustomEmojiView { Ok(CustomEmojiView::from_tuple_to_vec(emojis)) } + pub async fn list( + pool: &mut DbPool<'_>, + for_local_site_id: LocalSiteId, + page: Option, + limit: Option, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + let (limit, offset) = limit_and_offset(page, limit)?; + let emojis = custom_emoji::table + .filter(custom_emoji::local_site_id.eq(for_local_site_id)) + .left_join( + custom_emoji_keyword::table.on(custom_emoji_keyword::custom_emoji_id.eq(custom_emoji::id)), + ) + .order(custom_emoji::category) + .then_order_by(custom_emoji::id) + .select(( + custom_emoji::all_columns, + custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want) + )) + .limit(limit) + .offset(offset) + .load::(conn) + .await?; + + Ok(CustomEmojiView::from_tuple_to_vec(emojis)) + } + fn from_tuple_to_vec(items: Vec) -> Vec { let mut result = Vec::new(); let mut hash: HashMap> = HashMap::new(); diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index 013e2e092..0dcdf5cf6 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -104,9 +104,10 @@ use lemmy_api_crud::{ update::update_community, }, custom_emoji::{ - create::create_custom_emoji, - delete::delete_custom_emoji, - update::update_custom_emoji, + create::create_custom_emoji, + delete::delete_custom_emoji, + list::list_custom_emojis, + update::update_custom_emoji }, post::{ create::create_post, @@ -361,7 +362,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .wrap(rate_limit.message()) .route("", web::post().to(create_custom_emoji)) .route("", web::put().to(update_custom_emoji)) - .route("/delete", web::post().to(delete_custom_emoji)), + .route("/delete", web::post().to(delete_custom_emoji)) + .route("/list", web::get().to(list_custom_emojis)), ), ); cfg.service( From b4a86157a5881a13c5f846f436529a6d508f2ff4 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Sun, 31 Mar 2024 17:25:54 +0200 Subject: [PATCH 02/26] Add tagline list route --- crates/api_common/src/lib.rs | 1 + crates/api_common/src/tagline.rs | 23 +++++++++++++++++ crates/api_crud/src/lib.rs | 1 + crates/api_crud/src/tagline/list.rs | 27 ++++++++++++++++++++ crates/api_crud/src/tagline/mod.rs | 1 + crates/db_views/src/lib.rs | 2 ++ crates/db_views/src/structs.rs | 12 ++++++++- crates/db_views/src/tagline_view.rs | 39 +++++++++++++++++++++++++++++ src/api_routes_http.rs | 8 +++++- 9 files changed, 112 insertions(+), 2 deletions(-) create mode 100644 crates/api_common/src/tagline.rs create mode 100644 crates/api_crud/src/tagline/list.rs create mode 100644 crates/api_crud/src/tagline/mod.rs create mode 100644 crates/db_views/src/tagline_view.rs diff --git a/crates/api_common/src/lib.rs b/crates/api_common/src/lib.rs index b55dff32f..b3a5293df 100644 --- a/crates/api_common/src/lib.rs +++ b/crates/api_common/src/lib.rs @@ -16,6 +16,7 @@ pub mod request; pub mod send_activity; pub mod sensitive; pub mod site; +pub mod tagline; #[cfg(feature = "full")] pub mod utils; diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs new file mode 100644 index 000000000..e837e8195 --- /dev/null +++ b/crates/api_common/src/tagline.rs @@ -0,0 +1,23 @@ +use serde::{Deserialize, Serialize}; +use lemmy_db_views::structs::TaglineView; +use serde_with::skip_serializing_none; +#[cfg(feature = "full")] +use ts_rs::TS; + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +/// A response for custom emojis. +pub struct ListTaglinesResponse { + pub taglines: Vec, +} + +#[skip_serializing_none] +#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +/// Fetches a list of registration applications. +pub struct ListTaglines { + pub page: Option, + pub limit: Option, +} diff --git a/crates/api_crud/src/lib.rs b/crates/api_crud/src/lib.rs index aee3e8134..b60fb3240 100644 --- a/crates/api_crud/src/lib.rs +++ b/crates/api_crud/src/lib.rs @@ -4,4 +4,5 @@ pub mod custom_emoji; pub mod post; pub mod private_message; pub mod site; +pub mod tagline; pub mod user; diff --git a/crates/api_crud/src/tagline/list.rs b/crates/api_crud/src/tagline/list.rs new file mode 100644 index 000000000..35bfb71fc --- /dev/null +++ b/crates/api_crud/src/tagline/list.rs @@ -0,0 +1,27 @@ +use actix_web::web::{Data, Json, Query}; +use lemmy_api_common::{ + context::LemmyContext, + tagline::{ListTaglines, ListTaglinesResponse}, +}; +use lemmy_db_views::structs::{TaglineView, LocalUserView, SiteView}; +use lemmy_utils::error::LemmyError; + +#[tracing::instrument(skip(context))] +pub async fn list_taglines( + data: Query, + local_user_view: Option, + context: Data, +) -> Result, LemmyError> { + + let local_site = SiteView::read_local(&mut context.pool()).await?; + let taglines = TaglineView::list( + &mut context.pool(), + local_site.local_site.id, + data.page, + data.limit, + ) + .await + .map_err(|e| anyhow::anyhow!("Failed to construct taglines response: {e}"))?; + + Ok(Json(ListTaglinesResponse { taglines })) +} diff --git a/crates/api_crud/src/tagline/mod.rs b/crates/api_crud/src/tagline/mod.rs new file mode 100644 index 000000000..651aed76e --- /dev/null +++ b/crates/api_crud/src/tagline/mod.rs @@ -0,0 +1 @@ +pub mod list; \ No newline at end of file diff --git a/crates/db_views/src/lib.rs b/crates/db_views/src/lib.rs index 73310d743..64bb58d63 100644 --- a/crates/db_views/src/lib.rs +++ b/crates/db_views/src/lib.rs @@ -21,6 +21,8 @@ pub mod private_message_view; pub mod registration_application_view; #[cfg(feature = "full")] pub mod site_view; +#[cfg(feature = "full")] +pub mod tagline_view; pub mod structs; #[cfg(feature = "full")] pub mod vote_view; diff --git a/crates/db_views/src/structs.rs b/crates/db_views/src/structs.rs index a290ca4a1..849897f73 100644 --- a/crates/db_views/src/structs.rs +++ b/crates/db_views/src/structs.rs @@ -18,7 +18,8 @@ use lemmy_db_schema::{ private_message::PrivateMessage, private_message_report::PrivateMessageReport, registration_application::RegistrationApplication, - site::Site, + site::Site, + tagline::Tagline, }, SubscribedType, }; @@ -193,6 +194,15 @@ pub struct SiteView { pub counts: SiteAggregates, } +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(feature = "full", derive(TS, Queryable))] +#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] +#[cfg_attr(feature = "full", ts(export))] +/// A custom emoji view. +pub struct TaglineView { + pub tagline: Tagline, +} + #[derive(Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(TS, Queryable))] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] diff --git a/crates/db_views/src/tagline_view.rs b/crates/db_views/src/tagline_view.rs new file mode 100644 index 000000000..35abb1706 --- /dev/null +++ b/crates/db_views/src/tagline_view.rs @@ -0,0 +1,39 @@ +use crate::structs::TaglineView; +use diesel::{result::Error, ExpressionMethods, QueryDsl}; +use diesel_async::RunQueryDsl; +use lemmy_db_schema::{ + newtypes::LocalSiteId, + schema::tagline, + source::tagline::Tagline, + utils::{get_conn, limit_and_offset, DbPool}, +}; + +impl TaglineView { + + pub async fn list( + pool: &mut DbPool<'_>, + for_local_site_id: LocalSiteId, + page: Option, + limit: Option, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + let (limit, offset) = limit_and_offset(page, limit)?; + let taglines = tagline::table + .filter(tagline::local_site_id.eq(for_local_site_id)) + .order(tagline::id) + .select(tagline::all_columns) + .limit(limit) + .offset(offset) + .load::(conn) + .await?; + + let mut result = Vec::new(); + for tagline in &taglines { + result.push(TaglineView { + tagline: tagline.clone() + }); + } + + Ok(result) + } + } diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index 0dcdf5cf6..e309d2daf 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -123,6 +123,7 @@ use lemmy_api_crud::{ update::update_private_message, }, site::{create::create_site, read::get_site, update::update_site}, + tagline::list::list_taglines, user::{create::register, delete::delete_account}, }; use lemmy_apub::api::{ @@ -364,7 +365,12 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .route("", web::put().to(update_custom_emoji)) .route("/delete", web::post().to(delete_custom_emoji)) .route("/list", web::get().to(list_custom_emojis)), - ), + ) + .service( + web::scope("/tagline") + .wrap(rate_limit.message()) + .route("/list", web::get().to(list_taglines)) + ) ); cfg.service( web::scope("/sitemap.xml") From 10a8f01fade6ff4ebe84f5d90d5f155f074a9875 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Sun, 31 Mar 2024 17:42:48 +0200 Subject: [PATCH 03/26] Apply linting --- crates/api_common/src/tagline.rs | 2 +- crates/api_crud/src/custom_emoji/list.rs | 3 +-- crates/api_crud/src/tagline/list.rs | 5 ++--- crates/api_crud/src/tagline/mod.rs | 2 +- crates/db_views/src/lib.rs | 2 +- crates/db_views/src/structs.rs | 2 +- crates/db_views/src/tagline_view.rs | 7 +++---- src/api_routes_http.rs | 14 +++++++------- 8 files changed, 17 insertions(+), 20 deletions(-) diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index e837e8195..4728d0cbb 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -1,5 +1,5 @@ -use serde::{Deserialize, Serialize}; use lemmy_db_views::structs::TaglineView; +use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] use ts_rs::TS; diff --git a/crates/api_crud/src/custom_emoji/list.rs b/crates/api_crud/src/custom_emoji/list.rs index 86ec74bad..811e32652 100644 --- a/crates/api_crud/src/custom_emoji/list.rs +++ b/crates/api_crud/src/custom_emoji/list.rs @@ -12,7 +12,6 @@ pub async fn list_custom_emojis( local_user_view: Option, context: Data, ) -> Result, LemmyError> { - let local_site = SiteView::read_local(&mut context.pool()).await?; let custom_emojis = CustomEmojiView::list( &mut context.pool(), @@ -20,7 +19,7 @@ pub async fn list_custom_emojis( data.page, data.limit, ) - .await + .await .map_err(|e| anyhow::anyhow!("Failed to construct custom emojis response: {e}"))?; Ok(Json(ListCustomEmojisResponse { custom_emojis })) diff --git a/crates/api_crud/src/tagline/list.rs b/crates/api_crud/src/tagline/list.rs index 35bfb71fc..22deeeb61 100644 --- a/crates/api_crud/src/tagline/list.rs +++ b/crates/api_crud/src/tagline/list.rs @@ -3,7 +3,7 @@ use lemmy_api_common::{ context::LemmyContext, tagline::{ListTaglines, ListTaglinesResponse}, }; -use lemmy_db_views::structs::{TaglineView, LocalUserView, SiteView}; +use lemmy_db_views::structs::{LocalUserView, SiteView, TaglineView}; use lemmy_utils::error::LemmyError; #[tracing::instrument(skip(context))] @@ -12,7 +12,6 @@ pub async fn list_taglines( local_user_view: Option, context: Data, ) -> Result, LemmyError> { - let local_site = SiteView::read_local(&mut context.pool()).await?; let taglines = TaglineView::list( &mut context.pool(), @@ -20,7 +19,7 @@ pub async fn list_taglines( data.page, data.limit, ) - .await + .await .map_err(|e| anyhow::anyhow!("Failed to construct taglines response: {e}"))?; Ok(Json(ListTaglinesResponse { taglines })) diff --git a/crates/api_crud/src/tagline/mod.rs b/crates/api_crud/src/tagline/mod.rs index 651aed76e..d17e233fb 100644 --- a/crates/api_crud/src/tagline/mod.rs +++ b/crates/api_crud/src/tagline/mod.rs @@ -1 +1 @@ -pub mod list; \ No newline at end of file +pub mod list; diff --git a/crates/db_views/src/lib.rs b/crates/db_views/src/lib.rs index 64bb58d63..6cc951c86 100644 --- a/crates/db_views/src/lib.rs +++ b/crates/db_views/src/lib.rs @@ -21,8 +21,8 @@ pub mod private_message_view; pub mod registration_application_view; #[cfg(feature = "full")] pub mod site_view; +pub mod structs; #[cfg(feature = "full")] pub mod tagline_view; -pub mod structs; #[cfg(feature = "full")] pub mod vote_view; diff --git a/crates/db_views/src/structs.rs b/crates/db_views/src/structs.rs index 849897f73..f9ad4dcd3 100644 --- a/crates/db_views/src/structs.rs +++ b/crates/db_views/src/structs.rs @@ -18,7 +18,7 @@ use lemmy_db_schema::{ private_message::PrivateMessage, private_message_report::PrivateMessageReport, registration_application::RegistrationApplication, - site::Site, + site::Site, tagline::Tagline, }, SubscribedType, diff --git a/crates/db_views/src/tagline_view.rs b/crates/db_views/src/tagline_view.rs index 35abb1706..954460c58 100644 --- a/crates/db_views/src/tagline_view.rs +++ b/crates/db_views/src/tagline_view.rs @@ -9,7 +9,6 @@ use lemmy_db_schema::{ }; impl TaglineView { - pub async fn list( pool: &mut DbPool<'_>, for_local_site_id: LocalSiteId, @@ -29,11 +28,11 @@ impl TaglineView { let mut result = Vec::new(); for tagline in &taglines { - result.push(TaglineView { - tagline: tagline.clone() + result.push(TaglineView { + tagline: tagline.clone(), }); } Ok(result) } - } +} diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index e309d2daf..ea84fd249 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -104,10 +104,10 @@ use lemmy_api_crud::{ update::update_community, }, custom_emoji::{ - create::create_custom_emoji, - delete::delete_custom_emoji, - list::list_custom_emojis, - update::update_custom_emoji + create::create_custom_emoji, + delete::delete_custom_emoji, + list::list_custom_emojis, + update::update_custom_emoji, }, post::{ create::create_post, @@ -123,7 +123,7 @@ use lemmy_api_crud::{ update::update_private_message, }, site::{create::create_site, read::get_site, update::update_site}, - tagline::list::list_taglines, + tagline::list::list_taglines, user::{create::register, delete::delete_account}, }; use lemmy_apub::api::{ @@ -369,8 +369,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .service( web::scope("/tagline") .wrap(rate_limit.message()) - .route("/list", web::get().to(list_taglines)) - ) + .route("/list", web::get().to(list_taglines)), + ), ); cfg.service( web::scope("/sitemap.xml") From 17e2834e1b032e5d9ab3515c4a6fbfcdff6aad57 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Tue, 2 Apr 2024 22:27:53 +0200 Subject: [PATCH 04/26] Remove unecessary TaglineView --- crates/api_common/src/tagline.rs | 4 +-- crates/api_crud/src/tagline/list.rs | 8 +++--- crates/db_schema/src/impls/tagline.rs | 21 +++++++++++++-- crates/db_views/src/lib.rs | 2 -- crates/db_views/src/structs.rs | 10 ------- crates/db_views/src/tagline_view.rs | 38 --------------------------- 6 files changed, 25 insertions(+), 58 deletions(-) delete mode 100644 crates/db_views/src/tagline_view.rs diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index 4728d0cbb..332a838c2 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -1,4 +1,4 @@ -use lemmy_db_views::structs::TaglineView; +use lemmy_db_schema::source::tagline::Tagline; use serde::{Deserialize, Serialize}; use serde_with::skip_serializing_none; #[cfg(feature = "full")] @@ -9,7 +9,7 @@ use ts_rs::TS; #[cfg_attr(feature = "full", ts(export))] /// A response for custom emojis. pub struct ListTaglinesResponse { - pub taglines: Vec, + pub taglines: Vec, } #[skip_serializing_none] diff --git a/crates/api_crud/src/tagline/list.rs b/crates/api_crud/src/tagline/list.rs index 22deeeb61..d8f067ef1 100644 --- a/crates/api_crud/src/tagline/list.rs +++ b/crates/api_crud/src/tagline/list.rs @@ -3,7 +3,8 @@ use lemmy_api_common::{ context::LemmyContext, tagline::{ListTaglines, ListTaglinesResponse}, }; -use lemmy_db_views::structs::{LocalUserView, SiteView, TaglineView}; +use lemmy_db_schema::source::tagline::Tagline; +use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_utils::error::LemmyError; #[tracing::instrument(skip(context))] @@ -13,14 +14,13 @@ pub async fn list_taglines( context: Data, ) -> Result, LemmyError> { let local_site = SiteView::read_local(&mut context.pool()).await?; - let taglines = TaglineView::list( + let taglines = Tagline::list( &mut context.pool(), local_site.local_site.id, data.page, data.limit, ) - .await - .map_err(|e| anyhow::anyhow!("Failed to construct taglines response: {e}"))?; + .await?; Ok(Json(ListTaglinesResponse { taglines })) } diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index be4860e17..94faee249 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -1,8 +1,8 @@ use crate::{ newtypes::LocalSiteId, - schema::tagline::dsl::{local_site_id, tagline}, + schema::tagline::dsl::{local_site_id, published, tagline}, source::tagline::{Tagline, TaglineForm}, - utils::{get_conn, DbPool}, + utils::{get_conn, limit_and_offset, DbPool}, }; use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::{AsyncPgConnection, RunQueryDsl}; @@ -55,4 +55,21 @@ impl Tagline { .get_results::(conn) .await } + + pub async fn list( + pool: &mut DbPool<'_>, + for_local_site_id: LocalSiteId, + page: Option, + limit: Option, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + let (limit, offset) = limit_and_offset(page, limit)?; + tagline + .order(published.desc()) + .offset(offset) + .limit(limit) + .filter(local_site_id.eq(for_local_site_id)) + .get_results::(conn) + .await + } } diff --git a/crates/db_views/src/lib.rs b/crates/db_views/src/lib.rs index 6cc951c86..73310d743 100644 --- a/crates/db_views/src/lib.rs +++ b/crates/db_views/src/lib.rs @@ -23,6 +23,4 @@ pub mod registration_application_view; pub mod site_view; pub mod structs; #[cfg(feature = "full")] -pub mod tagline_view; -#[cfg(feature = "full")] pub mod vote_view; diff --git a/crates/db_views/src/structs.rs b/crates/db_views/src/structs.rs index f9ad4dcd3..a290ca4a1 100644 --- a/crates/db_views/src/structs.rs +++ b/crates/db_views/src/structs.rs @@ -19,7 +19,6 @@ use lemmy_db_schema::{ private_message_report::PrivateMessageReport, registration_application::RegistrationApplication, site::Site, - tagline::Tagline, }, SubscribedType, }; @@ -194,15 +193,6 @@ pub struct SiteView { pub counts: SiteAggregates, } -#[derive(Debug, Serialize, Deserialize, Clone)] -#[cfg_attr(feature = "full", derive(TS, Queryable))] -#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] -#[cfg_attr(feature = "full", ts(export))] -/// A custom emoji view. -pub struct TaglineView { - pub tagline: Tagline, -} - #[derive(Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(TS, Queryable))] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] diff --git a/crates/db_views/src/tagline_view.rs b/crates/db_views/src/tagline_view.rs deleted file mode 100644 index 954460c58..000000000 --- a/crates/db_views/src/tagline_view.rs +++ /dev/null @@ -1,38 +0,0 @@ -use crate::structs::TaglineView; -use diesel::{result::Error, ExpressionMethods, QueryDsl}; -use diesel_async::RunQueryDsl; -use lemmy_db_schema::{ - newtypes::LocalSiteId, - schema::tagline, - source::tagline::Tagline, - utils::{get_conn, limit_and_offset, DbPool}, -}; - -impl TaglineView { - pub async fn list( - pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, - page: Option, - limit: Option, - ) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let (limit, offset) = limit_and_offset(page, limit)?; - let taglines = tagline::table - .filter(tagline::local_site_id.eq(for_local_site_id)) - .order(tagline::id) - .select(tagline::all_columns) - .limit(limit) - .offset(offset) - .load::(conn) - .await?; - - let mut result = Vec::new(); - for tagline in &taglines { - result.push(TaglineView { - tagline: tagline.clone(), - }); - } - - Ok(result) - } -} From 486c746eb9be84992489edc52dfc5da9e47fcf93 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Tue, 2 Apr 2024 23:22:50 +0200 Subject: [PATCH 05/26] Add category filter for custom emoji --- crates/api_common/src/custom_emoji.rs | 5 +++-- crates/api_crud/src/custom_emoji/list.rs | 4 ++-- crates/db_views/src/custom_emoji_view.rs | 13 +++++++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/crates/api_common/src/custom_emoji.rs b/crates/api_common/src/custom_emoji.rs index eab09f2a9..7a846eae2 100644 --- a/crates/api_common/src/custom_emoji.rs +++ b/crates/api_common/src/custom_emoji.rs @@ -57,11 +57,12 @@ pub struct ListCustomEmojisResponse { } #[skip_serializing_none] -#[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)] +#[derive(Debug, Serialize, Deserialize, Clone, Default, PartialEq, Eq, Hash)] #[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", ts(export))] -/// Fetches a list of registration applications. +/// Fetches a list of custom emojis. pub struct ListCustomEmojis { pub page: Option, pub limit: Option, + pub category: Option, } diff --git a/crates/api_crud/src/custom_emoji/list.rs b/crates/api_crud/src/custom_emoji/list.rs index 811e32652..dab3e8823 100644 --- a/crates/api_crud/src/custom_emoji/list.rs +++ b/crates/api_crud/src/custom_emoji/list.rs @@ -16,11 +16,11 @@ pub async fn list_custom_emojis( let custom_emojis = CustomEmojiView::list( &mut context.pool(), local_site.local_site.id, + &data.category, data.page, data.limit, ) - .await - .map_err(|e| anyhow::anyhow!("Failed to construct custom emojis response: {e}"))?; + .await?; Ok(Json(ListCustomEmojisResponse { custom_emojis })) } diff --git a/crates/db_views/src/custom_emoji_view.rs b/crates/db_views/src/custom_emoji_view.rs index ef391954b..70dfb31e5 100644 --- a/crates/db_views/src/custom_emoji_view.rs +++ b/crates/db_views/src/custom_emoji_view.rs @@ -60,16 +60,25 @@ impl CustomEmojiView { pub async fn list( pool: &mut DbPool<'_>, for_local_site_id: LocalSiteId, + category: &Option, page: Option, limit: Option, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; let (limit, offset) = limit_and_offset(page, limit)?; - let emojis = custom_emoji::table - .filter(custom_emoji::local_site_id.eq(for_local_site_id)) + let mut query = custom_emoji::table .left_join( custom_emoji_keyword::table.on(custom_emoji_keyword::custom_emoji_id.eq(custom_emoji::id)), ) + .into_boxed(); + + query = query.filter(custom_emoji::local_site_id.eq(for_local_site_id)); + + if let Some(category) = category { + query = query.filter(custom_emoji::category.eq(category)) + } + + let emojis = query .order(custom_emoji::category) .then_order_by(custom_emoji::id) .select(( From a1a7e2ca339b1fd7a664802ad953180e17d94f3c Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Wed, 3 Apr 2024 22:14:25 +0200 Subject: [PATCH 06/26] Add create tagline endpoint --- crates/api_common/src/tagline.rs | 15 ++++++++++++ crates/api_crud/src/tagline/create.rs | 34 ++++++++++++++++++++++++++ crates/api_crud/src/tagline/mod.rs | 1 + crates/db_schema/src/impls/tagline.rs | 15 ++++++++++-- crates/db_schema/src/source/tagline.rs | 2 +- src/api_routes_http.rs | 3 ++- 6 files changed, 66 insertions(+), 4 deletions(-) create mode 100644 crates/api_crud/src/tagline/create.rs diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index 332a838c2..cfc2501e8 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -4,6 +4,21 @@ use serde_with::skip_serializing_none; #[cfg(feature = "full")] use ts_rs::TS; +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +/// Create a tagline +pub struct CreateTagline { + pub content: String, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +pub struct TaglineResponse { + pub tagline: Tagline, +} + #[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/create.rs b/crates/api_crud/src/tagline/create.rs new file mode 100644 index 000000000..43a48c0e7 --- /dev/null +++ b/crates/api_crud/src/tagline/create.rs @@ -0,0 +1,34 @@ +use activitypub_federation::config::Data; +use actix_web::web::Json; +use lemmy_api_common::{ + context::LemmyContext, + tagline::{CreateTagline, TaglineResponse}, + utils::is_admin, +}; +use lemmy_db_schema::source::{ + local_site::LocalSite, + tagline::{Tagline, TaglineInsertForm}, +}; +use lemmy_db_views::structs::LocalUserView; +use lemmy_utils::error::LemmyError; + +#[tracing::instrument(skip(context))] +pub async fn create_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 = TaglineInsertForm { + local_site_id: local_site.id, + content: data.content.to_string(), + updated: None, + }; + + let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?; + + Ok(Json(TaglineResponse { tagline })) +} diff --git a/crates/api_crud/src/tagline/mod.rs b/crates/api_crud/src/tagline/mod.rs index d17e233fb..fa6613e51 100644 --- a/crates/api_crud/src/tagline/mod.rs +++ b/crates/api_crud/src/tagline/mod.rs @@ -1 +1,2 @@ +pub mod create; pub mod list; diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index 94faee249..9b3dffc13 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, schema::tagline::dsl::{local_site_id, published, tagline}, - source::tagline::{Tagline, TaglineForm}, + source::tagline::{Tagline, TaglineInsertForm}, utils::{get_conn, limit_and_offset, DbPool}, }; use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; @@ -22,7 +22,7 @@ impl Tagline { Self::clear(conn).await?; for item in list { - let form = TaglineForm { + let form = TaglineInsertForm { local_site_id: for_local_site_id, content: item, updated: None, @@ -41,6 +41,17 @@ impl Tagline { } } + pub async fn create( + pool: &mut DbPool<'_>, + form: &TaglineInsertForm, + ) -> Result { + let conn = &mut get_conn(pool).await?; + insert_into(tagline) + .values(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/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index dbc904a78..c08d9f22c 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -32,7 +32,7 @@ pub struct Tagline { #[derive(Clone, Default)] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = tagline))] -pub struct TaglineForm { +pub struct TaglineInsertForm { pub local_site_id: LocalSiteId, pub content: String, pub updated: Option>, diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index ea84fd249..80b7ce630 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -123,7 +123,7 @@ use lemmy_api_crud::{ update::update_private_message, }, site::{create::create_site, read::get_site, update::update_site}, - tagline::list::list_taglines, + tagline::{create::create_tagline, list::list_taglines}, user::{create::register, delete::delete_account}, }; use lemmy_apub::api::{ @@ -369,6 +369,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .service( web::scope("/tagline") .wrap(rate_limit.message()) + .route("/", web::post().to(create_tagline)) .route("/list", web::get().to(list_taglines)), ), ); From ab938062e4730a3fb6596848d34aa307bbb3c83a Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Wed, 3 Apr 2024 22:33:43 +0200 Subject: [PATCH 07/26] Add update tagline endpoint --- crates/api_common/src/tagline.rs | 11 ++++++++- crates/api_crud/src/tagline/mod.rs | 1 + crates/api_crud/src/tagline/update.rs | 34 ++++++++++++++++++++++++++ crates/db_schema/src/impls/tagline.rs | 16 ++++++++++-- crates/db_schema/src/newtypes.rs | 6 +++++ crates/db_schema/src/source/tagline.rs | 9 +++++++ src/api_routes_http.rs | 7 +++++- 7 files changed, 80 insertions(+), 4 deletions(-) create mode 100644 crates/api_crud/src/tagline/update.rs 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)), ), ); From e536d39c2b7c46e680b40c7dbe039f73af6df805 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Wed, 3 Apr 2024 22:42:25 +0200 Subject: [PATCH 08/26] Add delete tagline endpoint --- crates/api_common/src/tagline.rs | 8 ++++++++ crates/api_crud/src/tagline/delete.rs | 25 +++++++++++++++++++++++++ crates/api_crud/src/tagline/mod.rs | 1 + crates/db_schema/src/impls/tagline.rs | 10 ++++++---- src/api_routes_http.rs | 2 ++ 5 files changed, 42 insertions(+), 4 deletions(-) create mode 100644 crates/api_crud/src/tagline/delete.rs diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index e2dfd8ffd..88d5b64d3 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -21,6 +21,14 @@ pub struct UpdateTagline { pub content: String, } +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] +#[cfg_attr(feature = "full", derive(TS))] +#[cfg_attr(feature = "full", ts(export))] +/// Delete a tagline +pub struct DeleteTagline { + pub id: TaglineId, +} + #[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/delete.rs b/crates/api_crud/src/tagline/delete.rs new file mode 100644 index 000000000..26ae7fabb --- /dev/null +++ b/crates/api_crud/src/tagline/delete.rs @@ -0,0 +1,25 @@ +use activitypub_federation::config::Data; +use actix_web::web::Json; +use lemmy_api_common::{ + context::LemmyContext, + tagline::DeleteTagline, + utils::is_admin, + SuccessResponse, +}; +use lemmy_db_schema::source::tagline::Tagline; +use lemmy_db_views::structs::LocalUserView; +use lemmy_utils::error::LemmyError; + +#[tracing::instrument(skip(context))] +pub async fn delete_tagline( + data: Json, + context: Data, + local_user_view: LocalUserView, +) -> Result, LemmyError> { + // Make sure user is an admin + is_admin(&local_user_view)?; + + Tagline::delete(&mut context.pool(), data.id).await?; + + Ok(Json(SuccessResponse::default())) +} diff --git a/crates/api_crud/src/tagline/mod.rs b/crates/api_crud/src/tagline/mod.rs index 9ab1b43f8..709cbaf88 100644 --- a/crates/api_crud/src/tagline/mod.rs +++ b/crates/api_crud/src/tagline/mod.rs @@ -1,3 +1,4 @@ pub mod create; pub mod list; pub mod update; +pub mod delete; diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index d70d2fcd8..686474f22 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -41,10 +41,7 @@ impl Tagline { } } - pub async fn create( - pool: &mut DbPool<'_>, - form: &TaglineInsertForm, - ) -> Result { + pub async fn create(pool: &mut DbPool<'_>, form: &TaglineInsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(tagline) .values(form) @@ -64,6 +61,11 @@ impl Tagline { .await } + pub async fn delete(pool: &mut DbPool<'_>, tagline_id: TaglineId) -> Result { + let conn = &mut get_conn(pool).await?; + diesel::delete(tagline.find(tagline_id)).execute(conn).await + } + async fn clear(conn: &mut AsyncPgConnection) -> Result { diesel::delete(tagline).execute(conn).await } diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index 305d66663..bfe567d1a 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -125,6 +125,7 @@ use lemmy_api_crud::{ site::{create::create_site, read::get_site, update::update_site}, tagline::{ create::create_tagline, + delete::delete_tagline, list::list_taglines, update::update_tagline }, @@ -375,6 +376,7 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .wrap(rate_limit.message()) .route("/", web::post().to(create_tagline)) .route("/", web::put().to(update_tagline)) + .route("/delete", web::post().to(delete_tagline)) .route("/list", web::get().to(list_taglines)), ), ); From 5358908c17c61b91e9d6162e8d0b439081d55beb Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Wed, 3 Apr 2024 22:49:02 +0200 Subject: [PATCH 09/26] Format through lint.sh --- crates/api_crud/src/tagline/mod.rs | 2 +- crates/api_crud/src/tagline/update.rs | 13 ++++++++----- crates/db_schema/src/source/tagline.rs | 2 +- src/api_routes_http.rs | 8 ++++---- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/crates/api_crud/src/tagline/mod.rs b/crates/api_crud/src/tagline/mod.rs index 709cbaf88..ffd48daf6 100644 --- a/crates/api_crud/src/tagline/mod.rs +++ b/crates/api_crud/src/tagline/mod.rs @@ -1,4 +1,4 @@ pub mod create; +pub mod delete; pub mod list; pub mod update; -pub mod delete; diff --git a/crates/api_crud/src/tagline/update.rs b/crates/api_crud/src/tagline/update.rs index fe5709375..d94cbd46a 100644 --- a/crates/api_crud/src/tagline/update.rs +++ b/crates/api_crud/src/tagline/update.rs @@ -2,13 +2,16 @@ use activitypub_federation::config::Data; use actix_web::web::Json; use lemmy_api_common::{ context::LemmyContext, - tagline::{UpdateTagline, TaglineResponse}, + tagline::{TaglineResponse, UpdateTagline}, utils::is_admin, }; -use lemmy_db_schema::{source::{ - local_site::LocalSite, - tagline::{Tagline, TaglineUpdateForm}, -}, utils::naive_now}; +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; diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index 6194d25c4..2fd913db6 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -45,4 +45,4 @@ 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 bfe567d1a..5c6461bae 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -124,10 +124,10 @@ use lemmy_api_crud::{ }, site::{create::create_site, read::get_site, update::update_site}, tagline::{ - create::create_tagline, - delete::delete_tagline, - list::list_taglines, - update::update_tagline + create::create_tagline, + delete::delete_tagline, + list::list_taglines, + update::update_tagline, }, user::{create::register, delete::delete_account}, }; From 2b1fdf106b1fb01c5aab191ff483fee1176045ad Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Thu, 4 Apr 2024 21:57:43 +0200 Subject: [PATCH 10/26] Remove custom_emojis and taglines from site resource --- crates/api/src/site/leave_admin.rs | 8 +----- crates/api_common/src/site.rs | 10 ------- crates/api_crud/src/site/create.rs | 10 +------ crates/api_crud/src/site/read.rs | 8 +----- crates/api_crud/src/site/update.rs | 10 +------ crates/db_schema/src/impls/tagline.rs | 39 +-------------------------- src/api_routes_http.rs | 4 +-- 7 files changed, 7 insertions(+), 82 deletions(-) diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index 0d149d07d..2ca010761 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -7,11 +7,10 @@ use lemmy_db_schema::{ local_site_url_blocklist::LocalSiteUrlBlocklist, local_user::{LocalUser, LocalUserUpdateForm}, moderator::{ModAdd, ModAddForm}, - tagline::Tagline, }, traits::Crud, }; -use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView}; +use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_db_views_actor::structs::PersonView; use lemmy_utils::{ error::{LemmyError, LemmyErrorType}, @@ -60,9 +59,6 @@ pub async fn leave_admin( let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; - let taglines = Tagline::get_all(&mut context.pool(), site_view.local_site.id).await?; - let custom_emojis = - CustomEmojiView::get_all(&mut context.pool(), site_view.local_site.id).await?; let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?; Ok(Json(GetSiteResponse { @@ -72,8 +68,6 @@ pub async fn leave_admin( my_user: None, all_languages, discussion_languages, - taglines, - custom_emojis, blocked_urls, })) } diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index d87cbdaaf..e5ce3c2f8 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -7,7 +7,6 @@ use lemmy_db_schema::{ instance::Instance, language::Language, local_site_url_blocklist::LocalSiteUrlBlocklist, - tagline::Tagline, }, ListingType, ModlogActionType, @@ -18,7 +17,6 @@ use lemmy_db_schema::{ }; use lemmy_db_views::structs::{ CommentView, - CustomEmojiView, LocalUserView, PostView, RegistrationApplicationView, @@ -190,7 +188,6 @@ pub struct CreateSite { pub captcha_difficulty: Option, pub allowed_instances: Option>, pub blocked_instances: Option>, - pub taglines: Option>, pub registration_mode: Option, pub content_warning: Option, pub default_post_listing_mode: Option, @@ -271,8 +268,6 @@ pub struct EditSite { pub blocked_instances: Option>, /// A list of blocked URLs pub blocked_urls: Option>, - /// A list of taglines shown at the top of the front page. - pub taglines: Option>, pub registration_mode: Option, /// Whether to email admins for new reports. pub reports_email_admins: Option, @@ -289,7 +284,6 @@ pub struct EditSite { /// The response for a site. pub struct SiteResponse { pub site_view: SiteView, - pub taglines: Vec, } #[skip_serializing_none] @@ -304,10 +298,6 @@ pub struct GetSiteResponse { pub my_user: Option, pub all_languages: Vec, pub discussion_languages: Vec, - /// A list of taglines shown at the top of the front page. - pub taglines: Vec, - /// A list of custom emojis your site supports. - pub custom_emojis: Vec, pub blocked_urls: Vec, } diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs index 8542117e7..b917cc999 100644 --- a/crates/api_crud/src/site/create.rs +++ b/crates/api_crud/src/site/create.rs @@ -20,7 +20,6 @@ use lemmy_db_schema::{ local_site::{LocalSite, LocalSiteUpdateForm}, local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm}, site::{Site, SiteUpdateForm}, - tagline::Tagline, }, traits::Crud, utils::{diesel_option_overwrite, naive_now}, @@ -131,17 +130,11 @@ pub async fn create_site( let site_view = SiteView::read_local(&mut context.pool()).await?; - let new_taglines = data.taglines.clone(); - let taglines = Tagline::replace(&mut context.pool(), local_site.id, new_taglines).await?; - let rate_limit_config = local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit); context.rate_limit_cell().set_config(rate_limit_config); - Ok(Json(SiteResponse { - site_view, - taglines, - })) + Ok(Json(SiteResponse { site_view })) } fn validate_create_payload(local_site: &LocalSite, create_site: &CreateSite) -> LemmyResult<()> { @@ -584,7 +577,6 @@ mod tests { captcha_difficulty: None, allowed_instances: None, blocked_instances: None, - taglines: None, registration_mode: site_registration_mode, content_warning: None, default_post_listing_mode: None, diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index e99a222fa..637e85b8f 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -7,9 +7,8 @@ use lemmy_db_schema::source::{ actor_language::{LocalUserLanguage, SiteLanguage}, language::Language, local_site_url_blocklist::LocalSiteUrlBlocklist, - tagline::Tagline, }; -use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView}; +use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_db_views_actor::structs::{ CommunityBlockView, CommunityFollowerView, @@ -45,9 +44,6 @@ pub async fn get_site( let admins = PersonView::admins(&mut context.pool()).await?; let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; - let taglines = Tagline::get_all(&mut context.pool(), site_view.local_site.id).await?; - let custom_emojis = - CustomEmojiView::get_all(&mut context.pool(), site_view.local_site.id).await?; let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?; Ok(GetSiteResponse { site_view, @@ -56,8 +52,6 @@ pub async fn get_site( my_user: None, all_languages, discussion_languages, - taglines, - custom_emojis, blocked_urls, }) }) diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index 530dbb47f..25914bbf5 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -24,7 +24,6 @@ use lemmy_db_schema::{ local_site_url_blocklist::LocalSiteUrlBlocklist, local_user::LocalUser, site::{Site, SiteUpdateForm}, - tagline::Tagline, }, traits::Crud, utils::{diesel_option_overwrite, naive_now}, @@ -178,19 +177,13 @@ pub async fn update_site( .with_lemmy_type(LemmyErrorType::CouldntSetAllEmailVerified)?; } - let new_taglines = data.taglines.clone(); - let taglines = Tagline::replace(&mut context.pool(), local_site.id, new_taglines).await?; - let site_view = SiteView::read_local(&mut context.pool()).await?; let rate_limit_config = local_site_rate_limit_to_rate_limit_config(&site_view.local_site_rate_limit); context.rate_limit_cell().set_config(rate_limit_config); - Ok(Json(SiteResponse { - site_view, - taglines, - })) + Ok(Json(SiteResponse { site_view })) } fn validate_update_payload(local_site: &LocalSite, edit_site: &EditSite) -> LemmyResult<()> { @@ -593,7 +586,6 @@ mod tests { allowed_instances: None, blocked_instances: None, blocked_urls: None, - taglines: None, registration_mode: site_registration_mode, reports_email_admins: None, content_warning: None, diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index 686474f22..9e28c62ad 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -5,42 +5,9 @@ use crate::{ utils::{get_conn, limit_and_offset, DbPool}, }; use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; -use diesel_async::{AsyncPgConnection, RunQueryDsl}; +use diesel_async::RunQueryDsl; impl Tagline { - pub async fn replace( - pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, - list_content: Option>, - ) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - if let Some(list) = list_content { - conn - .build_transaction() - .run(|conn| { - Box::pin(async move { - Self::clear(conn).await?; - - for item in list { - let form = TaglineInsertForm { - local_site_id: for_local_site_id, - content: item, - updated: None, - }; - insert_into(tagline) - .values(form) - .get_result::(conn) - .await?; - } - Self::get_all(&mut conn.into(), for_local_site_id).await - }) as _ - }) - .await - } else { - Self::get_all(&mut conn.into(), for_local_site_id).await - } - } - pub async fn create(pool: &mut DbPool<'_>, form: &TaglineInsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(tagline) @@ -66,10 +33,6 @@ impl Tagline { diesel::delete(tagline.find(tagline_id)).execute(conn).await } - async fn clear(conn: &mut AsyncPgConnection) -> Result { - diesel::delete(tagline).execute(conn).await - } - pub async fn get_all( pool: &mut DbPool<'_>, for_local_site_id: LocalSiteId, diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index 5c6461bae..afd4dafe3 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -374,8 +374,8 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .service( web::scope("/tagline") .wrap(rate_limit.message()) - .route("/", web::post().to(create_tagline)) - .route("/", web::put().to(update_tagline)) + .route("", web::post().to(create_tagline)) + .route("", web::put().to(update_tagline)) .route("/delete", web::post().to(delete_tagline)) .route("/list", web::get().to(list_taglines)), ), From f1b993f90ed262b20e03bde4ed4233afd11695d0 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Thu, 4 Apr 2024 23:07:51 +0200 Subject: [PATCH 11/26] Get random tagline on site requets --- crates/api/src/site/leave_admin.rs | 3 +++ crates/api_common/src/site.rs | 2 ++ crates/api_crud/src/site/read.rs | 3 +++ crates/db_schema/src/impls/tagline.rs | 17 ++++++++++++++++- 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index 2ca010761..297c84084 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -7,6 +7,7 @@ use lemmy_db_schema::{ local_site_url_blocklist::LocalSiteUrlBlocklist, local_user::{LocalUser, LocalUserUpdateForm}, moderator::{ModAdd, ModAddForm}, + tagline::Tagline, }, traits::Crud, }; @@ -60,6 +61,7 @@ pub async fn leave_admin( let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?; + let tagline = Tagline::get_random(&mut context.pool(), site_view.local_site.id).await?; Ok(Json(GetSiteResponse { site_view, @@ -69,5 +71,6 @@ pub async fn leave_admin( all_languages, discussion_languages, blocked_urls, + tagline, })) } diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index e5ce3c2f8..7fae24783 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -7,6 +7,7 @@ use lemmy_db_schema::{ instance::Instance, language::Language, local_site_url_blocklist::LocalSiteUrlBlocklist, + tagline::Tagline, }, ListingType, ModlogActionType, @@ -298,6 +299,7 @@ pub struct GetSiteResponse { pub my_user: Option, pub all_languages: Vec, pub discussion_languages: Vec, + pub tagline: Option, pub blocked_urls: Vec, } diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 637e85b8f..85a63a59e 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -7,6 +7,7 @@ use lemmy_db_schema::source::{ actor_language::{LocalUserLanguage, SiteLanguage}, language::Language, local_site_url_blocklist::LocalSiteUrlBlocklist, + tagline::Tagline, }; use lemmy_db_views::structs::{LocalUserView, SiteView}; use lemmy_db_views_actor::structs::{ @@ -45,6 +46,7 @@ pub async fn get_site( let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?; + let tagline = Tagline::get_random(&mut context.pool(), site_view.local_site.id).await?; Ok(GetSiteResponse { site_view, admins, @@ -53,6 +55,7 @@ pub async fn get_site( all_languages, discussion_languages, blocked_urls, + tagline, }) }) .await diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index 9e28c62ad..1baabd9d1 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -4,7 +4,7 @@ use crate::{ source::tagline::{Tagline, TaglineInsertForm, TaglineUpdateForm}, utils::{get_conn, limit_and_offset, DbPool}, }; -use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; +use diesel::{insert_into, result::Error, ExpressionMethods, OptionalExtension, QueryDsl}; use diesel_async::RunQueryDsl; impl Tagline { @@ -60,4 +60,19 @@ impl Tagline { .get_results::(conn) .await } + + pub async fn get_random( + pool: &mut DbPool<'_>, + for_local_site_id: LocalSiteId, + ) -> Result, Error> { + let conn = &mut get_conn(pool).await?; + sql_function!(fn random() -> Text); + tagline + .order(random()) + .limit(1) + .filter(local_site_id.eq(for_local_site_id)) + .first::(conn) + .await + .optional() + } } From 144c11224707c59caee527b9d417658073d1d46a Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Fri, 5 Apr 2024 22:36:08 +0200 Subject: [PATCH 12/26] Impl Crud for Tagline Remove superfluous properties --- crates/api_crud/src/tagline/create.rs | 10 ++++---- crates/api_crud/src/tagline/delete.rs | 2 +- crates/api_crud/src/tagline/update.rs | 12 ++++------ crates/db_schema/src/impls/tagline.rs | 33 ++++++++++++-------------- crates/db_schema/src/source/tagline.rs | 6 ++--- 5 files changed, 28 insertions(+), 35 deletions(-) diff --git a/crates/api_crud/src/tagline/create.rs b/crates/api_crud/src/tagline/create.rs index 43a48c0e7..29821d321 100644 --- a/crates/api_crud/src/tagline/create.rs +++ b/crates/api_crud/src/tagline/create.rs @@ -5,9 +5,12 @@ use lemmy_api_common::{ tagline::{CreateTagline, TaglineResponse}, utils::is_admin, }; -use lemmy_db_schema::source::{ - local_site::LocalSite, - tagline::{Tagline, TaglineInsertForm}, +use lemmy_db_schema::{ + source::{ + local_site::LocalSite, + tagline::{Tagline, TaglineInsertForm}, + }, + traits::Crud, }; use lemmy_db_views::structs::LocalUserView; use lemmy_utils::error::LemmyError; @@ -25,7 +28,6 @@ pub async fn create_tagline( let tagline_form = TaglineInsertForm { local_site_id: local_site.id, content: data.content.to_string(), - updated: None, }; let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?; diff --git a/crates/api_crud/src/tagline/delete.rs b/crates/api_crud/src/tagline/delete.rs index 26ae7fabb..9add3cfe6 100644 --- a/crates/api_crud/src/tagline/delete.rs +++ b/crates/api_crud/src/tagline/delete.rs @@ -6,7 +6,7 @@ use lemmy_api_common::{ utils::is_admin, SuccessResponse, }; -use lemmy_db_schema::source::tagline::Tagline; +use lemmy_db_schema::{source::tagline::Tagline, traits::Crud}; use lemmy_db_views::structs::LocalUserView; use lemmy_utils::error::LemmyError; diff --git a/crates/api_crud/src/tagline/update.rs b/crates/api_crud/src/tagline/update.rs index d94cbd46a..7db5c3dc5 100644 --- a/crates/api_crud/src/tagline/update.rs +++ b/crates/api_crud/src/tagline/update.rs @@ -6,10 +6,8 @@ use lemmy_api_common::{ utils::is_admin, }; use lemmy_db_schema::{ - source::{ - local_site::LocalSite, - tagline::{Tagline, TaglineUpdateForm}, - }, + source::tagline::{Tagline, TaglineUpdateForm}, + traits::Crud, utils::naive_now, }; use lemmy_db_views::structs::LocalUserView; @@ -21,14 +19,12 @@ pub async fn update_tagline( 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()), + content: Some(data.content.to_string()), + updated: Some(Some(naive_now())), }; let tagline = Tagline::update(&mut context.pool(), data.id, &tagline_form).await?; diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index 1baabd9d1..c872b1438 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -2,13 +2,19 @@ use crate::{ newtypes::{LocalSiteId, TaglineId}, schema::tagline::dsl::{local_site_id, published, tagline}, source::tagline::{Tagline, TaglineInsertForm, TaglineUpdateForm}, + traits::Crud, utils::{get_conn, limit_and_offset, DbPool}, }; use diesel::{insert_into, result::Error, ExpressionMethods, OptionalExtension, QueryDsl}; use diesel_async::RunQueryDsl; -impl Tagline { - pub async fn create(pool: &mut DbPool<'_>, form: &TaglineInsertForm) -> Result { +#[async_trait] +impl Crud for Tagline { + type InsertForm = TaglineInsertForm; + type UpdateForm = TaglineUpdateForm; + type IdType = TaglineId; + + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(tagline) .values(form) @@ -16,34 +22,25 @@ impl Tagline { .await } - pub async fn update( + async fn update( pool: &mut DbPool<'_>, tagline_id: TaglineId, - form: &TaglineUpdateForm, + new_tagline: &Self::UpdateForm, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(tagline.find(tagline_id)) - .set(form) + .set(new_tagline) .get_result::(conn) .await } - pub async fn delete(pool: &mut DbPool<'_>, tagline_id: TaglineId) -> Result { - let conn = &mut get_conn(pool).await?; - diesel::delete(tagline.find(tagline_id)).execute(conn).await - } - - pub async fn get_all( - pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, - ) -> Result, Error> { + async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { let conn = &mut get_conn(pool).await?; - tagline - .filter(local_site_id.eq(for_local_site_id)) - .get_results::(conn) - .await + diesel::delete(tagline.find(id)).execute(conn).await } +} +impl Tagline { pub async fn list( pool: &mut DbPool<'_>, for_local_site_id: LocalSiteId, diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index 2fd913db6..62dcf5a34 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -35,14 +35,12 @@ pub struct Tagline { pub struct TaglineInsertForm { pub local_site_id: LocalSiteId, 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>, + pub content: Option, + pub updated: Option>>, } From 5342a227654aa8c1f41b514a747df5c9cc156602 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Fri, 5 Apr 2024 22:36:37 +0200 Subject: [PATCH 13/26] Move tagline endpoints under /admin --- src/api_routes_http.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/api_routes_http.rs b/src/api_routes_http.rs index afd4dafe3..9bea2c88b 100644 --- a/src/api_routes_http.rs +++ b/src/api_routes_http.rs @@ -361,6 +361,14 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .route("/community", web::post().to(purge_community)) .route("/post", web::post().to(purge_post)) .route("/comment", web::post().to(purge_comment)), + ) + .service( + web::scope("/tagline") + .wrap(rate_limit.message()) + .route("", web::post().to(create_tagline)) + .route("", web::put().to(update_tagline)) + .route("/delete", web::post().to(delete_tagline)) + .route("/list", web::get().to(list_taglines)), ), ) .service( @@ -370,14 +378,6 @@ pub fn config(cfg: &mut web::ServiceConfig, rate_limit: &RateLimitCell) { .route("", web::put().to(update_custom_emoji)) .route("/delete", web::post().to(delete_custom_emoji)) .route("/list", web::get().to(list_custom_emojis)), - ) - .service( - web::scope("/tagline") - .wrap(rate_limit.message()) - .route("", web::post().to(create_tagline)) - .route("", web::put().to(update_tagline)) - .route("/delete", web::post().to(delete_tagline)) - .route("/list", web::get().to(list_taglines)), ), ); cfg.service( From bab417a73e64dddd032bff6af94a375b2bf7174e Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Fri, 5 Apr 2024 22:55:36 +0200 Subject: [PATCH 14/26] Impl Crud for CustomEmoji --- crates/api_crud/src/custom_emoji/create.rs | 11 +++++---- crates/api_crud/src/custom_emoji/delete.rs | 2 +- crates/api_crud/src/custom_emoji/update.rs | 11 +++++---- crates/db_schema/src/impls/custom_emoji.rs | 26 +++++++++++++--------- 4 files changed, 31 insertions(+), 19 deletions(-) diff --git a/crates/api_crud/src/custom_emoji/create.rs b/crates/api_crud/src/custom_emoji/create.rs index cd30ef1e9..54745577a 100644 --- a/crates/api_crud/src/custom_emoji/create.rs +++ b/crates/api_crud/src/custom_emoji/create.rs @@ -5,10 +5,13 @@ use lemmy_api_common::{ custom_emoji::{CreateCustomEmoji, CustomEmojiResponse}, utils::is_admin, }; -use lemmy_db_schema::source::{ - custom_emoji::{CustomEmoji, CustomEmojiInsertForm}, - custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, - local_site::LocalSite, +use lemmy_db_schema::{ + source::{ + custom_emoji::{CustomEmoji, CustomEmojiInsertForm}, + custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, + local_site::LocalSite, + }, + traits::Crud, }; use lemmy_db_views::structs::{CustomEmojiView, LocalUserView}; use lemmy_utils::error::LemmyError; diff --git a/crates/api_crud/src/custom_emoji/delete.rs b/crates/api_crud/src/custom_emoji/delete.rs index 93c5f8d80..9165a2a19 100644 --- a/crates/api_crud/src/custom_emoji/delete.rs +++ b/crates/api_crud/src/custom_emoji/delete.rs @@ -6,7 +6,7 @@ use lemmy_api_common::{ utils::is_admin, SuccessResponse, }; -use lemmy_db_schema::source::custom_emoji::CustomEmoji; +use lemmy_db_schema::{source::custom_emoji::CustomEmoji, traits::Crud}; use lemmy_db_views::structs::LocalUserView; use lemmy_utils::error::LemmyError; diff --git a/crates/api_crud/src/custom_emoji/update.rs b/crates/api_crud/src/custom_emoji/update.rs index 5a2631a62..0cabf77b9 100644 --- a/crates/api_crud/src/custom_emoji/update.rs +++ b/crates/api_crud/src/custom_emoji/update.rs @@ -5,10 +5,13 @@ use lemmy_api_common::{ custom_emoji::{CustomEmojiResponse, EditCustomEmoji}, utils::is_admin, }; -use lemmy_db_schema::source::{ - custom_emoji::{CustomEmoji, CustomEmojiUpdateForm}, - custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, - local_site::LocalSite, +use lemmy_db_schema::{ + source::{ + custom_emoji::{CustomEmoji, CustomEmojiUpdateForm}, + custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, + local_site::LocalSite, + }, + traits::Crud, }; use lemmy_db_views::structs::{CustomEmojiView, LocalUserView}; use lemmy_utils::error::LemmyError; diff --git a/crates/db_schema/src/impls/custom_emoji.rs b/crates/db_schema/src/impls/custom_emoji.rs index 050301659..80b2ebbff 100644 --- a/crates/db_schema/src/impls/custom_emoji.rs +++ b/crates/db_schema/src/impls/custom_emoji.rs @@ -8,35 +8,41 @@ use crate::{ custom_emoji::{CustomEmoji, CustomEmojiInsertForm, CustomEmojiUpdateForm}, custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, }, + traits::Crud, utils::{get_conn, DbPool}, }; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; -impl CustomEmoji { - pub async fn create(pool: &mut DbPool<'_>, form: &CustomEmojiInsertForm) -> Result { +#[async_trait] +impl Crud for CustomEmoji { + type InsertForm = CustomEmojiInsertForm; + type UpdateForm = CustomEmojiUpdateForm; + type IdType = CustomEmojiId; + + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result { let conn = &mut get_conn(pool).await?; insert_into(custom_emoji) .values(form) .get_result::(conn) .await } - pub async fn update( + + async fn update( pool: &mut DbPool<'_>, - emoji_id: CustomEmojiId, - form: &CustomEmojiUpdateForm, + emoji_id: Self::IdType, + new_custom_emoji: &Self::UpdateForm, ) -> Result { let conn = &mut get_conn(pool).await?; diesel::update(custom_emoji.find(emoji_id)) - .set(form) + .set(new_custom_emoji) .get_result::(conn) .await } - pub async fn delete(pool: &mut DbPool<'_>, emoji_id: CustomEmojiId) -> Result { + + async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { let conn = &mut get_conn(pool).await?; - diesel::delete(custom_emoji.find(emoji_id)) - .execute(conn) - .await + diesel::delete(custom_emoji.find(id)).execute(conn).await } } From ead51517e4a2f65c9abe78a7108ff1e255b3c760 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Fri, 5 Apr 2024 22:57:36 +0200 Subject: [PATCH 15/26] Remove delete from tagline and custom emoji impls --- crates/db_schema/src/impls/custom_emoji.rs | 5 ----- crates/db_schema/src/impls/tagline.rs | 5 ----- 2 files changed, 10 deletions(-) diff --git a/crates/db_schema/src/impls/custom_emoji.rs b/crates/db_schema/src/impls/custom_emoji.rs index 80b2ebbff..9ba359071 100644 --- a/crates/db_schema/src/impls/custom_emoji.rs +++ b/crates/db_schema/src/impls/custom_emoji.rs @@ -39,11 +39,6 @@ impl Crud for CustomEmoji { .get_result::(conn) .await } - - async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { - let conn = &mut get_conn(pool).await?; - diesel::delete(custom_emoji.find(id)).execute(conn).await - } } impl CustomEmojiKeyword { diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index c872b1438..1bd755562 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -33,11 +33,6 @@ impl Crud for Tagline { .get_result::(conn) .await } - - async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { - let conn = &mut get_conn(pool).await?; - diesel::delete(tagline.find(id)).execute(conn).await - } } impl Tagline { From abdfc90dbe120a4962eb9f6c96006f24f8980a57 Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Fri, 5 Apr 2024 23:20:59 +0200 Subject: [PATCH 16/26] Check markdown for tagline --- crates/api_common/src/tagline.rs | 2 +- crates/api_crud/src/tagline/update.rs | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index 88d5b64d3..40456277d 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -18,7 +18,7 @@ pub struct CreateTagline { /// Update a tagline pub struct UpdateTagline { pub id: TaglineId, - pub content: String, + pub content: Option, } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] diff --git a/crates/api_crud/src/tagline/update.rs b/crates/api_crud/src/tagline/update.rs index 7db5c3dc5..24475a24f 100644 --- a/crates/api_crud/src/tagline/update.rs +++ b/crates/api_crud/src/tagline/update.rs @@ -3,15 +3,18 @@ use actix_web::web::Json; use lemmy_api_common::{ context::LemmyContext, tagline::{TaglineResponse, UpdateTagline}, - utils::is_admin, + utils::{get_url_blocklist, is_admin, local_site_to_slur_regex, process_markdown_opt}, }; use lemmy_db_schema::{ - source::tagline::{Tagline, TaglineUpdateForm}, + source::{ + local_site::LocalSite, + tagline::{Tagline, TaglineUpdateForm}, + }, traits::Crud, utils::naive_now, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::error::LemmyError; +use lemmy_utils::{error::LemmyError, utils::validation::is_valid_body_field}; #[tracing::instrument(skip(context))] pub async fn update_tagline( @@ -22,8 +25,15 @@ pub async fn update_tagline( // Make sure user is an admin is_admin(&local_user_view)?; + let local_site = LocalSite::read(&mut context.pool()).await?; + + let slur_regex = local_site_to_slur_regex(&local_site); + let url_blocklist = get_url_blocklist(&context).await?; + let content = process_markdown_opt(&data.content, &slur_regex, &url_blocklist, &context).await?; + is_valid_body_field(&content, false)?; + let tagline_form = TaglineUpdateForm { - content: Some(data.content.to_string()), + content, updated: Some(Some(naive_now())), }; From b2b8c43757ca8c2bd77dc04a42857df8d5e2a39a Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Fri, 5 Apr 2024 23:34:43 +0200 Subject: [PATCH 17/26] Validate markdown on tagline --- crates/api_common/src/tagline.rs | 2 +- crates/api_crud/src/tagline/create.rs | 14 ++++++++++---- crates/db_schema/src/source/tagline.rs | 2 +- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index 40456277d..14f2be768 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -9,7 +9,7 @@ use ts_rs::TS; #[cfg_attr(feature = "full", ts(export))] /// Create a tagline pub struct CreateTagline { - pub content: String, + pub content: Option, } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] diff --git a/crates/api_crud/src/tagline/create.rs b/crates/api_crud/src/tagline/create.rs index 29821d321..85e0281a1 100644 --- a/crates/api_crud/src/tagline/create.rs +++ b/crates/api_crud/src/tagline/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Json; use lemmy_api_common::{ context::LemmyContext, tagline::{CreateTagline, TaglineResponse}, - utils::is_admin, + utils::{get_url_blocklist, is_admin, local_site_to_slur_regex, process_markdown_opt}, }; use lemmy_db_schema::{ source::{ @@ -13,7 +13,7 @@ use lemmy_db_schema::{ traits::Crud, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::error::LemmyError; +use lemmy_utils::{error::LemmyError, utils::validation::is_valid_body_field}; #[tracing::instrument(skip(context))] pub async fn create_tagline( @@ -21,13 +21,19 @@ pub async fn create_tagline( 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 local_site = LocalSite::read(&mut context.pool()).await?; + + let slur_regex = local_site_to_slur_regex(&local_site); + let url_blocklist = get_url_blocklist(&context).await?; + let content = process_markdown_opt(&data.content, &slur_regex, &url_blocklist, &context).await?; + is_valid_body_field(&content, false)?; + let tagline_form = TaglineInsertForm { local_site_id: local_site.id, - content: data.content.to_string(), + content, }; let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?; diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index 62dcf5a34..ba026ca70 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -34,7 +34,7 @@ pub struct Tagline { #[cfg_attr(feature = "full", diesel(table_name = tagline))] pub struct TaglineInsertForm { pub local_site_id: LocalSiteId, - pub content: String, + pub content: Option, } #[derive(Clone, Default)] From 7759602c9de8aaf7cd3d71dcb6cebb38d597094b Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Sat, 6 Apr 2024 12:16:19 +0200 Subject: [PATCH 18/26] Make content fields non optional Add error types for tagline validation --- crates/api_common/src/tagline.rs | 4 ++-- crates/api_crud/src/tagline/create.rs | 12 +++++++++--- crates/api_crud/src/tagline/update.rs | 12 +++++++++--- crates/db_schema/src/source/tagline.rs | 4 ++-- crates/utils/src/error.rs | 2 ++ crates/utils/src/utils/validation.rs | 21 +++++++++++++++++++++ 6 files changed, 45 insertions(+), 10 deletions(-) diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index 14f2be768..88d5b64d3 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -9,7 +9,7 @@ use ts_rs::TS; #[cfg_attr(feature = "full", ts(export))] /// Create a tagline pub struct CreateTagline { - pub content: Option, + pub content: String, } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] @@ -18,7 +18,7 @@ pub struct CreateTagline { /// Update a tagline pub struct UpdateTagline { pub id: TaglineId, - pub content: Option, + pub content: String, } #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] diff --git a/crates/api_crud/src/tagline/create.rs b/crates/api_crud/src/tagline/create.rs index 85e0281a1..01c5a8813 100644 --- a/crates/api_crud/src/tagline/create.rs +++ b/crates/api_crud/src/tagline/create.rs @@ -13,7 +13,7 @@ use lemmy_db_schema::{ traits::Crud, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::{error::LemmyError, utils::validation::is_valid_body_field}; +use lemmy_utils::{error::LemmyError, utils::validation::is_valid_tagline_content}; #[tracing::instrument(skip(context))] pub async fn create_tagline( @@ -28,8 +28,14 @@ pub async fn create_tagline( let slur_regex = local_site_to_slur_regex(&local_site); let url_blocklist = get_url_blocklist(&context).await?; - let content = process_markdown_opt(&data.content, &slur_regex, &url_blocklist, &context).await?; - is_valid_body_field(&content, false)?; + let processed_content = process_markdown_opt( + &Some(data.content.to_owned()), + &slur_regex, + &url_blocklist, + &context, + ) + .await?; + let content = is_valid_tagline_content(processed_content)?; let tagline_form = TaglineInsertForm { local_site_id: local_site.id, diff --git a/crates/api_crud/src/tagline/update.rs b/crates/api_crud/src/tagline/update.rs index 24475a24f..e80193f3e 100644 --- a/crates/api_crud/src/tagline/update.rs +++ b/crates/api_crud/src/tagline/update.rs @@ -14,7 +14,7 @@ use lemmy_db_schema::{ utils::naive_now, }; use lemmy_db_views::structs::LocalUserView; -use lemmy_utils::{error::LemmyError, utils::validation::is_valid_body_field}; +use lemmy_utils::{error::LemmyError, utils::validation::is_valid_tagline_content}; #[tracing::instrument(skip(context))] pub async fn update_tagline( @@ -29,8 +29,14 @@ pub async fn update_tagline( let slur_regex = local_site_to_slur_regex(&local_site); let url_blocklist = get_url_blocklist(&context).await?; - let content = process_markdown_opt(&data.content, &slur_regex, &url_blocklist, &context).await?; - is_valid_body_field(&content, false)?; + let processed_content = process_markdown_opt( + &Some(data.content.to_owned()), + &slur_regex, + &url_blocklist, + &context, + ) + .await?; + let content = is_valid_tagline_content(processed_content)?; let tagline_form = TaglineUpdateForm { content, diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index ba026ca70..df207e10e 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -34,13 +34,13 @@ pub struct Tagline { #[cfg_attr(feature = "full", diesel(table_name = tagline))] pub struct TaglineInsertForm { pub local_site_id: LocalSiteId, - pub content: Option, + pub content: String, } #[derive(Clone, Default)] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = tagline))] pub struct TaglineUpdateForm { - pub content: Option, + pub content: String, pub updated: Option>>, } diff --git a/crates/utils/src/error.rs b/crates/utils/src/error.rs index d25845894..8a102b655 100644 --- a/crates/utils/src/error.rs +++ b/crates/utils/src/error.rs @@ -165,6 +165,8 @@ pub enum LemmyErrorType { InvalidUnixTime, InvalidBotAction, CantBlockLocalInstance, + TaglineContentRequired, + TaglineContentLengthOverflow, Unknown(String), } diff --git a/crates/utils/src/utils/validation.rs b/crates/utils/src/utils/validation.rs index 93d581327..3a2881151 100644 --- a/crates/utils/src/utils/validation.rs +++ b/crates/utils/src/utils/validation.rs @@ -22,6 +22,8 @@ const BIO_MAX_LENGTH: usize = 300; const ALT_TEXT_MAX_LENGTH: usize = 300; const SITE_NAME_MAX_LENGTH: usize = 20; const SITE_NAME_MIN_LENGTH: usize = 1; +const TAGLINE_CONTENT_MIN_LENGTH: usize = 1; +const TAGLINE_CONTENT_MAX_LENGTH: usize = 50000; const SITE_DESCRIPTION_MAX_LENGTH: usize = 150; //Invisible unicode characters, taken from https://invisible-characters.com/ const FORBIDDEN_DISPLAY_CHARS: [char; 53] = [ @@ -169,6 +171,25 @@ pub fn is_valid_body_field(body: &Option, post: bool) -> LemmyResult<()> Ok(()) } +pub fn is_valid_tagline_content(content: Option) -> LemmyResult { + match content { + Some(content) => { + min_length_check( + &content, + TAGLINE_CONTENT_MIN_LENGTH, + LemmyErrorType::TaglineContentRequired, + )?; + max_length_check( + &content, + TAGLINE_CONTENT_MAX_LENGTH, + LemmyErrorType::TaglineContentLengthOverflow, + )?; + Ok(content) + } + None => Err(LemmyErrorType::TaglineContentRequired.into()), + } +} + pub fn is_valid_bio_field(bio: &str) -> LemmyResult<()> { max_length_check(bio, BIO_MAX_LENGTH, LemmyErrorType::BioLengthOverflow) } From 6ffcc1f98a4a80d474f4986c1f57223eab17a49b Mon Sep 17 00:00:00 2001 From: Freakazoid182 <> Date: Sat, 6 Apr 2024 12:33:49 +0200 Subject: [PATCH 19/26] Use process_markdown instead of process_markdown_opt --- crates/api_crud/src/tagline/create.rs | 14 ++++--------- crates/api_crud/src/tagline/update.rs | 12 +++-------- crates/utils/src/utils/validation.rs | 29 +++++++++++---------------- 3 files changed, 19 insertions(+), 36 deletions(-) diff --git a/crates/api_crud/src/tagline/create.rs b/crates/api_crud/src/tagline/create.rs index 01c5a8813..8111bd279 100644 --- a/crates/api_crud/src/tagline/create.rs +++ b/crates/api_crud/src/tagline/create.rs @@ -3,7 +3,7 @@ use actix_web::web::Json; use lemmy_api_common::{ context::LemmyContext, tagline::{CreateTagline, TaglineResponse}, - utils::{get_url_blocklist, is_admin, local_site_to_slur_regex, process_markdown_opt}, + utils::{get_url_blocklist, is_admin, local_site_to_slur_regex, process_markdown}, }; use lemmy_db_schema::{ source::{ @@ -28,18 +28,12 @@ pub async fn create_tagline( let slur_regex = local_site_to_slur_regex(&local_site); let url_blocklist = get_url_blocklist(&context).await?; - let processed_content = process_markdown_opt( - &Some(data.content.to_owned()), - &slur_regex, - &url_blocklist, - &context, - ) - .await?; - let content = is_valid_tagline_content(processed_content)?; + let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?; + is_valid_tagline_content(&content)?; let tagline_form = TaglineInsertForm { local_site_id: local_site.id, - content, + content: content.clone(), }; let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?; diff --git a/crates/api_crud/src/tagline/update.rs b/crates/api_crud/src/tagline/update.rs index e80193f3e..44a22f0c1 100644 --- a/crates/api_crud/src/tagline/update.rs +++ b/crates/api_crud/src/tagline/update.rs @@ -3,7 +3,7 @@ use actix_web::web::Json; use lemmy_api_common::{ context::LemmyContext, tagline::{TaglineResponse, UpdateTagline}, - utils::{get_url_blocklist, is_admin, local_site_to_slur_regex, process_markdown_opt}, + utils::{get_url_blocklist, is_admin, local_site_to_slur_regex, process_markdown}, }; use lemmy_db_schema::{ source::{ @@ -29,14 +29,8 @@ pub async fn update_tagline( let slur_regex = local_site_to_slur_regex(&local_site); let url_blocklist = get_url_blocklist(&context).await?; - let processed_content = process_markdown_opt( - &Some(data.content.to_owned()), - &slur_regex, - &url_blocklist, - &context, - ) - .await?; - let content = is_valid_tagline_content(processed_content)?; + let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?; + is_valid_tagline_content(&content)?; let tagline_form = TaglineUpdateForm { content, diff --git a/crates/utils/src/utils/validation.rs b/crates/utils/src/utils/validation.rs index 3a2881151..067a7520c 100644 --- a/crates/utils/src/utils/validation.rs +++ b/crates/utils/src/utils/validation.rs @@ -171,23 +171,18 @@ pub fn is_valid_body_field(body: &Option, post: bool) -> LemmyResult<()> Ok(()) } -pub fn is_valid_tagline_content(content: Option) -> LemmyResult { - match content { - Some(content) => { - min_length_check( - &content, - TAGLINE_CONTENT_MIN_LENGTH, - LemmyErrorType::TaglineContentRequired, - )?; - max_length_check( - &content, - TAGLINE_CONTENT_MAX_LENGTH, - LemmyErrorType::TaglineContentLengthOverflow, - )?; - Ok(content) - } - None => Err(LemmyErrorType::TaglineContentRequired.into()), - } +pub fn is_valid_tagline_content(content: &str) -> LemmyResult<()> { + min_length_check( + content, + TAGLINE_CONTENT_MIN_LENGTH, + LemmyErrorType::TaglineContentRequired, + )?; + max_length_check( + content, + TAGLINE_CONTENT_MAX_LENGTH, + LemmyErrorType::TaglineContentLengthOverflow, + )?; + Ok(()) } pub fn is_valid_bio_field(bio: &str) -> LemmyResult<()> { From 7ce89fb5e6a240ec334e025d39bfbb072cfa86cf Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Mon, 8 Apr 2024 21:28:55 +0200 Subject: [PATCH 20/26] Consolidate Tagline error types --- crates/utils/src/error.rs | 3 +-- crates/utils/src/utils/validation.rs | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/crates/utils/src/error.rs b/crates/utils/src/error.rs index 8a102b655..61bc5091b 100644 --- a/crates/utils/src/error.rs +++ b/crates/utils/src/error.rs @@ -165,8 +165,7 @@ pub enum LemmyErrorType { InvalidUnixTime, InvalidBotAction, CantBlockLocalInstance, - TaglineContentRequired, - TaglineContentLengthOverflow, + TaglineInvalid, Unknown(String), } diff --git a/crates/utils/src/utils/validation.rs b/crates/utils/src/utils/validation.rs index 067a7520c..dd89609f8 100644 --- a/crates/utils/src/utils/validation.rs +++ b/crates/utils/src/utils/validation.rs @@ -175,12 +175,12 @@ pub fn is_valid_tagline_content(content: &str) -> LemmyResult<()> { min_length_check( content, TAGLINE_CONTENT_MIN_LENGTH, - LemmyErrorType::TaglineContentRequired, + LemmyErrorType::TaglineInvalid, )?; max_length_check( content, TAGLINE_CONTENT_MAX_LENGTH, - LemmyErrorType::TaglineContentLengthOverflow, + LemmyErrorType::TaglineInvalid, )?; Ok(()) } From 8c7b3001ae5f1e015f28008f86dae0f818b0df33 Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Mon, 8 Apr 2024 21:39:17 +0200 Subject: [PATCH 21/26] Remove unecessary clone --- crates/api_crud/src/tagline/create.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/api_crud/src/tagline/create.rs b/crates/api_crud/src/tagline/create.rs index 8111bd279..0b33a03f9 100644 --- a/crates/api_crud/src/tagline/create.rs +++ b/crates/api_crud/src/tagline/create.rs @@ -33,7 +33,7 @@ pub async fn create_tagline( let tagline_form = TaglineInsertForm { local_site_id: local_site.id, - content: content.clone(), + content, }; let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?; From 562c909f4b37f566c23e12fd8bbc238652c56f12 Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Mon, 8 Apr 2024 21:40:11 +0200 Subject: [PATCH 22/26] Updat misleading comments --- crates/api_common/src/tagline.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/api_common/src/tagline.rs b/crates/api_common/src/tagline.rs index 88d5b64d3..3090a2678 100644 --- a/crates/api_common/src/tagline.rs +++ b/crates/api_common/src/tagline.rs @@ -39,7 +39,7 @@ pub struct TaglineResponse { #[derive(Debug, Serialize, Deserialize, Clone)] #[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", ts(export))] -/// A response for custom emojis. +/// A response for taglines. pub struct ListTaglinesResponse { pub taglines: Vec, } @@ -48,7 +48,7 @@ pub struct ListTaglinesResponse { #[derive(Debug, Serialize, Deserialize, Clone, Copy, Default, PartialEq, Eq, Hash)] #[cfg_attr(feature = "full", derive(TS))] #[cfg_attr(feature = "full", ts(export))] -/// Fetches a list of registration applications. +/// Fetches a list of taglines. pub struct ListTaglines { pub page: Option, pub limit: Option, From 6935acba2ffbdf887b02e2701486cfacc84fccaa Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Mon, 8 Apr 2024 23:14:03 +0200 Subject: [PATCH 23/26] Remove local_site_id from tagline and custom_emoji --- crates/api/src/site/leave_admin.rs | 2 +- crates/api_crud/src/custom_emoji/create.rs | 3 -- crates/api_crud/src/custom_emoji/list.rs | 13 ++------ crates/api_crud/src/custom_emoji/update.rs | 3 -- crates/api_crud/src/site/read.rs | 2 +- crates/api_crud/src/tagline/create.rs | 5 +-- crates/api_crud/src/tagline/list.rs | 11 ++----- crates/db_schema/src/impls/tagline.rs | 12 ++----- crates/db_schema/src/schema.rs | 4 --- crates/db_schema/src/source/custom_emoji.rs | 14 ++------ crates/db_schema/src/source/tagline.rs | 12 +------ crates/db_views/src/custom_emoji_view.rs | 11 ++----- .../down.sql | 32 +++++++++++++++++++ .../up.sql | 6 ++++ 14 files changed, 54 insertions(+), 76 deletions(-) create mode 100644 migrations/2024-04-08-204327_custom_emoji_tagline_changes/down.sql create mode 100644 migrations/2024-04-08-204327_custom_emoji_tagline_changes/up.sql diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index fa91c726f..57a134238 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -61,7 +61,7 @@ pub async fn leave_admin( let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?; - let tagline = Tagline::get_random(&mut context.pool(), site_view.local_site.id).await?; + let tagline = Tagline::get_random(&mut context.pool()).await?; Ok(Json(GetSiteResponse { site_view, diff --git a/crates/api_crud/src/custom_emoji/create.rs b/crates/api_crud/src/custom_emoji/create.rs index 54745577a..5846c1243 100644 --- a/crates/api_crud/src/custom_emoji/create.rs +++ b/crates/api_crud/src/custom_emoji/create.rs @@ -9,7 +9,6 @@ use lemmy_db_schema::{ source::{ custom_emoji::{CustomEmoji, CustomEmojiInsertForm}, custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, - local_site::LocalSite, }, traits::Crud, }; @@ -22,12 +21,10 @@ pub async fn create_custom_emoji( 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 emoji_form = CustomEmojiInsertForm::builder() - .local_site_id(local_site.id) .shortcode(data.shortcode.to_lowercase().trim().to_string()) .alt_text(data.alt_text.to_string()) .category(data.category.to_string()) diff --git a/crates/api_crud/src/custom_emoji/list.rs b/crates/api_crud/src/custom_emoji/list.rs index dab3e8823..c60a03ed4 100644 --- a/crates/api_crud/src/custom_emoji/list.rs +++ b/crates/api_crud/src/custom_emoji/list.rs @@ -3,7 +3,7 @@ use lemmy_api_common::{ context::LemmyContext, custom_emoji::{ListCustomEmojis, ListCustomEmojisResponse}, }; -use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView}; +use lemmy_db_views::structs::{CustomEmojiView, LocalUserView}; use lemmy_utils::error::LemmyError; #[tracing::instrument(skip(context))] @@ -12,15 +12,8 @@ pub async fn list_custom_emojis( local_user_view: Option, context: Data, ) -> Result, LemmyError> { - let local_site = SiteView::read_local(&mut context.pool()).await?; - let custom_emojis = CustomEmojiView::list( - &mut context.pool(), - local_site.local_site.id, - &data.category, - data.page, - data.limit, - ) - .await?; + let custom_emojis = + CustomEmojiView::list(&mut context.pool(), &data.category, data.page, data.limit).await?; Ok(Json(ListCustomEmojisResponse { custom_emojis })) } diff --git a/crates/api_crud/src/custom_emoji/update.rs b/crates/api_crud/src/custom_emoji/update.rs index 0cabf77b9..4119ed944 100644 --- a/crates/api_crud/src/custom_emoji/update.rs +++ b/crates/api_crud/src/custom_emoji/update.rs @@ -9,7 +9,6 @@ use lemmy_db_schema::{ source::{ custom_emoji::{CustomEmoji, CustomEmojiUpdateForm}, custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm}, - local_site::LocalSite, }, traits::Crud, }; @@ -22,12 +21,10 @@ pub async fn update_custom_emoji( 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 emoji_form = CustomEmojiUpdateForm::builder() - .local_site_id(local_site.id) .alt_text(data.alt_text.to_string()) .category(data.category.to_string()) .image_url(data.clone().image_url.into()) diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 90e04946e..bbead78a0 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -46,7 +46,7 @@ pub async fn get_site( let all_languages = Language::read_all(&mut context.pool()).await?; let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?; let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?; - let tagline = Tagline::get_random(&mut context.pool(), site_view.local_site.id).await?; + let tagline = Tagline::get_random(&mut context.pool()).await?; Ok(GetSiteResponse { site_view, admins, diff --git a/crates/api_crud/src/tagline/create.rs b/crates/api_crud/src/tagline/create.rs index 0b33a03f9..50bb50c12 100644 --- a/crates/api_crud/src/tagline/create.rs +++ b/crates/api_crud/src/tagline/create.rs @@ -31,10 +31,7 @@ pub async fn create_tagline( let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?; is_valid_tagline_content(&content)?; - let tagline_form = TaglineInsertForm { - local_site_id: local_site.id, - content, - }; + let tagline_form = TaglineInsertForm { content }; let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?; diff --git a/crates/api_crud/src/tagline/list.rs b/crates/api_crud/src/tagline/list.rs index d8f067ef1..21929f547 100644 --- a/crates/api_crud/src/tagline/list.rs +++ b/crates/api_crud/src/tagline/list.rs @@ -4,7 +4,7 @@ use lemmy_api_common::{ tagline::{ListTaglines, ListTaglinesResponse}, }; use lemmy_db_schema::source::tagline::Tagline; -use lemmy_db_views::structs::{LocalUserView, SiteView}; +use lemmy_db_views::structs::LocalUserView; use lemmy_utils::error::LemmyError; #[tracing::instrument(skip(context))] @@ -13,14 +13,7 @@ pub async fn list_taglines( local_user_view: Option, context: Data, ) -> Result, LemmyError> { - let local_site = SiteView::read_local(&mut context.pool()).await?; - let taglines = Tagline::list( - &mut context.pool(), - local_site.local_site.id, - data.page, - data.limit, - ) - .await?; + let taglines = Tagline::list(&mut context.pool(), data.page, data.limit).await?; Ok(Json(ListTaglinesResponse { taglines })) } diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs index 1bd755562..656d537d6 100644 --- a/crates/db_schema/src/impls/tagline.rs +++ b/crates/db_schema/src/impls/tagline.rs @@ -1,6 +1,6 @@ use crate::{ - newtypes::{LocalSiteId, TaglineId}, - schema::tagline::dsl::{local_site_id, published, tagline}, + newtypes::TaglineId, + schema::tagline::dsl::{published, tagline}, source::tagline::{Tagline, TaglineInsertForm, TaglineUpdateForm}, traits::Crud, utils::{get_conn, limit_and_offset, DbPool}, @@ -38,7 +38,6 @@ impl Crud for Tagline { impl Tagline { pub async fn list( pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, page: Option, limit: Option, ) -> Result, Error> { @@ -48,21 +47,16 @@ impl Tagline { .order(published.desc()) .offset(offset) .limit(limit) - .filter(local_site_id.eq(for_local_site_id)) .get_results::(conn) .await } - pub async fn get_random( - pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, - ) -> Result, Error> { + pub async fn get_random(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; sql_function!(fn random() -> Text); tagline .order(random()) .limit(1) - .filter(local_site_id.eq(for_local_site_id)) .first::(conn) .await .optional() diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 408ed0540..dca1cac48 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -254,7 +254,6 @@ diesel::table! { diesel::table! { custom_emoji (id) { id -> Int4, - local_site_id -> Int4, #[max_length = 128] shortcode -> Varchar, image_url -> Text, @@ -929,7 +928,6 @@ diesel::table! { diesel::table! { tagline (id) { id -> Int4, - local_site_id -> Int4, content -> Text, published -> Timestamptz, updated -> Nullable, @@ -966,7 +964,6 @@ diesel::joinable!(community_moderator -> community (community_id)); diesel::joinable!(community_moderator -> person (person_id)); diesel::joinable!(community_person_ban -> community (community_id)); diesel::joinable!(community_person_ban -> person (person_id)); -diesel::joinable!(custom_emoji -> local_site (local_site_id)); diesel::joinable!(custom_emoji_keyword -> custom_emoji (custom_emoji_id)); diesel::joinable!(email_verification -> local_user (local_user_id)); diesel::joinable!(federation_allowlist -> instance (instance_id)); @@ -1028,7 +1025,6 @@ diesel::joinable!(site -> instance (instance_id)); diesel::joinable!(site_aggregates -> site (site_id)); diesel::joinable!(site_language -> language (language_id)); diesel::joinable!(site_language -> site (site_id)); -diesel::joinable!(tagline -> local_site (local_site_id)); diesel::allow_tables_to_appear_in_same_query!( admin_purge_comment, diff --git a/crates/db_schema/src/source/custom_emoji.rs b/crates/db_schema/src/source/custom_emoji.rs index 3217c9736..788885c97 100644 --- a/crates/db_schema/src/source/custom_emoji.rs +++ b/crates/db_schema/src/source/custom_emoji.rs @@ -1,4 +1,4 @@ -use crate::newtypes::{CustomEmojiId, DbUrl, LocalSiteId}; +use crate::newtypes::{CustomEmojiId, DbUrl}; #[cfg(feature = "full")] use crate::schema::custom_emoji; use chrono::{DateTime, Utc}; @@ -10,21 +10,13 @@ use typed_builder::TypedBuilder; #[skip_serializing_none] #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] -#[cfg_attr( - feature = "full", - derive(Queryable, Selectable, Associations, Identifiable, TS) -)] +#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))] #[cfg_attr(feature = "full", diesel(table_name = custom_emoji))] -#[cfg_attr( - feature = "full", - diesel(belongs_to(crate::source::local_site::LocalSite)) -)] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] #[cfg_attr(feature = "full", ts(export))] /// A custom emoji. pub struct CustomEmoji { pub id: CustomEmojiId, - pub local_site_id: LocalSiteId, pub shortcode: String, pub image_url: DbUrl, pub alt_text: String, @@ -37,7 +29,6 @@ pub struct CustomEmoji { #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = custom_emoji))] pub struct CustomEmojiInsertForm { - pub local_site_id: LocalSiteId, pub shortcode: String, pub image_url: DbUrl, pub alt_text: String, @@ -48,7 +39,6 @@ pub struct CustomEmojiInsertForm { #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = custom_emoji))] pub struct CustomEmojiUpdateForm { - pub local_site_id: LocalSiteId, pub image_url: DbUrl, pub alt_text: String, pub category: String, diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index df207e10e..ac6726601 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -1,4 +1,3 @@ -use crate::newtypes::LocalSiteId; #[cfg(feature = "full")] use crate::schema::tagline; use chrono::{DateTime, Utc}; @@ -9,21 +8,13 @@ use ts_rs::TS; #[skip_serializing_none] #[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] -#[cfg_attr( - feature = "full", - derive(Queryable, Selectable, Associations, Identifiable, TS) -)] +#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))] #[cfg_attr(feature = "full", diesel(table_name = tagline))] -#[cfg_attr( - feature = "full", - diesel(belongs_to(crate::source::local_site::LocalSite)) -)] #[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))] #[cfg_attr(feature = "full", ts(export))] /// A tagline, shown at the top of your site. pub struct Tagline { pub id: i32, - pub local_site_id: LocalSiteId, pub content: String, pub published: DateTime, pub updated: Option>, @@ -33,7 +24,6 @@ pub struct Tagline { #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = tagline))] pub struct TaglineInsertForm { - pub local_site_id: LocalSiteId, pub content: String, } diff --git a/crates/db_views/src/custom_emoji_view.rs b/crates/db_views/src/custom_emoji_view.rs index 70dfb31e5..8c17190a2 100644 --- a/crates/db_views/src/custom_emoji_view.rs +++ b/crates/db_views/src/custom_emoji_view.rs @@ -2,7 +2,7 @@ use crate::structs::CustomEmojiView; use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ - newtypes::{CustomEmojiId, LocalSiteId}, + newtypes::CustomEmojiId, schema::{custom_emoji, custom_emoji_keyword}, source::{custom_emoji::CustomEmoji, custom_emoji_keyword::CustomEmojiKeyword}, utils::{get_conn, limit_and_offset, DbPool}, @@ -35,13 +35,9 @@ impl CustomEmojiView { } } - pub async fn get_all( - pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, - ) -> Result, Error> { + pub async fn get_all(pool: &mut DbPool<'_>) -> Result, Error> { let conn = &mut get_conn(pool).await?; let emojis = custom_emoji::table - .filter(custom_emoji::local_site_id.eq(for_local_site_id)) .left_join( custom_emoji_keyword::table.on(custom_emoji_keyword::custom_emoji_id.eq(custom_emoji::id)), ) @@ -59,7 +55,6 @@ impl CustomEmojiView { pub async fn list( pool: &mut DbPool<'_>, - for_local_site_id: LocalSiteId, category: &Option, page: Option, limit: Option, @@ -72,8 +67,6 @@ impl CustomEmojiView { ) .into_boxed(); - query = query.filter(custom_emoji::local_site_id.eq(for_local_site_id)); - if let Some(category) = category { query = query.filter(custom_emoji::category.eq(category)) } diff --git a/migrations/2024-04-08-204327_custom_emoji_tagline_changes/down.sql b/migrations/2024-04-08-204327_custom_emoji_tagline_changes/down.sql new file mode 100644 index 000000000..a6b01a1d1 --- /dev/null +++ b/migrations/2024-04-08-204327_custom_emoji_tagline_changes/down.sql @@ -0,0 +1,32 @@ +ALTER TABLE custom_emoji + ADD COLUMN local_site_id int REFERENCES local_site (site_id) ON UPDATE CASCADE ON DELETE CASCADE; + +UPDATE + custom_emoji +SET + local_site_id = ( + SELECT + site_id + FROM + local_site + LIMIT 1); + +ALTER TABLE custom_emoji + ALTER COLUMN local_site_id SET NOT NULL; + +ALTER TABLE tagline + ADD COLUMN local_site_id int REFERENCES local_site (site_id) ON UPDATE CASCADE ON DELETE CASCADE; + +UPDATE + tagline +SET + local_site_id = ( + SELECT + site_id + FROM + local_site + LIMIT 1); + +ALTER TABLE tagline + ALTER COLUMN local_site_id SET NOT NULL; + diff --git a/migrations/2024-04-08-204327_custom_emoji_tagline_changes/up.sql b/migrations/2024-04-08-204327_custom_emoji_tagline_changes/up.sql new file mode 100644 index 000000000..5aa073f76 --- /dev/null +++ b/migrations/2024-04-08-204327_custom_emoji_tagline_changes/up.sql @@ -0,0 +1,6 @@ +ALTER TABLE custom_emoji + DROP COLUMN local_site_id; + +ALTER TABLE tagline + DROP COLUMN local_site_id; + From 999e9845e79b7f63f7342bd0717b7fcdd4bac0a5 Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Thu, 11 Apr 2024 21:40:50 +0200 Subject: [PATCH 24/26] Update TaglineInserForm and TaglineUpdateForm --- crates/api_crud/src/tagline/create.rs | 4 +++- crates/api_crud/src/tagline/update.rs | 2 +- crates/db_schema/src/source/tagline.rs | 4 ++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/crates/api_crud/src/tagline/create.rs b/crates/api_crud/src/tagline/create.rs index 50bb50c12..cf9fc8aa0 100644 --- a/crates/api_crud/src/tagline/create.rs +++ b/crates/api_crud/src/tagline/create.rs @@ -31,7 +31,9 @@ pub async fn create_tagline( let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?; is_valid_tagline_content(&content)?; - let tagline_form = TaglineInsertForm { content }; + let tagline_form = TaglineInsertForm { + content: Some(content), + }; let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?; diff --git a/crates/api_crud/src/tagline/update.rs b/crates/api_crud/src/tagline/update.rs index 44a22f0c1..ba293b0b0 100644 --- a/crates/api_crud/src/tagline/update.rs +++ b/crates/api_crud/src/tagline/update.rs @@ -33,7 +33,7 @@ pub async fn update_tagline( is_valid_tagline_content(&content)?; let tagline_form = TaglineUpdateForm { - content, + content: Some(content), updated: Some(Some(naive_now())), }; diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs index ac6726601..7ad1475ec 100644 --- a/crates/db_schema/src/source/tagline.rs +++ b/crates/db_schema/src/source/tagline.rs @@ -24,13 +24,13 @@ pub struct Tagline { #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = tagline))] pub struct TaglineInsertForm { - pub content: String, + pub content: Option, } #[derive(Clone, Default)] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", diesel(table_name = tagline))] pub struct TaglineUpdateForm { - pub content: String, + pub content: Option, pub updated: Option>>, } From 1f629cc02d6e2e39d45429d0330bd7c5465f1b25 Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Sun, 14 Apr 2024 12:44:46 +0200 Subject: [PATCH 25/26] Add ignore_page_limits for custom emojis EmojiPicker needs to be able to retrieve all emojis in 1 call --- crates/api_common/src/custom_emoji.rs | 1 + crates/api_crud/src/custom_emoji/list.rs | 10 ++++++++-- crates/db_views/src/custom_emoji_view.rs | 10 +++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/api_common/src/custom_emoji.rs b/crates/api_common/src/custom_emoji.rs index 7a846eae2..3804b71af 100644 --- a/crates/api_common/src/custom_emoji.rs +++ b/crates/api_common/src/custom_emoji.rs @@ -65,4 +65,5 @@ pub struct ListCustomEmojis { pub page: Option, pub limit: Option, pub category: Option, + pub ignore_page_limits: Option, } diff --git a/crates/api_crud/src/custom_emoji/list.rs b/crates/api_crud/src/custom_emoji/list.rs index c60a03ed4..6ee5a44b0 100644 --- a/crates/api_crud/src/custom_emoji/list.rs +++ b/crates/api_crud/src/custom_emoji/list.rs @@ -12,8 +12,14 @@ pub async fn list_custom_emojis( local_user_view: Option, context: Data, ) -> Result, LemmyError> { - let custom_emojis = - CustomEmojiView::list(&mut context.pool(), &data.category, data.page, data.limit).await?; + let custom_emojis = CustomEmojiView::list( + &mut context.pool(), + &data.category, + data.page, + data.limit, + data.ignore_page_limits.unwrap_or(false), + ) + .await?; Ok(Json(ListCustomEmojisResponse { custom_emojis })) } diff --git a/crates/db_views/src/custom_emoji_view.rs b/crates/db_views/src/custom_emoji_view.rs index 8c17190a2..1ee40fac3 100644 --- a/crates/db_views/src/custom_emoji_view.rs +++ b/crates/db_views/src/custom_emoji_view.rs @@ -58,15 +58,21 @@ impl CustomEmojiView { category: &Option, page: Option, limit: Option, + ignore_page_limits: bool, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let (limit, offset) = limit_and_offset(page, limit)?; + let mut query = custom_emoji::table .left_join( custom_emoji_keyword::table.on(custom_emoji_keyword::custom_emoji_id.eq(custom_emoji::id)), ) .into_boxed(); + if !ignore_page_limits { + let (limit, offset) = limit_and_offset(page, limit)?; + query = query.limit(limit).offset(offset); + } + if let Some(category) = category { query = query.filter(custom_emoji::category.eq(category)) } @@ -78,8 +84,6 @@ impl CustomEmojiView { custom_emoji::all_columns, custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want) )) - .limit(limit) - .offset(offset) .load::(conn) .await?; From 47710852531f874fd0bfade2498d5cc5ef621223 Mon Sep 17 00:00:00 2001 From: Freek van Zee Date: Tue, 16 Apr 2024 22:03:06 +0200 Subject: [PATCH 26/26] Update custom_emoji_view Only keep get_all als helper function calling list with paging ignored Only order on category when filtering on category --- crates/db_views/src/custom_emoji_view.rs | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/crates/db_views/src/custom_emoji_view.rs b/crates/db_views/src/custom_emoji_view.rs index 1ee40fac3..0f1b9e0de 100644 --- a/crates/db_views/src/custom_emoji_view.rs +++ b/crates/db_views/src/custom_emoji_view.rs @@ -36,21 +36,7 @@ impl CustomEmojiView { } pub async fn get_all(pool: &mut DbPool<'_>) -> Result, Error> { - let conn = &mut get_conn(pool).await?; - let emojis = custom_emoji::table - .left_join( - custom_emoji_keyword::table.on(custom_emoji_keyword::custom_emoji_id.eq(custom_emoji::id)), - ) - .order(custom_emoji::category) - .then_order_by(custom_emoji::id) - .select(( - custom_emoji::all_columns, - custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want) - )) - .load::(conn) - .await?; - - Ok(CustomEmojiView::from_tuple_to_vec(emojis)) + Self::list(pool, &None, None, None, true).await } pub async fn list( @@ -74,12 +60,14 @@ impl CustomEmojiView { } if let Some(category) = category { - query = query.filter(custom_emoji::category.eq(category)) + query = query + .filter(custom_emoji::category.eq(category)) + .order(custom_emoji::category) } + query = query.then_order_by(custom_emoji::id); + let emojis = query - .order(custom_emoji::category) - .then_order_by(custom_emoji::id) .select(( custom_emoji::all_columns, custom_emoji_keyword::all_columns.nullable(), // (or all the columns if you want)