From c27d2a56873de58142e531915c3df002878d59e1 Mon Sep 17 00:00:00 2001 From: Anon Date: Sat, 19 Nov 2022 08:48:29 -0600 Subject: [PATCH] Add support for Taglines (#2548) * Add support for Taglines * recreate migration * Update content column -> Text * Optimize replace function * Change taglines to Option * Move Ok() call * Move taglines to GetSiteResponse * Fix logic Co-authored-by: layla Co-authored-by: Dessalines --- crates/api/src/site/leave_admin.rs | 4 ++ crates/api_common/src/site.rs | 4 +- crates/api_crud/src/site/read.rs | 5 +- crates/api_crud/src/site/update.rs | 4 ++ crates/db_schema/src/impls/mod.rs | 1 + crates/db_schema/src/impls/tagline.rs | 55 +++++++++++++++++++ crates/db_schema/src/schema.rs | 12 ++++ crates/db_schema/src/source/mod.rs | 1 + crates/db_schema/src/source/tagline.rs | 28 ++++++++++ .../down.sql | 1 + .../2022-11-13-181529_create_taglines/up.sql | 7 +++ 11 files changed, 120 insertions(+), 2 deletions(-) create mode 100644 crates/db_schema/src/impls/tagline.rs create mode 100644 crates/db_schema/src/source/tagline.rs create mode 100644 migrations/2022-11-13-181529_create_taglines/down.sql create mode 100644 migrations/2022-11-13-181529_create_taglines/up.sql diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index aee747be..4a6687b3 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -10,6 +10,7 @@ use lemmy_db_schema::{ language::Language, moderator::{ModAdd, ModAddForm}, person::{Person, PersonUpdateForm}, + tagline::Tagline, }, traits::Crud, }; @@ -63,6 +64,8 @@ impl Perform for LeaveAdmin { let all_languages = Language::read_all(context.pool()).await?; let discussion_languages = SiteLanguage::read_local(context.pool()).await?; + let taglines_res = Tagline::get_all(context.pool(), site_view.local_site.id).await?; + let taglines = taglines_res.is_empty().then_some(taglines_res); Ok(GetSiteResponse { site_view, @@ -73,6 +76,7 @@ impl Perform for LeaveAdmin { federated_instances: None, all_languages, discussion_languages, + taglines, }) } } diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index e4619e64..c25d58e6 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -1,7 +1,7 @@ use crate::sensitive::Sensitive; use lemmy_db_schema::{ newtypes::{CommentId, CommunityId, LanguageId, PersonId, PostId}, - source::language::Language, + source::{language::Language, tagline::Tagline}, ListingType, ModlogActionType, SearchType, @@ -199,6 +199,7 @@ pub struct EditSite { pub captcha_difficulty: Option, pub allowed_instances: Option>, pub blocked_instances: Option>, + pub taglines: Option>, pub auth: Sensitive, } @@ -222,6 +223,7 @@ pub struct GetSiteResponse { pub federated_instances: Option, // Federation may be disabled pub all_languages: Vec, pub discussion_languages: Vec, + pub taglines: Option>, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index 2f4203a0..dbf64c6c 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -4,7 +4,7 @@ use lemmy_api_common::{ site::{GetSite, GetSiteResponse, MyUserInfo}, utils::{build_federated_instances, get_local_user_settings_view_from_jwt_opt}, }; -use lemmy_db_schema::source::{actor_language::SiteLanguage, language::Language}; +use lemmy_db_schema::source::{actor_language::SiteLanguage, language::Language, tagline::Tagline}; use lemmy_db_views::structs::{LocalUserDiscussionLanguageView, SiteView}; use lemmy_db_views_actor::structs::{ CommunityBlockView, @@ -89,6 +89,8 @@ impl PerformCrud for GetSite { let all_languages = Language::read_all(context.pool()).await?; let discussion_languages = SiteLanguage::read_local(context.pool()).await?; + let taglines_res = Tagline::get_all(context.pool(), site_view.local_site.id).await?; + let taglines = (!taglines_res.is_empty()).then_some(taglines_res); Ok(GetSiteResponse { site_view, @@ -99,6 +101,7 @@ impl PerformCrud for GetSite { federated_instances, all_languages, discussion_languages, + taglines, }) } } diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs index de3751d4..40a356b6 100644 --- a/crates/api_crud/src/site/update.rs +++ b/crates/api_crud/src/site/update.rs @@ -19,6 +19,7 @@ use lemmy_db_schema::{ local_site_rate_limit::{LocalSiteRateLimit, LocalSiteRateLimitUpdateForm}, local_user::LocalUser, site::{Site, SiteUpdateForm}, + tagline::Tagline, }, traits::Crud, utils::{diesel_option_overwrite, diesel_option_overwrite_to_url, naive_now}, @@ -175,6 +176,9 @@ impl PerformCrud for EditSite { .map_err(|e| LemmyError::from_error_message(e, "couldnt_set_all_email_verified"))?; } + let taglines = data.taglines.to_owned(); + Tagline::replace(context.pool(), local_site.id, taglines).await?; + let site_view = SiteView::read_local(context.pool()).await?; let rate_limit_config = diff --git a/crates/db_schema/src/impls/mod.rs b/crates/db_schema/src/impls/mod.rs index b1570fe9..456565ad 100644 --- a/crates/db_schema/src/impls/mod.rs +++ b/crates/db_schema/src/impls/mod.rs @@ -25,3 +25,4 @@ pub mod private_message_report; pub mod registration_application; pub mod secret; pub mod site; +pub mod tagline; diff --git a/crates/db_schema/src/impls/tagline.rs b/crates/db_schema/src/impls/tagline.rs new file mode 100644 index 00000000..3696ac57 --- /dev/null +++ b/crates/db_schema/src/impls/tagline.rs @@ -0,0 +1,55 @@ +use crate::{ + newtypes::LocalSiteId, + schema::tagline::dsl::*, + source::tagline::*, + utils::{get_conn, DbPool}, +}; +use diesel::{insert_into, result::Error, ExpressionMethods, QueryDsl}; +use diesel_async::{AsyncPgConnection, RunQueryDsl}; + +impl Tagline { + pub async fn replace( + pool: &DbPool, + for_local_site_id: LocalSiteId, + list_content: Option>, + ) -> Result<(), Error> { + if let Some(list) = list_content { + let conn = &mut get_conn(pool).await?; + conn + .build_transaction() + .run(|conn| { + Box::pin(async move { + Self::clear(conn).await?; + + for item in list { + let form = TaglineForm { + local_site_id: for_local_site_id, + content: item, + updated: None, + }; + insert_into(tagline) + .values(form) + .get_result::(conn) + .await?; + } + Ok(()) + }) as _ + }) + .await + } else { + Ok(()) + } + } + + async fn clear(conn: &mut AsyncPgConnection) -> Result { + diesel::delete(tagline).execute(conn).await + } + pub async fn get_all(pool: &DbPool, for_local_site_id: LocalSiteId) -> Result, Error> { + use crate::schema::tagline::dsl::*; + let conn = &mut get_conn(pool).await?; + tagline + .filter(local_site_id.eq(for_local_site_id)) + .get_results::(conn) + .await + } +} diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 9f0b1eb9..850b7843 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -721,6 +721,16 @@ table! { } } +table! { + tagline(id) { + id -> Int4, + local_site_id -> Int4, + content -> Text, + published -> Timestamp, + updated -> Nullable, + } +} + joinable!(person_block -> person (person_id)); joinable!(comment -> person (creator_id)); @@ -804,6 +814,7 @@ joinable!(federation_allowlist -> instance (instance_id)); joinable!(federation_blocklist -> instance (instance_id)); joinable!(local_site -> site (site_id)); joinable!(local_site_rate_limit -> local_site (local_site_id)); +joinable!(tagline -> local_site (local_site_id)); allow_tables_to_appear_in_same_query!( activity, @@ -855,6 +866,7 @@ allow_tables_to_appear_in_same_query!( email_verification, registration_application, language, + tagline, local_user_language, site_language, community_language, diff --git a/crates/db_schema/src/source/mod.rs b/crates/db_schema/src/source/mod.rs index b8e091fc..dbc83211 100644 --- a/crates/db_schema/src/source/mod.rs +++ b/crates/db_schema/src/source/mod.rs @@ -26,3 +26,4 @@ pub mod private_message_report; pub mod registration_application; pub mod secret; pub mod site; +pub mod tagline; diff --git a/crates/db_schema/src/source/tagline.rs b/crates/db_schema/src/source/tagline.rs new file mode 100644 index 00000000..2152a0ec --- /dev/null +++ b/crates/db_schema/src/source/tagline.rs @@ -0,0 +1,28 @@ +use crate::newtypes::LocalSiteId; +#[cfg(feature = "full")] +use crate::schema::tagline; +use serde::{Deserialize, Serialize}; + +#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)] +#[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable))] +#[cfg_attr(feature = "full", diesel(table_name = tagline))] +#[cfg_attr( + feature = "full", + diesel(belongs_to(crate::source::local_site::LocalSite)) +)] +pub struct Tagline { + pub id: i32, + pub local_site_id: LocalSiteId, + pub content: String, + pub published: chrono::NaiveDateTime, + pub updated: Option, +} + +#[derive(Clone, Default)] +#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] +#[cfg_attr(feature = "full", diesel(table_name = tagline))] +pub struct TaglineForm { + pub local_site_id: LocalSiteId, + pub content: String, + pub updated: Option, +} diff --git a/migrations/2022-11-13-181529_create_taglines/down.sql b/migrations/2022-11-13-181529_create_taglines/down.sql new file mode 100644 index 00000000..4867e5f1 --- /dev/null +++ b/migrations/2022-11-13-181529_create_taglines/down.sql @@ -0,0 +1 @@ +drop table tagline; \ No newline at end of file diff --git a/migrations/2022-11-13-181529_create_taglines/up.sql b/migrations/2022-11-13-181529_create_taglines/up.sql new file mode 100644 index 00000000..ff88cf4c --- /dev/null +++ b/migrations/2022-11-13-181529_create_taglines/up.sql @@ -0,0 +1,7 @@ +create table tagline ( + id serial primary key, + local_site_id int references local_site on update cascade on delete cascade not null, + content text not null, + published timestamp without time zone default now() not null, + updated timestamp without time zone +); \ No newline at end of file -- 2.44.1