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 <layla@chapo.dev>
Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
This commit is contained in:
Anon 2022-11-19 08:48:29 -06:00 committed by GitHub
parent 6f3bf4634b
commit c27d2a5687
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 120 additions and 2 deletions

View 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,
})
}
}

View 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)]

View 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,
})
}
}

View 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 =

View file

@ -25,3 +25,4 @@ pub mod private_message_report;
pub mod registration_application;
pub mod secret;
pub mod site;
pub mod tagline;

View file

@ -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
}
}

View 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,

View file

@ -26,3 +26,4 @@ pub mod private_message_report;
pub mod registration_application;
pub mod secret;
pub mod site;
pub mod tagline;

View file

@ -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>,
}

View file

@ -0,0 +1 @@
drop table tagline;

View file

@ -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
);