From 985fe24669d3fdeecc0aa76cc74dd6570cbad5c8 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 1 Mar 2023 12:19:46 -0500 Subject: [PATCH] Get rid of Safe Views, use serde_skip (#2767) * Get rid of Safe Views, use serde_skip - Also change the ViewToVec, to work with non-vector cases. Might be necessary in preparation for #2763 - Fixes #2712 * Forgot one safe --------- Co-authored-by: Nutomic --- crates/api/src/community/ban.rs | 4 +- crates/api/src/local_user/add_admin.rs | 4 +- crates/api/src/local_user/ban_person.rs | 4 +- crates/api/src/local_user/block.rs | 4 +- crates/api/src/local_user/list_banned.rs | 4 +- crates/api/src/site/leave_admin.rs | 6 +- crates/api_common/src/community.rs | 4 +- crates/api_common/src/person.rs | 12 +- crates/api_common/src/site.rs | 12 +- crates/api_common/src/utils.rs | 19 +- crates/api_crud/src/site/read.rs | 4 +- crates/apub/src/api/read_person.rs | 4 +- crates/apub/src/api/resolve_object.rs | 4 +- crates/db_schema/src/impls/community.rs | 68 -------- crates/db_schema/src/impls/local_user.rs | 73 -------- crates/db_schema/src/impls/person.rs | 77 --------- crates/db_schema/src/source/community.rs | 29 +--- crates/db_schema/src/source/local_user.rs | 25 +-- crates/db_schema/src/source/person.rs | 31 +--- crates/db_schema/src/traits.rs | 16 +- crates/db_views/src/comment_report_view.rs | 162 ++++++++---------- crates/db_views/src/comment_view.rs | 70 ++++---- crates/db_views/src/local_user_view.rs | 60 ++----- crates/db_views/src/post_report_view.rs | 105 ++++++------ crates/db_views/src/post_view.rs | 74 ++++---- .../src/private_message_report_view.rs | 56 +++--- crates/db_views/src/private_message_view.rs | 43 +++-- .../src/registration_application_view.rs | 75 ++++---- crates/db_views/src/structs.rs | 53 +++--- .../db_views_actor/src/comment_reply_view.rs | 63 ++++--- .../src/community_block_view.rs | 33 ++-- .../src/community_follower_view.rs | 40 ++--- .../src/community_moderator_view.rs | 47 ++--- .../src/community_person_ban_view.rs | 13 +- crates/db_views_actor/src/community_view.rs | 37 ++-- .../db_views_actor/src/person_block_view.rs | 29 ++-- .../db_views_actor/src/person_mention_view.rs | 63 ++++--- crates/db_views_actor/src/person_view.rs | 55 +++--- crates/db_views_actor/src/structs.rs | 42 ++--- .../src/admin_purge_comment_view.rs | 33 ++-- .../src/admin_purge_community_view.rs | 30 ++-- .../src/admin_purge_person_view.rs | 30 ++-- .../src/admin_purge_post_view.rs | 35 ++-- .../src/mod_add_community_view.rs | 44 ++--- crates/db_views_moderator/src/mod_add_view.rs | 34 ++-- .../src/mod_ban_from_community_view.rs | 44 ++--- crates/db_views_moderator/src/mod_ban_view.rs | 34 ++-- .../src/mod_feature_post_view.rs | 38 ++-- .../src/mod_hide_community_view.rs | 35 ++-- .../src/mod_lock_post_view.rs | 38 ++-- .../src/mod_remove_comment_view.rs | 45 +++-- .../src/mod_remove_community_view.rs | 35 ++-- .../src/mod_remove_post_view.rs | 38 ++-- .../src/mod_transfer_community_view.rs | 44 ++--- crates/db_views_moderator/src/structs.rs | 66 +++---- 55 files changed, 825 insertions(+), 1322 deletions(-) diff --git a/crates/api/src/community/ban.rs b/crates/api/src/community/ban.rs index 41a8ae5f..b2d4260e 100644 --- a/crates/api/src/community/ban.rs +++ b/crates/api/src/community/ban.rs @@ -18,7 +18,7 @@ use lemmy_db_schema::{ }, traits::{Bannable, Crud, Followable}, }; -use lemmy_db_views_actor::structs::PersonViewSafe; +use lemmy_db_views_actor::structs::PersonView; use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId}; #[async_trait::async_trait(?Send)] @@ -88,7 +88,7 @@ impl Perform for BanFromCommunity { ModBanFromCommunity::create(context.pool(), &form).await?; let person_id = data.person_id; - let person_view = PersonViewSafe::read(context.pool(), person_id).await?; + let person_view = PersonView::read(context.pool(), person_id).await?; let res = BanFromCommunityResponse { person_view, diff --git a/crates/api/src/local_user/add_admin.rs b/crates/api/src/local_user/add_admin.rs index 3b22c447..ca30b939 100644 --- a/crates/api/src/local_user/add_admin.rs +++ b/crates/api/src/local_user/add_admin.rs @@ -13,7 +13,7 @@ use lemmy_db_schema::{ }, traits::Crud, }; -use lemmy_db_views_actor::structs::PersonViewSafe; +use lemmy_db_views_actor::structs::PersonView; use lemmy_utils::{error::LemmyError, ConnectionId}; #[async_trait::async_trait(?Send)] @@ -52,7 +52,7 @@ impl Perform for AddAdmin { ModAdd::create(context.pool(), &form).await?; - let admins = PersonViewSafe::admins(context.pool()).await?; + let admins = PersonView::admins(context.pool()).await?; let res = AddAdminResponse { admins }; diff --git a/crates/api/src/local_user/ban_person.rs b/crates/api/src/local_user/ban_person.rs index b0686949..d2c9789e 100644 --- a/crates/api/src/local_user/ban_person.rs +++ b/crates/api/src/local_user/ban_person.rs @@ -13,7 +13,7 @@ use lemmy_db_schema::{ }, traits::Crud, }; -use lemmy_db_views_actor::structs::PersonViewSafe; +use lemmy_db_views_actor::structs::PersonView; use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId}; #[async_trait::async_trait(?Send)] @@ -72,7 +72,7 @@ impl Perform for BanPerson { ModBan::create(context.pool(), &form).await?; let person_id = data.person_id; - let person_view = PersonViewSafe::read(context.pool(), person_id).await?; + let person_view = PersonView::read(context.pool(), person_id).await?; let res = BanPersonResponse { person_view, diff --git a/crates/api/src/local_user/block.rs b/crates/api/src/local_user/block.rs index b57ecd55..6dbf7cff 100644 --- a/crates/api/src/local_user/block.rs +++ b/crates/api/src/local_user/block.rs @@ -9,7 +9,7 @@ use lemmy_db_schema::{ source::person_block::{PersonBlock, PersonBlockForm}, traits::Blockable, }; -use lemmy_db_views_actor::structs::PersonViewSafe; +use lemmy_db_views_actor::structs::PersonView; use lemmy_utils::{error::LemmyError, ConnectionId}; #[async_trait::async_trait(?Send)] @@ -39,7 +39,7 @@ impl Perform for BlockPerson { target_id, }; - let target_person_view = PersonViewSafe::read(context.pool(), target_id).await?; + let target_person_view = PersonView::read(context.pool(), target_id).await?; if target_person_view.person.admin { return Err(LemmyError::from_message("cant_block_admin")); diff --git a/crates/api/src/local_user/list_banned.rs b/crates/api/src/local_user/list_banned.rs index 60eb32b3..3fd19f55 100644 --- a/crates/api/src/local_user/list_banned.rs +++ b/crates/api/src/local_user/list_banned.rs @@ -5,7 +5,7 @@ use lemmy_api_common::{ person::{BannedPersonsResponse, GetBannedPersons}, utils::{get_local_user_view_from_jwt, is_admin}, }; -use lemmy_db_views_actor::structs::PersonViewSafe; +use lemmy_db_views_actor::structs::PersonView; use lemmy_utils::{error::LemmyError, ConnectionId}; #[async_trait::async_trait(?Send)] @@ -24,7 +24,7 @@ impl Perform for GetBannedPersons { // Make sure user is an admin is_admin(&local_user_view)?; - let banned = PersonViewSafe::banned(context.pool()).await?; + let banned = PersonView::banned(context.pool()).await?; let res = Self::Response { banned }; diff --git a/crates/api/src/site/leave_admin.rs b/crates/api/src/site/leave_admin.rs index 7a75db51..ebe69790 100644 --- a/crates/api/src/site/leave_admin.rs +++ b/crates/api/src/site/leave_admin.rs @@ -16,7 +16,7 @@ use lemmy_db_schema::{ traits::Crud, }; use lemmy_db_views::structs::SiteView; -use lemmy_db_views_actor::structs::PersonViewSafe; +use lemmy_db_views_actor::structs::PersonView; use lemmy_utils::{error::LemmyError, version, ConnectionId}; #[async_trait::async_trait(?Send)] @@ -36,7 +36,7 @@ impl Perform for LeaveAdmin { is_admin(&local_user_view)?; // Make sure there isn't just one admin (so if one leaves, there will still be one left) - let admins = PersonViewSafe::admins(context.pool()).await?; + let admins = PersonView::admins(context.pool()).await?; if admins.len() == 1 { return Err(LemmyError::from_message("cannot_leave_admin")); } @@ -60,7 +60,7 @@ impl Perform for LeaveAdmin { // Reread site and admins let site_view = SiteView::read_local(context.pool()).await?; - let admins = PersonViewSafe::admins(context.pool()).await?; + let admins = PersonView::admins(context.pool()).await?; let all_languages = Language::read_all(context.pool()).await?; let discussion_languages = SiteLanguage::read_local(context.pool()).await?; diff --git a/crates/api_common/src/community.rs b/crates/api_common/src/community.rs index b0b2b0c6..0f290c3d 100644 --- a/crates/api_common/src/community.rs +++ b/crates/api_common/src/community.rs @@ -5,7 +5,7 @@ use lemmy_db_schema::{ ListingType, SortType, }; -use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonViewSafe}; +use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonView}; use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Clone, Default)] @@ -74,7 +74,7 @@ pub struct BanFromCommunity { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct BanFromCommunityResponse { - pub person_view: PersonViewSafe, + pub person_view: PersonView, pub banned: bool, } diff --git a/crates/api_common/src/person.rs b/crates/api_common/src/person.rs index 897dd998..99213664 100644 --- a/crates/api_common/src/person.rs +++ b/crates/api_common/src/person.rs @@ -9,7 +9,7 @@ use lemmy_db_views_actor::structs::{ CommentReplyView, CommunityModeratorView, PersonMentionView, - PersonViewSafe, + PersonView, }; use serde::{Deserialize, Serialize}; @@ -104,7 +104,7 @@ pub struct GetPersonDetails { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct GetPersonDetailsResponse { - pub person_view: PersonViewSafe, + pub person_view: PersonView, pub comments: Vec, pub posts: Vec, pub moderates: Vec, @@ -134,7 +134,7 @@ pub struct AddAdmin { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AddAdminResponse { - pub admins: Vec, + pub admins: Vec, } #[derive(Debug, Serialize, Deserialize, Clone, Default)] @@ -154,12 +154,12 @@ pub struct GetBannedPersons { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct BannedPersonsResponse { - pub banned: Vec, + pub banned: Vec, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct BanPersonResponse { - pub person_view: PersonViewSafe, + pub person_view: PersonView, pub banned: bool, } @@ -172,7 +172,7 @@ pub struct BlockPerson { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct BlockPersonResponse { - pub person_view: PersonViewSafe, + pub person_view: PersonView, pub blocked: bool, } diff --git a/crates/api_common/src/site.rs b/crates/api_common/src/site.rs index 6c967068..0000ecd3 100644 --- a/crates/api_common/src/site.rs +++ b/crates/api_common/src/site.rs @@ -14,7 +14,7 @@ use lemmy_db_schema::{ }; use lemmy_db_views::structs::{ CommentView, - LocalUserSettingsView, + LocalUserView, PostView, RegistrationApplicationView, SiteView, @@ -25,7 +25,7 @@ use lemmy_db_views_actor::structs::{ CommunityModeratorView, CommunityView, PersonBlockView, - PersonViewSafe, + PersonView, }; use lemmy_db_views_moderator::structs::{ AdminPurgeCommentView, @@ -66,7 +66,7 @@ pub struct SearchResponse { pub comments: Vec, pub posts: Vec, pub communities: Vec, - pub users: Vec, + pub users: Vec, } #[derive(Debug, Serialize, Deserialize, Clone, Default)] @@ -80,7 +80,7 @@ pub struct ResolveObjectResponse { pub comment: Option, pub post: Option, pub community: Option, - pub person: Option, + pub person: Option, } #[derive(Debug, Serialize, Deserialize, Clone, Default)] @@ -217,7 +217,7 @@ pub struct SiteResponse { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct GetSiteResponse { pub site_view: SiteView, - pub admins: Vec, + pub admins: Vec, pub online: usize, pub version: String, pub my_user: Option, @@ -229,7 +229,7 @@ pub struct GetSiteResponse { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct MyUserInfo { - pub local_user_view: LocalUserSettingsView, + pub local_user_view: LocalUserView, pub follows: Vec, pub moderates: Vec, pub community_blocks: Vec, diff --git a/crates/api_common/src/utils.rs b/crates/api_common/src/utils.rs index 0058128c..c4828d35 100644 --- a/crates/api_common/src/utils.rs +++ b/crates/api_common/src/utils.rs @@ -22,15 +22,12 @@ use lemmy_db_schema::{ utils::DbPool, ListingType, }; -use lemmy_db_views::{ - comment_view::CommentQuery, - structs::{LocalUserSettingsView, LocalUserView}, -}; +use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView}; use lemmy_db_views_actor::structs::{ CommunityModeratorView, CommunityPersonBanView, CommunityView, - PersonViewSafe, + PersonView, }; use lemmy_utils::{ claims::Claims, @@ -79,7 +76,7 @@ pub async fn is_mod_or_admin_opt( } pub async fn is_top_admin(pool: &DbPool, person_id: PersonId) -> Result<(), LemmyError> { - let admins = PersonViewSafe::admins(pool).await?; + let admins = PersonView::admins(pool).await?; let top_admin = admins .first() .ok_or_else(|| LemmyError::from_message("no admins"))?; @@ -197,14 +194,14 @@ pub async fn get_local_user_settings_view_from_jwt_opt( jwt: Option<&Sensitive>, pool: &DbPool, secret: &Secret, -) -> Result, LemmyError> { +) -> Result, LemmyError> { match jwt { Some(jwt) => { let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret) .map_err(|e| e.with_message("not_logged_in"))? .claims; let local_user_id = LocalUserId(claims.sub); - let local_user_view = LocalUserSettingsView::read(pool, local_user_id).await?; + let local_user_view = LocalUserView::read(pool, local_user_id).await?; check_user_valid( local_user_view.person.banned, local_user_view.person.ban_expires, @@ -450,7 +447,7 @@ pub fn get_interface_language(user: &LocalUserView) -> Lang { lang_str_to_lang(&user.local_user.interface_language) } -pub fn get_interface_language_from_settings(user: &LocalUserSettingsView) -> Lang { +pub fn get_interface_language_from_settings(user: &LocalUserView) -> Lang { lang_str_to_lang(&user.local_user.interface_language) } @@ -511,7 +508,7 @@ pub async fn send_new_applicant_email_to_admins( settings: &Settings, ) -> Result<(), LemmyError> { // Collect the admins with emails - let admins = LocalUserSettingsView::list_admins_with_emails(pool).await?; + let admins = LocalUserView::list_admins_with_emails(pool).await?; let applications_link = &format!( "{}/registration_applications", @@ -536,7 +533,7 @@ pub async fn send_new_report_email_to_admins( settings: &Settings, ) -> Result<(), LemmyError> { // Collect the admins with emails - let admins = LocalUserSettingsView::list_admins_with_emails(pool).await?; + let admins = LocalUserView::list_admins_with_emails(pool).await?; let reports_link = &format!("{}/reports", settings.get_protocol_and_hostname(),); diff --git a/crates/api_crud/src/site/read.rs b/crates/api_crud/src/site/read.rs index db386921..51457b88 100644 --- a/crates/api_crud/src/site/read.rs +++ b/crates/api_crud/src/site/read.rs @@ -16,7 +16,7 @@ use lemmy_db_views_actor::structs::{ CommunityFollowerView, CommunityModeratorView, PersonBlockView, - PersonViewSafe, + PersonView, }; use lemmy_utils::{error::LemmyError, version, ConnectionId}; @@ -34,7 +34,7 @@ impl PerformCrud for GetSite { let site_view = SiteView::read_local(context.pool()).await?; - let admins = PersonViewSafe::admins(context.pool()).await?; + let admins = PersonView::admins(context.pool()).await?; let online = context.chat_server().get_users_online()?; diff --git a/crates/apub/src/api/read_person.rs b/crates/apub/src/api/read_person.rs index c73d710f..a3a5f943 100644 --- a/crates/apub/src/api/read_person.rs +++ b/crates/apub/src/api/read_person.rs @@ -10,7 +10,7 @@ use lemmy_db_schema::{ utils::post_to_comment_sort_type, }; use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery}; -use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonViewSafe}; +use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonView}; use lemmy_utils::{error::LemmyError, ConnectionId}; #[async_trait::async_trait(?Send)] @@ -56,7 +56,7 @@ impl PerformApub for GetPersonDetails { // You don't need to return settings for the user, since this comes back with GetSite // `my_user` - let person_view = PersonViewSafe::read(context.pool(), person_details_id).await?; + let person_view = PersonView::read(context.pool(), person_details_id).await?; let sort = data.sort; let page = data.page; diff --git a/crates/apub/src/api/resolve_object.rs b/crates/apub/src/api/resolve_object.rs index 537e1adb..cd5ee47c 100644 --- a/crates/apub/src/api/resolve_object.rs +++ b/crates/apub/src/api/resolve_object.rs @@ -11,7 +11,7 @@ use lemmy_api_common::{ }; use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool}; use lemmy_db_views::structs::{CommentView, PostView}; -use lemmy_db_views_actor::structs::{CommunityView, PersonViewSafe}; +use lemmy_db_views_actor::structs::{CommunityView, PersonView}; use lemmy_utils::{error::LemmyError, ConnectionId}; #[async_trait::async_trait(?Send)] @@ -52,7 +52,7 @@ async fn convert_response( match object { Person(p) => { removed_or_deleted = p.deleted; - res.person = Some(PersonViewSafe::read(pool, p.id).await?) + res.person = Some(PersonView::read(pool, p.id).await?) } Community(c) => { removed_or_deleted = c.deleted || c.removed; diff --git a/crates/db_schema/src/impls/community.rs b/crates/db_schema/src/impls/community.rs index d4dd2ebc..fb22a5e3 100644 --- a/crates/db_schema/src/impls/community.rs +++ b/crates/db_schema/src/impls/community.rs @@ -22,74 +22,6 @@ use crate::{ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods}; use diesel_async::RunQueryDsl; -mod safe_type { - use crate::{ - schema::community::{ - actor_id, - banner, - deleted, - description, - hidden, - icon, - id, - instance_id, - local, - name, - nsfw, - posting_restricted_to_mods, - published, - removed, - title, - updated, - }, - source::community::Community, - traits::ToSafe, - }; - - type Columns = ( - id, - name, - title, - description, - removed, - published, - updated, - deleted, - nsfw, - actor_id, - local, - icon, - banner, - hidden, - posting_restricted_to_mods, - instance_id, - ); - - impl ToSafe for Community { - type SafeColumns = Columns; - fn safe_columns_tuple() -> Self::SafeColumns { - ( - id, - name, - title, - description, - removed, - published, - updated, - deleted, - nsfw, - actor_id, - local, - icon, - banner, - hidden, - posting_restricted_to_mods, - instance_id, - ) - } - } -} - #[async_trait] impl Crud for Community { type InsertForm = CommunityInsertForm; diff --git a/crates/db_schema/src/impls/local_user.rs b/crates/db_schema/src/impls/local_user.rs index 650eac65..a35096dc 100644 --- a/crates/db_schema/src/impls/local_user.rs +++ b/crates/db_schema/src/impls/local_user.rs @@ -18,79 +18,6 @@ use bcrypt::{hash, DEFAULT_COST}; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel_async::RunQueryDsl; -mod safe_settings_type { - use crate::{ - schema::local_user::columns::{ - accepted_application, - default_listing_type, - default_sort_type, - email, - email_verified, - id, - interface_language, - person_id, - send_notifications_to_email, - show_avatars, - show_bot_accounts, - show_new_post_notifs, - show_nsfw, - show_read_posts, - show_scores, - theme, - validator_time, - }, - source::local_user::LocalUser, - traits::ToSafeSettings, - }; - - type Columns = ( - id, - person_id, - email, - show_nsfw, - theme, - default_sort_type, - default_listing_type, - interface_language, - show_avatars, - send_notifications_to_email, - validator_time, - show_bot_accounts, - show_scores, - show_read_posts, - show_new_post_notifs, - email_verified, - accepted_application, - ); - - impl ToSafeSettings for LocalUser { - type SafeSettingsColumns = Columns; - - /// Includes everything but the hashed password - fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns { - ( - id, - person_id, - email, - show_nsfw, - theme, - default_sort_type, - default_listing_type, - interface_language, - show_avatars, - send_notifications_to_email, - validator_time, - show_bot_accounts, - show_scores, - show_read_posts, - show_new_post_notifs, - email_verified, - accepted_application, - ) - } - } -} - impl LocalUser { pub async fn update_password( pool: &DbPool, diff --git a/crates/db_schema/src/impls/person.rs b/crates/db_schema/src/impls/person.rs index 13be5843..032e2e01 100644 --- a/crates/db_schema/src/impls/person.rs +++ b/crates/db_schema/src/impls/person.rs @@ -26,83 +26,6 @@ use crate::{ use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods}; use diesel_async::RunQueryDsl; -mod safe_type { - use crate::{ - schema::person::columns::{ - actor_id, - admin, - avatar, - ban_expires, - banned, - banner, - bio, - bot_account, - deleted, - display_name, - id, - inbox_url, - instance_id, - local, - matrix_user_id, - name, - published, - shared_inbox_url, - updated, - }, - source::person::Person, - traits::ToSafe, - }; - - type Columns = ( - id, - name, - display_name, - avatar, - banned, - published, - updated, - actor_id, - bio, - local, - banner, - deleted, - inbox_url, - shared_inbox_url, - matrix_user_id, - admin, - bot_account, - ban_expires, - instance_id, - ); - - impl ToSafe for Person { - type SafeColumns = Columns; - fn safe_columns_tuple() -> Self::SafeColumns { - ( - id, - name, - display_name, - avatar, - banned, - published, - updated, - actor_id, - bio, - local, - banner, - deleted, - inbox_url, - shared_inbox_url, - matrix_user_id, - admin, - bot_account, - ban_expires, - instance_id, - ) - } - } -} - #[async_trait] impl Crud for Person { type InsertForm = PersonInsertForm; diff --git a/crates/db_schema/src/source/community.rs b/crates/db_schema/src/source/community.rs index 664e29b4..d2db46f2 100644 --- a/crates/db_schema/src/source/community.rs +++ b/crates/db_schema/src/source/community.rs @@ -19,13 +19,19 @@ pub struct Community { pub nsfw: bool, pub actor_id: DbUrl, pub local: bool, + #[serde(skip)] pub private_key: Option, + #[serde(skip)] pub public_key: String, + #[serde(skip)] pub last_refreshed_at: chrono::NaiveDateTime, pub icon: Option, pub banner: Option, + #[serde(skip_serializing)] pub followers_url: DbUrl, + #[serde(skip_serializing)] pub inbox_url: DbUrl, + #[serde(skip)] pub shared_inbox_url: Option, /// Url where moderators collection is served over Activitypub #[serde(skip)] @@ -38,29 +44,6 @@ pub struct Community { pub instance_id: InstanceId, } -/// A safe representation of community, without the sensitive info -#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "full", derive(Queryable, Identifiable))] -#[cfg_attr(feature = "full", diesel(table_name = community))] -pub struct CommunitySafe { - pub id: CommunityId, - pub name: String, - pub title: String, - pub description: Option, - pub removed: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub deleted: bool, - pub nsfw: bool, - pub actor_id: DbUrl, - pub local: bool, - pub icon: Option, - pub banner: Option, - pub hidden: bool, - pub posting_restricted_to_mods: bool, - pub instance_id: InstanceId, -} - #[derive(Debug, Clone, TypedBuilder)] #[builder(field_defaults(default))] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] diff --git a/crates/db_schema/src/source/local_user.rs b/crates/db_schema/src/source/local_user.rs index 37a559bc..c38a5ac6 100644 --- a/crates/db_schema/src/source/local_user.rs +++ b/crates/db_schema/src/source/local_user.rs @@ -10,6 +10,7 @@ use typed_builder::TypedBuilder; pub struct LocalUser { pub id: LocalUserId, pub person_id: PersonId, + #[serde(skip)] pub password_encrypted: String, pub email: Option, pub show_nsfw: bool, @@ -28,30 +29,6 @@ pub struct LocalUser { pub accepted_application: bool, } -/// A local user view that removes password encrypted -#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "full", derive(Queryable, Identifiable))] -#[cfg_attr(feature = "full", diesel(table_name = local_user))] -pub struct LocalUserSettings { - pub id: LocalUserId, - pub person_id: PersonId, - pub email: Option, - pub show_nsfw: bool, - pub theme: String, - pub default_sort_type: i16, - pub default_listing_type: i16, - pub interface_language: String, - pub show_avatars: bool, - pub send_notifications_to_email: bool, - pub validator_time: chrono::NaiveDateTime, - pub show_bot_accounts: bool, - pub show_scores: bool, - pub show_read_posts: bool, - pub show_new_post_notifs: bool, - pub email_verified: bool, - pub accepted_application: bool, -} - #[derive(Clone, TypedBuilder)] #[builder(field_defaults(default))] #[cfg_attr(feature = "full", derive(Insertable))] diff --git a/crates/db_schema/src/source/person.rs b/crates/db_schema/src/source/person.rs index 9cbafbfc..d9d8050d 100644 --- a/crates/db_schema/src/source/person.rs +++ b/crates/db_schema/src/source/person.rs @@ -18,38 +18,17 @@ pub struct Person { pub actor_id: DbUrl, pub bio: Option, pub local: bool, + #[serde(skip)] pub private_key: Option, + #[serde(skip)] pub public_key: String, + #[serde(skip)] pub last_refreshed_at: chrono::NaiveDateTime, pub banner: Option, pub deleted: bool, + #[serde(skip_serializing)] pub inbox_url: DbUrl, - pub shared_inbox_url: Option, - pub matrix_user_id: Option, - pub admin: bool, - pub bot_account: bool, - pub ban_expires: Option, - pub instance_id: InstanceId, -} - -/// A safe representation of person, without the sensitive info -#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] -#[cfg_attr(feature = "full", derive(Queryable, Identifiable))] -#[cfg_attr(feature = "full", diesel(table_name = person))] -pub struct PersonSafe { - pub id: PersonId, - pub name: String, - pub display_name: Option, - pub avatar: Option, - pub banned: bool, - pub published: chrono::NaiveDateTime, - pub updated: Option, - pub actor_id: DbUrl, - pub bio: Option, - pub local: bool, - pub banner: Option, - pub deleted: bool, - pub inbox_url: DbUrl, + #[serde(skip)] pub shared_inbox_url: Option, pub matrix_user_id: Option, pub admin: bool, diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index adbbf802..d4e01547 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -140,19 +140,9 @@ pub trait Reportable { Self: Sized; } -pub trait ToSafe { - type SafeColumns; - fn safe_columns_tuple() -> Self::SafeColumns; -} - -pub trait ToSafeSettings { - type SafeSettingsColumns; - fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns; -} - -pub trait ViewToVec { - type DbTuple; - fn from_tuple_to_vec(tuple: Vec) -> Vec +pub trait JoinView { + type JoinTuple; + fn from_tuple(tuple: Self::JoinTuple) -> Self where Self: Sized; } diff --git a/crates/db_views/src/comment_report_view.rs b/crates/db_views/src/comment_report_view.rs index f9d2077f..df14ea55 100644 --- a/crates/db_views/src/comment_report_view.rs +++ b/crates/db_views/src/comment_report_view.rs @@ -26,28 +26,15 @@ use lemmy_db_schema::{ source::{ comment::Comment, comment_report::CommentReport, - community::{Community, CommunityPersonBan, CommunitySafe}, - person::{Person, PersonSafe}, + community::{Community, CommunityPersonBan}, + person::Person, post::Post, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; use typed_builder::TypedBuilder; -type CommentReportViewTuple = ( - CommentReport, - Comment, - Post, - CommunitySafe, - PersonSafe, - PersonSafe, - CommentAggregates, - Option, - Option, - Option, -); - impl CommentReportView { /// returns the CommentReportView for the provided report_id /// @@ -61,18 +48,7 @@ impl CommentReportView { let (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2); - let ( - comment_report, - comment, - post, - community, - creator, - comment_creator, - counts, - creator_banned_from_community, - comment_like, - resolver, - ) = comment_report::table + let res = comment_report::table .find(report_id) .inner_join(comment::table) .inner_join(post::table.on(comment::post_id.eq(post::id))) @@ -109,33 +85,18 @@ impl CommentReportView { comment_report::all_columns, comment::all_columns, post::all_columns, - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), comment_like::score.nullable(), - person_alias_2 - .fields(Person::safe_columns_tuple()) - .nullable(), + person_alias_2.fields(person::all_columns).nullable(), )) - .first::(conn) + .first::<::JoinTuple>(conn) .await?; - let my_vote = comment_like; - - Ok(Self { - comment_report, - comment, - post, - community, - creator, - comment_creator, - counts, - creator_banned_from_community: creator_banned_from_community.is_some(), - my_vote, - resolver, - }) + Ok(Self::from_tuple(res)) } /// Returns the current unresolved post report count for the communities you mod @@ -238,15 +199,13 @@ impl<'a> CommentReportQuery<'a> { comment_report::all_columns, comment::all_columns, post::all_columns, - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), comment_like::score.nullable(), - person_alias_2 - .fields(Person::safe_columns_tuple()) - .nullable(), + person_alias_2.fields(person::all_columns).nullable(), )) .into_boxed(); @@ -275,34 +234,45 @@ impl<'a> CommentReportQuery<'a> { .and(community_moderator::person_id.eq(self.my_person_id)), ), ) - .load::(conn) + .load::<::JoinTuple>(conn) .await? } else { - query.load::(conn).await? + query + .load::<::JoinTuple>(conn) + .await? }; - Ok(CommentReportView::from_tuple_to_vec(res)) + Ok(res.into_iter().map(CommentReportView::from_tuple).collect()) } } -impl ViewToVec for CommentReportView { - type DbTuple = CommentReportViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - comment_report: a.0, - comment: a.1, - post: a.2, - community: a.3, - creator: a.4, - comment_creator: a.5, - counts: a.6, - creator_banned_from_community: a.7.is_some(), - my_vote: a.8, - resolver: a.9, - }) - .collect::>() +impl JoinView for CommentReportView { + type JoinTuple = ( + CommentReport, + Comment, + Post, + Community, + Person, + Person, + CommentAggregates, + Option, + Option, + Option, + ); + + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + comment_report: a.0, + comment: a.1, + post: a.2, + community: a.3, + creator: a.4, + comment_creator: a.5, + counts: a.6, + creator_banned_from_community: a.7.is_some(), + my_vote: a.8, + resolver: a.9, + } } } @@ -314,15 +284,9 @@ mod tests { source::{ comment::{Comment, CommentInsertForm}, comment_report::{CommentReport, CommentReportForm}, - community::{ - Community, - CommunityInsertForm, - CommunityModerator, - CommunityModeratorForm, - CommunitySafe, - }, + community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm}, instance::Instance, - person::{Person, PersonInsertForm, PersonSafe}, + person::{Person, PersonInsertForm}, post::{Post, PostInsertForm}, }, traits::{Crud, Joinable, Reportable}, @@ -435,7 +399,7 @@ mod tests { comment_report: inserted_jessica_report.clone(), comment: inserted_comment.clone(), post: inserted_post, - community: CommunitySafe { + community: Community { id: inserted_community.id, name: inserted_community.name, icon: None, @@ -451,9 +415,17 @@ mod tests { hidden: false, posting_restricted_to_mods: false, published: inserted_community.published, + private_key: inserted_community.private_key, + public_key: inserted_community.public_key, + last_refreshed_at: inserted_community.last_refreshed_at, + followers_url: inserted_community.followers_url, + inbox_url: inserted_community.inbox_url, + shared_inbox_url: inserted_community.shared_inbox_url, + moderators_url: inserted_community.moderators_url, + featured_url: inserted_community.featured_url, instance_id: inserted_instance.id, }, - creator: PersonSafe { + creator: Person { id: inserted_jessica.id, name: inserted_jessica.name, display_name: None, @@ -473,8 +445,11 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: inserted_instance.id, + private_key: inserted_jessica.private_key, + public_key: inserted_jessica.public_key, + last_refreshed_at: inserted_jessica.last_refreshed_at, }, - comment_creator: PersonSafe { + comment_creator: Person { id: inserted_timmy.id, name: inserted_timmy.name.clone(), display_name: None, @@ -494,6 +469,9 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: inserted_instance.id, + private_key: inserted_timmy.private_key.clone(), + public_key: inserted_timmy.public_key.clone(), + last_refreshed_at: inserted_timmy.last_refreshed_at, }, creator_banned_from_community: false, counts: CommentAggregates { @@ -513,7 +491,7 @@ mod tests { let mut expected_sara_report_view = expected_jessica_report_view.clone(); expected_sara_report_view.comment_report = inserted_sara_report; - expected_sara_report_view.creator = PersonSafe { + expected_sara_report_view.creator = Person { id: inserted_sara.id, name: inserted_sara.name, display_name: None, @@ -533,6 +511,9 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: inserted_instance.id, + private_key: inserted_sara.private_key, + public_key: inserted_sara.public_key, + last_refreshed_at: inserted_sara.last_refreshed_at, }; // Do a batch read of timmys reports @@ -580,7 +561,7 @@ mod tests { .updated = read_jessica_report_view_after_resolve .comment_report .updated; - expected_jessica_report_view_after_resolve.resolver = Some(PersonSafe { + expected_jessica_report_view_after_resolve.resolver = Some(Person { id: inserted_timmy.id, name: inserted_timmy.name.clone(), display_name: None, @@ -596,6 +577,9 @@ mod tests { banner: None, updated: None, inbox_url: inserted_timmy.inbox_url.clone(), + private_key: inserted_timmy.private_key.clone(), + public_key: inserted_timmy.public_key.clone(), + last_refreshed_at: inserted_timmy.last_refreshed_at, shared_inbox_url: None, matrix_user_id: None, ban_expires: None, diff --git a/crates/db_views/src/comment_view.rs b/crates/db_views/src/comment_view.rs index 4fb950a5..159ccaf1 100644 --- a/crates/db_views/src/comment_view.rs +++ b/crates/db_views/src/comment_view.rs @@ -30,13 +30,13 @@ use lemmy_db_schema::{ }, source::{ comment::{Comment, CommentSaved}, - community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, + community::{Community, CommunityFollower, CommunityPersonBan}, local_user::LocalUser, - person::{Person, PersonSafe}, + person::Person, person_block::PersonBlock, post::Post, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{functions::hot_rank, fuzzy_search, get_conn, limit_and_offset_unlimited, DbPool}, CommentSortType, ListingType, @@ -45,9 +45,9 @@ use typed_builder::TypedBuilder; type CommentViewTuple = ( Comment, - PersonSafe, + Person, Post, - CommunitySafe, + Community, CommentAggregates, Option, Option, @@ -126,9 +126,9 @@ impl CommentView { ) .select(( comment::all_columns, - Person::safe_columns_tuple(), + person::all_columns, post::all_columns, - Community::safe_columns_tuple(), + community::all_columns, comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -252,9 +252,9 @@ impl<'a> CommentQuery<'a> { ) .select(( comment::all_columns, - Person::safe_columns_tuple(), + person::all_columns, post::all_columns, - Community::safe_columns_tuple(), + community::all_columns, comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -375,28 +375,25 @@ impl<'a> CommentQuery<'a> { .load::(conn) .await?; - Ok(CommentView::from_tuple_to_vec(res)) + Ok(res.into_iter().map(CommentView::from_tuple).collect()) } } -impl ViewToVec for CommentView { - type DbTuple = CommentViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - comment: a.0, - creator: a.1, - post: a.2, - community: a.3, - counts: a.4, - creator_banned_from_community: a.5.is_some(), - subscribed: CommunityFollower::to_subscribed_type(&a.6), - saved: a.7.is_some(), - creator_blocked: a.8.is_some(), - my_vote: a.9, - }) - .collect::>() +impl JoinView for CommentView { + type JoinTuple = CommentViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + comment: a.0, + creator: a.1, + post: a.2, + community: a.3, + counts: a.4, + creator_banned_from_community: a.5.is_some(), + subscribed: CommunityFollower::to_subscribed_type(&a.6), + saved: a.7.is_some(), + creator_blocked: a.8.is_some(), + my_vote: a.9, + } } } @@ -408,12 +405,10 @@ mod tests { CommentSortType, CommentView, Community, - CommunitySafe, DbPool, LocalUser, Person, PersonBlock, - PersonSafe, Post, }; use lemmy_db_schema::{ @@ -841,7 +836,7 @@ mod tests { path: data.inserted_comment_0.clone().path, language_id: LanguageId(0), }, - creator: PersonSafe { + creator: Person { id: data.inserted_person.id, name: "timmy".into(), display_name: None, @@ -861,6 +856,9 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: data.inserted_instance.id, + private_key: data.inserted_person.private_key.clone(), + public_key: data.inserted_person.public_key.clone(), + last_refreshed_at: data.inserted_person.last_refreshed_at, }, post: Post { id: data.inserted_post.id, @@ -885,7 +883,7 @@ mod tests { featured_community: false, featured_local: false, }, - community: CommunitySafe { + community: Community { id: data.inserted_community.id, name: "test community 5".to_string(), icon: None, @@ -902,6 +900,14 @@ mod tests { posting_restricted_to_mods: false, published: data.inserted_community.published, instance_id: data.inserted_instance.id, + private_key: data.inserted_community.private_key.clone(), + public_key: data.inserted_community.public_key.clone(), + last_refreshed_at: data.inserted_community.last_refreshed_at, + followers_url: data.inserted_community.followers_url.clone(), + inbox_url: data.inserted_community.inbox_url.clone(), + shared_inbox_url: data.inserted_community.shared_inbox_url.clone(), + moderators_url: data.inserted_community.moderators_url.clone(), + featured_url: data.inserted_community.featured_url.clone(), }, counts: CommentAggregates { id: agg.id, diff --git a/crates/db_views/src/local_user_view.rs b/crates/db_views/src/local_user_view.rs index da735aae..65d25160 100644 --- a/crates/db_views/src/local_user_view.rs +++ b/crates/db_views/src/local_user_view.rs @@ -1,15 +1,12 @@ -use crate::structs::{LocalUserSettingsView, LocalUserView}; +use crate::structs::LocalUserView; use diesel::{result::Error, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl}; use diesel_async::RunQueryDsl; use lemmy_db_schema::{ aggregates::structs::PersonAggregates, newtypes::{LocalUserId, PersonId}, schema::{local_user, person, person_aggregates}, - source::{ - local_user::{LocalUser, LocalUserSettings}, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ToSafeSettings, ViewToVec}, + source::{local_user::LocalUser, person::Person}, + traits::JoinView, utils::{functions::lower, get_conn, DbPool}, }; @@ -121,30 +118,6 @@ impl LocalUserView { counts, }) } -} - -type LocalUserSettingsViewTuple = (LocalUserSettings, PersonSafe, PersonAggregates); - -impl LocalUserSettingsView { - pub async fn read(pool: &DbPool, local_user_id: LocalUserId) -> Result { - let conn = &mut get_conn(pool).await?; - let (local_user, person, counts) = local_user::table - .find(local_user_id) - .inner_join(person::table) - .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) - .select(( - LocalUser::safe_settings_columns_tuple(), - Person::safe_columns_tuple(), - person_aggregates::all_columns, - )) - .first::(conn) - .await?; - Ok(Self { - local_user, - person, - counts, - }) - } pub async fn list_admins_with_emails(pool: &DbPool) -> Result, Error> { let conn = &mut get_conn(pool).await?; @@ -154,27 +127,24 @@ impl LocalUserSettingsView { .inner_join(person::table) .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) .select(( - LocalUser::safe_settings_columns_tuple(), - Person::safe_columns_tuple(), + local_user::all_columns, + person::all_columns, person_aggregates::all_columns, )) - .load::(conn) + .load::(conn) .await?; - Ok(LocalUserSettingsView::from_tuple_to_vec(res)) + Ok(res.into_iter().map(LocalUserView::from_tuple).collect()) } } -impl ViewToVec for LocalUserSettingsView { - type DbTuple = LocalUserSettingsViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - local_user: a.0, - person: a.1, - counts: a.2, - }) - .collect::>() +impl JoinView for LocalUserView { + type JoinTuple = LocalUserViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + local_user: a.0, + person: a.1, + counts: a.2, + } } } diff --git a/crates/db_views/src/post_report_view.rs b/crates/db_views/src/post_report_view.rs index e2661d6d..afda89e6 100644 --- a/crates/db_views/src/post_report_view.rs +++ b/crates/db_views/src/post_report_view.rs @@ -23,12 +23,12 @@ use lemmy_db_schema::{ post_report, }, source::{ - community::{Community, CommunityPersonBan, CommunitySafe}, - person::{Person, PersonSafe}, + community::{Community, CommunityPersonBan}, + person::Person, post::Post, post_report::PostReport, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; use typed_builder::TypedBuilder; @@ -36,13 +36,13 @@ use typed_builder::TypedBuilder; type PostReportViewTuple = ( PostReport, Post, - CommunitySafe, - PersonSafe, - PersonSafe, + Community, + Person, + Person, Option, Option, PostAggregates, - Option, + Option, ); impl PostReportView { @@ -99,13 +99,13 @@ impl PostReportView { .select(( post_report::all_columns, post::all_columns, - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), community_person_ban::all_columns.nullable(), post_like::score.nullable(), post_aggregates::all_columns, - person_alias_2.fields(Person::safe_columns_tuple().nullable()), + person_alias_2.fields(person::all_columns.nullable()), )) .first::(conn) .await?; @@ -216,15 +216,13 @@ impl<'a> PostReportQuery<'a> { .select(( post_report::all_columns, post::all_columns, - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + community::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns), community_person_ban::all_columns.nullable(), post_like::score.nullable(), post_aggregates::all_columns, - person_alias_2 - .fields(Person::safe_columns_tuple()) - .nullable(), + person_alias_2.fields(person::all_columns.nullable()), )) .into_boxed(); @@ -259,27 +257,24 @@ impl<'a> PostReportQuery<'a> { query.load::(conn).await? }; - Ok(PostReportView::from_tuple_to_vec(res)) + Ok(res.into_iter().map(PostReportView::from_tuple).collect()) } } -impl ViewToVec for PostReportView { - type DbTuple = PostReportViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - post_report: a.0, - post: a.1, - community: a.2, - creator: a.3, - post_creator: a.4, - creator_banned_from_community: a.5.is_some(), - my_vote: a.6, - counts: a.7, - resolver: a.8, - }) - .collect::>() +impl JoinView for PostReportView { + type JoinTuple = PostReportViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + post_report: a.0, + post: a.1, + community: a.2, + creator: a.3, + post_creator: a.4, + creator_banned_from_community: a.5.is_some(), + my_vote: a.6, + counts: a.7, + resolver: a.8, + } } } @@ -289,15 +284,9 @@ mod tests { use lemmy_db_schema::{ aggregates::structs::PostAggregates, source::{ - community::{ - Community, - CommunityInsertForm, - CommunityModerator, - CommunityModeratorForm, - CommunitySafe, - }, + community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm}, instance::Instance, - person::{Person, PersonInsertForm, PersonSafe}, + person::{Person, PersonInsertForm}, post::{Post, PostInsertForm}, post_report::{PostReport, PostReportForm}, }, @@ -402,7 +391,7 @@ mod tests { let expected_jessica_report_view = PostReportView { post_report: inserted_jessica_report.clone(), post: inserted_post.clone(), - community: CommunitySafe { + community: Community { id: inserted_community.id, name: inserted_community.name, icon: None, @@ -419,8 +408,16 @@ mod tests { posting_restricted_to_mods: false, published: inserted_community.published, instance_id: inserted_instance.id, + private_key: inserted_community.private_key.clone(), + public_key: inserted_community.public_key.clone(), + last_refreshed_at: inserted_community.last_refreshed_at, + followers_url: inserted_community.followers_url.clone(), + inbox_url: inserted_community.inbox_url.clone(), + shared_inbox_url: inserted_community.shared_inbox_url.clone(), + moderators_url: inserted_community.moderators_url.clone(), + featured_url: inserted_community.featured_url.clone(), }, - creator: PersonSafe { + creator: Person { id: inserted_jessica.id, name: inserted_jessica.name, display_name: None, @@ -440,8 +437,11 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: inserted_instance.id, + private_key: inserted_jessica.private_key, + public_key: inserted_jessica.public_key, + last_refreshed_at: inserted_jessica.last_refreshed_at, }, - post_creator: PersonSafe { + post_creator: Person { id: inserted_timmy.id, name: inserted_timmy.name.clone(), display_name: None, @@ -461,6 +461,9 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: inserted_instance.id, + private_key: inserted_timmy.private_key.clone(), + public_key: inserted_timmy.public_key.clone(), + last_refreshed_at: inserted_timmy.last_refreshed_at, }, creator_banned_from_community: false, my_vote: None, @@ -485,7 +488,7 @@ mod tests { let mut expected_sara_report_view = expected_jessica_report_view.clone(); expected_sara_report_view.post_report = inserted_sara_report; expected_sara_report_view.my_vote = None; - expected_sara_report_view.creator = PersonSafe { + expected_sara_report_view.creator = Person { id: inserted_sara.id, name: inserted_sara.name, display_name: None, @@ -505,6 +508,9 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: inserted_instance.id, + private_key: inserted_sara.private_key, + public_key: inserted_sara.public_key, + last_refreshed_at: inserted_sara.last_refreshed_at, }; // Do a batch read of timmys reports @@ -550,7 +556,7 @@ mod tests { expected_jessica_report_view_after_resolve .post_report .updated = read_jessica_report_view_after_resolve.post_report.updated; - expected_jessica_report_view_after_resolve.resolver = Some(PersonSafe { + expected_jessica_report_view_after_resolve.resolver = Some(Person { id: inserted_timmy.id, name: inserted_timmy.name.clone(), display_name: None, @@ -570,6 +576,9 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: inserted_instance.id, + private_key: inserted_timmy.private_key.clone(), + public_key: inserted_timmy.public_key.clone(), + last_refreshed_at: inserted_timmy.last_refreshed_at, }); assert_eq!( diff --git a/crates/db_views/src/post_view.rs b/crates/db_views/src/post_view.rs index ed2711c4..449da8b8 100644 --- a/crates/db_views/src/post_view.rs +++ b/crates/db_views/src/post_view.rs @@ -33,13 +33,13 @@ use lemmy_db_schema::{ post_saved, }, source::{ - community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, + community::{Community, CommunityFollower, CommunityPersonBan}, local_user::LocalUser, - person::{Person, PersonSafe}, + person::Person, person_block::PersonBlock, post::{Post, PostRead, PostSaved}, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{functions::hot_rank, fuzzy_search, get_conn, limit_and_offset, DbPool}, ListingType, SortType, @@ -49,8 +49,8 @@ use typed_builder::TypedBuilder; type PostViewTuple = ( Post, - PersonSafe, - CommunitySafe, + Person, + Community, Option, PostAggregates, Option, @@ -144,8 +144,8 @@ impl PostView { ) .select(( post::all_columns, - Person::safe_columns_tuple(), - Community::safe_columns_tuple(), + person::all_columns, + community::all_columns, community_person_ban::all_columns.nullable(), post_aggregates::all_columns, community_follower::all_columns.nullable(), @@ -326,8 +326,8 @@ impl<'a> PostQuery<'a> { ) .select(( post::all_columns, - Person::safe_columns_tuple(), - Community::safe_columns_tuple(), + person::all_columns, + community::all_columns, community_person_ban::all_columns.nullable(), post_aggregates::all_columns, community_follower::all_columns.nullable(), @@ -485,29 +485,26 @@ impl<'a> PostQuery<'a> { let res = query.load::(conn).await?; - Ok(PostView::from_tuple_to_vec(res)) + Ok(res.into_iter().map(PostView::from_tuple).collect()) } } -impl ViewToVec for PostView { - type DbTuple = PostViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - post: a.0, - creator: a.1, - community: a.2, - creator_banned_from_community: a.3.is_some(), - counts: a.4, - subscribed: CommunityFollower::to_subscribed_type(&a.5), - saved: a.6.is_some(), - read: a.7.is_some(), - creator_blocked: a.8.is_some(), - my_vote: a.9, - unread_comments: a.10, - }) - .collect::>() +impl JoinView for PostView { + type JoinTuple = PostViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + post: a.0, + creator: a.1, + community: a.2, + creator_banned_from_community: a.3.is_some(), + counts: a.4, + subscribed: CommunityFollower::to_subscribed_type(&a.5), + saved: a.6.is_some(), + read: a.7.is_some(), + creator_blocked: a.8.is_some(), + my_vote: a.9, + unread_comments: a.10, + } } } @@ -520,12 +517,12 @@ mod tests { newtypes::LanguageId, source::{ actor_language::LocalUserLanguage, - community::{Community, CommunityInsertForm, CommunitySafe}, + community::{Community, CommunityInsertForm}, community_block::{CommunityBlock, CommunityBlockForm}, instance::Instance, language::Language, local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm}, - person::{Person, PersonInsertForm, PersonSafe}, + person::{Person, PersonInsertForm}, person_block::{PersonBlock, PersonBlockForm}, post::{Post, PostInsertForm, PostLike, PostLikeForm}, }, @@ -934,7 +931,7 @@ mod tests { }, my_vote: None, unread_comments: 0, - creator: PersonSafe { + creator: Person { id: inserted_person.id, name: inserted_person.name.clone(), display_name: None, @@ -954,9 +951,12 @@ mod tests { matrix_user_id: None, ban_expires: None, instance_id: data.inserted_instance.id, + private_key: inserted_person.private_key.clone(), + public_key: inserted_person.public_key.clone(), + last_refreshed_at: inserted_person.last_refreshed_at, }, creator_banned_from_community: false, - community: CommunitySafe { + community: Community { id: inserted_community.id, name: inserted_community.name.clone(), icon: None, @@ -973,6 +973,14 @@ mod tests { posting_restricted_to_mods: false, published: inserted_community.published, instance_id: data.inserted_instance.id, + private_key: inserted_community.private_key.clone(), + public_key: inserted_community.public_key.clone(), + last_refreshed_at: inserted_community.last_refreshed_at, + followers_url: inserted_community.followers_url.clone(), + inbox_url: inserted_community.inbox_url.clone(), + shared_inbox_url: inserted_community.shared_inbox_url.clone(), + moderators_url: inserted_community.moderators_url.clone(), + featured_url: inserted_community.featured_url.clone(), }, counts: PostAggregates { id: agg.id, diff --git a/crates/db_views/src/private_message_report_view.rs b/crates/db_views/src/private_message_report_view.rs index 647256ad..ace9a5da 100644 --- a/crates/db_views/src/private_message_report_view.rs +++ b/crates/db_views/src/private_message_report_view.rs @@ -5,11 +5,11 @@ use lemmy_db_schema::{ newtypes::PrivateMessageReportId, schema::{person, private_message, private_message_report}, source::{ - person::{Person, PersonSafe}, + person::Person, private_message::PrivateMessage, private_message_report::PrivateMessageReport, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; use typed_builder::TypedBuilder; @@ -17,9 +17,9 @@ use typed_builder::TypedBuilder; type PrivateMessageReportViewTuple = ( PrivateMessageReport, PrivateMessage, - PersonSafe, - PersonSafe, - Option, + Person, + Person, + Option, ); impl PrivateMessageReportView { @@ -47,11 +47,9 @@ impl PrivateMessageReportView { .select(( private_message_report::all_columns, private_message::all_columns, - Person::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), - person_alias_2 - .fields(Person::safe_columns_tuple()) - .nullable(), + person::all_columns, + person_alias_1.fields(person::all_columns), + person_alias_2.fields(person::all_columns).nullable(), )) .first::(conn) .await?; @@ -108,11 +106,9 @@ impl<'a> PrivateMessageReportQuery<'a> { .select(( private_message_report::all_columns, private_message::all_columns, - Person::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), - person_alias_2 - .fields(Person::safe_columns_tuple()) - .nullable(), + person::all_columns, + person_alias_1.fields(person::all_columns), + person_alias_2.fields(person::all_columns).nullable(), )) .into_boxed(); @@ -129,23 +125,25 @@ impl<'a> PrivateMessageReportQuery<'a> { let res = query.load::(conn).await?; - Ok(PrivateMessageReportView::from_tuple_to_vec(res)) + Ok( + res + .into_iter() + .map(PrivateMessageReportView::from_tuple) + .collect(), + ) } } -impl ViewToVec for PrivateMessageReportView { - type DbTuple = PrivateMessageReportViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - private_message_report: a.0, - private_message: a.1, - private_message_creator: a.2, - creator: a.3, - resolver: a.4, - }) - .collect::>() +impl JoinView for PrivateMessageReportView { + type JoinTuple = PrivateMessageReportViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + private_message_report: a.0, + private_message: a.1, + private_message_creator: a.2, + creator: a.3, + resolver: a.4, + } } } diff --git a/crates/db_views/src/private_message_view.rs b/crates/db_views/src/private_message_view.rs index 516fa51b..76b817a9 100644 --- a/crates/db_views/src/private_message_view.rs +++ b/crates/db_views/src/private_message_view.rs @@ -12,17 +12,14 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::{PersonId, PrivateMessageId}, schema::{person, private_message}, - source::{ - person::{Person, PersonSafe}, - private_message::PrivateMessage, - }, - traits::{ToSafe, ViewToVec}, + source::{person::Person, private_message::PrivateMessage}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; use tracing::debug; use typed_builder::TypedBuilder; -type PrivateMessageViewTuple = (PrivateMessage, PersonSafe, PersonSafe); +type PrivateMessageViewTuple = (PrivateMessage, Person, Person); impl PrivateMessageView { pub async fn read(pool: &DbPool, private_message_id: PrivateMessageId) -> Result { @@ -38,8 +35,8 @@ impl PrivateMessageView { .order_by(private_message::published.desc()) .select(( private_message::all_columns, - Person::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + person::all_columns, + person_alias_1.fields(person::all_columns), )) .first::(conn) .await?; @@ -89,8 +86,8 @@ impl<'a> PrivateMessageQuery<'a> { ) .select(( private_message::all_columns, - Person::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + person::all_columns, + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -124,20 +121,22 @@ impl<'a> PrivateMessageQuery<'a> { let res = query.load::(conn).await?; - Ok(PrivateMessageView::from_tuple_to_vec(res)) + Ok( + res + .into_iter() + .map(PrivateMessageView::from_tuple) + .collect(), + ) } } -impl ViewToVec for PrivateMessageView { - type DbTuple = PrivateMessageViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - private_message: a.0, - creator: a.1, - recipient: a.2, - }) - .collect::>() +impl JoinView for PrivateMessageView { + type JoinTuple = PrivateMessageViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + private_message: a.0, + creator: a.1, + recipient: a.2, + } } } diff --git a/crates/db_views/src/registration_application_view.rs b/crates/db_views/src/registration_application_view.rs index 0b5fa05e..a8e2e659 100644 --- a/crates/db_views/src/registration_application_view.rs +++ b/crates/db_views/src/registration_application_view.rs @@ -11,21 +11,17 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ schema::{local_user, person, registration_application}, source::{ - local_user::{LocalUser, LocalUserSettings}, - person::{Person, PersonSafe}, + local_user::LocalUser, + person::Person, registration_application::RegistrationApplication, }, - traits::{ToSafe, ToSafeSettings, ViewToVec}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; use typed_builder::TypedBuilder; -type RegistrationApplicationViewTuple = ( - RegistrationApplication, - LocalUserSettings, - PersonSafe, - Option, -); +type RegistrationApplicationViewTuple = + (RegistrationApplication, LocalUser, Person, Option); impl RegistrationApplicationView { pub async fn read(pool: &DbPool, registration_application_id: i32) -> Result { @@ -46,11 +42,9 @@ impl RegistrationApplicationView { .order_by(registration_application::published.desc()) .select(( registration_application::all_columns, - LocalUser::safe_settings_columns_tuple(), - Person::safe_columns_tuple(), - person_alias_1 - .fields(Person::safe_columns_tuple()) - .nullable(), + local_user::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns).nullable(), )) .first::(conn) .await?; @@ -115,11 +109,9 @@ impl<'a> RegistrationApplicationQuery<'a> { .order_by(registration_application::published.desc()) .select(( registration_application::all_columns, - LocalUser::safe_settings_columns_tuple(), - Person::safe_columns_tuple(), - person_alias_1 - .fields(Person::safe_columns_tuple()) - .nullable(), + local_user::all_columns, + person::all_columns, + person_alias_1.fields(person::all_columns).nullable(), )) .into_boxed(); @@ -140,22 +132,24 @@ impl<'a> RegistrationApplicationQuery<'a> { let res = query.load::(conn).await?; - Ok(RegistrationApplicationView::from_tuple_to_vec(res)) + Ok( + res + .into_iter() + .map(RegistrationApplicationView::from_tuple) + .collect(), + ) } } -impl ViewToVec for RegistrationApplicationView { - type DbTuple = RegistrationApplicationViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - registration_application: a.0, - creator_local_user: a.1, - creator: a.2, - admin: a.3, - }) - .collect::>() +impl JoinView for RegistrationApplicationView { + type JoinTuple = RegistrationApplicationViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + registration_application: a.0, + creator_local_user: a.1, + creator: a.2, + admin: a.3, + } } } @@ -168,8 +162,8 @@ mod tests { use lemmy_db_schema::{ source::{ instance::Instance, - local_user::{LocalUser, LocalUserInsertForm, LocalUserSettings, LocalUserUpdateForm}, - person::{Person, PersonInsertForm, PersonSafe}, + local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm}, + person::{Person, PersonInsertForm}, registration_application::{ RegistrationApplication, RegistrationApplicationInsertForm, @@ -272,7 +266,7 @@ mod tests { let mut expected_sara_app_view = RegistrationApplicationView { registration_application: sara_app.clone(), - creator_local_user: LocalUserSettings { + creator_local_user: LocalUser { id: inserted_sara_local_user.id, person_id: inserted_sara_local_user.person_id, email: inserted_sara_local_user.email, @@ -290,8 +284,9 @@ mod tests { show_new_post_notifs: inserted_sara_local_user.show_new_post_notifs, email_verified: inserted_sara_local_user.email_verified, accepted_application: inserted_sara_local_user.accepted_application, + password_encrypted: inserted_sara_local_user.password_encrypted, }, - creator: PersonSafe { + creator: Person { id: inserted_sara_person.id, name: inserted_sara_person.name.clone(), display_name: None, @@ -311,6 +306,9 @@ mod tests { shared_inbox_url: None, matrix_user_id: None, instance_id: inserted_instance.id, + private_key: inserted_sara_person.private_key, + public_key: inserted_sara_person.public_key, + last_refreshed_at: inserted_sara_person.last_refreshed_at, }, admin: None, }; @@ -366,7 +364,7 @@ mod tests { .accepted_application = true; expected_sara_app_view.registration_application.admin_id = Some(inserted_timmy_person.id); - expected_sara_app_view.admin = Some(PersonSafe { + expected_sara_app_view.admin = Some(Person { id: inserted_timmy_person.id, name: inserted_timmy_person.name.clone(), display_name: None, @@ -386,6 +384,9 @@ mod tests { shared_inbox_url: None, matrix_user_id: None, instance_id: inserted_instance.id, + private_key: inserted_timmy_person.private_key, + public_key: inserted_timmy_person.public_key, + last_refreshed_at: inserted_timmy_person.last_refreshed_at, }); assert_eq!(read_sara_app_view_after_approve, expected_sara_app_view); diff --git a/crates/db_views/src/structs.rs b/crates/db_views/src/structs.rs index 9a5aa05b..4521221f 100644 --- a/crates/db_views/src/structs.rs +++ b/crates/db_views/src/structs.rs @@ -3,11 +3,11 @@ use lemmy_db_schema::{ source::{ comment::Comment, comment_report::CommentReport, - community::CommunitySafe, + community::Community, local_site::LocalSite, local_site_rate_limit::LocalSiteRateLimit, - local_user::{LocalUser, LocalUserSettings}, - person::{Person, PersonSafe}, + local_user::LocalUser, + person::Person, post::Post, post_report::PostReport, private_message::PrivateMessage, @@ -24,21 +24,21 @@ pub struct CommentReportView { pub comment_report: CommentReport, pub comment: Comment, pub post: Post, - pub community: CommunitySafe, - pub creator: PersonSafe, - pub comment_creator: PersonSafe, + pub community: Community, + pub creator: Person, + pub comment_creator: Person, pub counts: CommentAggregates, pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub my_vote: Option, // Left join to CommentLike - pub resolver: Option, + pub resolver: Option, } #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] pub struct CommentView { pub comment: Comment, - pub creator: PersonSafe, + pub creator: Person, pub post: Post, - pub community: CommunitySafe, + pub community: Community, pub counts: CommentAggregates, pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub subscribed: SubscribedType, // Left join to CommunityFollower @@ -54,31 +54,24 @@ pub struct LocalUserView { pub counts: PersonAggregates, } -#[derive(Debug, Serialize, Deserialize, Clone)] -pub struct LocalUserSettingsView { - pub local_user: LocalUserSettings, - pub person: PersonSafe, - pub counts: PersonAggregates, -} - #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] pub struct PostReportView { pub post_report: PostReport, pub post: Post, - pub community: CommunitySafe, - pub creator: PersonSafe, - pub post_creator: PersonSafe, + pub community: Community, + pub creator: Person, + pub post_creator: Person, pub creator_banned_from_community: bool, pub my_vote: Option, pub counts: PostAggregates, - pub resolver: Option, + pub resolver: Option, } #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] pub struct PostView { pub post: Post, - pub creator: PersonSafe, - pub community: CommunitySafe, + pub creator: Person, + pub community: Community, pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub counts: PostAggregates, pub subscribed: SubscribedType, // Left join to CommunityFollower @@ -92,25 +85,25 @@ pub struct PostView { #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] pub struct PrivateMessageView { pub private_message: PrivateMessage, - pub creator: PersonSafe, - pub recipient: PersonSafe, + pub creator: Person, + pub recipient: Person, } #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] pub struct PrivateMessageReportView { pub private_message_report: PrivateMessageReport, pub private_message: PrivateMessage, - pub private_message_creator: PersonSafe, - pub creator: PersonSafe, - pub resolver: Option, + pub private_message_creator: Person, + pub creator: Person, + pub resolver: Option, } #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] pub struct RegistrationApplicationView { pub registration_application: RegistrationApplication, - pub creator_local_user: LocalUserSettings, - pub creator: PersonSafe, - pub admin: Option, + pub creator_local_user: LocalUser, + pub creator: Person, + pub admin: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] diff --git a/crates/db_views_actor/src/comment_reply_view.rs b/crates/db_views_actor/src/comment_reply_view.rs index 90bcdbc9..c9a3d971 100644 --- a/crates/db_views_actor/src/comment_reply_view.rs +++ b/crates/db_views_actor/src/comment_reply_view.rs @@ -28,12 +28,12 @@ use lemmy_db_schema::{ source::{ comment::{Comment, CommentSaved}, comment_reply::CommentReply, - community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, - person::{Person, PersonSafe}, + community::{Community, CommunityFollower, CommunityPersonBan}, + person::Person, person_block::PersonBlock, post::Post, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool}, CommentSortType, }; @@ -42,10 +42,10 @@ use typed_builder::TypedBuilder; type CommentReplyViewTuple = ( CommentReply, Comment, - PersonSafe, + Person, Post, - CommunitySafe, - PersonSafe, + Community, + Person, CommentAggregates, Option, Option, @@ -130,10 +130,10 @@ impl CommentReplyView { .select(( comment_reply::all_columns, comment::all_columns, - Person::safe_columns_tuple(), + person::all_columns, post::all_columns, - Community::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + community::all_columns, + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -251,10 +251,10 @@ impl<'a> CommentReplyQuery<'a> { .select(( comment_reply::all_columns, comment::all_columns, - Person::safe_columns_tuple(), + person::all_columns, post::all_columns, - Community::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + community::all_columns, + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -293,29 +293,26 @@ impl<'a> CommentReplyQuery<'a> { .load::(conn) .await?; - Ok(CommentReplyView::from_tuple_to_vec(res)) + Ok(res.into_iter().map(CommentReplyView::from_tuple).collect()) } } -impl ViewToVec for CommentReplyView { - type DbTuple = CommentReplyViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - comment_reply: 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(), - creator_blocked: a.10.is_some(), - my_vote: a.11, - }) - .collect::>() +impl JoinView for CommentReplyView { + type JoinTuple = CommentReplyViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + comment_reply: 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(), + creator_blocked: a.10.is_some(), + my_vote: a.11, + } } } diff --git a/crates/db_views_actor/src/community_block_view.rs b/crates/db_views_actor/src/community_block_view.rs index 2a774b03..d87bea37 100644 --- a/crates/db_views_actor/src/community_block_view.rs +++ b/crates/db_views_actor/src/community_block_view.rs @@ -4,15 +4,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, community_block, person}, - source::{ - community::{Community, CommunitySafe}, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, person::Person}, + traits::JoinView, utils::{get_conn, DbPool}, }; -type CommunityBlockViewTuple = (PersonSafe, CommunitySafe); +type CommunityBlockViewTuple = (Person, Community); impl CommunityBlockView { pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result, Error> { @@ -20,10 +17,7 @@ impl CommunityBlockView { let res = community_block::table .inner_join(person::table) .inner_join(community::table) - .select(( - Person::safe_columns_tuple(), - Community::safe_columns_tuple(), - )) + .select((person::all_columns, community::all_columns)) .filter(community_block::person_id.eq(person_id)) .filter(community::deleted.eq(false)) .filter(community::removed.eq(false)) @@ -31,19 +25,16 @@ impl CommunityBlockView { .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(res)) + Ok(res.into_iter().map(Self::from_tuple).collect()) } } -impl ViewToVec for CommunityBlockView { - type DbTuple = CommunityBlockViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - person: a.0, - community: a.1, - }) - .collect::>() +impl JoinView for CommunityBlockView { + type JoinTuple = CommunityBlockViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + person: a.0, + community: a.1, + } } } diff --git a/crates/db_views_actor/src/community_follower_view.rs b/crates/db_views_actor/src/community_follower_view.rs index 3b03b027..ccc7ae70 100644 --- a/crates/db_views_actor/src/community_follower_view.rs +++ b/crates/db_views_actor/src/community_follower_view.rs @@ -4,15 +4,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::{CommunityId, PersonId}, schema::{community, community_follower, person}, - source::{ - community::{Community, CommunitySafe}, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, person::Person}, + traits::JoinView, utils::{get_conn, DbPool}, }; -type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe); +type CommunityFollowerViewTuple = (Community, Person); impl CommunityFollowerView { pub async fn for_community(pool: &DbPool, community_id: CommunityId) -> Result, Error> { @@ -20,16 +17,13 @@ impl CommunityFollowerView { let res = community_follower::table .inner_join(community::table) .inner_join(person::table) - .select(( - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - )) + .select((community::all_columns, person::all_columns)) .filter(community_follower::community_id.eq(community_id)) .order_by(community::title) .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(res)) + Ok(res.into_iter().map(Self::from_tuple).collect()) } pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result, Error> { @@ -37,10 +31,7 @@ impl CommunityFollowerView { let res = community_follower::table .inner_join(community::table) .inner_join(person::table) - .select(( - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - )) + .select((community::all_columns, person::all_columns)) .filter(community_follower::person_id.eq(person_id)) .filter(community::deleted.eq(false)) .filter(community::removed.eq(false)) @@ -48,19 +39,16 @@ impl CommunityFollowerView { .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(res)) + Ok(res.into_iter().map(Self::from_tuple).collect()) } } -impl ViewToVec for CommunityFollowerView { - type DbTuple = CommunityFollowerViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - community: a.0, - follower: a.1, - }) - .collect::>() +impl JoinView for CommunityFollowerView { + type JoinTuple = CommunityFollowerViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + community: a.0, + follower: a.1, + } } } diff --git a/crates/db_views_actor/src/community_moderator_view.rs b/crates/db_views_actor/src/community_moderator_view.rs index e9e6625c..70e799b8 100644 --- a/crates/db_views_actor/src/community_moderator_view.rs +++ b/crates/db_views_actor/src/community_moderator_view.rs @@ -4,15 +4,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::{CommunityId, PersonId}, schema::{community, community_moderator, person}, - source::{ - community::{Community, CommunitySafe}, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, person::Person}, + traits::JoinView, utils::{get_conn, DbPool}, }; -type CommunityModeratorViewTuple = (CommunitySafe, PersonSafe); +type CommunityModeratorViewTuple = (Community, Person); impl CommunityModeratorView { pub async fn for_community(pool: &DbPool, community_id: CommunityId) -> Result, Error> { @@ -20,16 +17,13 @@ impl CommunityModeratorView { let res = community_moderator::table .inner_join(community::table) .inner_join(person::table) - .select(( - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - )) + .select((community::all_columns, person::all_columns)) .filter(community_moderator::community_id.eq(community_id)) .order_by(community_moderator::published) .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(res)) + Ok(res.into_iter().map(Self::from_tuple).collect()) } pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result, Error> { @@ -37,10 +31,7 @@ impl CommunityModeratorView { let res = community_moderator::table .inner_join(community::table) .inner_join(person::table) - .select(( - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - )) + .select((community::all_columns, person::all_columns)) .filter(community_moderator::person_id.eq(person_id)) .filter(community::deleted.eq(false)) .filter(community::removed.eq(false)) @@ -48,7 +39,7 @@ impl CommunityModeratorView { .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(res)) + Ok(res.into_iter().map(Self::from_tuple).collect()) } /// Finds all communities first mods / creators @@ -58,10 +49,7 @@ impl CommunityModeratorView { let res = community_moderator::table .inner_join(community::table) .inner_join(person::table) - .select(( - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - )) + .select((community::all_columns, person::all_columns)) // A hacky workaround instead of group_bys // https://stackoverflow.com/questions/24042359/how-to-join-only-one-row-in-joined-table-with-postgres .distinct_on(community_moderator::community_id) @@ -72,19 +60,16 @@ impl CommunityModeratorView { .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(res)) + Ok(res.into_iter().map(Self::from_tuple).collect()) } } -impl ViewToVec for CommunityModeratorView { - type DbTuple = CommunityModeratorViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - community: a.0, - moderator: a.1, - }) - .collect::>() +impl JoinView for CommunityModeratorView { + type JoinTuple = CommunityModeratorViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + community: a.0, + moderator: a.1, + } } } diff --git a/crates/db_views_actor/src/community_person_ban_view.rs b/crates/db_views_actor/src/community_person_ban_view.rs index 0d7df921..ebfce9ba 100644 --- a/crates/db_views_actor/src/community_person_ban_view.rs +++ b/crates/db_views_actor/src/community_person_ban_view.rs @@ -4,11 +4,7 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::{CommunityId, PersonId}, schema::{community, community_person_ban, person}, - source::{ - community::{Community, CommunitySafe}, - person::{Person, PersonSafe}, - }, - traits::ToSafe, + source::{community::Community, person::Person}, utils::{get_conn, DbPool}, }; @@ -22,10 +18,7 @@ impl CommunityPersonBanView { let (community, person) = community_person_ban::table .inner_join(community::table) .inner_join(person::table) - .select(( - Community::safe_columns_tuple(), - Person::safe_columns_tuple(), - )) + .select((community::all_columns, person::all_columns)) .filter(community_person_ban::community_id.eq(from_community_id)) .filter(community_person_ban::person_id.eq(from_person_id)) .filter( @@ -34,7 +27,7 @@ impl CommunityPersonBanView { .or(community_person_ban::expires.gt(now)), ) .order_by(community_person_ban::published) - .first::<(CommunitySafe, PersonSafe)>(conn) + .first::<(Community, Person)>(conn) .await?; Ok(CommunityPersonBanView { community, person }) diff --git a/crates/db_views_actor/src/community_view.rs b/crates/db_views_actor/src/community_view.rs index ff30d5dc..9d48f17d 100644 --- a/crates/db_views_actor/src/community_view.rs +++ b/crates/db_views_actor/src/community_view.rs @@ -1,4 +1,4 @@ -use crate::structs::{CommunityModeratorView, CommunityView, PersonViewSafe}; +use crate::structs::{CommunityModeratorView, CommunityView, PersonView}; use diesel::{ result::Error, BoolExpressionMethods, @@ -14,11 +14,11 @@ use lemmy_db_schema::{ newtypes::{CommunityId, PersonId}, schema::{community, community_aggregates, community_block, community_follower, local_user}, source::{ - community::{Community, CommunityFollower, CommunitySafe}, + community::{Community, CommunityFollower}, community_block::CommunityBlock, local_user::LocalUser, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{fuzzy_search, get_conn, limit_and_offset, DbPool}, ListingType, SortType, @@ -26,7 +26,7 @@ use lemmy_db_schema::{ use typed_builder::TypedBuilder; type CommunityViewTuple = ( - CommunitySafe, + Community, CommunityAggregates, Option, Option, @@ -61,7 +61,7 @@ impl CommunityView { ), ) .select(( - Community::safe_columns_tuple(), + community::all_columns, community_aggregates::all_columns, community_follower::all_columns.nullable(), community_block::all_columns.nullable(), @@ -103,7 +103,7 @@ impl CommunityView { return Ok(true); } - let is_admin = PersonViewSafe::admins(pool) + let is_admin = PersonView::admins(pool) .await .map(|v| { v.into_iter() @@ -155,7 +155,7 @@ impl<'a> CommunityQuery<'a> { ), ) .select(( - Community::safe_columns_tuple(), + community::all_columns, community_aggregates::all_columns, community_follower::all_columns.nullable(), community_block::all_columns.nullable(), @@ -221,21 +221,18 @@ impl<'a> CommunityQuery<'a> { .load::(conn) .await?; - Ok(CommunityView::from_tuple_to_vec(res)) + Ok(res.into_iter().map(CommunityView::from_tuple).collect()) } } -impl ViewToVec for CommunityView { - type DbTuple = CommunityViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - community: a.0, - counts: a.1, - subscribed: CommunityFollower::to_subscribed_type(&a.2), - blocked: a.3.is_some(), - }) - .collect::>() +impl JoinView for CommunityView { + type JoinTuple = CommunityViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + 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/person_block_view.rs b/crates/db_views_actor/src/person_block_view.rs index 7e34a2df..88c27702 100644 --- a/crates/db_views_actor/src/person_block_view.rs +++ b/crates/db_views_actor/src/person_block_view.rs @@ -4,12 +4,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{person, person_block}, - source::person::{Person, PersonSafe}, - traits::{ToSafe, ViewToVec}, + source::person::Person, + traits::JoinView, utils::{get_conn, DbPool}, }; -type PersonBlockViewTuple = (PersonSafe, PersonSafe); +type PersonBlockViewTuple = (Person, Person); impl PersonBlockView { pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result, Error> { @@ -22,8 +22,8 @@ impl PersonBlockView { target_person_alias.on(person_block::target_id.eq(target_person_alias.field(person::id))), ) .select(( - Person::safe_columns_tuple(), - target_person_alias.fields(Person::safe_columns_tuple()), + person::all_columns, + target_person_alias.fields(person::all_columns), )) .filter(person_block::person_id.eq(person_id)) .filter(target_person_alias.field(person::deleted).eq(false)) @@ -31,19 +31,16 @@ impl PersonBlockView { .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(res)) + Ok(res.into_iter().map(Self::from_tuple).collect()) } } -impl ViewToVec for PersonBlockView { - type DbTuple = PersonBlockViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - person: a.0, - target: a.1, - }) - .collect::>() +impl JoinView for PersonBlockView { + type JoinTuple = PersonBlockViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + person: a.0, + target: a.1, + } } } diff --git a/crates/db_views_actor/src/person_mention_view.rs b/crates/db_views_actor/src/person_mention_view.rs index 16c53e6e..c0fefbe5 100644 --- a/crates/db_views_actor/src/person_mention_view.rs +++ b/crates/db_views_actor/src/person_mention_view.rs @@ -27,13 +27,13 @@ use lemmy_db_schema::{ }, source::{ comment::{Comment, CommentSaved}, - community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, - person::{Person, PersonSafe}, + community::{Community, CommunityFollower, CommunityPersonBan}, + person::Person, person_block::PersonBlock, person_mention::PersonMention, post::Post, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool}, CommentSortType, }; @@ -42,10 +42,10 @@ use typed_builder::TypedBuilder; type PersonMentionViewTuple = ( PersonMention, Comment, - PersonSafe, + Person, Post, - CommunitySafe, - PersonSafe, + Community, + Person, CommentAggregates, Option, Option, @@ -130,10 +130,10 @@ impl PersonMentionView { .select(( person_mention::all_columns, comment::all_columns, - Person::safe_columns_tuple(), + person::all_columns, post::all_columns, - Community::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + community::all_columns, + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -250,10 +250,10 @@ impl<'a> PersonMentionQuery<'a> { .select(( person_mention::all_columns, comment::all_columns, - Person::safe_columns_tuple(), + person::all_columns, post::all_columns, - Community::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + community::all_columns, + person_alias_1.fields(person::all_columns), comment_aggregates::all_columns, community_person_ban::all_columns.nullable(), community_follower::all_columns.nullable(), @@ -292,29 +292,26 @@ impl<'a> PersonMentionQuery<'a> { .load::(conn) .await?; - Ok(PersonMentionView::from_tuple_to_vec(res)) + Ok(res.into_iter().map(PersonMentionView::from_tuple).collect()) } } -impl ViewToVec for PersonMentionView { - type DbTuple = PersonMentionViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - 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(), - creator_blocked: a.10.is_some(), - my_vote: a.11, - }) - .collect::>() +impl JoinView for PersonMentionView { + type JoinTuple = PersonMentionViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + 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(), + creator_blocked: a.10.is_some(), + my_vote: a.11, + } } } diff --git a/crates/db_views_actor/src/person_view.rs b/crates/db_views_actor/src/person_view.rs index 14c413c8..43f99dfd 100644 --- a/crates/db_views_actor/src/person_view.rs +++ b/crates/db_views_actor/src/person_view.rs @@ -1,4 +1,4 @@ -use crate::structs::PersonViewSafe; +use crate::structs::PersonView; use diesel::{ dsl::{now, IntervalDsl}, result::Error, @@ -12,47 +12,47 @@ use lemmy_db_schema::{ aggregates::structs::PersonAggregates, newtypes::PersonId, schema::{person, person_aggregates}, - source::person::{Person, PersonSafe}, - traits::{ToSafe, ViewToVec}, + source::person::Person, + traits::JoinView, utils::{fuzzy_search, get_conn, limit_and_offset, DbPool}, SortType, }; use std::iter::Iterator; use typed_builder::TypedBuilder; -type PersonViewSafeTuple = (PersonSafe, PersonAggregates); +type PersonViewTuple = (Person, PersonAggregates); -impl PersonViewSafe { +impl PersonView { pub async fn read(pool: &DbPool, person_id: PersonId) -> Result { let conn = &mut get_conn(pool).await?; - let (person, counts) = person::table + let res = person::table .find(person_id) .inner_join(person_aggregates::table) - .select((Person::safe_columns_tuple(), person_aggregates::all_columns)) - .first::(conn) + .select((person::all_columns, person_aggregates::all_columns)) + .first::(conn) .await?; - Ok(Self { person, counts }) + Ok(Self::from_tuple(res)) } pub async fn admins(pool: &DbPool) -> Result, Error> { let conn = &mut get_conn(pool).await?; let admins = person::table .inner_join(person_aggregates::table) - .select((Person::safe_columns_tuple(), person_aggregates::all_columns)) + .select((person::all_columns, person_aggregates::all_columns)) .filter(person::admin.eq(true)) .filter(person::deleted.eq(false)) .order_by(person::published) - .load::(conn) + .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(admins)) + Ok(admins.into_iter().map(Self::from_tuple).collect()) } pub async fn banned(pool: &DbPool) -> Result, Error> { let conn = &mut get_conn(pool).await?; let banned = person::table .inner_join(person_aggregates::table) - .select((Person::safe_columns_tuple(), person_aggregates::all_columns)) + .select((person::all_columns, person_aggregates::all_columns)) .filter( person::banned.eq(true).and( person::ban_expires @@ -61,10 +61,10 @@ impl PersonViewSafe { ), ) .filter(person::deleted.eq(false)) - .load::(conn) + .load::(conn) .await?; - Ok(Self::from_tuple_to_vec(banned)) + Ok(banned.into_iter().map(Self::from_tuple).collect()) } } @@ -80,11 +80,11 @@ pub struct PersonQuery<'a> { } impl<'a> PersonQuery<'a> { - pub async fn list(self) -> Result, Error> { + pub async fn list(self) -> Result, Error> { let conn = &mut get_conn(self.pool).await?; let mut query = person::table .inner_join(person_aggregates::table) - .select((Person::safe_columns_tuple(), person_aggregates::all_columns)) + .select((person::all_columns, person_aggregates::all_columns)) .into_boxed(); if let Some(search_term) = self.search_term { @@ -118,21 +118,18 @@ impl<'a> PersonQuery<'a> { let (limit, offset) = limit_and_offset(self.page, self.limit)?; query = query.limit(limit).offset(offset); - let res = query.load::(conn).await?; + let res = query.load::(conn).await?; - Ok(PersonViewSafe::from_tuple_to_vec(res)) + Ok(res.into_iter().map(PersonView::from_tuple).collect()) } } -impl ViewToVec for PersonViewSafe { - type DbTuple = PersonViewSafeTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - person: a.0, - counts: a.1, - }) - .collect::>() +impl JoinView for PersonView { + type JoinTuple = PersonViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + person: a.0, + counts: a.1, + } } } diff --git a/crates/db_views_actor/src/structs.rs b/crates/db_views_actor/src/structs.rs index 91d40e96..af328f1d 100644 --- a/crates/db_views_actor/src/structs.rs +++ b/crates/db_views_actor/src/structs.rs @@ -3,8 +3,8 @@ use lemmy_db_schema::{ source::{ comment::Comment, comment_reply::CommentReply, - community::CommunitySafe, - person::PersonSafe, + community::Community, + person::Person, person_mention::PersonMention, post::Post, }, @@ -14,31 +14,31 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct CommunityBlockView { - pub person: PersonSafe, - pub community: CommunitySafe, + pub person: Person, + pub community: Community, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct CommunityFollowerView { - pub community: CommunitySafe, - pub follower: PersonSafe, + pub community: Community, + pub follower: Person, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct CommunityModeratorView { - pub community: CommunitySafe, - pub moderator: PersonSafe, + pub community: Community, + pub moderator: Person, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct CommunityPersonBanView { - pub community: CommunitySafe, - pub person: PersonSafe, + pub community: Community, + pub person: Person, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct CommunityView { - pub community: CommunitySafe, + pub community: Community, pub subscribed: SubscribedType, pub blocked: bool, pub counts: CommunityAggregates, @@ -46,18 +46,18 @@ pub struct CommunityView { #[derive(Debug, Serialize, Deserialize, Clone)] pub struct PersonBlockView { - pub person: PersonSafe, - pub target: PersonSafe, + pub person: Person, + pub target: Person, } #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] pub struct PersonMentionView { pub person_mention: PersonMention, pub comment: Comment, - pub creator: PersonSafe, + pub creator: Person, pub post: Post, - pub community: CommunitySafe, - pub recipient: PersonSafe, + pub community: Community, + pub recipient: Person, pub counts: CommentAggregates, pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub subscribed: SubscribedType, // Left join to CommunityFollower @@ -70,10 +70,10 @@ pub struct PersonMentionView { pub struct CommentReplyView { pub comment_reply: CommentReply, pub comment: Comment, - pub creator: PersonSafe, + pub creator: Person, pub post: Post, - pub community: CommunitySafe, - pub recipient: PersonSafe, + pub community: Community, + pub recipient: Person, pub counts: CommentAggregates, pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub subscribed: SubscribedType, // Left join to CommunityFollower @@ -83,7 +83,7 @@ pub struct CommentReplyView { } #[derive(Debug, Serialize, Deserialize, Clone)] -pub struct PersonViewSafe { - pub person: PersonSafe, +pub struct PersonView { + pub person: Person, pub counts: PersonAggregates, } diff --git a/crates/db_views_moderator/src/admin_purge_comment_view.rs b/crates/db_views_moderator/src/admin_purge_comment_view.rs index d9edaa10..12e47fbb 100644 --- a/crates/db_views_moderator/src/admin_purge_comment_view.rs +++ b/crates/db_views_moderator/src/admin_purge_comment_view.rs @@ -12,16 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{admin_purge_comment, person, post}, - source::{ - moderator::AdminPurgeComment, - person::{Person, PersonSafe}, - post::Post, - }, - traits::{ToSafe, ViewToVec}, + source::{moderator::AdminPurgeComment, person::Person, post::Post}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type AdminPurgeCommentViewTuple = (AdminPurgeComment, Option, Post); +type AdminPurgeCommentViewTuple = (AdminPurgeComment, Option, Post); impl AdminPurgeCommentView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -39,7 +35,7 @@ impl AdminPurgeCommentView { .inner_join(post::table) .select(( admin_purge_comment::all_columns, - Person::safe_columns_tuple().nullable(), + person::all_columns.nullable(), post::all_columns, )) .into_boxed(); @@ -57,21 +53,18 @@ impl AdminPurgeCommentView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for AdminPurgeCommentView { - type DbTuple = AdminPurgeCommentViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - admin_purge_comment: a.0, - admin: a.1, - post: a.2, - }) - .collect::>() +impl JoinView for AdminPurgeCommentView { + type JoinTuple = AdminPurgeCommentViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + admin_purge_comment: a.0, + admin: a.1, + post: a.2, + } } } 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 501013d0..e3a7a789 100644 --- a/crates/db_views_moderator/src/admin_purge_community_view.rs +++ b/crates/db_views_moderator/src/admin_purge_community_view.rs @@ -12,15 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{admin_purge_community, person}, - source::{ - moderator::AdminPurgeCommunity, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{moderator::AdminPurgeCommunity, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, Option); +type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, Option); impl AdminPurgeCommunityView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -37,7 +34,7 @@ impl AdminPurgeCommunityView { .left_join(person::table.on(admin_names_join)) .select(( admin_purge_community::all_columns, - Person::safe_columns_tuple().nullable(), + person::all_columns.nullable(), )) .into_boxed(); @@ -54,20 +51,17 @@ impl AdminPurgeCommunityView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for AdminPurgeCommunityView { - type DbTuple = AdminPurgeCommunityViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - admin_purge_community: a.0, - admin: a.1, - }) - .collect::>() +impl JoinView for AdminPurgeCommunityView { + type JoinTuple = AdminPurgeCommunityViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + admin_purge_community: a.0, + admin: a.1, + } } } diff --git a/crates/db_views_moderator/src/admin_purge_person_view.rs b/crates/db_views_moderator/src/admin_purge_person_view.rs index a0abe0ca..fff5b2d5 100644 --- a/crates/db_views_moderator/src/admin_purge_person_view.rs +++ b/crates/db_views_moderator/src/admin_purge_person_view.rs @@ -12,15 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{admin_purge_person, person}, - source::{ - moderator::AdminPurgePerson, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{moderator::AdminPurgePerson, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type AdminPurgePersonViewTuple = (AdminPurgePerson, Option); +type AdminPurgePersonViewTuple = (AdminPurgePerson, Option); impl AdminPurgePersonView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -37,7 +34,7 @@ impl AdminPurgePersonView { .left_join(person::table.on(admin_names_join)) .select(( admin_purge_person::all_columns, - Person::safe_columns_tuple().nullable(), + person::all_columns.nullable(), )) .into_boxed(); @@ -54,20 +51,17 @@ impl AdminPurgePersonView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for AdminPurgePersonView { - type DbTuple = AdminPurgePersonViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - admin_purge_person: a.0, - admin: a.1, - }) - .collect::>() +impl JoinView for AdminPurgePersonView { + type JoinTuple = AdminPurgePersonViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + admin_purge_person: a.0, + admin: a.1, + } } } diff --git a/crates/db_views_moderator/src/admin_purge_post_view.rs b/crates/db_views_moderator/src/admin_purge_post_view.rs index 38cb6b5e..e37e66b1 100644 --- a/crates/db_views_moderator/src/admin_purge_post_view.rs +++ b/crates/db_views_moderator/src/admin_purge_post_view.rs @@ -12,16 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{admin_purge_post, community, person}, - source::{ - community::{Community, CommunitySafe}, - moderator::AdminPurgePost, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::AdminPurgePost, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type AdminPurgePostViewTuple = (AdminPurgePost, Option, CommunitySafe); +type AdminPurgePostViewTuple = (AdminPurgePost, Option, Community); impl AdminPurgePostView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -39,8 +35,8 @@ impl AdminPurgePostView { .inner_join(community::table) .select(( admin_purge_post::all_columns, - Person::safe_columns_tuple().nullable(), - Community::safe_columns_tuple(), + person::all_columns.nullable(), + community::all_columns, )) .into_boxed(); @@ -57,21 +53,18 @@ impl AdminPurgePostView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for AdminPurgePostView { - type DbTuple = AdminPurgePostViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - admin_purge_post: a.0, - admin: a.1, - community: a.2, - }) - .collect::>() +impl JoinView for AdminPurgePostView { + type JoinTuple = AdminPurgePostViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + admin_purge_post: a.0, + admin: a.1, + community: a.2, + } } } diff --git a/crates/db_views_moderator/src/mod_add_community_view.rs b/crates/db_views_moderator/src/mod_add_community_view.rs index bad3a1de..6df6376f 100644 --- a/crates/db_views_moderator/src/mod_add_community_view.rs +++ b/crates/db_views_moderator/src/mod_add_community_view.rs @@ -12,21 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, mod_add_community, person}, - source::{ - community::{Community, CommunitySafe}, - moderator::ModAddCommunity, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::ModAddCommunity, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModAddCommunityViewTuple = ( - ModAddCommunity, - Option, - CommunitySafe, - PersonSafe, -); +type ModAddCommunityViewTuple = (ModAddCommunity, Option, Community, Person); impl ModAddCommunityView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -47,9 +38,9 @@ impl ModAddCommunityView { ) .select(( mod_add_community::all_columns, - Person::safe_columns_tuple().nullable(), - Community::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + person::all_columns.nullable(), + community::all_columns, + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -74,22 +65,19 @@ impl ModAddCommunityView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModAddCommunityView { - type DbTuple = ModAddCommunityViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_add_community: a.0, - moderator: a.1, - community: a.2, - modded_person: a.3, - }) - .collect::>() +impl JoinView for ModAddCommunityView { + type JoinTuple = ModAddCommunityViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_add_community: a.0, + moderator: a.1, + community: a.2, + modded_person: a.3, + } } } diff --git a/crates/db_views_moderator/src/mod_add_view.rs b/crates/db_views_moderator/src/mod_add_view.rs index 3fc4b35b..ef408ce3 100644 --- a/crates/db_views_moderator/src/mod_add_view.rs +++ b/crates/db_views_moderator/src/mod_add_view.rs @@ -12,15 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{mod_add, person}, - source::{ - moderator::ModAdd, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{moderator::ModAdd, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModAddViewTuple = (ModAdd, Option, PersonSafe); +type ModAddViewTuple = (ModAdd, Option, Person); impl ModAddView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -38,8 +35,8 @@ impl ModAddView { .inner_join(person_alias_1.on(mod_add::other_person_id.eq(person_alias_1.field(person::id)))) .select(( mod_add::all_columns, - Person::safe_columns_tuple().nullable(), - person_alias_1.fields(Person::safe_columns_tuple()), + person::all_columns.nullable(), + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -60,21 +57,18 @@ impl ModAddView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModAddView { - type DbTuple = ModAddViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_add: a.0, - moderator: a.1, - modded_person: a.2, - }) - .collect::>() +impl JoinView for ModAddView { + type JoinTuple = ModAddViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_add: a.0, + moderator: a.1, + modded_person: a.2, + } } } diff --git a/crates/db_views_moderator/src/mod_ban_from_community_view.rs b/crates/db_views_moderator/src/mod_ban_from_community_view.rs index cf10c53a..c7fb7395 100644 --- a/crates/db_views_moderator/src/mod_ban_from_community_view.rs +++ b/crates/db_views_moderator/src/mod_ban_from_community_view.rs @@ -12,21 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, mod_ban_from_community, person}, - source::{ - community::{Community, CommunitySafe}, - moderator::ModBanFromCommunity, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::ModBanFromCommunity, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModBanFromCommunityViewTuple = ( - ModBanFromCommunity, - Option, - CommunitySafe, - PersonSafe, -); +type ModBanFromCommunityViewTuple = (ModBanFromCommunity, Option, Community, Person); impl ModBanFromCommunityView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -49,9 +40,9 @@ impl ModBanFromCommunityView { ) .select(( mod_ban_from_community::all_columns, - Person::safe_columns_tuple().nullable(), - Community::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + person::all_columns.nullable(), + community::all_columns, + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -76,22 +67,19 @@ impl ModBanFromCommunityView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModBanFromCommunityView { - type DbTuple = ModBanFromCommunityViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_ban_from_community: a.0, - moderator: a.1, - community: a.2, - banned_person: a.3, - }) - .collect::>() +impl JoinView for ModBanFromCommunityView { + type JoinTuple = ModBanFromCommunityViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_ban_from_community: a.0, + moderator: a.1, + community: a.2, + banned_person: a.3, + } } } diff --git a/crates/db_views_moderator/src/mod_ban_view.rs b/crates/db_views_moderator/src/mod_ban_view.rs index adb0623c..7bdf0dcf 100644 --- a/crates/db_views_moderator/src/mod_ban_view.rs +++ b/crates/db_views_moderator/src/mod_ban_view.rs @@ -12,15 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{mod_ban, person}, - source::{ - moderator::ModBan, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{moderator::ModBan, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModBanViewTuple = (ModBan, Option, PersonSafe); +type ModBanViewTuple = (ModBan, Option, Person); impl ModBanView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -38,8 +35,8 @@ impl ModBanView { .inner_join(person_alias_1.on(mod_ban::other_person_id.eq(person_alias_1.field(person::id)))) .select(( mod_ban::all_columns, - Person::safe_columns_tuple().nullable(), - person_alias_1.fields(Person::safe_columns_tuple()), + person::all_columns.nullable(), + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -60,21 +57,18 @@ impl ModBanView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModBanView { - type DbTuple = ModBanViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_ban: a.0, - moderator: a.1, - banned_person: a.2, - }) - .collect::>() +impl JoinView for ModBanView { + type JoinTuple = ModBanViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_ban: a.0, + moderator: a.1, + banned_person: a.2, + } } } diff --git a/crates/db_views_moderator/src/mod_feature_post_view.rs b/crates/db_views_moderator/src/mod_feature_post_view.rs index 852746de..66bd4264 100644 --- a/crates/db_views_moderator/src/mod_feature_post_view.rs +++ b/crates/db_views_moderator/src/mod_feature_post_view.rs @@ -12,17 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, mod_feature_post, person, post}, - source::{ - community::{Community, CommunitySafe}, - moderator::ModFeaturePost, - person::{Person, PersonSafe}, - post::Post, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::ModFeaturePost, person::Person, post::Post}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModFeaturePostViewTuple = (ModFeaturePost, Option, Post, CommunitySafe); +type ModFeaturePostViewTuple = (ModFeaturePost, Option, Post, Community); impl ModFeaturePostView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -42,9 +37,9 @@ impl ModFeaturePostView { .inner_join(community::table.on(post::community_id.eq(community::id))) .select(( mod_feature_post::all_columns, - Person::safe_columns_tuple().nullable(), + person::all_columns.nullable(), post::all_columns, - Community::safe_columns_tuple(), + community::all_columns, )) .into_boxed(); @@ -69,22 +64,19 @@ impl ModFeaturePostView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModFeaturePostView { - type DbTuple = ModFeaturePostViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_feature_post: a.0, - moderator: a.1, - post: a.2, - community: a.3, - }) - .collect::>() +impl JoinView for ModFeaturePostView { + type JoinTuple = ModFeaturePostViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_feature_post: a.0, + moderator: a.1, + post: a.2, + community: a.3, + } } } diff --git a/crates/db_views_moderator/src/mod_hide_community_view.rs b/crates/db_views_moderator/src/mod_hide_community_view.rs index 4b382d51..c2c6294b 100644 --- a/crates/db_views_moderator/src/mod_hide_community_view.rs +++ b/crates/db_views_moderator/src/mod_hide_community_view.rs @@ -12,16 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, mod_hide_community, person}, - source::{ - community::{Community, CommunitySafe}, - moderator::ModHideCommunity, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::ModHideCommunity, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModHideCommunityViewTuple = (ModHideCommunity, Option, CommunitySafe); +type ModHideCommunityViewTuple = (ModHideCommunity, Option, Community); impl ModHideCommunityView { // Pass in mod_id as admin_id because only admins can do this action @@ -40,8 +36,8 @@ impl ModHideCommunityView { .inner_join(community::table.on(mod_hide_community::community_id.eq(community::id))) .select(( mod_hide_community::all_columns, - Person::safe_columns_tuple().nullable(), - Community::safe_columns_tuple(), + person::all_columns.nullable(), + community::all_columns, )) .into_boxed(); @@ -62,21 +58,18 @@ impl ModHideCommunityView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModHideCommunityView { - type DbTuple = ModHideCommunityViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_hide_community: a.0, - admin: a.1, - community: a.2, - }) - .collect::>() +impl JoinView for ModHideCommunityView { + type JoinTuple = ModHideCommunityViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_hide_community: a.0, + admin: a.1, + community: a.2, + } } } diff --git a/crates/db_views_moderator/src/mod_lock_post_view.rs b/crates/db_views_moderator/src/mod_lock_post_view.rs index 27758446..15ff7855 100644 --- a/crates/db_views_moderator/src/mod_lock_post_view.rs +++ b/crates/db_views_moderator/src/mod_lock_post_view.rs @@ -12,17 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, mod_lock_post, person, post}, - source::{ - community::{Community, CommunitySafe}, - moderator::ModLockPost, - person::{Person, PersonSafe}, - post::Post, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::ModLockPost, person::Person, post::Post}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModLockPostViewTuple = (ModLockPost, Option, Post, CommunitySafe); +type ModLockPostViewTuple = (ModLockPost, Option, Post, Community); impl ModLockPostView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -43,9 +38,9 @@ impl ModLockPostView { .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .select(( mod_lock_post::all_columns, - Person::safe_columns_tuple().nullable(), + person::all_columns.nullable(), post::all_columns, - Community::safe_columns_tuple(), + community::all_columns, )) .into_boxed(); @@ -70,22 +65,19 @@ impl ModLockPostView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModLockPostView { - type DbTuple = ModLockPostViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_lock_post: a.0, - moderator: a.1, - post: a.2, - community: a.3, - }) - .collect::>() +impl JoinView for ModLockPostView { + type JoinTuple = ModLockPostViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_lock_post: a.0, + moderator: a.1, + post: a.2, + community: a.3, + } } } diff --git a/crates/db_views_moderator/src/mod_remove_comment_view.rs b/crates/db_views_moderator/src/mod_remove_comment_view.rs index 52eb8e75..0d33974b 100644 --- a/crates/db_views_moderator/src/mod_remove_comment_view.rs +++ b/crates/db_views_moderator/src/mod_remove_comment_view.rs @@ -14,22 +14,22 @@ use lemmy_db_schema::{ schema::{comment, community, mod_remove_comment, person, post}, source::{ comment::Comment, - community::{Community, CommunitySafe}, + community::Community, moderator::ModRemoveComment, - person::{Person, PersonSafe}, + person::Person, post::Post, }, - traits::{ToSafe, ViewToVec}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; type ModRemoveCommentViewTuple = ( ModRemoveComment, - Option, + Option, Comment, - PersonSafe, + Person, Post, - CommunitySafe, + Community, ); impl ModRemoveCommentView { @@ -51,11 +51,11 @@ impl ModRemoveCommentView { .inner_join(community::table.on(post::community_id.eq(community::id))) .select(( mod_remove_comment::all_columns, - Person::safe_columns_tuple().nullable(), + person::all_columns.nullable(), comment::all_columns, - person_alias_1.fields(Person::safe_columns_tuple()), + person_alias_1.fields(person::all_columns), post::all_columns, - Community::safe_columns_tuple(), + community::all_columns, )) .into_boxed(); @@ -80,24 +80,21 @@ impl ModRemoveCommentView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModRemoveCommentView { - type DbTuple = ModRemoveCommentViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_remove_comment: a.0, - moderator: a.1, - comment: a.2, - commenter: a.3, - post: a.4, - community: a.5, - }) - .collect::>() +impl JoinView for ModRemoveCommentView { + type JoinTuple = ModRemoveCommentViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_remove_comment: a.0, + moderator: a.1, + comment: a.2, + commenter: a.3, + post: a.4, + community: a.5, + } } } diff --git a/crates/db_views_moderator/src/mod_remove_community_view.rs b/crates/db_views_moderator/src/mod_remove_community_view.rs index 293b8455..50f99e52 100644 --- a/crates/db_views_moderator/src/mod_remove_community_view.rs +++ b/crates/db_views_moderator/src/mod_remove_community_view.rs @@ -12,16 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, mod_remove_community, person}, - source::{ - community::{Community, CommunitySafe}, - moderator::ModRemoveCommunity, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::ModRemoveCommunity, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModRemoveCommunityTuple = (ModRemoveCommunity, Option, CommunitySafe); +type ModRemoveCommunityTuple = (ModRemoveCommunity, Option, Community); impl ModRemoveCommunityView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -38,8 +34,8 @@ impl ModRemoveCommunityView { .inner_join(community::table) .select(( mod_remove_community::all_columns, - Person::safe_columns_tuple().nullable(), - Community::safe_columns_tuple(), + person::all_columns.nullable(), + community::all_columns, )) .into_boxed(); @@ -56,21 +52,18 @@ impl ModRemoveCommunityView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModRemoveCommunityView { - type DbTuple = ModRemoveCommunityTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_remove_community: a.0, - moderator: a.1, - community: a.2, - }) - .collect::>() +impl JoinView for ModRemoveCommunityView { + type JoinTuple = ModRemoveCommunityTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_remove_community: a.0, + moderator: a.1, + community: a.2, + } } } diff --git a/crates/db_views_moderator/src/mod_remove_post_view.rs b/crates/db_views_moderator/src/mod_remove_post_view.rs index 20d5b09b..fff5df46 100644 --- a/crates/db_views_moderator/src/mod_remove_post_view.rs +++ b/crates/db_views_moderator/src/mod_remove_post_view.rs @@ -12,17 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, mod_remove_post, person, post}, - source::{ - community::{Community, CommunitySafe}, - moderator::ModRemovePost, - person::{Person, PersonSafe}, - post::Post, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::ModRemovePost, person::Person, post::Post}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModRemovePostViewTuple = (ModRemovePost, Option, Post, CommunitySafe); +type ModRemovePostViewTuple = (ModRemovePost, Option, Post, Community); impl ModRemovePostView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -43,9 +38,9 @@ impl ModRemovePostView { .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .select(( mod_remove_post::all_columns, - Person::safe_columns_tuple().nullable(), + person::all_columns.nullable(), post::all_columns, - Community::safe_columns_tuple(), + community::all_columns, )) .into_boxed(); @@ -70,22 +65,19 @@ impl ModRemovePostView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModRemovePostView { - type DbTuple = ModRemovePostViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_remove_post: a.0, - moderator: a.1, - post: a.2, - community: a.3, - }) - .collect::>() +impl JoinView for ModRemovePostView { + type JoinTuple = ModRemovePostViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_remove_post: a.0, + moderator: a.1, + post: a.2, + community: a.3, + } } } diff --git a/crates/db_views_moderator/src/mod_transfer_community_view.rs b/crates/db_views_moderator/src/mod_transfer_community_view.rs index 182558a4..60da8e83 100644 --- a/crates/db_views_moderator/src/mod_transfer_community_view.rs +++ b/crates/db_views_moderator/src/mod_transfer_community_view.rs @@ -12,21 +12,12 @@ use diesel_async::RunQueryDsl; use lemmy_db_schema::{ newtypes::PersonId, schema::{community, mod_transfer_community, person}, - source::{ - community::{Community, CommunitySafe}, - moderator::ModTransferCommunity, - person::{Person, PersonSafe}, - }, - traits::{ToSafe, ViewToVec}, + source::{community::Community, moderator::ModTransferCommunity, person::Person}, + traits::JoinView, utils::{get_conn, limit_and_offset, DbPool}, }; -type ModTransferCommunityViewTuple = ( - ModTransferCommunity, - Option, - CommunitySafe, - PersonSafe, -); +type ModTransferCommunityViewTuple = (ModTransferCommunity, Option, Community, Person); impl ModTransferCommunityView { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result, Error> { @@ -49,9 +40,9 @@ impl ModTransferCommunityView { ) .select(( mod_transfer_community::all_columns, - Person::safe_columns_tuple().nullable(), - Community::safe_columns_tuple(), - person_alias_1.fields(Person::safe_columns_tuple()), + person::all_columns.nullable(), + community::all_columns, + person_alias_1.fields(person::all_columns), )) .into_boxed(); @@ -76,22 +67,19 @@ impl ModTransferCommunityView { .load::(conn) .await?; - let results = Self::from_tuple_to_vec(res); + let results = res.into_iter().map(Self::from_tuple).collect(); Ok(results) } } -impl ViewToVec for ModTransferCommunityView { - type DbTuple = ModTransferCommunityViewTuple; - fn from_tuple_to_vec(items: Vec) -> Vec { - items - .into_iter() - .map(|a| Self { - mod_transfer_community: a.0, - moderator: a.1, - community: a.2, - modded_person: a.3, - }) - .collect::>() +impl JoinView for ModTransferCommunityView { + type JoinTuple = ModTransferCommunityViewTuple; + fn from_tuple(a: Self::JoinTuple) -> Self { + Self { + mod_transfer_community: a.0, + moderator: a.1, + community: a.2, + modded_person: a.3, + } } } diff --git a/crates/db_views_moderator/src/structs.rs b/crates/db_views_moderator/src/structs.rs index 85dc006b..4a62ba9e 100644 --- a/crates/db_views_moderator/src/structs.rs +++ b/crates/db_views_moderator/src/structs.rs @@ -2,7 +2,7 @@ use lemmy_db_schema::{ newtypes::{CommunityId, PersonId}, source::{ comment::Comment, - community::CommunitySafe, + community::Community, moderator::{ AdminPurgeComment, AdminPurgeCommunity, @@ -20,7 +20,7 @@ use lemmy_db_schema::{ ModRemovePost, ModTransferCommunity, }, - person::PersonSafe, + person::Person, post::Post, }, }; @@ -29,113 +29,113 @@ use serde::{Deserialize, Serialize}; #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModAddCommunityView { pub mod_add_community: ModAddCommunity, - pub moderator: Option, - pub community: CommunitySafe, - pub modded_person: PersonSafe, + pub moderator: Option, + pub community: Community, + pub modded_person: Person, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModAddView { pub mod_add: ModAdd, - pub moderator: Option, - pub modded_person: PersonSafe, + pub moderator: Option, + pub modded_person: Person, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModBanFromCommunityView { pub mod_ban_from_community: ModBanFromCommunity, - pub moderator: Option, - pub community: CommunitySafe, - pub banned_person: PersonSafe, + pub moderator: Option, + pub community: Community, + pub banned_person: Person, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModBanView { pub mod_ban: ModBan, - pub moderator: Option, - pub banned_person: PersonSafe, + pub moderator: Option, + pub banned_person: Person, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModHideCommunityView { pub mod_hide_community: ModHideCommunity, - pub admin: Option, - pub community: CommunitySafe, + pub admin: Option, + pub community: Community, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModLockPostView { pub mod_lock_post: ModLockPost, - pub moderator: Option, + pub moderator: Option, pub post: Post, - pub community: CommunitySafe, + pub community: Community, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModRemoveCommentView { pub mod_remove_comment: ModRemoveComment, - pub moderator: Option, + pub moderator: Option, pub comment: Comment, - pub commenter: PersonSafe, + pub commenter: Person, pub post: Post, - pub community: CommunitySafe, + pub community: Community, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModRemoveCommunityView { pub mod_remove_community: ModRemoveCommunity, - pub moderator: Option, - pub community: CommunitySafe, + pub moderator: Option, + pub community: Community, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModRemovePostView { pub mod_remove_post: ModRemovePost, - pub moderator: Option, + pub moderator: Option, pub post: Post, - pub community: CommunitySafe, + pub community: Community, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModFeaturePostView { pub mod_feature_post: ModFeaturePost, - pub moderator: Option, + pub moderator: Option, pub post: Post, - pub community: CommunitySafe, + pub community: Community, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct ModTransferCommunityView { pub mod_transfer_community: ModTransferCommunity, - pub moderator: Option, - pub community: CommunitySafe, - pub modded_person: PersonSafe, + pub moderator: Option, + pub community: Community, + pub modded_person: Person, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AdminPurgeCommentView { pub admin_purge_comment: AdminPurgeComment, - pub admin: Option, + pub admin: Option, pub post: Post, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AdminPurgeCommunityView { pub admin_purge_community: AdminPurgeCommunity, - pub admin: Option, + pub admin: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AdminPurgePersonView { pub admin_purge_person: AdminPurgePerson, - pub admin: Option, + pub admin: Option, } #[derive(Debug, Serialize, Deserialize, Clone)] pub struct AdminPurgePostView { pub admin_purge_post: AdminPurgePost, - pub admin: Option, - pub community: CommunitySafe, + pub admin: Option, + pub community: Community, } #[derive(Debug, Serialize, Deserialize, Clone, Copy)] -- 2.44.1