]> Untitled Git - lemmy.git/commitdiff
Adding a short site description. Fixes #1496 (#1532)
authorDessalines <dessalines@users.noreply.github.com>
Wed, 7 Apr 2021 11:40:35 +0000 (07:40 -0400)
committerGitHub <noreply@github.com>
Wed, 7 Apr 2021 11:40:35 +0000 (11:40 +0000)
* Adding a short site description. Fixes #1496

- Renaming old description to sidebar

* Adding a back end site desc length check.

crates/api_common/src/lib.rs
crates/api_common/src/site.rs
crates/api_crud/src/site/create.rs
crates/api_crud/src/site/read.rs
crates/api_crud/src/site/update.rs
crates/db_queries/src/aggregates/site_aggregates.rs
crates/db_schema/src/schema.rs
crates/db_schema/src/source/site.rs
migrations/2021-03-31-144349_add_site_short_description/down.sql [new file with mode: 0644]
migrations/2021-03-31-144349_add_site_short_description/up.sql [new file with mode: 0644]

index dccc3bce5c6508b2ee96e20a58c4ac5b2ead2f57..2f820f1a296cb8397cc440b9dabfcb4c31e0845e 100644 (file)
@@ -418,3 +418,12 @@ pub fn password_length_check(pass: &str) -> Result<(), LemmyError> {
     Ok(())
   }
 }
+
+/// Checks the site description length
+pub fn site_description_length_check(description: &str) -> Result<(), LemmyError> {
+  if description.len() > 150 {
+    Err(ApiError::err("site_description_length_overflow").into())
+  } else {
+    Ok(())
+  }
+}
index 090fa8f5b04eae8d3ff822ba2014f2ce4c26a575..397b5a72ba3ded1795ea5d793e27c3cfe7e6957f 100644 (file)
@@ -18,7 +18,6 @@ use lemmy_db_views_moderator::{
   mod_sticky_post_view::ModStickyPostView,
 };
 use serde::{Deserialize, Serialize};
