From fb8c05d6ba176ed2d803f3fdf892d9b0919793dd Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Thu, 22 Apr 2021 14:38:04 +0200 Subject: [PATCH] fix compilation, move user languages to separate table --- crates/api_common/src/person.rs | 12 +++++++++--- crates/apub/src/extensions/note_extension.rs | 6 +++--- crates/apub/src/extensions/page_extension.rs | 6 +++--- crates/db_schema/src/lib.rs | 3 +-- crates/db_schema/src/schema.rs | 10 ++++++++++ crates/db_schema/src/source/languages.rs | 10 ++++++++++ crates/db_schema/src/source/mod.rs | 1 + crates/db_views/src/post_view.rs | 6 +++--- migrations/2021-04-16-130927_language-tags/down.sql | 2 +- migrations/2021-04-16-130927_language-tags/up.sql | 2 +- 10 files changed, 42 insertions(+), 16 deletions(-) create mode 100644 crates/db_schema/src/source/languages.rs diff --git a/crates/api_common/src/person.rs b/crates/api_common/src/person.rs index 73d2ec17b..4a1028851 100644 --- a/crates/api_common/src/person.rs +++ b/crates/api_common/src/person.rs @@ -1,3 +1,10 @@ +use lemmy_db_schema::{ + CommunityId, + PersonId, + PersonMentionId, + PrimaryLanguageTag, + PrivateMessageId, +}; use lemmy_db_views::{ comment_view::CommentView, post_view::PostView, @@ -16,7 +23,6 @@ pub struct Login { pub username_or_email: String, pub password: String, } -use lemmy_db_schema::{CommunityId, DbLanguage, PersonId, PersonMentionId, PrivateMessageId}; #[derive(Deserialize)] pub struct Register { @@ -27,7 +33,7 @@ pub struct Register { pub show_nsfw: bool, pub captcha_uuid: Option, pub captcha_answer: Option, - pub discussion_languages: Vec, + pub discussion_languages: Vec, } #[derive(Deserialize)] @@ -62,7 +68,7 @@ pub struct SaveUserSettings { pub show_avatars: Option, pub send_notifications_to_email: Option, pub auth: String, - pub discussion_languages: Option>, + pub discussion_languages: Option>, } #[derive(Deserialize)] diff --git a/crates/apub/src/extensions/note_extension.rs b/crates/apub/src/extensions/note_extension.rs index c77ba0b60..e099a2e58 100644 --- a/crates/apub/src/extensions/note_extension.rs +++ b/crates/apub/src/extensions/note_extension.rs @@ -1,6 +1,6 @@ use activitystreams::unparsed::UnparsedMutExt; use activitystreams_ext::UnparsedExtension; -use lemmy_db_schema::DbLanguage; +use lemmy_db_schema::PrimaryLanguageTag; use lemmy_utils::LemmyError; use serde::{Deserialize, Serialize}; @@ -10,11 +10,11 @@ use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, Default, Deserialize, Serialize)] #[serde(rename_all = "camelCase")] pub struct NoteExtension { - pub language: Option, + pub language: Option, } impl NoteExtension { - pub fn new(language: DbLanguage) -> Result { + pub fn new(language: PrimaryLanguageTag) -> Result { Ok(NoteExtension { language: Some(language), }) diff --git a/crates/apub/src/extensions/page_extension.rs b/crates/apub/src/extensions/page_extension.rs index 353c4d12f..61008fe33 100644 --- a/crates/apub/src/extensions/page_extension.rs +++ b/crates/apub/src/extensions/page_extension.rs @@ -1,6 +1,6 @@ use activitystreams::unparsed::UnparsedMutExt; use activitystreams_ext::UnparsedExtension; -use lemmy_db_schema::DbLanguage; +use lemmy_db_schema::PrimaryLanguageTag; use lemmy_utils::LemmyError; use serde::{Deserialize, Serialize}; @@ -13,7 +13,7 @@ pub struct PageExtension { pub comments_enabled: Option, pub sensitive: Option, pub stickied: Option, - pub language: Option, + pub language: Option, } impl PageExtension { @@ -21,7 +21,7 @@ impl PageExtension { comments_enabled: bool, sensitive: bool, stickied: bool, - language: DbLanguage, + language: PrimaryLanguageTag, ) -> Result { Ok(PageExtension { comments_enabled: Some(comments_enabled), diff --git a/crates/db_schema/src/lib.rs b/crates/db_schema/src/lib.rs index 38b885c1c..f353584bb 100644 --- a/crates/db_schema/src/lib.rs +++ b/crates/db_schema/src/lib.rs @@ -123,8 +123,7 @@ pub fn naive_now() -> NaiveDateTime { } #[repr(transparent)] -#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, Queryable)] -#[sql_type = "Text"] +#[derive(Debug, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)] pub struct PrimaryLanguageTag(String); impl TryFrom for PrimaryLanguageTag { diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 7c27a730b..ba3c50eba 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -519,6 +519,14 @@ table! { } } +table! { + user_languages (id) { + id -> Int4, + local_user_id -> Int4, + language -> Text, + } +} + joinable!(comment_alias_1 -> person_alias_1 (creator_id)); joinable!(comment -> comment_alias_1 (parent_id)); joinable!(person_mention -> person_alias_1 (recipient_id)); @@ -574,6 +582,7 @@ joinable!(post_saved -> person (person_id)); joinable!(post_saved -> post (post_id)); joinable!(site -> person (creator_id)); joinable!(site_aggregates -> site (site_id)); +joinable!(user_languages -> local_user (local_user_id)); allow_tables_to_appear_in_same_query!( activity, @@ -587,6 +596,7 @@ allow_tables_to_appear_in_same_query!( community_follower, community_moderator, community_person_ban, + user_languages, local_user, mod_add, mod_add_community, diff --git a/crates/db_schema/src/source/languages.rs b/crates/db_schema/src/source/languages.rs new file mode 100644 index 000000000..d11e3338a --- /dev/null +++ b/crates/db_schema/src/source/languages.rs @@ -0,0 +1,10 @@ +use crate::{schema::user_languages, LocalUserId, PrimaryLanguageTag}; +use serde::Serialize; + +#[derive(Clone, Queryable, Identifiable, PartialEq, Debug, Serialize)] +#[table_name = "user_languages"] +pub struct UserLanguages { + pub id: i32, + pub local_user_id: LocalUserId, + pub language: PrimaryLanguageTag, +} diff --git a/crates/db_schema/src/source/mod.rs b/crates/db_schema/src/source/mod.rs index db928bd4f..bd9ae94ab 100644 --- a/crates/db_schema/src/source/mod.rs +++ b/crates/db_schema/src/source/mod.rs @@ -2,6 +2,7 @@ pub mod activity; pub mod comment; pub mod comment_report; pub mod community; +pub mod languages; pub mod local_user; pub mod moderator; pub mod password_reset_request; diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index 348d29915..bb9683874 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -28,9 +28,9 @@ use lemmy_db_schema::{ post::{Post, PostRead, PostSaved}, }, CommunityId, - DbLanguage, PersonId, PostId, + PrimaryLanguageTag, }; use log::debug; use serde::Serialize; @@ -169,7 +169,7 @@ pub struct PostQueryBuilder<'a> { unread_only: bool, page: Option, limit: Option, - languages: Option>, + languages: Option>, } impl<'a> PostQueryBuilder<'a> { @@ -253,7 +253,7 @@ impl<'a> PostQueryBuilder<'a> { self } - pub fn languages>>(mut self, languages: T) -> Self { + pub fn languages>>(mut self, languages: T) -> Self { self.languages = languages.get_optional(); self } diff --git a/migrations/2021-04-16-130927_language-tags/down.sql b/migrations/2021-04-16-130927_language-tags/down.sql index a5f9ba2cf..36125b78d 100644 --- a/migrations/2021-04-16-130927_language-tags/down.sql +++ b/migrations/2021-04-16-130927_language-tags/down.sql @@ -2,5 +2,5 @@ ALTER TABLE comment DROP COLUMN language; ALTER TABLE post DROP COLUMN language; ALTER TABLE private_message DROP COLUMN language; -ALTER TABLE local_user DROP COLUMN discussion_languages; +DROP TABLE discussion_languages; ALTER TABLE local_user RENAME COLUMN interface_language TO lang; diff --git a/migrations/2021-04-16-130927_language-tags/up.sql b/migrations/2021-04-16-130927_language-tags/up.sql index 312b9e203..0b882d460 100644 --- a/migrations/2021-04-16-130927_language-tags/up.sql +++ b/migrations/2021-04-16-130927_language-tags/up.sql @@ -2,5 +2,5 @@ ALTER TABLE comment ADD COLUMN language TEXT NOT NULL; ALTER TABLE post ADD COLUMN language TEXT NOT NULL; ALTER TABLE private_message ADD COLUMN language TEXT NOT NULL; -ALTER TABLE local_user ADD COLUMN discussion_languages TEXT[] NOT NULL; +CREATE TABLE discussion_languages(id INTEGER PRIMARY KEY, local_user_id INT NOT NULL, language TEXT NOT NULL); ALTER TABLE local_user RENAME COLUMN lang TO interface_language;