Remove local_site_id from tagline and custom_emoji

This commit is contained in:
Freek van Zee 2024-04-08 23:14:03 +02:00
parent 562c909f4b
commit 6935acba2f
14 changed files with 54 additions and 76 deletions

View file

@ -61,7 +61,7 @@ pub async fn leave_admin(
let all_languages = Language::read_all(&mut context.pool()).await?;
let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?;
let tagline = Tagline::get_random(&mut context.pool(), site_view.local_site.id).await?;
let tagline = Tagline::get_random(&mut context.pool()).await?;
Ok(Json(GetSiteResponse {
site_view,

View file

@ -9,7 +9,6 @@ use lemmy_db_schema::{
source::{
custom_emoji::{CustomEmoji, CustomEmojiInsertForm},
custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm},
local_site::LocalSite,
},
traits::Crud,
};
@ -22,12 +21,10 @@ pub async fn create_custom_emoji(
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CustomEmojiResponse>, LemmyError> {
let local_site = LocalSite::read(&mut context.pool()).await?;
// Make sure user is an admin
is_admin(&local_user_view)?;
let emoji_form = CustomEmojiInsertForm::builder()
.local_site_id(local_site.id)
.shortcode(data.shortcode.to_lowercase().trim().to_string())
.alt_text(data.alt_text.to_string())
.category(data.category.to_string())

View file

@ -3,7 +3,7 @@ use lemmy_api_common::{
context::LemmyContext,
custom_emoji::{ListCustomEmojis, ListCustomEmojisResponse},
};
use lemmy_db_views::structs::{CustomEmojiView, LocalUserView, SiteView};
use lemmy_db_views::structs::{CustomEmojiView, LocalUserView};
use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))]
@ -12,15 +12,8 @@ pub async fn list_custom_emojis(
local_user_view: Option<LocalUserView>,
context: Data<LemmyContext>,
) -> Result<Json<ListCustomEmojisResponse>, LemmyError> {
let local_site = SiteView::read_local(&mut context.pool()).await?;
let custom_emojis = CustomEmojiView::list(
&mut context.pool(),
local_site.local_site.id,
&data.category,
data.page,
data.limit,
)
.await?;
let custom_emojis =
CustomEmojiView::list(&mut context.pool(), &data.category, data.page, data.limit).await?;
Ok(Json(ListCustomEmojisResponse { custom_emojis }))
}

View file

@ -9,7 +9,6 @@ use lemmy_db_schema::{
source::{
custom_emoji::{CustomEmoji, CustomEmojiUpdateForm},
custom_emoji_keyword::{CustomEmojiKeyword, CustomEmojiKeywordInsertForm},
local_site::LocalSite,
},
traits::Crud,
};
@ -22,12 +21,10 @@ pub async fn update_custom_emoji(
context: Data<LemmyContext>,
local_user_view: LocalUserView,
) -> Result<Json<CustomEmojiResponse>, LemmyError> {
let local_site = LocalSite::read(&mut context.pool()).await?;
// Make sure user is an admin
is_admin(&local_user_view)?;
let emoji_form = CustomEmojiUpdateForm::builder()
.local_site_id(local_site.id)
.alt_text(data.alt_text.to_string())
.category(data.category.to_string())
.image_url(data.clone().image_url.into())

View file

@ -46,7 +46,7 @@ pub async fn get_site(
let all_languages = Language::read_all(&mut context.pool()).await?;
let discussion_languages = SiteLanguage::read_local_raw(&mut context.pool()).await?;
let blocked_urls = LocalSiteUrlBlocklist::get_all(&mut context.pool()).await?;
let tagline = Tagline::get_random(&mut context.pool(), site_view.local_site.id).await?;
let tagline = Tagline::get_random(&mut context.pool()).await?;
Ok(GetSiteResponse {
site_view,
admins,

View file

@ -31,10 +31,7 @@ pub async fn create_tagline(
let content = process_markdown(&data.content, &slur_regex, &url_blocklist, &context).await?;
is_valid_tagline_content(&content)?;
let tagline_form = TaglineInsertForm {
local_site_id: local_site.id,
content,
};
let tagline_form = TaglineInsertForm { content };
let tagline = Tagline::create(&mut context.pool(), &tagline_form).await?;

View file

@ -4,7 +4,7 @@ use lemmy_api_common::{
tagline::{ListTaglines, ListTaglinesResponse},
};
use lemmy_db_schema::source::tagline::Tagline;
use lemmy_db_views::structs::{LocalUserView, SiteView};
use lemmy_db_views::structs::LocalUserView;
use lemmy_utils::error::LemmyError;
#[tracing::instrument(skip(context))]
@ -13,14 +13,7 @@ pub async fn list_taglines(
local_user_view: Option<LocalUserView>,
context: Data<LemmyContext>,
) -> Result<Json<ListTaglinesResponse>, LemmyError> {
let local_site = SiteView::read_local(&mut context.pool()).await?;
let taglines = Tagline::list(
&mut context.pool(),
local_site.local_site.id,
data.page,
data.limit,
)
.await?;
let taglines = Tagline::list(&mut context.pool(), data.page, data.limit).await?;
Ok(Json(ListTaglinesResponse { taglines }))
}

View file

@ -1,6 +1,6 @@
use crate::{
newtypes::{LocalSiteId, TaglineId},
schema::tagline::dsl::{local_site_id, published, tagline},
newtypes::TaglineId,
schema::tagline::dsl::{published, tagline},
source::tagline::{Tagline, TaglineInsertForm, TaglineUpdateForm},
traits::Crud,
utils::{get_conn, limit_and_offset, DbPool},
@ -38,7 +38,6 @@ impl Crud for Tagline {
impl Tagline {
pub async fn list(
pool: &mut DbPool<'_>,
for_local_site_id: LocalSiteId,
page: Option<i64>,
limit: Option<i64>,
) -> Result<Vec<Self>, Error> {
@ -48,21 +47,16 @@ impl Tagline {
.order(published.desc())
.offset(offset)
.limit(limit)
.filter(local_site_id.eq(for_local_site_id))
.get_results::<Self>(conn)
.await
}
pub async fn get_random(
pool: &mut DbPool<'_>,
for_local_site_id: LocalSiteId,
) -> Result<Option<Self>, Error> {
pub async fn get_random(pool: &mut DbPool<'_>) -> Result<Option<Self>, Error> {
let conn = &mut get_conn(pool).await?;
sql_function!(fn random() -> Text);
tagline
.order(random())
.limit(1)
.filter(local_site_id.eq(for_local_site_id))
.first::<Self>(conn)
.await
.optional()

View file

@ -254,7 +254,6 @@ diesel::table! {
diesel::table! {
custom_emoji (id) {
id -> Int4,
local_site_id -> Int4,
#[max_length = 128]
shortcode -> Varchar,
image_url -> Text,
@ -929,7 +928,6 @@ diesel::table! {
diesel::table! {
tagline (id) {
id -> Int4,
local_site_id -> Int4,
content -> Text,
published -> Timestamptz,
updated -> Nullable<Timestamptz>,
@ -966,7 +964,6 @@ diesel::joinable!(community_moderator -> community (community_id));
diesel::joinable!(community_moderator -> person (person_id));
diesel::joinable!(community_person_ban -> community (community_id));
diesel::joinable!(community_person_ban -> person (person_id));
diesel::joinable!(custom_emoji -> local_site (local_site_id));
diesel::joinable!(custom_emoji_keyword -> custom_emoji (custom_emoji_id));
diesel::joinable!(email_verification -> local_user (local_user_id));
diesel::joinable!(federation_allowlist -> instance (instance_id));
@ -1028,7 +1025,6 @@ diesel::joinable!(site -> instance (instance_id));
diesel::joinable!(site_aggregates -> site (site_id));
diesel::joinable!(site_language -> language (language_id));
diesel::joinable!(site_language -> site (site_id));
diesel::joinable!(tagline -> local_site (local_site_id));
diesel::allow_tables_to_appear_in_same_query!(
admin_purge_comment,

View file

@ -1,4 +1,4 @@
use crate::newtypes::{CustomEmojiId, DbUrl, LocalSiteId};
use crate::newtypes::{CustomEmojiId, DbUrl};
#[cfg(feature = "full")]
use crate::schema::custom_emoji;
use chrono::{DateTime, Utc};
@ -10,21 +10,13 @@ use typed_builder::TypedBuilder;
#[skip_serializing_none]
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(
feature = "full",
derive(Queryable, Selectable, Associations, Identifiable, TS)
)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = custom_emoji))]
#[cfg_attr(
feature = "full",
diesel(belongs_to(crate::source::local_site::LocalSite))
)]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// A custom emoji.
pub struct CustomEmoji {
pub id: CustomEmojiId,
pub local_site_id: LocalSiteId,
pub shortcode: String,
pub image_url: DbUrl,
pub alt_text: String,
@ -37,7 +29,6 @@ pub struct CustomEmoji {
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = custom_emoji))]
pub struct CustomEmojiInsertForm {
pub local_site_id: LocalSiteId,
pub shortcode: String,
pub image_url: DbUrl,
pub alt_text: String,
@ -48,7 +39,6 @@ pub struct CustomEmojiInsertForm {
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = custom_emoji))]
pub struct CustomEmojiUpdateForm {
pub local_site_id: LocalSiteId,
pub image_url: DbUrl,
pub alt_text: String,
pub category: String,

View file

@ -1,4 +1,3 @@
use crate::newtypes::LocalSiteId;
#[cfg(feature = "full")]
use crate::schema::tagline;
use chrono::{DateTime, Utc};
@ -9,21 +8,13 @@ use ts_rs::TS;
#[skip_serializing_none]
#[derive(PartialEq, Eq, Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(
feature = "full",
derive(Queryable, Selectable, Associations, Identifiable, TS)
)]
#[cfg_attr(feature = "full", derive(Queryable, Selectable, Identifiable, TS))]
#[cfg_attr(feature = "full", diesel(table_name = tagline))]
#[cfg_attr(
feature = "full",
diesel(belongs_to(crate::source::local_site::LocalSite))
)]
#[cfg_attr(feature = "full", diesel(check_for_backend(diesel::pg::Pg)))]
#[cfg_attr(feature = "full", ts(export))]
/// A tagline, shown at the top of your site.
pub struct Tagline {
pub id: i32,
pub local_site_id: LocalSiteId,
pub content: String,
pub published: DateTime<Utc>,
pub updated: Option<DateTime<Utc>>,
@ -33,7 +24,6 @@ pub struct Tagline {
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
#[cfg_attr(feature = "full", diesel(table_name = tagline))]
pub struct TaglineInsertForm {
pub local_site_id: LocalSiteId,
pub content: String,
}

View file

@ -2,7 +2,7 @@ use crate::structs::CustomEmojiView;
use diesel::{result::Error, ExpressionMethods, JoinOnDsl, NullableExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl;
use lemmy_db_schema::{
newtypes::{CustomEmojiId, LocalSiteId},
newtypes::CustomEmojiId,
schema::{custom_emoji, custom_emoji_keyword},
source::{custom_emoji::CustomEmoji, custom_emoji_keyword::CustomEmojiKeyword},
utils::{get_conn, limit_and_offset, DbPool},
@ -35,13 +35,9 @@ impl CustomEmojiView {
}
}
pub async fn get_all(
pool: &mut DbPool<'_>,
for_local_site_id: LocalSiteId,
) -> Result<Vec<Self>, Error> {
pub async fn get_all(pool: &mut DbPool<'_>) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?;
let emojis = custom_emoji::table
.filter(custom_emoji::local_site_id.eq(for_local_site_id))
.left_join(
custom_emoji_keyword::table.on(custom_emoji_keyword::custom_emoji_id.eq(custom_emoji::id)),
)
@ -59,7 +55,6 @@ impl CustomEmojiView {
pub async fn list(
pool: &mut DbPool<'_>,
for_local_site_id: LocalSiteId,
category: &Option<String>,
page: Option<i64>,
limit: Option<i64>,
@ -72,8 +67,6 @@ impl CustomEmojiView {
)
.into_boxed();
query = query.filter(custom_emoji::local_site_id.eq(for_local_site_id));
if let Some(category) = category {
query = query.filter(custom_emoji::category.eq(category))
}

View file

@ -0,0 +1,32 @@
ALTER TABLE custom_emoji
ADD COLUMN local_site_id int REFERENCES local_site (site_id) ON UPDATE CASCADE ON DELETE CASCADE;
UPDATE
custom_emoji
SET
local_site_id = (
SELECT
site_id
FROM
local_site
LIMIT 1);
ALTER TABLE custom_emoji
ALTER COLUMN local_site_id SET NOT NULL;
ALTER TABLE tagline
ADD COLUMN local_site_id int REFERENCES local_site (site_id) ON UPDATE CASCADE ON DELETE CASCADE;
UPDATE
tagline
SET
local_site_id = (
SELECT
site_id
FROM
local_site
LIMIT 1);
ALTER TABLE tagline
ALTER COLUMN local_site_id SET NOT NULL;

View file

@ -0,0 +1,6 @@
ALTER TABLE custom_emoji
DROP COLUMN local_site_id;
ALTER TABLE tagline
DROP COLUMN local_site_id;