]> Untitled Git - lemmy.git/commitdiff
Add support for Taglines (#2548)
authorAnon <makotech222@users.noreply.github.com>
Sat, 19 Nov 2022 14:48:29 +0000 (08:48 -0600)
committerGitHub <noreply@github.com>
Sat, 19 Nov 2022 14:48:29 +0000 (09:48 -0500)
* 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 <layla@chapo.dev>
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
crates/api/src/site/leave_admin.rs
crates/api_common/src/site.rs
crates/api_crud/src/site/read.rs
crates/api_crud/src/site/update.rs
crates/db_schema/src/impls/mod.rs
crates/db_schema/src/impls/tagline.rs [new file with mode: 0644]
crates/db_schema/src/schema.rs
crates/db_schema/src/source/mod.rs
crates/db_schema/src/source/tagline.rs [new file with mode: 0644]
migrations/2022-11-13-181529_create_taglines/down.sql [new file with mode: 0644]
migrations/2022-11-13-181529_create_taglines/up.sql [new file with mode: 0644]

index aee747bece68ef0d4ffde0522d5780769acc5745..4a6687b3289d176d5ba8819ba4b636bd654c603c 100644 (file)
@@ -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,
     })
   }
 }
index e4619e64f6a7710f9788c9d0f636a6c052b5d621..c25d58e62ab1d420eaccbf409230f56920364ff5 100644 (file)
@@ -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<String>,
   pub allowed_instances: Option<Vec<String>>,
   pub blocked_instances: Option<Vec<String>>,
+  pub taglines: Option<Vec<String>>,
   pub auth: Sensitive<String>,
 }
 
@@ -222,6 +223,7 @@ pub struct GetSiteResponse {
   pub federated_instances: Option<FederatedInstances>, // Federation may be disabled
   pub all_languages: Vec<Language>,
   pub discussion_languages: Vec<LanguageId>,
+  pub taglines: Option<Vec<Tagline>>,
 }
 
 #[derive(Debug, Serialize, Deserialize, Clone)]
index 2f4203a0b72d474e823e5d22210ecfd03686a49a..dbf64c6c3afbdffdffcf9c305b61d4d550d68e38 100644 (file)
@@ -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,
     })
   }
 }
index de3751d400a4a538e75ba27f30c1e537aca2b6c1..40a356b6dde1e4663a544b2c59313a12ccbbe246 100644 (file)
@@ -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 =
index b1570fe95e9dc7d6e2f51b5f6579ac1b8a2d6e36..456565adf00516ff8ff6145dee997864e7f35994 100644 (file)
@@ -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 (file)
index 0000000..3696ac5
--- /dev/null
@@ -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<Vec<String>>,
+  ) -> 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::<Self>(conn)
+                .await?;
+            }
+            Ok(())
+          }) as _
+        })
+        .await
+    } else {
+      Ok(())
+    }
+  }
+
+  async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> {
+    diesel::delete(tagline).execute(conn).await
+  }
+  pub async fn get_all(pool: &DbPool, for_local_site_id: LocalSiteId) -> Result<Vec<Self>, 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::<Self>(conn)
+      .await
+  }
+}
index 9f0b1eb9da21a9893058c4b9966ae18849da9ceb..850b784338b828e14becf75ff074950276e57078 100644 (file)
@@ -721,6 +721,16 @@ table! {
   }
 }
 
+table! {
+  tagline(id) {
+    id -> Int4,
+    local_site_id -> Int4,
+    content -> Text,
+    published -> Timestamp,
+    updated -> Nullable<Timestamp>,
+  }
+}
+
 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,
index b8e091fc0ca2463c4aa549aaccba2c8206566106..dbc83211fe00a3824e0d29e984410456dea0c772 100644 (file)
@@ -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 (file)
index 0000000..2152a0e
--- /dev/null
@@ -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<chrono::NaiveDateTime>,
+}
+
+#[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<chrono::NaiveDateTime>,
+}
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 (file)
index 0000000..4867e5f
--- /dev/null
@@ -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 (file)
index 0000000..ff88cf4
--- /dev/null
@@ -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