-use url::Url;
 
 #[derive(Deserialize, Debug)]
 pub struct Search {
@@ -65,9 +64,10 @@ pub struct GetModlogResponse {
 #[derive(Deserialize)]
 pub struct CreateSite {
   pub name: String,
+  pub sidebar: Option<String>,
   pub description: Option<String>,
-  pub icon: Option<Url>,
-  pub banner: Option<Url>,
+  pub icon: Option<String>,
+  pub banner: Option<String>,
   pub enable_downvotes: bool,
   pub open_registration: bool,
   pub enable_nsfw: bool,
@@ -77,6 +77,7 @@ pub struct CreateSite {
 #[derive(Deserialize)]
 pub struct EditSite {
   pub name: String,
+  pub sidebar: Option<String>,
   pub description: Option<String>,
   pub icon: Option<String>,
   pub banner: Option<String>,
index 855e41c1fefcc542320a1212502591bf71f2f91d..b9889bf1eda1b1c0e81bfb61697828ffbf00a32f 100644 (file)
@@ -1,7 +1,18 @@
 use crate::PerformCrud;
 use actix_web::web::Data;
-use lemmy_api_common::{blocking, get_local_user_view_from_jwt, is_admin, site::*};
-use lemmy_db_queries::{source::site::Site_, Crud};
+use lemmy_api_common::{
+  blocking,
+  get_local_user_view_from_jwt,
+  is_admin,
+  site::*,
+  site_description_length_check,
+};
+use lemmy_db_queries::{
+  diesel_option_overwrite,
+  diesel_option_overwrite_to_url,
+  source::site::Site_,
+  Crud,
+};
 use lemmy_db_schema::source::site::{Site, *};
 use lemmy_db_views::site_view::SiteView;
 use lemmy_utils::{
@@ -36,11 +47,21 @@ impl PerformCrud for CreateSite {
     // Make sure user is an admin
     is_admin(&local_user_view)?;
 
+    let sidebar = diesel_option_overwrite(&data.sidebar);
+    let description = diesel_option_overwrite(&data.description);
+    let icon = diesel_option_overwrite_to_url(&data.icon)?;
+    let banner = diesel_option_overwrite_to_url(&data.banner)?;
+
+    if let Some(Some(desc)) = &description {
+      site_description_length_check(desc)?;
+    }
+
     let site_form = SiteForm {
       name: data.name.to_owned(),
-      description: data.description.to_owned(),
-      icon: Some(data.icon.to_owned().map(|url| url.into())),
-      banner: Some(data.banner.to_owned().map(|url| url.into())),
+      sidebar,
+      description,
+      icon,
+      banner,
       creator_id: local_user_view.person.id,
       enable_downvotes: data.enable_downvotes,
       open_registration: data.open_registration,
index 2706651958ff4b3086de0ead78e11db9d934d861..d3bf0d2d1b6d12e4f2f52fc008b9324288344910 100644 (file)
@@ -43,6 +43,7 @@ impl PerformCrud for GetSite {
 
           let create_site = CreateSite {
             name: setup.site_name.to_owned(),
+            sidebar: None,
             description: None,
             icon: None,
             banner: None,
index 06940a75ce828217ff9b7ea72ea68627739de5c0..e9c5828dfef0277829b5ee7f51cde81af9bcc906 100644 (file)
@@ -5,8 +5,14 @@ use lemmy_api_common::{
   get_local_user_view_from_jwt,
   is_admin,
   site::{EditSite, SiteResponse},
+  site_description_length_check,
+};
+use lemmy_db_queries::{
+  diesel_option_overwrite,
+  diesel_option_overwrite_to_url,
+  source::site::Site_,
+  Crud,
 };
-use lemmy_db_queries::{diesel_option_overwrite_to_url, source::site::Site_, Crud};
 use lemmy_db_schema::{
   naive_now,
   source::site::{Site, SiteForm},
@@ -39,12 +45,19 @@ impl PerformCrud for EditSite {
 
     let found_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
 
+    let sidebar = diesel_option_overwrite(&data.sidebar);
+    let description = diesel_option_overwrite(&data.description);
     let icon = diesel_option_overwrite_to_url(&data.icon)?;
     let banner = diesel_option_overwrite_to_url(&data.banner)?;
 
+    if let Some(Some(desc)) = &description {
+      site_description_length_check(desc)?;
+    }
+
     let site_form = SiteForm {
       name: data.name.to_owned(),
-      description: data.description.to_owned(),
+      sidebar,
+      description,
       icon,
       banner,
       creator_id: found_site.creator_id,
index 64fbdf7df404e5d850958cbd59053f1013abef3a..e7c6bfaef30db4db6382454fc8df7814683b0fac 100644 (file)
@@ -49,6 +49,7 @@ mod tests {
 
     let site_form = SiteForm {
       name: "test_site".into(),
+      sidebar: None,
       description: None,
       icon: None,
       banner: None,
index 32fd3de1889d74cf066267c718f5a42ccdf39109..ee94b6c5209406dd1780cd7a342f2c3182a8ecf8 100644 (file)
@@ -422,7 +422,7 @@ table! {
     site (id) {
         id -> Int4,
         name -> Varchar,
-        description -> Nullable<Text>,
+        sidebar -> Nullable<Text>,
         creator_id -> Int4,
         published -> Timestamp,
         updated -> Nullable<Timestamp>,
@@ -431,6 +431,7 @@ table! {
         enable_nsfw -> Bool,
         icon -> Nullable<Varchar>,
         banner -> Nullable<Varchar>,
+        description -> Nullable<Text>,
     }
 }
 
index 0723772eb372d872a5c514fff0b956a7a7158e8a..41042bc5cb2fc177b977b1ad3c19a493ec61dd3d 100644 (file)
@@ -6,7 +6,7 @@ use serde::Serialize;
 pub struct Site {
   pub id: i32,
   pub name: String,
-  pub description: Option<String>,
+  pub sidebar: Option<String>,
   pub creator_id: PersonId,
   pub published: chrono::NaiveDateTime,
   pub updated: Option<chrono::NaiveDateTime>,
@@ -15,13 +15,14 @@ pub struct Site {
   pub enable_nsfw: bool,
   pub icon: Option<DbUrl>,
   pub banner: Option<DbUrl>,
+  pub description: Option<String>,
 }
 
 #[derive(Insertable, AsChangeset)]
 #[table_name = "site"]
 pub struct SiteForm {
   pub name: String,
-  pub description: Option<String>,
+  pub sidebar: Option<Option<String>>,
   pub creator_id: PersonId,
   pub updated: Option<chrono::NaiveDateTime>,
   pub enable_downvotes: bool,
@@ -30,4 +31,5 @@ pub struct SiteForm {
   // when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column.
   pub icon: Option<Option<DbUrl>>,
   pub banner: Option<Option<DbUrl>>,
+  pub description: Option<Option<String>>,
 }
diff --git a/migrations/2021-03-31-144349_add_site_short_description/down.sql b/migrations/2021-03-31-144349_add_site_short_description/down.sql
new file mode 100644 (file)
index 0000000..0035ee7
--- /dev/null
@@ -0,0 +1,2 @@
+alter table site drop column description;
+alter table site rename column sidebar to description;
diff --git a/migrations/2021-03-31-144349_add_site_short_description/up.sql b/migrations/2021-03-31-144349_add_site_short_description/up.sql
new file mode 100644 (file)
index 0000000..09dc36a
--- /dev/null
@@ -0,0 +1,5 @@
+-- Renaming description to sidebar
+alter table site rename column description to sidebar;
+
+-- Adding a short description column
+alter table site add column description varchar(150);