From 624f4dfbabdc09bac09e088026e437d88700abc8 Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Wed, 13 Jul 2022 12:38:19 +0200 Subject: [PATCH] add code for local_user_discussion_language_view --- crates/api/src/local_user/save_settings.rs | 1 - crates/api_common/src/person.rs | 4 +- crates/api_common/src/site.rs | 1 + crates/db_schema/src/impls/local_user.rs | 2 - crates/db_schema/src/schema.rs | 1 - crates/db_views/src/post_view.rs | 7 ++-- .../src/community_block_view.rs | 6 +-- .../src/community_follower_view.rs | 6 +-- .../src/community_moderator_view.rs | 6 +-- crates/db_views_actor/src/community_view.rs | 6 +-- crates/db_views_actor/src/lib.rs | 2 + .../src/local_user_discusion_language_view.rs | 37 +++++++++++++++++++ .../db_views_actor/src/person_block_view.rs | 6 +-- .../db_views_actor/src/person_mention_view.rs | 16 ++++---- crates/db_views_actor/src/person_view.rs | 6 +-- crates/db_views_actor/src/structs.rs | 8 ++++ .../src/admin_purge_community_view.rs | 6 +-- 17 files changed, 82 insertions(+), 39 deletions(-) create mode 100644 crates/db_views_actor/src/local_user_discusion_language_view.rs diff --git a/crates/api/src/local_user/save_settings.rs b/crates/api/src/local_user/save_settings.rs index 9e9c325dd..4d28ee3dd 100644 --- a/crates/api/src/local_user/save_settings.rs +++ b/crates/api/src/local_user/save_settings.rs @@ -152,7 +152,6 @@ impl Perform for SaveUserSettings { send_notifications_to_email: data.send_notifications_to_email, email_verified: None, accepted_application: None, - discussion_languages, }; let local_user_res = blocking(context.pool(), move |conn| { diff --git a/crates/api_common/src/person.rs b/crates/api_common/src/person.rs index 32d98715c..8faca2d5c 100644 --- a/crates/api_common/src/person.rs +++ b/crates/api_common/src/person.rs @@ -9,7 +9,7 @@ pub struct Login { pub password: Sensitive, } use lemmy_db_schema::{ - newtypes::{CommunityId, PersonId, PersonMentionId, PrivateMessageId}, + newtypes::{CommunityId, LanguageId, PersonId, PersonMentionId, PrivateMessageId}, SortType, }; @@ -63,7 +63,7 @@ pub struct SaveUserSettings { pub show_bot_accounts: Option, pub show_read_posts: Option, pub show_new_post_notifs: Option, - pub discussion_languages: Option>, + pub discussion_languages: Option>, pub auth: Sensitive, } diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 61d2edacb..735bfbfcc 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -174,6 +174,7 @@ pub struct MyUserInfo { pub moderates: Vec, pub community_blocks: Vec, pub person_blocks: Vec, + pub discussion_languages: Vec, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/crates/db_schema/src/impls/local_user.rs b/crates/db_schema/src/impls/local_user.rs index f261ceb6a..8d5370d0a 100644 --- a/crates/db_schema/src/impls/local_user.rs +++ b/crates/db_schema/src/impls/local_user.rs @@ -33,7 +33,6 @@ mod safe_settings_type { show_new_post_notifs, email_verified, accepted_application, - discussion_languages, ); impl ToSafeSettings for LocalUser { @@ -59,7 +58,6 @@ mod safe_settings_type { show_new_post_notifs, email_verified, accepted_application, - discussion_languages, ) } } diff --git a/crates/db_schema/src/schema.rs b/crates/db_schema/src/schema.rs index 320c3bf7b..bb19fbbcc 100644 --- a/crates/db_schema/src/schema.rs +++ b/crates/db_schema/src/schema.rs @@ -162,7 +162,6 @@ table! { show_new_post_notifs -> Bool, email_verified -> Bool, accepted_application -> Bool, - discussion_languages -> Array, } } diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index f8488baa8..1cd54bffe 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -24,6 +24,7 @@ use lemmy_db_schema::{ }, source::{ community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, + language::Language, person::{Person, PersonSafe}, person_block::PersonBlock, post::{Post, PostRead, PostSaved}, @@ -46,6 +47,7 @@ type PostViewTuple = ( Option, Option, Option, + Language, ); impl PostView { @@ -257,10 +259,7 @@ impl<'a> PostQueryBuilder<'a> { self.show_nsfw = Some(user.local_user.show_nsfw); self.show_bot_accounts = Some(user.local_user.show_bot_accounts); self.show_read_posts = Some(user.local_user.show_read_posts); - // if user has no languages set, then dont filter by language - if !user.local_user.discussion_languages.is_empty() { - self.languages = Some(user.local_user.discussion_languages.clone()); - } + //TODO: add local user discussion languages (need to store them in LocalUserView?) } self } diff --git a/crates/db_views_actor/src/community_block_view.rs b/crates/db_views_actor/src/community_block_view.rs index 7c753cd20..59d48ea2a 100644 --- a/crates/db_views_actor/src/community_block_view.rs +++ b/crates/db_views_actor/src/community_block_view.rs @@ -33,10 +33,10 @@ impl ViewToVec for CommunityBlockView { type DbTuple = CommunityBlockViewTuple; fn from_tuple_to_vec(items: Vec) -> Vec { items - .iter() + .into_iter() .map(|a| Self { - person: a.0.to_owned(), - community: a.1.to_owned(), + person: a.0, + community: a.1, }) .collect::>() } diff --git a/crates/db_views_actor/src/community_follower_view.rs b/crates/db_views_actor/src/community_follower_view.rs index 6d5d94a59..6441536e2 100644 --- a/crates/db_views_actor/src/community_follower_view.rs +++ b/crates/db_views_actor/src/community_follower_view.rs @@ -48,10 +48,10 @@ impl ViewToVec for CommunityFollowerView { type DbTuple = CommunityFollowerViewTuple; fn from_tuple_to_vec(items: Vec) -> Vec { items - .iter() + .into_iter() .map(|a| Self { - community: a.0.to_owned(), - follower: a.1.to_owned(), + community: a.0, + follower: a.1, }) .collect::>() } diff --git a/crates/db_views_actor/src/community_moderator_view.rs b/crates/db_views_actor/src/community_moderator_view.rs index 13e7d8cfd..b2df95510 100644 --- a/crates/db_views_actor/src/community_moderator_view.rs +++ b/crates/db_views_actor/src/community_moderator_view.rs @@ -70,10 +70,10 @@ impl ViewToVec for CommunityModeratorView { type DbTuple = CommunityModeratorViewTuple; fn from_tuple_to_vec(items: Vec) -> Vec { items - .iter() + .into_iter() .map(|a| Self { - community: a.0.to_owned(), - moderator: a.1.to_owned(), + community: a.0, + moderator: a.1, }) .collect::>() } diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index 21a717258..a61104374 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -258,10 +258,10 @@ impl ViewToVec for CommunityView { type DbTuple = CommunityViewTuple; fn from_tuple_to_vec(items: Vec) -> Vec { items - .iter() + .into_iter() .map(|a| Self { - community: a.0.to_owned(), - counts: a.1.to_owned(), + community: a.0, + counts: a.1, subscribed: CommunityFollower::to_subscribed_type(&a.2), blocked: a.3.is_some(), }) diff --git a/crates/db_views_actor/src/lib.rs b/crates/db_views_actor/src/lib.rs index 878979f7d..dc0491217 100644 --- a/crates/db_views_actor/src/lib.rs +++ b/crates/db_views_actor/src/lib.rs @@ -9,6 +9,8 @@ pub mod community_person_ban_view; #[cfg(feature = "full")] pub mod community_view; #[cfg(feature = "full")] +pub mod local_user_discusion_language_view; +#[cfg(feature = "full")] pub mod person_block_view; #[cfg(feature = "full")] pub mod person_mention_view; diff --git a/crates/db_views_actor/src/local_user_discusion_language_view.rs b/crates/db_views_actor/src/local_user_discusion_language_view.rs new file mode 100644 index 000000000..a6e0ac96d --- /dev/null +++ b/crates/db_views_actor/src/local_user_discusion_language_view.rs @@ -0,0 +1,37 @@ +use crate::structs::LocalUserDiscussionLanguageView; +use diesel::{result::Error, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl}; +use lemmy_db_schema::{ + newtypes::LocalUserId, + schema::{language, local_user, local_user_language}, + source::{ + language::Language, + local_user::{LocalUser, LocalUserSettings}, + }, + traits::ToSafeSettings, +}; + +type LocalUserDiscussionLanguageViewTuple = (LocalUserSettings, Language); + +impl LocalUserDiscussionLanguageView { + pub fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result, Error> { + let res = local_user_language::table + .inner_join(local_user::table) + .inner_join(language::table) + .select(( + LocalUser::safe_settings_columns_tuple(), + language::all_columns, + )) + .filter(local_user::id.eq(local_user_id)) + .load::(conn)?; + + Ok( + res + .into_iter() + .map(|a| Self { + local_user: a.0, + language: a.1, + }) + .collect::>(), + ) + } +} diff --git a/crates/db_views_actor/src/person_block_view.rs b/crates/db_views_actor/src/person_block_view.rs index b93f81672..19ff26337 100644 --- a/crates/db_views_actor/src/person_block_view.rs +++ b/crates/db_views_actor/src/person_block_view.rs @@ -30,10 +30,10 @@ impl ViewToVec for PersonBlockView { type DbTuple = PersonBlockViewTuple; fn from_tuple_to_vec(items: Vec) -> Vec { items - .iter() + .into_iter() .map(|a| Self { - person: a.0.to_owned(), - target: a.1.to_owned(), + person: a.0, + target: a.1, }) .collect::>() } diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 8b92c2f9a..ceaec26cc 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -326,15 +326,15 @@ impl ViewToVec for PersonMentionView { type DbTuple = PersonMentionViewTuple; fn from_tuple_to_vec(items: Vec) -> Vec { items - .iter() + .into_iter() .map(|a| Self { - person_mention: a.0.to_owned(), - comment: a.1.to_owned(), - creator: a.2.to_owned(), - post: a.3.to_owned(), - community: a.4.to_owned(), - recipient: a.5.to_owned(), - counts: a.6.to_owned(), + person_mention: a.0, + comment: a.1, + creator: a.2, + post: a.3, + community: a.4, + recipient: a.5, + counts: a.6, creator_banned_from_community: a.7.is_some(), subscribed: CommunityFollower::to_subscribed_type(&a.8), saved: a.9.is_some(), diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 307f02484..0c6ca9536 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -137,10 +137,10 @@ impl ViewToVec for PersonViewSafe { type DbTuple = PersonViewSafeTuple; fn from_tuple_to_vec(items: Vec) -> Vec { items - .iter() + .into_iter() .map(|a| Self { - person: a.0.to_owned(), - counts: a.1.to_owned(), + person: a.0, + counts: a.1, }) .collect::>() } diff --git a/crates/db_views_actor/src/structs.rs b/crates/db_views_actor/src/structs.rs index b45728af5..cce82bb8e 100644 --- a/crates/db_views_actor/src/structs.rs +++ b/crates/db_views_actor/src/structs.rs @@ -3,6 +3,8 @@ use lemmy_db_schema::{ source::{ comment::Comment, community::CommunitySafe, + language::Language, + local_user::LocalUserSettings, person::{PersonSafe, PersonSafeAlias1}, person_mention::PersonMention, post::Post, @@ -70,3 +72,9 @@ pub struct PersonViewSafe { pub person: PersonSafe, pub counts: PersonAggregates, } + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct LocalUserDiscussionLanguageView { + pub local_user: LocalUserSettings, + pub language: Language, +} diff --git a/crates/db_views_moderator/src/admin_purge_community_view.rs b/crates/db_views_moderator/src/admin_purge_community_view.rs index 20903efcc..7510779ff 100644 --- a/crates/db_views_moderator/src/admin_purge_community_view.rs +++ b/crates/db_views_moderator/src/admin_purge_community_view.rs @@ -48,10 +48,10 @@ impl ViewToVec for AdminPurgeCommunityView { type DbTuple = AdminPurgeCommunityViewTuple; fn from_tuple_to_vec(items: Vec) -> Vec { items - .iter() + .into_iter() .map(|a| Self { - admin_purge_community: a.0.to_owned(), - admin: a.1.to_owned(), + admin_purge_community: a.0, + admin: a.1, }) .collect::>() }