From 7bd474a8433f752e52af86ffaec926edd2290824 Mon Sep 17 00:00:00 2001
From: Dessalines <dessalines@users.noreply.github.com>
Date: Wed, 7 Apr 2021 07:40:35 -0400
Subject: [PATCH] Adding a short site description. Fixes #1496 (#1532)

* 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                  |  9 ++++++
 crates/api_common/src/site.rs                 |  7 +++--
 crates/api_crud/src/site/create.rs            | 31 ++++++++++++++++---
 crates/api_crud/src/site/read.rs              |  1 +
 crates/api_crud/src/site/update.rs            | 17 ++++++++--
 .../src/aggregates/site_aggregates.rs         |  1 +
 crates/db_schema/src/schema.rs                |  3 +-
 crates/db_schema/src/source/site.rs           |  6 ++--
 .../down.sql                                  |  2 ++
 .../up.sql                                    |  5 +++
 10 files changed, 69 insertions(+), 13 deletions(-)
 create mode 100644 migrations/2021-03-31-144349_add_site_short_description/down.sql
 create mode 100644 migrations/2021-03-31-144349_add_site_short_description/up.sql

diff --git a/crates/api_common/src/lib.rs b/crates/api_common/src/lib.rs
index dccc3bce..2f820f1a 100644
--- a/crates/api_common/src/lib.rs
+++ b/crates/api_common/src/lib.rs
@@ -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(())
+  }
+}
diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs
index 090fa8f5..397b5a72 100644
--- a/crates/api_common/src/site.rs
+++ b/crates/api_common/src/site.rs
@@ -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>,
diff --git a/crates/api_crud/src/site/create.rs b/crates/api_crud/src/site/create.rs
index 855e41c1..b9889bf1 100644
--- a/crates/api_crud/src/site/create.rs
+++ b/crates/api_crud/src/site/create.rs
@@ -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,
diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs
index 27066519..d3bf0d2d 100644
--- a/crates/api_crud/src/site/read.rs
+++ b/crates/api_crud/src/site/read.rs
@@ -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,
diff --git a/crates/api_crud/src/site/update.rs b/crates/api_crud/src/site/update.rs
index 06940a75..e9c5828d 100644
--- a/crates/api_crud/src/site/update.rs
+++ b/crates/api_crud/src/site/update.rs
@@ -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,
diff --git a/crates/db_queries/src/aggregates/site_aggregates.rs b/crates/db_queries/src/aggregates/site_aggregates.rs
index 64fbdf7d..e7c6bfae 100644
--- a/crates/db_queries/src/aggregates/site_aggregates.rs
+++ b/crates/db_queries/src/aggregates/site_aggregates.rs
@@ -49,6 +49,7 @@ mod tests {
 
     let site_form = SiteForm {
       name: "test_site".into(),
+      sidebar: None,
       description: None,
       icon: None,
       banner: None,
diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs
index 32fd3de1..ee94b6c5 100644
--- a/crates/db_schema/src/schema.rs
+++ b/crates/db_schema/src/schema.rs
@@ -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>,
     }
 }
 
diff --git a/crates/db_schema/src/source/site.rs b/crates/db_schema/src/source/site.rs
index 0723772e..41042bc5 100644
--- a/crates/db_schema/src/source/site.rs
+++ b/crates/db_schema/src/source/site.rs
@@ -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
index 00000000..0035ee77
--- /dev/null
+++ b/migrations/2021-03-31-144349_add_site_short_description/down.sql
@@ -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
index 00000000..09dc36a7
--- /dev/null
+++ b/migrations/2021-03-31-144349_add_site_short_description/up.sql
@@ -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);
-- 
2.44.1