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 <me@nutomic.com>
This commit is contained in:
Dessalines 2023-03-01 12:19:46 -05:00 committed by GitHub
parent 48f187188b
commit 985fe24669
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
55 changed files with 825 additions and 1322 deletions

View file

@ -18,7 +18,7 @@ use lemmy_db_schema::{
}, },
traits::{Bannable, Crud, Followable}, 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}; use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -88,7 +88,7 @@ impl Perform for BanFromCommunity {
ModBanFromCommunity::create(context.pool(), &form).await?; ModBanFromCommunity::create(context.pool(), &form).await?;
let person_id = data.person_id; 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 { let res = BanFromCommunityResponse {
person_view, person_view,

View file

@ -13,7 +13,7 @@ use lemmy_db_schema::{
}, },
traits::Crud, traits::Crud,
}; };
use lemmy_db_views_actor::structs::PersonViewSafe; use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, ConnectionId}; use lemmy_utils::{error::LemmyError, ConnectionId};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -52,7 +52,7 @@ impl Perform for AddAdmin {
ModAdd::create(context.pool(), &form).await?; ModAdd::create(context.pool(), &form).await?;
let admins = PersonViewSafe::admins(context.pool()).await?; let admins = PersonView::admins(context.pool()).await?;
let res = AddAdminResponse { admins }; let res = AddAdminResponse { admins };

View file

@ -13,7 +13,7 @@ use lemmy_db_schema::{
}, },
traits::Crud, 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}; use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -72,7 +72,7 @@ impl Perform for BanPerson {
ModBan::create(context.pool(), &form).await?; ModBan::create(context.pool(), &form).await?;
let person_id = data.person_id; 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 { let res = BanPersonResponse {
person_view, person_view,

View file

@ -9,7 +9,7 @@ use lemmy_db_schema::{
source::person_block::{PersonBlock, PersonBlockForm}, source::person_block::{PersonBlock, PersonBlockForm},
traits::Blockable, traits::Blockable,
}; };
use lemmy_db_views_actor::structs::PersonViewSafe; use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, ConnectionId}; use lemmy_utils::{error::LemmyError, ConnectionId};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -39,7 +39,7 @@ impl Perform for BlockPerson {
target_id, 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 { if target_person_view.person.admin {
return Err(LemmyError::from_message("cant_block_admin")); return Err(LemmyError::from_message("cant_block_admin"));

View file

@ -5,7 +5,7 @@ use lemmy_api_common::{
person::{BannedPersonsResponse, GetBannedPersons}, person::{BannedPersonsResponse, GetBannedPersons},
utils::{get_local_user_view_from_jwt, is_admin}, 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}; use lemmy_utils::{error::LemmyError, ConnectionId};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -24,7 +24,7 @@ impl Perform for GetBannedPersons {
// Make sure user is an admin // Make sure user is an admin
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
let banned = PersonViewSafe::banned(context.pool()).await?; let banned = PersonView::banned(context.pool()).await?;
let res = Self::Response { banned }; let res = Self::Response { banned };

View file

@ -16,7 +16,7 @@ use lemmy_db_schema::{
traits::Crud, traits::Crud,
}; };
use lemmy_db_views::structs::SiteView; 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}; use lemmy_utils::{error::LemmyError, version, ConnectionId};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -36,7 +36,7 @@ impl Perform for LeaveAdmin {
is_admin(&local_user_view)?; is_admin(&local_user_view)?;
// Make sure there isn't just one admin (so if one leaves, there will still be one left) // 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 { if admins.len() == 1 {
return Err(LemmyError::from_message("cannot_leave_admin")); return Err(LemmyError::from_message("cannot_leave_admin"));
} }
@ -60,7 +60,7 @@ impl Perform for LeaveAdmin {
// Reread site and admins // Reread site and admins
let site_view = SiteView::read_local(context.pool()).await?; 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 all_languages = Language::read_all(context.pool()).await?;
let discussion_languages = SiteLanguage::read_local(context.pool()).await?; let discussion_languages = SiteLanguage::read_local(context.pool()).await?;

View file

@ -5,7 +5,7 @@ use lemmy_db_schema::{
ListingType, ListingType,
SortType, SortType,
}; };
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonViewSafe}; use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonView};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -74,7 +74,7 @@ pub struct BanFromCommunity {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BanFromCommunityResponse { pub struct BanFromCommunityResponse {
pub person_view: PersonViewSafe, pub person_view: PersonView,
pub banned: bool, pub banned: bool,
} }

View file

@ -9,7 +9,7 @@ use lemmy_db_views_actor::structs::{
CommentReplyView, CommentReplyView,
CommunityModeratorView, CommunityModeratorView,
PersonMentionView, PersonMentionView,
PersonViewSafe, PersonView,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
@ -104,7 +104,7 @@ pub struct GetPersonDetails {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GetPersonDetailsResponse { pub struct GetPersonDetailsResponse {
pub person_view: PersonViewSafe, pub person_view: PersonView,
pub comments: Vec<CommentView>, pub comments: Vec<CommentView>,
pub posts: Vec<PostView>, pub posts: Vec<PostView>,
pub moderates: Vec<CommunityModeratorView>, pub moderates: Vec<CommunityModeratorView>,
@ -134,7 +134,7 @@ pub struct AddAdmin {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AddAdminResponse { pub struct AddAdminResponse {
pub admins: Vec<PersonViewSafe>, pub admins: Vec<PersonView>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -154,12 +154,12 @@ pub struct GetBannedPersons {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BannedPersonsResponse { pub struct BannedPersonsResponse {
pub banned: Vec<PersonViewSafe>, pub banned: Vec<PersonView>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BanPersonResponse { pub struct BanPersonResponse {
pub person_view: PersonViewSafe, pub person_view: PersonView,
pub banned: bool, pub banned: bool,
} }
@ -172,7 +172,7 @@ pub struct BlockPerson {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BlockPersonResponse { pub struct BlockPersonResponse {
pub person_view: PersonViewSafe, pub person_view: PersonView,
pub blocked: bool, pub blocked: bool,
} }

View file

@ -14,7 +14,7 @@ use lemmy_db_schema::{
}; };
use lemmy_db_views::structs::{ use lemmy_db_views::structs::{
CommentView, CommentView,
LocalUserSettingsView, LocalUserView,
PostView, PostView,
RegistrationApplicationView, RegistrationApplicationView,
SiteView, SiteView,
@ -25,7 +25,7 @@ use lemmy_db_views_actor::structs::{
CommunityModeratorView, CommunityModeratorView,
CommunityView, CommunityView,
PersonBlockView, PersonBlockView,
PersonViewSafe, PersonView,
}; };
use lemmy_db_views_moderator::structs::{ use lemmy_db_views_moderator::structs::{
AdminPurgeCommentView, AdminPurgeCommentView,
@ -66,7 +66,7 @@ pub struct SearchResponse {
pub comments: Vec<CommentView>, pub comments: Vec<CommentView>,
pub posts: Vec<PostView>, pub posts: Vec<PostView>,
pub communities: Vec<CommunityView>, pub communities: Vec<CommunityView>,
pub users: Vec<PersonViewSafe>, pub users: Vec<PersonView>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -80,7 +80,7 @@ pub struct ResolveObjectResponse {
pub comment: Option<CommentView>, pub comment: Option<CommentView>,
pub post: Option<PostView>, pub post: Option<PostView>,
pub community: Option<CommunityView>, pub community: Option<CommunityView>,
pub person: Option<PersonViewSafe>, pub person: Option<PersonView>,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Default)] #[derive(Debug, Serialize, Deserialize, Clone, Default)]
@ -217,7 +217,7 @@ pub struct SiteResponse {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GetSiteResponse { pub struct GetSiteResponse {
pub site_view: SiteView, pub site_view: SiteView,
pub admins: Vec<PersonViewSafe>, pub admins: Vec<PersonView>,
pub online: usize, pub online: usize,
pub version: String, pub version: String,
pub my_user: Option<MyUserInfo>, pub my_user: Option<MyUserInfo>,
@ -229,7 +229,7 @@ pub struct GetSiteResponse {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MyUserInfo { pub struct MyUserInfo {
pub local_user_view: LocalUserSettingsView, pub local_user_view: LocalUserView,
pub follows: Vec<CommunityFollowerView>, pub follows: Vec<CommunityFollowerView>,
pub moderates: Vec<CommunityModeratorView>, pub moderates: Vec<CommunityModeratorView>,
pub community_blocks: Vec<CommunityBlockView>, pub community_blocks: Vec<CommunityBlockView>,

View file

@ -22,15 +22,12 @@ use lemmy_db_schema::{
utils::DbPool, utils::DbPool,
ListingType, ListingType,
}; };
use lemmy_db_views::{ use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView};
comment_view::CommentQuery,
structs::{LocalUserSettingsView, LocalUserView},
};
use lemmy_db_views_actor::structs::{ use lemmy_db_views_actor::structs::{
CommunityModeratorView, CommunityModeratorView,
CommunityPersonBanView, CommunityPersonBanView,
CommunityView, CommunityView,
PersonViewSafe, PersonView,
}; };
use lemmy_utils::{ use lemmy_utils::{
claims::Claims, 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> { 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 let top_admin = admins
.first() .first()
.ok_or_else(|| LemmyError::from_message("no admins"))?; .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<String>>, jwt: Option<&Sensitive<String>>,
pool: &DbPool, pool: &DbPool,
secret: &Secret, secret: &Secret,
) -> Result<Option<LocalUserSettingsView>, LemmyError> { ) -> Result<Option<LocalUserView>, LemmyError> {
match jwt { match jwt {
Some(jwt) => { Some(jwt) => {
let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret) let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret)
.map_err(|e| e.with_message("not_logged_in"))? .map_err(|e| e.with_message("not_logged_in"))?
.claims; .claims;
let local_user_id = LocalUserId(claims.sub); 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( check_user_valid(
local_user_view.person.banned, local_user_view.person.banned,
local_user_view.person.ban_expires, 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) 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) lang_str_to_lang(&user.local_user.interface_language)
} }
@ -511,7 +508,7 @@ pub async fn send_new_applicant_email_to_admins(
settings: &Settings, settings: &Settings,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// Collect the admins with emails // 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!( let applications_link = &format!(
"{}/registration_applications", "{}/registration_applications",
@ -536,7 +533,7 @@ pub async fn send_new_report_email_to_admins(
settings: &Settings, settings: &Settings,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// Collect the admins with emails // 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(),); let reports_link = &format!("{}/reports", settings.get_protocol_and_hostname(),);

View file

@ -16,7 +16,7 @@ use lemmy_db_views_actor::structs::{
CommunityFollowerView, CommunityFollowerView,
CommunityModeratorView, CommunityModeratorView,
PersonBlockView, PersonBlockView,
PersonViewSafe, PersonView,
}; };
use lemmy_utils::{error::LemmyError, version, ConnectionId}; 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 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()?; let online = context.chat_server().get_users_online()?;

View file

@ -10,7 +10,7 @@ use lemmy_db_schema::{
utils::post_to_comment_sort_type, utils::post_to_comment_sort_type,
}; };
use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery}; 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}; use lemmy_utils::{error::LemmyError, ConnectionId};
#[async_trait::async_trait(?Send)] #[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 // You don't need to return settings for the user, since this comes back with GetSite
// `my_user` // `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 sort = data.sort;
let page = data.page; let page = data.page;

View file

@ -11,7 +11,7 @@ use lemmy_api_common::{
}; };
use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool}; use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool};
use lemmy_db_views::structs::{CommentView, PostView}; 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}; use lemmy_utils::{error::LemmyError, ConnectionId};
#[async_trait::async_trait(?Send)] #[async_trait::async_trait(?Send)]
@ -52,7 +52,7 @@ async fn convert_response(
match object { match object {
Person(p) => { Person(p) => {
removed_or_deleted = p.deleted; 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) => { Community(c) => {
removed_or_deleted = c.deleted || c.removed; removed_or_deleted = c.deleted || c.removed;

View file

@ -22,74 +22,6 @@ use crate::{
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods}; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
use diesel_async::RunQueryDsl; 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] #[async_trait]
impl Crud for Community { impl Crud for Community {
type InsertForm = CommunityInsertForm; type InsertForm = CommunityInsertForm;

View file

@ -18,79 +18,6 @@ use bcrypt::{hash, DEFAULT_COST};
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
use diesel_async::RunQueryDsl; 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 { impl LocalUser {
pub async fn update_password( pub async fn update_password(
pool: &DbPool, pool: &DbPool,

View file

@ -26,83 +26,6 @@ use crate::{
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods}; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
use diesel_async::RunQueryDsl; 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] #[async_trait]
impl Crud for Person { impl Crud for Person {
type InsertForm = PersonInsertForm; type InsertForm = PersonInsertForm;

View file

@ -19,13 +19,19 @@ pub struct Community {
pub nsfw: bool, pub nsfw: bool,
pub actor_id: DbUrl, pub actor_id: DbUrl,
pub local: bool, pub local: bool,
#[serde(skip)]
pub private_key: Option<String>, pub private_key: Option<String>,
#[serde(skip)]
pub public_key: String, pub public_key: String,
#[serde(skip)]
pub last_refreshed_at: chrono::NaiveDateTime, pub last_refreshed_at: chrono::NaiveDateTime,
pub icon: Option<DbUrl>, pub icon: Option<DbUrl>,
pub banner: Option<DbUrl>, pub banner: Option<DbUrl>,
#[serde(skip_serializing)]
pub followers_url: DbUrl, pub followers_url: DbUrl,
#[serde(skip_serializing)]
pub inbox_url: DbUrl, pub inbox_url: DbUrl,
#[serde(skip)]
pub shared_inbox_url: Option<DbUrl>, pub shared_inbox_url: Option<DbUrl>,
/// Url where moderators collection is served over Activitypub /// Url where moderators collection is served over Activitypub
#[serde(skip)] #[serde(skip)]
@ -38,29 +44,6 @@ pub struct Community {
pub instance_id: InstanceId, 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<String>,
pub removed: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub nsfw: bool,
pub actor_id: DbUrl,
pub local: bool,
pub icon: Option<DbUrl>,
pub banner: Option<DbUrl>,
pub hidden: bool,
pub posting_restricted_to_mods: bool,
pub instance_id: InstanceId,
}
#[derive(Debug, Clone, TypedBuilder)] #[derive(Debug, Clone, TypedBuilder)]
#[builder(field_defaults(default))] #[builder(field_defaults(default))]
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]

View file

@ -10,6 +10,7 @@ use typed_builder::TypedBuilder;
pub struct LocalUser { pub struct LocalUser {
pub id: LocalUserId, pub id: LocalUserId,
pub person_id: PersonId, pub person_id: PersonId,
#[serde(skip)]
pub password_encrypted: String, pub password_encrypted: String,
pub email: Option<String>, pub email: Option<String>,
pub show_nsfw: bool, pub show_nsfw: bool,
@ -28,30 +29,6 @@ pub struct LocalUser {
pub accepted_application: bool, 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<String>,
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)] #[derive(Clone, TypedBuilder)]
#[builder(field_defaults(default))] #[builder(field_defaults(default))]
#[cfg_attr(feature = "full", derive(Insertable))] #[cfg_attr(feature = "full", derive(Insertable))]

View file

@ -18,38 +18,17 @@ pub struct Person {
pub actor_id: DbUrl, pub actor_id: DbUrl,
pub bio: Option<String>, pub bio: Option<String>,
pub local: bool, pub local: bool,
#[serde(skip)]
pub private_key: Option<String>, pub private_key: Option<String>,
#[serde(skip)]
pub public_key: String, pub public_key: String,
#[serde(skip)]
pub last_refreshed_at: chrono::NaiveDateTime, pub last_refreshed_at: chrono::NaiveDateTime,
pub banner: Option<DbUrl>, pub banner: Option<DbUrl>,
pub deleted: bool, pub deleted: bool,
#[serde(skip_serializing)]
pub inbox_url: DbUrl, pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>, #[serde(skip)]
pub matrix_user_id: Option<String>,
pub admin: bool,
pub bot_account: bool,
pub ban_expires: Option<chrono::NaiveDateTime>,
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<String>,
pub avatar: Option<DbUrl>,
pub banned: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub actor_id: DbUrl,
pub bio: Option<String>,
pub local: bool,
pub banner: Option<DbUrl>,
pub deleted: bool,
pub inbox_url: DbUrl,
pub shared_inbox_url: Option<DbUrl>, pub shared_inbox_url: Option<DbUrl>,
pub matrix_user_id: Option<String>, pub matrix_user_id: Option<String>,
pub admin: bool, pub admin: bool,

View file

@ -140,19 +140,9 @@ pub trait Reportable {
Self: Sized; Self: Sized;
} }
pub trait ToSafe { pub trait JoinView {
type SafeColumns; type JoinTuple;
fn safe_columns_tuple() -> Self::SafeColumns; fn from_tuple(tuple: Self::JoinTuple) -> Self
}
pub trait ToSafeSettings {
type SafeSettingsColumns;
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns;
}
pub trait ViewToVec {
type DbTuple;
fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
where where
Self: Sized; Self: Sized;
} }

View file

@ -26,28 +26,15 @@ use lemmy_db_schema::{
source::{ source::{
comment::Comment, comment::Comment,
comment_report::CommentReport, comment_report::CommentReport,
community::{Community, CommunityPersonBan, CommunitySafe}, community::{Community, CommunityPersonBan},
person::{Person, PersonSafe}, person::Person,
post::Post, post::Post,
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
type CommentReportViewTuple = (
CommentReport,
Comment,
Post,
CommunitySafe,
PersonSafe,
PersonSafe,
CommentAggregates,
Option<CommunityPersonBan>,
Option<i16>,
Option<PersonSafe>,
);
impl CommentReportView { impl CommentReportView {
/// returns the CommentReportView for the provided report_id /// 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 (person_alias_1, person_alias_2) = diesel::alias!(person as person1, person as person2);
let ( let res = comment_report::table
comment_report,
comment,
post,
community,
creator,
comment_creator,
counts,
creator_banned_from_community,
comment_like,
resolver,
) = comment_report::table
.find(report_id) .find(report_id)
.inner_join(comment::table) .inner_join(comment::table)
.inner_join(post::table.on(comment::post_id.eq(post::id))) .inner_join(post::table.on(comment::post_id.eq(post::id)))
@ -109,33 +85,18 @@ impl CommentReportView {
comment_report::all_columns, comment_report::all_columns,
comment::all_columns, comment::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
comment_like::score.nullable(), comment_like::score.nullable(),
person_alias_2 person_alias_2.fields(person::all_columns).nullable(),
.fields(Person::safe_columns_tuple())
.nullable(),
)) ))
.first::<CommentReportViewTuple>(conn) .first::<<CommentReportView as JoinView>::JoinTuple>(conn)
.await?; .await?;
let my_vote = comment_like; Ok(Self::from_tuple(res))
Ok(Self {
comment_report,
comment,
post,
community,
creator,
comment_creator,
counts,
creator_banned_from_community: creator_banned_from_community.is_some(),
my_vote,
resolver,
})
} }
/// Returns the current unresolved post report count for the communities you mod /// 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_report::all_columns,
comment::all_columns, comment::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
comment_like::score.nullable(), comment_like::score.nullable(),
person_alias_2 person_alias_2.fields(person::all_columns).nullable(),
.fields(Person::safe_columns_tuple())
.nullable(),
)) ))
.into_boxed(); .into_boxed();
@ -275,34 +234,45 @@ impl<'a> CommentReportQuery<'a> {
.and(community_moderator::person_id.eq(self.my_person_id)), .and(community_moderator::person_id.eq(self.my_person_id)),
), ),
) )
.load::<CommentReportViewTuple>(conn) .load::<<CommentReportView as JoinView>::JoinTuple>(conn)
.await? .await?
} else { } else {
query.load::<CommentReportViewTuple>(conn).await? query
.load::<<CommentReportView as JoinView>::JoinTuple>(conn)
.await?
}; };
Ok(CommentReportView::from_tuple_to_vec(res)) Ok(res.into_iter().map(CommentReportView::from_tuple).collect())
} }
} }
impl ViewToVec for CommentReportView { impl JoinView for CommentReportView {
type DbTuple = CommentReportViewTuple; type JoinTuple = (
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { CommentReport,
items Comment,
.into_iter() Post,
.map(|a| Self { Community,
comment_report: a.0, Person,
comment: a.1, Person,
post: a.2, CommentAggregates,
community: a.3, Option<CommunityPersonBan>,
creator: a.4, Option<i16>,
comment_creator: a.5, Option<Person>,
counts: a.6, );
creator_banned_from_community: a.7.is_some(),
my_vote: a.8, fn from_tuple(a: Self::JoinTuple) -> Self {
resolver: a.9, Self {
}) comment_report: a.0,
.collect::<Vec<Self>>() 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::{ source::{
comment::{Comment, CommentInsertForm}, comment::{Comment, CommentInsertForm},
comment_report::{CommentReport, CommentReportForm}, comment_report::{CommentReport, CommentReportForm},
community::{ community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
Community,
CommunityInsertForm,
CommunityModerator,
CommunityModeratorForm,
CommunitySafe,
},
instance::Instance, instance::Instance,
person::{Person, PersonInsertForm, PersonSafe}, person::{Person, PersonInsertForm},
post::{Post, PostInsertForm}, post::{Post, PostInsertForm},
}, },
traits::{Crud, Joinable, Reportable}, traits::{Crud, Joinable, Reportable},
@ -435,7 +399,7 @@ mod tests {
comment_report: inserted_jessica_report.clone(), comment_report: inserted_jessica_report.clone(),
comment: inserted_comment.clone(), comment: inserted_comment.clone(),
post: inserted_post, post: inserted_post,
community: CommunitySafe { community: Community {
id: inserted_community.id, id: inserted_community.id,
name: inserted_community.name, name: inserted_community.name,
icon: None, icon: None,
@ -451,9 +415,17 @@ mod tests {
hidden: false, hidden: false,
posting_restricted_to_mods: false, posting_restricted_to_mods: false,
published: inserted_community.published, 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, instance_id: inserted_instance.id,
}, },
creator: PersonSafe { creator: Person {
id: inserted_jessica.id, id: inserted_jessica.id,
name: inserted_jessica.name, name: inserted_jessica.name,
display_name: None, display_name: None,
@ -473,8 +445,11 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: inserted_instance.id, 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, id: inserted_timmy.id,
name: inserted_timmy.name.clone(), name: inserted_timmy.name.clone(),
display_name: None, display_name: None,
@ -494,6 +469,9 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: inserted_instance.id, 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, creator_banned_from_community: false,
counts: CommentAggregates { counts: CommentAggregates {
@ -513,7 +491,7 @@ mod tests {
let mut expected_sara_report_view = expected_jessica_report_view.clone(); let mut expected_sara_report_view = expected_jessica_report_view.clone();
expected_sara_report_view.comment_report = inserted_sara_report; 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, id: inserted_sara.id,
name: inserted_sara.name, name: inserted_sara.name,
display_name: None, display_name: None,
@ -533,6 +511,9 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: inserted_instance.id, 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 // Do a batch read of timmys reports
@ -580,7 +561,7 @@ mod tests {
.updated = read_jessica_report_view_after_resolve .updated = read_jessica_report_view_after_resolve
.comment_report .comment_report
.updated; .updated;
expected_jessica_report_view_after_resolve.resolver = Some(PersonSafe { expected_jessica_report_view_after_resolve.resolver = Some(Person {
id: inserted_timmy.id, id: inserted_timmy.id,
name: inserted_timmy.name.clone(), name: inserted_timmy.name.clone(),
display_name: None, display_name: None,
@ -596,6 +577,9 @@ mod tests {
banner: None, banner: None,
updated: None, updated: None,
inbox_url: inserted_timmy.inbox_url.clone(), 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, shared_inbox_url: None,
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,

View file

@ -30,13 +30,13 @@ use lemmy_db_schema::{
}, },
source::{ source::{
comment::{Comment, CommentSaved}, comment::{Comment, CommentSaved},
community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, community::{Community, CommunityFollower, CommunityPersonBan},
local_user::LocalUser, local_user::LocalUser,
person::{Person, PersonSafe}, person::Person,
person_block::PersonBlock, person_block::PersonBlock,
post::Post, post::Post,
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{functions::hot_rank, fuzzy_search, get_conn, limit_and_offset_unlimited, DbPool}, utils::{functions::hot_rank, fuzzy_search, get_conn, limit_and_offset_unlimited, DbPool},
CommentSortType, CommentSortType,
ListingType, ListingType,
@ -45,9 +45,9 @@ use typed_builder::TypedBuilder;
type CommentViewTuple = ( type CommentViewTuple = (
Comment, Comment,
PersonSafe, Person,
Post, Post,
CommunitySafe, Community,
CommentAggregates, CommentAggregates,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<CommunityFollower>, Option<CommunityFollower>,
@ -126,9 +126,9 @@ impl CommentView {
) )
.select(( .select((
comment::all_columns, comment::all_columns,
Person::safe_columns_tuple(), person::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
@ -252,9 +252,9 @@ impl<'a> CommentQuery<'a> {
) )
.select(( .select((
comment::all_columns, comment::all_columns,
Person::safe_columns_tuple(), person::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
@ -375,28 +375,25 @@ impl<'a> CommentQuery<'a> {
.load::<CommentViewTuple>(conn) .load::<CommentViewTuple>(conn)
.await?; .await?;
Ok(CommentView::from_tuple_to_vec(res)) Ok(res.into_iter().map(CommentView::from_tuple).collect())
} }
} }
impl ViewToVec for CommentView { impl JoinView for CommentView {
type DbTuple = CommentViewTuple; type JoinTuple = CommentViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() comment: a.0,
.map(|a| Self { creator: a.1,
comment: a.0, post: a.2,
creator: a.1, community: a.3,
post: a.2, counts: a.4,
community: a.3, creator_banned_from_community: a.5.is_some(),
counts: a.4, subscribed: CommunityFollower::to_subscribed_type(&a.6),
creator_banned_from_community: a.5.is_some(), saved: a.7.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.6), creator_blocked: a.8.is_some(),
saved: a.7.is_some(), my_vote: a.9,
creator_blocked: a.8.is_some(), }
my_vote: a.9,
})
.collect::<Vec<Self>>()
} }
} }
@ -408,12 +405,10 @@ mod tests {
CommentSortType, CommentSortType,
CommentView, CommentView,
Community, Community,
CommunitySafe,
DbPool, DbPool,
LocalUser, LocalUser,
Person, Person,
PersonBlock, PersonBlock,
PersonSafe,
Post, Post,
}; };
use lemmy_db_schema::{ use lemmy_db_schema::{
@ -841,7 +836,7 @@ mod tests {
path: data.inserted_comment_0.clone().path, path: data.inserted_comment_0.clone().path,
language_id: LanguageId(0), language_id: LanguageId(0),
}, },
creator: PersonSafe { creator: Person {
id: data.inserted_person.id, id: data.inserted_person.id,
name: "timmy".into(), name: "timmy".into(),
display_name: None, display_name: None,
@ -861,6 +856,9 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: data.inserted_instance.id, 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 { post: Post {
id: data.inserted_post.id, id: data.inserted_post.id,
@ -885,7 +883,7 @@ mod tests {
featured_community: false, featured_community: false,
featured_local: false, featured_local: false,
}, },
community: CommunitySafe { community: Community {
id: data.inserted_community.id, id: data.inserted_community.id,
name: "test community 5".to_string(), name: "test community 5".to_string(),
icon: None, icon: None,
@ -902,6 +900,14 @@ mod tests {
posting_restricted_to_mods: false, posting_restricted_to_mods: false,
published: data.inserted_community.published, published: data.inserted_community.published,
instance_id: data.inserted_instance.id, 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 { counts: CommentAggregates {
id: agg.id, id: agg.id,

View file

@ -1,15 +1,12 @@
use crate::structs::{LocalUserSettingsView, LocalUserView}; use crate::structs::LocalUserView;
use diesel::{result::Error, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl}; use diesel::{result::Error, BoolExpressionMethods, ExpressionMethods, JoinOnDsl, QueryDsl};
use diesel_async::RunQueryDsl; use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::PersonAggregates, aggregates::structs::PersonAggregates,
newtypes::{LocalUserId, PersonId}, newtypes::{LocalUserId, PersonId},
schema::{local_user, person, person_aggregates}, schema::{local_user, person, person_aggregates},
source::{ source::{local_user::LocalUser, person::Person},
local_user::{LocalUser, LocalUserSettings}, traits::JoinView,
person::{Person, PersonSafe},
},
traits::{ToSafe, ToSafeSettings, ViewToVec},
utils::{functions::lower, get_conn, DbPool}, utils::{functions::lower, get_conn, DbPool},
}; };
@ -121,30 +118,6 @@ impl LocalUserView {
counts, counts,
}) })
} }
}
type LocalUserSettingsViewTuple = (LocalUserSettings, PersonSafe, PersonAggregates);
impl LocalUserSettingsView {
pub async fn read(pool: &DbPool, local_user_id: LocalUserId) -> Result<Self, Error> {
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::<LocalUserSettingsViewTuple>(conn)
.await?;
Ok(Self {
local_user,
person,
counts,
})
}
pub async fn list_admins_with_emails(pool: &DbPool) -> Result<Vec<Self>, Error> { pub async fn list_admins_with_emails(pool: &DbPool) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
@ -154,27 +127,24 @@ impl LocalUserSettingsView {
.inner_join(person::table) .inner_join(person::table)
.inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id))) .inner_join(person_aggregates::table.on(person::id.eq(person_aggregates::person_id)))
.select(( .select((
LocalUser::safe_settings_columns_tuple(), local_user::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_aggregates::all_columns, person_aggregates::all_columns,
)) ))
.load::<LocalUserSettingsViewTuple>(conn) .load::<LocalUserViewTuple>(conn)
.await?; .await?;
Ok(LocalUserSettingsView::from_tuple_to_vec(res)) Ok(res.into_iter().map(LocalUserView::from_tuple).collect())
} }
} }
impl ViewToVec for LocalUserSettingsView { impl JoinView for LocalUserView {
type DbTuple = LocalUserSettingsViewTuple; type JoinTuple = LocalUserViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() local_user: a.0,
.map(|a| Self { person: a.1,
local_user: a.0, counts: a.2,
person: a.1, }
counts: a.2,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -23,12 +23,12 @@ use lemmy_db_schema::{
post_report, post_report,
}, },
source::{ source::{
community::{Community, CommunityPersonBan, CommunitySafe}, community::{Community, CommunityPersonBan},
person::{Person, PersonSafe}, person::Person,
post::Post, post::Post,
post_report::PostReport, post_report::PostReport,
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
@ -36,13 +36,13 @@ use typed_builder::TypedBuilder;
type PostReportViewTuple = ( type PostReportViewTuple = (
PostReport, PostReport,
Post, Post,
CommunitySafe, Community,
PersonSafe, Person,
PersonSafe, Person,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<i16>, Option<i16>,
PostAggregates, PostAggregates,
Option<PersonSafe>, Option<Person>,
); );
impl PostReportView { impl PostReportView {
@ -99,13 +99,13 @@ impl PostReportView {
.select(( .select((
post_report::all_columns, post_report::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
post_like::score.nullable(), post_like::score.nullable(),
post_aggregates::all_columns, post_aggregates::all_columns,
person_alias_2.fields(Person::safe_columns_tuple().nullable()), person_alias_2.fields(person::all_columns.nullable()),
)) ))
.first::<PostReportViewTuple>(conn) .first::<PostReportViewTuple>(conn)
.await?; .await?;
@ -216,15 +216,13 @@ impl<'a> PostReportQuery<'a> {
.select(( .select((
post_report::all_columns, post_report::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
post_like::score.nullable(), post_like::score.nullable(),
post_aggregates::all_columns, post_aggregates::all_columns,
person_alias_2 person_alias_2.fields(person::all_columns.nullable()),
.fields(Person::safe_columns_tuple())
.nullable(),
)) ))
.into_boxed(); .into_boxed();
@ -259,27 +257,24 @@ impl<'a> PostReportQuery<'a> {
query.load::<PostReportViewTuple>(conn).await? query.load::<PostReportViewTuple>(conn).await?
}; };
Ok(PostReportView::from_tuple_to_vec(res)) Ok(res.into_iter().map(PostReportView::from_tuple).collect())
} }
} }
impl ViewToVec for PostReportView { impl JoinView for PostReportView {
type DbTuple = PostReportViewTuple; type JoinTuple = PostReportViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() post_report: a.0,
.map(|a| Self { post: a.1,
post_report: a.0, community: a.2,
post: a.1, creator: a.3,
community: a.2, post_creator: a.4,
creator: a.3, creator_banned_from_community: a.5.is_some(),
post_creator: a.4, my_vote: a.6,
creator_banned_from_community: a.5.is_some(), counts: a.7,
my_vote: a.6, resolver: a.8,
counts: a.7, }
resolver: a.8,
})
.collect::<Vec<Self>>()
} }
} }
@ -289,15 +284,9 @@ mod tests {
use lemmy_db_schema::{ use lemmy_db_schema::{
aggregates::structs::PostAggregates, aggregates::structs::PostAggregates,
source::{ source::{
community::{ community::{Community, CommunityInsertForm, CommunityModerator, CommunityModeratorForm},
Community,
CommunityInsertForm,
CommunityModerator,
CommunityModeratorForm,
CommunitySafe,
},
instance::Instance, instance::Instance,
person::{Person, PersonInsertForm, PersonSafe}, person::{Person, PersonInsertForm},
post::{Post, PostInsertForm}, post::{Post, PostInsertForm},
post_report::{PostReport, PostReportForm}, post_report::{PostReport, PostReportForm},
}, },
@ -402,7 +391,7 @@ mod tests {
let expected_jessica_report_view = PostReportView { let expected_jessica_report_view = PostReportView {
post_report: inserted_jessica_report.clone(), post_report: inserted_jessica_report.clone(),
post: inserted_post.clone(), post: inserted_post.clone(),
community: CommunitySafe { community: Community {
id: inserted_community.id, id: inserted_community.id,
name: inserted_community.name, name: inserted_community.name,
icon: None, icon: None,
@ -419,8 +408,16 @@ mod tests {
posting_restricted_to_mods: false, posting_restricted_to_mods: false,
published: inserted_community.published, published: inserted_community.published,
instance_id: inserted_instance.id, 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, id: inserted_jessica.id,
name: inserted_jessica.name, name: inserted_jessica.name,
display_name: None, display_name: None,
@ -440,8 +437,11 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: inserted_instance.id, 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, id: inserted_timmy.id,
name: inserted_timmy.name.clone(), name: inserted_timmy.name.clone(),
display_name: None, display_name: None,
@ -461,6 +461,9 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: inserted_instance.id, 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, creator_banned_from_community: false,
my_vote: None, my_vote: None,
@ -485,7 +488,7 @@ mod tests {
let mut expected_sara_report_view = expected_jessica_report_view.clone(); let mut expected_sara_report_view = expected_jessica_report_view.clone();
expected_sara_report_view.post_report = inserted_sara_report; expected_sara_report_view.post_report = inserted_sara_report;
expected_sara_report_view.my_vote = None; expected_sara_report_view.my_vote = None;
expected_sara_report_view.creator = PersonSafe { expected_sara_report_view.creator = Person {
id: inserted_sara.id, id: inserted_sara.id,
name: inserted_sara.name, name: inserted_sara.name,
display_name: None, display_name: None,
@ -505,6 +508,9 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: inserted_instance.id, 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 // Do a batch read of timmys reports
@ -550,7 +556,7 @@ mod tests {
expected_jessica_report_view_after_resolve expected_jessica_report_view_after_resolve
.post_report .post_report
.updated = read_jessica_report_view_after_resolve.post_report.updated; .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, id: inserted_timmy.id,
name: inserted_timmy.name.clone(), name: inserted_timmy.name.clone(),
display_name: None, display_name: None,
@ -570,6 +576,9 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: inserted_instance.id, 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!( assert_eq!(

View file

@ -33,13 +33,13 @@ use lemmy_db_schema::{
post_saved, post_saved,
}, },
source::{ source::{
community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, community::{Community, CommunityFollower, CommunityPersonBan},
local_user::LocalUser, local_user::LocalUser,
person::{Person, PersonSafe}, person::Person,
person_block::PersonBlock, person_block::PersonBlock,
post::{Post, PostRead, PostSaved}, post::{Post, PostRead, PostSaved},
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{functions::hot_rank, fuzzy_search, get_conn, limit_and_offset, DbPool}, utils::{functions::hot_rank, fuzzy_search, get_conn, limit_and_offset, DbPool},
ListingType, ListingType,
SortType, SortType,
@ -49,8 +49,8 @@ use typed_builder::TypedBuilder;
type PostViewTuple = ( type PostViewTuple = (
Post, Post,
PersonSafe, Person,
CommunitySafe, Community,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
PostAggregates, PostAggregates,
Option<CommunityFollower>, Option<CommunityFollower>,
@ -144,8 +144,8 @@ impl PostView {
) )
.select(( .select((
post::all_columns, post::all_columns,
Person::safe_columns_tuple(), person::all_columns,
Community::safe_columns_tuple(), community::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
post_aggregates::all_columns, post_aggregates::all_columns,
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
@ -326,8 +326,8 @@ impl<'a> PostQuery<'a> {
) )
.select(( .select((
post::all_columns, post::all_columns,
Person::safe_columns_tuple(), person::all_columns,
Community::safe_columns_tuple(), community::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
post_aggregates::all_columns, post_aggregates::all_columns,
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
@ -485,29 +485,26 @@ impl<'a> PostQuery<'a> {
let res = query.load::<PostViewTuple>(conn).await?; let res = query.load::<PostViewTuple>(conn).await?;
Ok(PostView::from_tuple_to_vec(res)) Ok(res.into_iter().map(PostView::from_tuple).collect())
} }
} }
impl ViewToVec for PostView { impl JoinView for PostView {
type DbTuple = PostViewTuple; type JoinTuple = PostViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() post: a.0,
.map(|a| Self { creator: a.1,
post: a.0, community: a.2,
creator: a.1, creator_banned_from_community: a.3.is_some(),
community: a.2, counts: a.4,
creator_banned_from_community: a.3.is_some(), subscribed: CommunityFollower::to_subscribed_type(&a.5),
counts: a.4, saved: a.6.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.5), read: a.7.is_some(),
saved: a.6.is_some(), creator_blocked: a.8.is_some(),
read: a.7.is_some(), my_vote: a.9,
creator_blocked: a.8.is_some(), unread_comments: a.10,
my_vote: a.9, }
unread_comments: a.10,
})
.collect::<Vec<Self>>()
} }
} }
@ -520,12 +517,12 @@ mod tests {
newtypes::LanguageId, newtypes::LanguageId,
source::{ source::{
actor_language::LocalUserLanguage, actor_language::LocalUserLanguage,
community::{Community, CommunityInsertForm, CommunitySafe}, community::{Community, CommunityInsertForm},
community_block::{CommunityBlock, CommunityBlockForm}, community_block::{CommunityBlock, CommunityBlockForm},
instance::Instance, instance::Instance,
language::Language, language::Language,
local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm}, local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
person::{Person, PersonInsertForm, PersonSafe}, person::{Person, PersonInsertForm},
person_block::{PersonBlock, PersonBlockForm}, person_block::{PersonBlock, PersonBlockForm},
post::{Post, PostInsertForm, PostLike, PostLikeForm}, post::{Post, PostInsertForm, PostLike, PostLikeForm},
}, },
@ -934,7 +931,7 @@ mod tests {
}, },
my_vote: None, my_vote: None,
unread_comments: 0, unread_comments: 0,
creator: PersonSafe { creator: Person {
id: inserted_person.id, id: inserted_person.id,
name: inserted_person.name.clone(), name: inserted_person.name.clone(),
display_name: None, display_name: None,
@ -954,9 +951,12 @@ mod tests {
matrix_user_id: None, matrix_user_id: None,
ban_expires: None, ban_expires: None,
instance_id: data.inserted_instance.id, 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, creator_banned_from_community: false,
community: CommunitySafe { community: Community {
id: inserted_community.id, id: inserted_community.id,
name: inserted_community.name.clone(), name: inserted_community.name.clone(),
icon: None, icon: None,
@ -973,6 +973,14 @@ mod tests {
posting_restricted_to_mods: false, posting_restricted_to_mods: false,
published: inserted_community.published, published: inserted_community.published,
instance_id: data.inserted_instance.id, 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 { counts: PostAggregates {
id: agg.id, id: agg.id,

View file

@ -5,11 +5,11 @@ use lemmy_db_schema::{
newtypes::PrivateMessageReportId, newtypes::PrivateMessageReportId,
schema::{person, private_message, private_message_report}, schema::{person, private_message, private_message_report},
source::{ source::{
person::{Person, PersonSafe}, person::Person,
private_message::PrivateMessage, private_message::PrivateMessage,
private_message_report::PrivateMessageReport, private_message_report::PrivateMessageReport,
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
@ -17,9 +17,9 @@ use typed_builder::TypedBuilder;
type PrivateMessageReportViewTuple = ( type PrivateMessageReportViewTuple = (
PrivateMessageReport, PrivateMessageReport,
PrivateMessage, PrivateMessage,
PersonSafe, Person,
PersonSafe, Person,
Option<PersonSafe>, Option<Person>,
); );
impl PrivateMessageReportView { impl PrivateMessageReportView {
@ -47,11 +47,9 @@ impl PrivateMessageReportView {
.select(( .select((
private_message_report::all_columns, private_message_report::all_columns,
private_message::all_columns, private_message::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
person_alias_2 person_alias_2.fields(person::all_columns).nullable(),
.fields(Person::safe_columns_tuple())
.nullable(),
)) ))
.first::<PrivateMessageReportViewTuple>(conn) .first::<PrivateMessageReportViewTuple>(conn)
.await?; .await?;
@ -108,11 +106,9 @@ impl<'a> PrivateMessageReportQuery<'a> {
.select(( .select((
private_message_report::all_columns, private_message_report::all_columns,
private_message::all_columns, private_message::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
person_alias_2 person_alias_2.fields(person::all_columns).nullable(),
.fields(Person::safe_columns_tuple())
.nullable(),
)) ))
.into_boxed(); .into_boxed();
@ -129,23 +125,25 @@ impl<'a> PrivateMessageReportQuery<'a> {
let res = query.load::<PrivateMessageReportViewTuple>(conn).await?; let res = query.load::<PrivateMessageReportViewTuple>(conn).await?;
Ok(PrivateMessageReportView::from_tuple_to_vec(res)) Ok(
res
.into_iter()
.map(PrivateMessageReportView::from_tuple)
.collect(),
)
} }
} }
impl ViewToVec for PrivateMessageReportView { impl JoinView for PrivateMessageReportView {
type DbTuple = PrivateMessageReportViewTuple; type JoinTuple = PrivateMessageReportViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() private_message_report: a.0,
.map(|a| Self { private_message: a.1,
private_message_report: a.0, private_message_creator: a.2,
private_message: a.1, creator: a.3,
private_message_creator: a.2, resolver: a.4,
creator: a.3, }
resolver: a.4,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,17 +12,14 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{PersonId, PrivateMessageId}, newtypes::{PersonId, PrivateMessageId},
schema::{person, private_message}, schema::{person, private_message},
source::{ source::{person::Person, private_message::PrivateMessage},
person::{Person, PersonSafe}, traits::JoinView,
private_message::PrivateMessage,
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use tracing::debug; use tracing::debug;
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
type PrivateMessageViewTuple = (PrivateMessage, PersonSafe, PersonSafe); type PrivateMessageViewTuple = (PrivateMessage, Person, Person);
impl PrivateMessageView { impl PrivateMessageView {
pub async fn read(pool: &DbPool, private_message_id: PrivateMessageId) -> Result<Self, Error> { pub async fn read(pool: &DbPool, private_message_id: PrivateMessageId) -> Result<Self, Error> {
@ -38,8 +35,8 @@ impl PrivateMessageView {
.order_by(private_message::published.desc()) .order_by(private_message::published.desc())
.select(( .select((
private_message::all_columns, private_message::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
)) ))
.first::<PrivateMessageViewTuple>(conn) .first::<PrivateMessageViewTuple>(conn)
.await?; .await?;
@ -89,8 +86,8 @@ impl<'a> PrivateMessageQuery<'a> {
) )
.select(( .select((
private_message::all_columns, private_message::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
)) ))
.into_boxed(); .into_boxed();
@ -124,20 +121,22 @@ impl<'a> PrivateMessageQuery<'a> {
let res = query.load::<PrivateMessageViewTuple>(conn).await?; let res = query.load::<PrivateMessageViewTuple>(conn).await?;
Ok(PrivateMessageView::from_tuple_to_vec(res)) Ok(
res
.into_iter()
.map(PrivateMessageView::from_tuple)
.collect(),
)
} }
} }
impl ViewToVec for PrivateMessageView { impl JoinView for PrivateMessageView {
type DbTuple = PrivateMessageViewTuple; type JoinTuple = PrivateMessageViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() private_message: a.0,
.map(|a| Self { creator: a.1,
private_message: a.0, recipient: a.2,
creator: a.1, }
recipient: a.2,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -11,21 +11,17 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
schema::{local_user, person, registration_application}, schema::{local_user, person, registration_application},
source::{ source::{
local_user::{LocalUser, LocalUserSettings}, local_user::LocalUser,
person::{Person, PersonSafe}, person::Person,
registration_application::RegistrationApplication, registration_application::RegistrationApplication,
}, },
traits::{ToSafe, ToSafeSettings, ViewToVec}, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
type RegistrationApplicationViewTuple = ( type RegistrationApplicationViewTuple =
RegistrationApplication, (RegistrationApplication, LocalUser, Person, Option<Person>);
LocalUserSettings,
PersonSafe,
Option<PersonSafe>,
);
impl RegistrationApplicationView { impl RegistrationApplicationView {
pub async fn read(pool: &DbPool, registration_application_id: i32) -> Result<Self, Error> { pub async fn read(pool: &DbPool, registration_application_id: i32) -> Result<Self, Error> {
@ -46,11 +42,9 @@ impl RegistrationApplicationView {
.order_by(registration_application::published.desc()) .order_by(registration_application::published.desc())
.select(( .select((
registration_application::all_columns, registration_application::all_columns,
LocalUser::safe_settings_columns_tuple(), local_user::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1 person_alias_1.fields(person::all_columns).nullable(),
.fields(Person::safe_columns_tuple())
.nullable(),
)) ))
.first::<RegistrationApplicationViewTuple>(conn) .first::<RegistrationApplicationViewTuple>(conn)
.await?; .await?;
@ -115,11 +109,9 @@ impl<'a> RegistrationApplicationQuery<'a> {
.order_by(registration_application::published.desc()) .order_by(registration_application::published.desc())
.select(( .select((
registration_application::all_columns, registration_application::all_columns,
LocalUser::safe_settings_columns_tuple(), local_user::all_columns,
Person::safe_columns_tuple(), person::all_columns,
person_alias_1 person_alias_1.fields(person::all_columns).nullable(),
.fields(Person::safe_columns_tuple())
.nullable(),
)) ))
.into_boxed(); .into_boxed();
@ -140,22 +132,24 @@ impl<'a> RegistrationApplicationQuery<'a> {
let res = query.load::<RegistrationApplicationViewTuple>(conn).await?; let res = query.load::<RegistrationApplicationViewTuple>(conn).await?;
Ok(RegistrationApplicationView::from_tuple_to_vec(res)) Ok(
res
.into_iter()
.map(RegistrationApplicationView::from_tuple)
.collect(),
)
} }
} }
impl ViewToVec for RegistrationApplicationView { impl JoinView for RegistrationApplicationView {
type DbTuple = RegistrationApplicationViewTuple; type JoinTuple = RegistrationApplicationViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() registration_application: a.0,
.map(|a| Self { creator_local_user: a.1,
registration_application: a.0, creator: a.2,
creator_local_user: a.1, admin: a.3,
creator: a.2, }
admin: a.3,
})
.collect::<Vec<Self>>()
} }
} }
@ -168,8 +162,8 @@ mod tests {
use lemmy_db_schema::{ use lemmy_db_schema::{
source::{ source::{
instance::Instance, instance::Instance,
local_user::{LocalUser, LocalUserInsertForm, LocalUserSettings, LocalUserUpdateForm}, local_user::{LocalUser, LocalUserInsertForm, LocalUserUpdateForm},
person::{Person, PersonInsertForm, PersonSafe}, person::{Person, PersonInsertForm},
registration_application::{ registration_application::{
RegistrationApplication, RegistrationApplication,
RegistrationApplicationInsertForm, RegistrationApplicationInsertForm,
@ -272,7 +266,7 @@ mod tests {
let mut expected_sara_app_view = RegistrationApplicationView { let mut expected_sara_app_view = RegistrationApplicationView {
registration_application: sara_app.clone(), registration_application: sara_app.clone(),
creator_local_user: LocalUserSettings { creator_local_user: LocalUser {
id: inserted_sara_local_user.id, id: inserted_sara_local_user.id,
person_id: inserted_sara_local_user.person_id, person_id: inserted_sara_local_user.person_id,
email: inserted_sara_local_user.email, email: inserted_sara_local_user.email,
@ -290,8 +284,9 @@ mod tests {
show_new_post_notifs: inserted_sara_local_user.show_new_post_notifs, show_new_post_notifs: inserted_sara_local_user.show_new_post_notifs,
email_verified: inserted_sara_local_user.email_verified, email_verified: inserted_sara_local_user.email_verified,
accepted_application: inserted_sara_local_user.accepted_application, 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, id: inserted_sara_person.id,
name: inserted_sara_person.name.clone(), name: inserted_sara_person.name.clone(),
display_name: None, display_name: None,
@ -311,6 +306,9 @@ mod tests {
shared_inbox_url: None, shared_inbox_url: None,
matrix_user_id: None, matrix_user_id: None,
instance_id: inserted_instance.id, 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, admin: None,
}; };
@ -366,7 +364,7 @@ mod tests {
.accepted_application = true; .accepted_application = true;
expected_sara_app_view.registration_application.admin_id = Some(inserted_timmy_person.id); 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, id: inserted_timmy_person.id,
name: inserted_timmy_person.name.clone(), name: inserted_timmy_person.name.clone(),
display_name: None, display_name: None,
@ -386,6 +384,9 @@ mod tests {
shared_inbox_url: None, shared_inbox_url: None,
matrix_user_id: None, matrix_user_id: None,
instance_id: inserted_instance.id, 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); assert_eq!(read_sara_app_view_after_approve, expected_sara_app_view);

View file

@ -3,11 +3,11 @@ use lemmy_db_schema::{
source::{ source::{
comment::Comment, comment::Comment,
comment_report::CommentReport, comment_report::CommentReport,
community::CommunitySafe, community::Community,
local_site::LocalSite, local_site::LocalSite,
local_site_rate_limit::LocalSiteRateLimit, local_site_rate_limit::LocalSiteRateLimit,
local_user::{LocalUser, LocalUserSettings}, local_user::LocalUser,
person::{Person, PersonSafe}, person::Person,
post::Post, post::Post,
post_report::PostReport, post_report::PostReport,
private_message::PrivateMessage, private_message::PrivateMessage,
@ -24,21 +24,21 @@ pub struct CommentReportView {
pub comment_report: CommentReport, pub comment_report: CommentReport,
pub comment: Comment, pub comment: Comment,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
pub creator: PersonSafe, pub creator: Person,
pub comment_creator: PersonSafe, pub comment_creator: Person,
pub counts: CommentAggregates, pub counts: CommentAggregates,
pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
pub my_vote: Option<i16>, // Left join to CommentLike pub my_vote: Option<i16>, // Left join to CommentLike
pub resolver: Option<PersonSafe>, pub resolver: Option<Person>,
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct CommentView { pub struct CommentView {
pub comment: Comment, pub comment: Comment,
pub creator: PersonSafe, pub creator: Person,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
pub counts: CommentAggregates, pub counts: CommentAggregates,
pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
pub subscribed: SubscribedType, // Left join to CommunityFollower pub subscribed: SubscribedType, // Left join to CommunityFollower
@ -54,31 +54,24 @@ pub struct LocalUserView {
pub counts: PersonAggregates, 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)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct PostReportView { pub struct PostReportView {
pub post_report: PostReport, pub post_report: PostReport,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
pub creator: PersonSafe, pub creator: Person,
pub post_creator: PersonSafe, pub post_creator: Person,
pub creator_banned_from_community: bool, pub creator_banned_from_community: bool,
pub my_vote: Option<i16>, pub my_vote: Option<i16>,
pub counts: PostAggregates, pub counts: PostAggregates,
pub resolver: Option<PersonSafe>, pub resolver: Option<Person>,
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct PostView { pub struct PostView {
pub post: Post, pub post: Post,
pub creator: PersonSafe, pub creator: Person,
pub community: CommunitySafe, pub community: Community,
pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
pub counts: PostAggregates, pub counts: PostAggregates,
pub subscribed: SubscribedType, // Left join to CommunityFollower pub subscribed: SubscribedType, // Left join to CommunityFollower
@ -92,25 +85,25 @@ pub struct PostView {
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct PrivateMessageView { pub struct PrivateMessageView {
pub private_message: PrivateMessage, pub private_message: PrivateMessage,
pub creator: PersonSafe, pub creator: Person,
pub recipient: PersonSafe, pub recipient: Person,
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct PrivateMessageReportView { pub struct PrivateMessageReportView {
pub private_message_report: PrivateMessageReport, pub private_message_report: PrivateMessageReport,
pub private_message: PrivateMessage, pub private_message: PrivateMessage,
pub private_message_creator: PersonSafe, pub private_message_creator: Person,
pub creator: PersonSafe, pub creator: Person,
pub resolver: Option<PersonSafe>, pub resolver: Option<Person>,
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct RegistrationApplicationView { pub struct RegistrationApplicationView {
pub registration_application: RegistrationApplication, pub registration_application: RegistrationApplication,
pub creator_local_user: LocalUserSettings, pub creator_local_user: LocalUser,
pub creator: PersonSafe, pub creator: Person,
pub admin: Option<PersonSafe>, pub admin: Option<Person>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]

View file

@ -28,12 +28,12 @@ use lemmy_db_schema::{
source::{ source::{
comment::{Comment, CommentSaved}, comment::{Comment, CommentSaved},
comment_reply::CommentReply, comment_reply::CommentReply,
community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, community::{Community, CommunityFollower, CommunityPersonBan},
person::{Person, PersonSafe}, person::Person,
person_block::PersonBlock, person_block::PersonBlock,
post::Post, post::Post,
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool}, utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool},
CommentSortType, CommentSortType,
}; };
@ -42,10 +42,10 @@ use typed_builder::TypedBuilder;
type CommentReplyViewTuple = ( type CommentReplyViewTuple = (
CommentReply, CommentReply,
Comment, Comment,
PersonSafe, Person,
Post, Post,
CommunitySafe, Community,
PersonSafe, Person,
CommentAggregates, CommentAggregates,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<CommunityFollower>, Option<CommunityFollower>,
@ -130,10 +130,10 @@ impl CommentReplyView {
.select(( .select((
comment_reply::all_columns, comment_reply::all_columns,
comment::all_columns, comment::all_columns,
Person::safe_columns_tuple(), person::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
@ -251,10 +251,10 @@ impl<'a> CommentReplyQuery<'a> {
.select(( .select((
comment_reply::all_columns, comment_reply::all_columns,
comment::all_columns, comment::all_columns,
Person::safe_columns_tuple(), person::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
@ -293,29 +293,26 @@ impl<'a> CommentReplyQuery<'a> {
.load::<CommentReplyViewTuple>(conn) .load::<CommentReplyViewTuple>(conn)
.await?; .await?;
Ok(CommentReplyView::from_tuple_to_vec(res)) Ok(res.into_iter().map(CommentReplyView::from_tuple).collect())
} }
} }
impl ViewToVec for CommentReplyView { impl JoinView for CommentReplyView {
type DbTuple = CommentReplyViewTuple; type JoinTuple = CommentReplyViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() comment_reply: a.0,
.map(|a| Self { comment: a.1,
comment_reply: a.0, creator: a.2,
comment: a.1, post: a.3,
creator: a.2, community: a.4,
post: a.3, recipient: a.5,
community: a.4, counts: a.6,
recipient: a.5, creator_banned_from_community: a.7.is_some(),
counts: a.6, subscribed: CommunityFollower::to_subscribed_type(&a.8),
creator_banned_from_community: a.7.is_some(), saved: a.9.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.8), creator_blocked: a.10.is_some(),
saved: a.9.is_some(), my_vote: a.11,
creator_blocked: a.10.is_some(), }
my_vote: a.11,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -4,15 +4,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, community_block, person}, schema::{community, community_block, person},
source::{ source::{community::Community, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
}; };
type CommunityBlockViewTuple = (PersonSafe, CommunitySafe); type CommunityBlockViewTuple = (Person, Community);
impl CommunityBlockView { impl CommunityBlockView {
pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> { pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> {
@ -20,10 +17,7 @@ impl CommunityBlockView {
let res = community_block::table let res = community_block::table
.inner_join(person::table) .inner_join(person::table)
.inner_join(community::table) .inner_join(community::table)
.select(( .select((person::all_columns, community::all_columns))
Person::safe_columns_tuple(),
Community::safe_columns_tuple(),
))
.filter(community_block::person_id.eq(person_id)) .filter(community_block::person_id.eq(person_id))
.filter(community::deleted.eq(false)) .filter(community::deleted.eq(false))
.filter(community::removed.eq(false)) .filter(community::removed.eq(false))
@ -31,19 +25,16 @@ impl CommunityBlockView {
.load::<CommunityBlockViewTuple>(conn) .load::<CommunityBlockViewTuple>(conn)
.await?; .await?;
Ok(Self::from_tuple_to_vec(res)) Ok(res.into_iter().map(Self::from_tuple).collect())
} }
} }
impl ViewToVec for CommunityBlockView { impl JoinView for CommunityBlockView {
type DbTuple = CommunityBlockViewTuple; type JoinTuple = CommunityBlockViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() person: a.0,
.map(|a| Self { community: a.1,
person: a.0, }
community: a.1,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -4,15 +4,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommunityId, PersonId}, newtypes::{CommunityId, PersonId},
schema::{community, community_follower, person}, schema::{community, community_follower, person},
source::{ source::{community::Community, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
}; };
type CommunityFollowerViewTuple = (CommunitySafe, PersonSafe); type CommunityFollowerViewTuple = (Community, Person);
impl CommunityFollowerView { impl CommunityFollowerView {
pub async fn for_community(pool: &DbPool, community_id: CommunityId) -> Result<Vec<Self>, Error> { pub async fn for_community(pool: &DbPool, community_id: CommunityId) -> Result<Vec<Self>, Error> {
@ -20,16 +17,13 @@ impl CommunityFollowerView {
let res = community_follower::table let res = community_follower::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(person::table) .inner_join(person::table)
.select(( .select((community::all_columns, person::all_columns))
Community::safe_columns_tuple(),
Person::safe_columns_tuple(),
))
.filter(community_follower::community_id.eq(community_id)) .filter(community_follower::community_id.eq(community_id))
.order_by(community::title) .order_by(community::title)
.load::<CommunityFollowerViewTuple>(conn) .load::<CommunityFollowerViewTuple>(conn)
.await?; .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<Vec<Self>, Error> { pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> {
@ -37,10 +31,7 @@ impl CommunityFollowerView {
let res = community_follower::table let res = community_follower::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(person::table) .inner_join(person::table)
.select(( .select((community::all_columns, person::all_columns))
Community::safe_columns_tuple(),
Person::safe_columns_tuple(),
))
.filter(community_follower::person_id.eq(person_id)) .filter(community_follower::person_id.eq(person_id))
.filter(community::deleted.eq(false)) .filter(community::deleted.eq(false))
.filter(community::removed.eq(false)) .filter(community::removed.eq(false))
@ -48,19 +39,16 @@ impl CommunityFollowerView {
.load::<CommunityFollowerViewTuple>(conn) .load::<CommunityFollowerViewTuple>(conn)
.await?; .await?;
Ok(Self::from_tuple_to_vec(res)) Ok(res.into_iter().map(Self::from_tuple).collect())
} }
} }
impl ViewToVec for CommunityFollowerView { impl JoinView for CommunityFollowerView {
type DbTuple = CommunityFollowerViewTuple; type JoinTuple = CommunityFollowerViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() community: a.0,
.map(|a| Self { follower: a.1,
community: a.0, }
follower: a.1,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -4,15 +4,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommunityId, PersonId}, newtypes::{CommunityId, PersonId},
schema::{community, community_moderator, person}, schema::{community, community_moderator, person},
source::{ source::{community::Community, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
}; };
type CommunityModeratorViewTuple = (CommunitySafe, PersonSafe); type CommunityModeratorViewTuple = (Community, Person);
impl CommunityModeratorView { impl CommunityModeratorView {
pub async fn for_community(pool: &DbPool, community_id: CommunityId) -> Result<Vec<Self>, Error> { pub async fn for_community(pool: &DbPool, community_id: CommunityId) -> Result<Vec<Self>, Error> {
@ -20,16 +17,13 @@ impl CommunityModeratorView {
let res = community_moderator::table let res = community_moderator::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(person::table) .inner_join(person::table)
.select(( .select((community::all_columns, person::all_columns))
Community::safe_columns_tuple(),
Person::safe_columns_tuple(),
))
.filter(community_moderator::community_id.eq(community_id)) .filter(community_moderator::community_id.eq(community_id))
.order_by(community_moderator::published) .order_by(community_moderator::published)
.load::<CommunityModeratorViewTuple>(conn) .load::<CommunityModeratorViewTuple>(conn)
.await?; .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<Vec<Self>, Error> { pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> {
@ -37,10 +31,7 @@ impl CommunityModeratorView {
let res = community_moderator::table let res = community_moderator::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(person::table) .inner_join(person::table)
.select(( .select((community::all_columns, person::all_columns))
Community::safe_columns_tuple(),
Person::safe_columns_tuple(),
))
.filter(community_moderator::person_id.eq(person_id)) .filter(community_moderator::person_id.eq(person_id))
.filter(community::deleted.eq(false)) .filter(community::deleted.eq(false))
.filter(community::removed.eq(false)) .filter(community::removed.eq(false))
@ -48,7 +39,7 @@ impl CommunityModeratorView {
.load::<CommunityModeratorViewTuple>(conn) .load::<CommunityModeratorViewTuple>(conn)
.await?; .await?;
Ok(Self::from_tuple_to_vec(res)) Ok(res.into_iter().map(Self::from_tuple).collect())
} }
/// Finds all communities first mods / creators /// Finds all communities first mods / creators
@ -58,10 +49,7 @@ impl CommunityModeratorView {
let res = community_moderator::table let res = community_moderator::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(person::table) .inner_join(person::table)
.select(( .select((community::all_columns, person::all_columns))
Community::safe_columns_tuple(),
Person::safe_columns_tuple(),
))
// A hacky workaround instead of group_bys // A hacky workaround instead of group_bys
// https://stackoverflow.com/questions/24042359/how-to-join-only-one-row-in-joined-table-with-postgres // https://stackoverflow.com/questions/24042359/how-to-join-only-one-row-in-joined-table-with-postgres
.distinct_on(community_moderator::community_id) .distinct_on(community_moderator::community_id)
@ -72,19 +60,16 @@ impl CommunityModeratorView {
.load::<CommunityModeratorViewTuple>(conn) .load::<CommunityModeratorViewTuple>(conn)
.await?; .await?;
Ok(Self::from_tuple_to_vec(res)) Ok(res.into_iter().map(Self::from_tuple).collect())
} }
} }
impl ViewToVec for CommunityModeratorView { impl JoinView for CommunityModeratorView {
type DbTuple = CommunityModeratorViewTuple; type JoinTuple = CommunityModeratorViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() community: a.0,
.map(|a| Self { moderator: a.1,
community: a.0, }
moderator: a.1,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -4,11 +4,7 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::{CommunityId, PersonId}, newtypes::{CommunityId, PersonId},
schema::{community, community_person_ban, person}, schema::{community, community_person_ban, person},
source::{ source::{community::Community, person::Person},
community::{Community, CommunitySafe},
person::{Person, PersonSafe},
},
traits::ToSafe,
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
}; };
@ -22,10 +18,7 @@ impl CommunityPersonBanView {
let (community, person) = community_person_ban::table let (community, person) = community_person_ban::table
.inner_join(community::table) .inner_join(community::table)
.inner_join(person::table) .inner_join(person::table)
.select(( .select((community::all_columns, person::all_columns))
Community::safe_columns_tuple(),
Person::safe_columns_tuple(),
))
.filter(community_person_ban::community_id.eq(from_community_id)) .filter(community_person_ban::community_id.eq(from_community_id))
.filter(community_person_ban::person_id.eq(from_person_id)) .filter(community_person_ban::person_id.eq(from_person_id))
.filter( .filter(
@ -34,7 +27,7 @@ impl CommunityPersonBanView {
.or(community_person_ban::expires.gt(now)), .or(community_person_ban::expires.gt(now)),
) )
.order_by(community_person_ban::published) .order_by(community_person_ban::published)
.first::<(CommunitySafe, PersonSafe)>(conn) .first::<(Community, Person)>(conn)
.await?; .await?;
Ok(CommunityPersonBanView { community, person }) Ok(CommunityPersonBanView { community, person })

View file

@ -1,4 +1,4 @@
use crate::structs::{CommunityModeratorView, CommunityView, PersonViewSafe}; use crate::structs::{CommunityModeratorView, CommunityView, PersonView};
use diesel::{ use diesel::{
result::Error, result::Error,
BoolExpressionMethods, BoolExpressionMethods,
@ -14,11 +14,11 @@ use lemmy_db_schema::{
newtypes::{CommunityId, PersonId}, newtypes::{CommunityId, PersonId},
schema::{community, community_aggregates, community_block, community_follower, local_user}, schema::{community, community_aggregates, community_block, community_follower, local_user},
source::{ source::{
community::{Community, CommunityFollower, CommunitySafe}, community::{Community, CommunityFollower},
community_block::CommunityBlock, community_block::CommunityBlock,
local_user::LocalUser, local_user::LocalUser,
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{fuzzy_search, get_conn, limit_and_offset, DbPool}, utils::{fuzzy_search, get_conn, limit_and_offset, DbPool},
ListingType, ListingType,
SortType, SortType,
@ -26,7 +26,7 @@ use lemmy_db_schema::{
use typed_builder::TypedBuilder; use typed_builder::TypedBuilder;
type CommunityViewTuple = ( type CommunityViewTuple = (
CommunitySafe, Community,
CommunityAggregates, CommunityAggregates,
Option<CommunityFollower>, Option<CommunityFollower>,
Option<CommunityBlock>, Option<CommunityBlock>,
@ -61,7 +61,7 @@ impl CommunityView {
), ),
) )
.select(( .select((
Community::safe_columns_tuple(), community::all_columns,
community_aggregates::all_columns, community_aggregates::all_columns,
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
community_block::all_columns.nullable(), community_block::all_columns.nullable(),
@ -103,7 +103,7 @@ impl CommunityView {
return Ok(true); return Ok(true);
} }
let is_admin = PersonViewSafe::admins(pool) let is_admin = PersonView::admins(pool)
.await .await
.map(|v| { .map(|v| {
v.into_iter() v.into_iter()
@ -155,7 +155,7 @@ impl<'a> CommunityQuery<'a> {
), ),
) )
.select(( .select((
Community::safe_columns_tuple(), community::all_columns,
community_aggregates::all_columns, community_aggregates::all_columns,
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
community_block::all_columns.nullable(), community_block::all_columns.nullable(),
@ -221,21 +221,18 @@ impl<'a> CommunityQuery<'a> {
.load::<CommunityViewTuple>(conn) .load::<CommunityViewTuple>(conn)
.await?; .await?;
Ok(CommunityView::from_tuple_to_vec(res)) Ok(res.into_iter().map(CommunityView::from_tuple).collect())
} }
} }
impl ViewToVec for CommunityView { impl JoinView for CommunityView {
type DbTuple = CommunityViewTuple; type JoinTuple = CommunityViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() community: a.0,
.map(|a| Self { counts: a.1,
community: a.0, subscribed: CommunityFollower::to_subscribed_type(&a.2),
counts: a.1, blocked: a.3.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.2), }
blocked: a.3.is_some(),
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -4,12 +4,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{person, person_block}, schema::{person, person_block},
source::person::{Person, PersonSafe}, source::person::Person,
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{get_conn, DbPool}, utils::{get_conn, DbPool},
}; };
type PersonBlockViewTuple = (PersonSafe, PersonSafe); type PersonBlockViewTuple = (Person, Person);
impl PersonBlockView { impl PersonBlockView {
pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> { pub async fn for_person(pool: &DbPool, person_id: PersonId) -> Result<Vec<Self>, Error> {
@ -22,8 +22,8 @@ impl PersonBlockView {
target_person_alias.on(person_block::target_id.eq(target_person_alias.field(person::id))), target_person_alias.on(person_block::target_id.eq(target_person_alias.field(person::id))),
) )
.select(( .select((
Person::safe_columns_tuple(), person::all_columns,
target_person_alias.fields(Person::safe_columns_tuple()), target_person_alias.fields(person::all_columns),
)) ))
.filter(person_block::person_id.eq(person_id)) .filter(person_block::person_id.eq(person_id))
.filter(target_person_alias.field(person::deleted).eq(false)) .filter(target_person_alias.field(person::deleted).eq(false))
@ -31,19 +31,16 @@ impl PersonBlockView {
.load::<PersonBlockViewTuple>(conn) .load::<PersonBlockViewTuple>(conn)
.await?; .await?;
Ok(Self::from_tuple_to_vec(res)) Ok(res.into_iter().map(Self::from_tuple).collect())
} }
} }
impl ViewToVec for PersonBlockView { impl JoinView for PersonBlockView {
type DbTuple = PersonBlockViewTuple; type JoinTuple = PersonBlockViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() person: a.0,
.map(|a| Self { target: a.1,
person: a.0, }
target: a.1,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -27,13 +27,13 @@ use lemmy_db_schema::{
}, },
source::{ source::{
comment::{Comment, CommentSaved}, comment::{Comment, CommentSaved},
community::{Community, CommunityFollower, CommunityPersonBan, CommunitySafe}, community::{Community, CommunityFollower, CommunityPersonBan},
person::{Person, PersonSafe}, person::Person,
person_block::PersonBlock, person_block::PersonBlock,
person_mention::PersonMention, person_mention::PersonMention,
post::Post, post::Post,
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool}, utils::{functions::hot_rank, get_conn, limit_and_offset, DbPool},
CommentSortType, CommentSortType,
}; };
@ -42,10 +42,10 @@ use typed_builder::TypedBuilder;
type PersonMentionViewTuple = ( type PersonMentionViewTuple = (
PersonMention, PersonMention,
Comment, Comment,
PersonSafe, Person,
Post, Post,
CommunitySafe, Community,
PersonSafe, Person,
CommentAggregates, CommentAggregates,
Option<CommunityPersonBan>, Option<CommunityPersonBan>,
Option<CommunityFollower>, Option<CommunityFollower>,
@ -130,10 +130,10 @@ impl PersonMentionView {
.select(( .select((
person_mention::all_columns, person_mention::all_columns,
comment::all_columns, comment::all_columns,
Person::safe_columns_tuple(), person::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
@ -250,10 +250,10 @@ impl<'a> PersonMentionQuery<'a> {
.select(( .select((
person_mention::all_columns, person_mention::all_columns,
comment::all_columns, comment::all_columns,
Person::safe_columns_tuple(), person::all_columns,
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
comment_aggregates::all_columns, comment_aggregates::all_columns,
community_person_ban::all_columns.nullable(), community_person_ban::all_columns.nullable(),
community_follower::all_columns.nullable(), community_follower::all_columns.nullable(),
@ -292,29 +292,26 @@ impl<'a> PersonMentionQuery<'a> {
.load::<PersonMentionViewTuple>(conn) .load::<PersonMentionViewTuple>(conn)
.await?; .await?;
Ok(PersonMentionView::from_tuple_to_vec(res)) Ok(res.into_iter().map(PersonMentionView::from_tuple).collect())
} }
} }
impl ViewToVec for PersonMentionView { impl JoinView for PersonMentionView {
type DbTuple = PersonMentionViewTuple; type JoinTuple = PersonMentionViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() person_mention: a.0,
.map(|a| Self { comment: a.1,
person_mention: a.0, creator: a.2,
comment: a.1, post: a.3,
creator: a.2, community: a.4,
post: a.3, recipient: a.5,
community: a.4, counts: a.6,
recipient: a.5, creator_banned_from_community: a.7.is_some(),
counts: a.6, subscribed: CommunityFollower::to_subscribed_type(&a.8),
creator_banned_from_community: a.7.is_some(), saved: a.9.is_some(),
subscribed: CommunityFollower::to_subscribed_type(&a.8), creator_blocked: a.10.is_some(),
saved: a.9.is_some(), my_vote: a.11,
creator_blocked: a.10.is_some(), }
my_vote: a.11,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -1,4 +1,4 @@
use crate::structs::PersonViewSafe; use crate::structs::PersonView;
use diesel::{ use diesel::{
dsl::{now, IntervalDsl}, dsl::{now, IntervalDsl},
result::Error, result::Error,
@ -12,47 +12,47 @@ use lemmy_db_schema::{
aggregates::structs::PersonAggregates, aggregates::structs::PersonAggregates,
newtypes::PersonId, newtypes::PersonId,
schema::{person, person_aggregates}, schema::{person, person_aggregates},
source::person::{Person, PersonSafe}, source::person::Person,
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{fuzzy_search, get_conn, limit_and_offset, DbPool}, utils::{fuzzy_search, get_conn, limit_and_offset, DbPool},
SortType, SortType,
}; };
use std::iter::Iterator; use std::iter::Iterator;
use typed_builder::TypedBuilder; 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<Self, Error> { pub async fn read(pool: &DbPool, person_id: PersonId) -> Result<Self, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
let (person, counts) = person::table let res = person::table
.find(person_id) .find(person_id)
.inner_join(person_aggregates::table) .inner_join(person_aggregates::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns)) .select((person::all_columns, person_aggregates::all_columns))
.first::<PersonViewSafeTuple>(conn) .first::<PersonViewTuple>(conn)
.await?; .await?;
Ok(Self { person, counts }) Ok(Self::from_tuple(res))
} }
pub async fn admins(pool: &DbPool) -> Result<Vec<Self>, Error> { pub async fn admins(pool: &DbPool) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
let admins = person::table let admins = person::table
.inner_join(person_aggregates::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::admin.eq(true))
.filter(person::deleted.eq(false)) .filter(person::deleted.eq(false))
.order_by(person::published) .order_by(person::published)
.load::<PersonViewSafeTuple>(conn) .load::<PersonViewTuple>(conn)
.await?; .await?;
Ok(Self::from_tuple_to_vec(admins)) Ok(admins.into_iter().map(Self::from_tuple).collect())
} }
pub async fn banned(pool: &DbPool) -> Result<Vec<Self>, Error> { pub async fn banned(pool: &DbPool) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
let banned = person::table let banned = person::table
.inner_join(person_aggregates::table) .inner_join(person_aggregates::table)
.select((Person::safe_columns_tuple(), person_aggregates::all_columns)) .select((person::all_columns, person_aggregates::all_columns))
.filter( .filter(
person::banned.eq(true).and( person::banned.eq(true).and(
person::ban_expires person::ban_expires
@ -61,10 +61,10 @@ impl PersonViewSafe {
), ),
) )
.filter(person::deleted.eq(false)) .filter(person::deleted.eq(false))
.load::<PersonViewSafeTuple>(conn) .load::<PersonViewTuple>(conn)
.await?; .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> { impl<'a> PersonQuery<'a> {
pub async fn list(self) -> Result<Vec<PersonViewSafe>, Error> { pub async fn list(self) -> Result<Vec<PersonView>, Error> {
let conn = &mut get_conn(self.pool).await?; let conn = &mut get_conn(self.pool).await?;
let mut query = person::table let mut query = person::table
.inner_join(person_aggregates::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(); .into_boxed();
if let Some(search_term) = self.search_term { 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)?; let (limit, offset) = limit_and_offset(self.page, self.limit)?;
query = query.limit(limit).offset(offset); query = query.limit(limit).offset(offset);
let res = query.load::<PersonViewSafeTuple>(conn).await?; let res = query.load::<PersonViewTuple>(conn).await?;
Ok(PersonViewSafe::from_tuple_to_vec(res)) Ok(res.into_iter().map(PersonView::from_tuple).collect())
} }
} }
impl ViewToVec for PersonViewSafe { impl JoinView for PersonView {
type DbTuple = PersonViewSafeTuple; type JoinTuple = PersonViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() person: a.0,
.map(|a| Self { counts: a.1,
person: a.0, }
counts: a.1,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -3,8 +3,8 @@ use lemmy_db_schema::{
source::{ source::{
comment::Comment, comment::Comment,
comment_reply::CommentReply, comment_reply::CommentReply,
community::CommunitySafe, community::Community,
person::PersonSafe, person::Person,
person_mention::PersonMention, person_mention::PersonMention,
post::Post, post::Post,
}, },
@ -14,31 +14,31 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CommunityBlockView { pub struct CommunityBlockView {
pub person: PersonSafe, pub person: Person,
pub community: CommunitySafe, pub community: Community,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CommunityFollowerView { pub struct CommunityFollowerView {
pub community: CommunitySafe, pub community: Community,
pub follower: PersonSafe, pub follower: Person,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CommunityModeratorView { pub struct CommunityModeratorView {
pub community: CommunitySafe, pub community: Community,
pub moderator: PersonSafe, pub moderator: Person,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CommunityPersonBanView { pub struct CommunityPersonBanView {
pub community: CommunitySafe, pub community: Community,
pub person: PersonSafe, pub person: Person,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CommunityView { pub struct CommunityView {
pub community: CommunitySafe, pub community: Community,
pub subscribed: SubscribedType, pub subscribed: SubscribedType,
pub blocked: bool, pub blocked: bool,
pub counts: CommunityAggregates, pub counts: CommunityAggregates,
@ -46,18 +46,18 @@ pub struct CommunityView {
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PersonBlockView { pub struct PersonBlockView {
pub person: PersonSafe, pub person: Person,
pub target: PersonSafe, pub target: Person,
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)] #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone)]
pub struct PersonMentionView { pub struct PersonMentionView {
pub person_mention: PersonMention, pub person_mention: PersonMention,
pub comment: Comment, pub comment: Comment,
pub creator: PersonSafe, pub creator: Person,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
pub recipient: PersonSafe, pub recipient: Person,
pub counts: CommentAggregates, pub counts: CommentAggregates,
pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
pub subscribed: SubscribedType, // Left join to CommunityFollower pub subscribed: SubscribedType, // Left join to CommunityFollower
@ -70,10 +70,10 @@ pub struct PersonMentionView {
pub struct CommentReplyView { pub struct CommentReplyView {
pub comment_reply: CommentReply, pub comment_reply: CommentReply,
pub comment: Comment, pub comment: Comment,
pub creator: PersonSafe, pub creator: Person,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
pub recipient: PersonSafe, pub recipient: Person,
pub counts: CommentAggregates, pub counts: CommentAggregates,
pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan pub creator_banned_from_community: bool, // Left Join to CommunityPersonBan
pub subscribed: SubscribedType, // Left join to CommunityFollower pub subscribed: SubscribedType, // Left join to CommunityFollower
@ -83,7 +83,7 @@ pub struct CommentReplyView {
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct PersonViewSafe { pub struct PersonView {
pub person: PersonSafe, pub person: Person,
pub counts: PersonAggregates, pub counts: PersonAggregates,
} }

View file

@ -12,16 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{admin_purge_comment, person, post}, schema::{admin_purge_comment, person, post},
source::{ source::{moderator::AdminPurgeComment, person::Person, post::Post},
moderator::AdminPurgeComment, traits::JoinView,
person::{Person, PersonSafe},
post::Post,
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type AdminPurgeCommentViewTuple = (AdminPurgeComment, Option<PersonSafe>, Post); type AdminPurgeCommentViewTuple = (AdminPurgeComment, Option<Person>, Post);
impl AdminPurgeCommentView { impl AdminPurgeCommentView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -39,7 +35,7 @@ impl AdminPurgeCommentView {
.inner_join(post::table) .inner_join(post::table)
.select(( .select((
admin_purge_comment::all_columns, admin_purge_comment::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
post::all_columns, post::all_columns,
)) ))
.into_boxed(); .into_boxed();
@ -57,21 +53,18 @@ impl AdminPurgeCommentView {
.load::<AdminPurgeCommentViewTuple>(conn) .load::<AdminPurgeCommentViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for AdminPurgeCommentView { impl JoinView for AdminPurgeCommentView {
type DbTuple = AdminPurgeCommentViewTuple; type JoinTuple = AdminPurgeCommentViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() admin_purge_comment: a.0,
.map(|a| Self { admin: a.1,
admin_purge_comment: a.0, post: a.2,
admin: a.1, }
post: a.2,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,15 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{admin_purge_community, person}, schema::{admin_purge_community, person},
source::{ source::{moderator::AdminPurgeCommunity, person::Person},
moderator::AdminPurgeCommunity, traits::JoinView,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, Option<PersonSafe>); type AdminPurgeCommunityViewTuple = (AdminPurgeCommunity, Option<Person>);
impl AdminPurgeCommunityView { impl AdminPurgeCommunityView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -37,7 +34,7 @@ impl AdminPurgeCommunityView {
.left_join(person::table.on(admin_names_join)) .left_join(person::table.on(admin_names_join))
.select(( .select((
admin_purge_community::all_columns, admin_purge_community::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
)) ))
.into_boxed(); .into_boxed();
@ -54,20 +51,17 @@ impl AdminPurgeCommunityView {
.load::<AdminPurgeCommunityViewTuple>(conn) .load::<AdminPurgeCommunityViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for AdminPurgeCommunityView { impl JoinView for AdminPurgeCommunityView {
type DbTuple = AdminPurgeCommunityViewTuple; type JoinTuple = AdminPurgeCommunityViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() admin_purge_community: a.0,
.map(|a| Self { admin: a.1,
admin_purge_community: a.0, }
admin: a.1,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,15 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{admin_purge_person, person}, schema::{admin_purge_person, person},
source::{ source::{moderator::AdminPurgePerson, person::Person},
moderator::AdminPurgePerson, traits::JoinView,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type AdminPurgePersonViewTuple = (AdminPurgePerson, Option<PersonSafe>); type AdminPurgePersonViewTuple = (AdminPurgePerson, Option<Person>);
impl AdminPurgePersonView { impl AdminPurgePersonView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -37,7 +34,7 @@ impl AdminPurgePersonView {
.left_join(person::table.on(admin_names_join)) .left_join(person::table.on(admin_names_join))
.select(( .select((
admin_purge_person::all_columns, admin_purge_person::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
)) ))
.into_boxed(); .into_boxed();
@ -54,20 +51,17 @@ impl AdminPurgePersonView {
.load::<AdminPurgePersonViewTuple>(conn) .load::<AdminPurgePersonViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for AdminPurgePersonView { impl JoinView for AdminPurgePersonView {
type DbTuple = AdminPurgePersonViewTuple; type JoinTuple = AdminPurgePersonViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() admin_purge_person: a.0,
.map(|a| Self { admin: a.1,
admin_purge_person: a.0, }
admin: a.1,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,16 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{admin_purge_post, community, person}, schema::{admin_purge_post, community, person},
source::{ source::{community::Community, moderator::AdminPurgePost, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::AdminPurgePost,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type AdminPurgePostViewTuple = (AdminPurgePost, Option<PersonSafe>, CommunitySafe); type AdminPurgePostViewTuple = (AdminPurgePost, Option<Person>, Community);
impl AdminPurgePostView { impl AdminPurgePostView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -39,8 +35,8 @@ impl AdminPurgePostView {
.inner_join(community::table) .inner_join(community::table)
.select(( .select((
admin_purge_post::all_columns, admin_purge_post::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
Community::safe_columns_tuple(), community::all_columns,
)) ))
.into_boxed(); .into_boxed();
@ -57,21 +53,18 @@ impl AdminPurgePostView {
.load::<AdminPurgePostViewTuple>(conn) .load::<AdminPurgePostViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for AdminPurgePostView { impl JoinView for AdminPurgePostView {
type DbTuple = AdminPurgePostViewTuple; type JoinTuple = AdminPurgePostViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() admin_purge_post: a.0,
.map(|a| Self { admin: a.1,
admin_purge_post: a.0, community: a.2,
admin: a.1, }
community: a.2,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,21 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, mod_add_community, person}, schema::{community, mod_add_community, person},
source::{ source::{community::Community, moderator::ModAddCommunity, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::ModAddCommunity,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModAddCommunityViewTuple = ( type ModAddCommunityViewTuple = (ModAddCommunity, Option<Person>, Community, Person);
ModAddCommunity,
Option<PersonSafe>,
CommunitySafe,
PersonSafe,
);
impl ModAddCommunityView { impl ModAddCommunityView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -47,9 +38,9 @@ impl ModAddCommunityView {
) )
.select(( .select((
mod_add_community::all_columns, mod_add_community::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
Community::safe_columns_tuple(), community::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
)) ))
.into_boxed(); .into_boxed();
@ -74,22 +65,19 @@ impl ModAddCommunityView {
.load::<ModAddCommunityViewTuple>(conn) .load::<ModAddCommunityViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModAddCommunityView { impl JoinView for ModAddCommunityView {
type DbTuple = ModAddCommunityViewTuple; type JoinTuple = ModAddCommunityViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_add_community: a.0,
.map(|a| Self { moderator: a.1,
mod_add_community: a.0, community: a.2,
moderator: a.1, modded_person: a.3,
community: a.2, }
modded_person: a.3,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,15 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{mod_add, person}, schema::{mod_add, person},
source::{ source::{moderator::ModAdd, person::Person},
moderator::ModAdd, traits::JoinView,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModAddViewTuple = (ModAdd, Option<PersonSafe>, PersonSafe); type ModAddViewTuple = (ModAdd, Option<Person>, Person);
impl ModAddView { impl ModAddView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, 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)))) .inner_join(person_alias_1.on(mod_add::other_person_id.eq(person_alias_1.field(person::id))))
.select(( .select((
mod_add::all_columns, mod_add::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
)) ))
.into_boxed(); .into_boxed();
@ -60,21 +57,18 @@ impl ModAddView {
.load::<ModAddViewTuple>(conn) .load::<ModAddViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModAddView { impl JoinView for ModAddView {
type DbTuple = ModAddViewTuple; type JoinTuple = ModAddViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_add: a.0,
.map(|a| Self { moderator: a.1,
mod_add: a.0, modded_person: a.2,
moderator: a.1, }
modded_person: a.2,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,21 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, mod_ban_from_community, person}, schema::{community, mod_ban_from_community, person},
source::{ source::{community::Community, moderator::ModBanFromCommunity, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::ModBanFromCommunity,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModBanFromCommunityViewTuple = ( type ModBanFromCommunityViewTuple = (ModBanFromCommunity, Option<Person>, Community, Person);
ModBanFromCommunity,
Option<PersonSafe>,
CommunitySafe,
PersonSafe,
);
impl ModBanFromCommunityView { impl ModBanFromCommunityView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -49,9 +40,9 @@ impl ModBanFromCommunityView {
) )
.select(( .select((
mod_ban_from_community::all_columns, mod_ban_from_community::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
Community::safe_columns_tuple(), community::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
)) ))
.into_boxed(); .into_boxed();
@ -76,22 +67,19 @@ impl ModBanFromCommunityView {
.load::<ModBanFromCommunityViewTuple>(conn) .load::<ModBanFromCommunityViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModBanFromCommunityView { impl JoinView for ModBanFromCommunityView {
type DbTuple = ModBanFromCommunityViewTuple; type JoinTuple = ModBanFromCommunityViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_ban_from_community: a.0,
.map(|a| Self { moderator: a.1,
mod_ban_from_community: a.0, community: a.2,
moderator: a.1, banned_person: a.3,
community: a.2, }
banned_person: a.3,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,15 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{mod_ban, person}, schema::{mod_ban, person},
source::{ source::{moderator::ModBan, person::Person},
moderator::ModBan, traits::JoinView,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModBanViewTuple = (ModBan, Option<PersonSafe>, PersonSafe); type ModBanViewTuple = (ModBan, Option<Person>, Person);
impl ModBanView { impl ModBanView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, 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)))) .inner_join(person_alias_1.on(mod_ban::other_person_id.eq(person_alias_1.field(person::id))))
.select(( .select((
mod_ban::all_columns, mod_ban::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
)) ))
.into_boxed(); .into_boxed();
@ -60,21 +57,18 @@ impl ModBanView {
.load::<ModBanViewTuple>(conn) .load::<ModBanViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModBanView { impl JoinView for ModBanView {
type DbTuple = ModBanViewTuple; type JoinTuple = ModBanViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_ban: a.0,
.map(|a| Self { moderator: a.1,
mod_ban: a.0, banned_person: a.2,
moderator: a.1, }
banned_person: a.2,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,17 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, mod_feature_post, person, post}, schema::{community, mod_feature_post, person, post},
source::{ source::{community::Community, moderator::ModFeaturePost, person::Person, post::Post},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::ModFeaturePost,
person::{Person, PersonSafe},
post::Post,
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModFeaturePostViewTuple = (ModFeaturePost, Option<PersonSafe>, Post, CommunitySafe); type ModFeaturePostViewTuple = (ModFeaturePost, Option<Person>, Post, Community);
impl ModFeaturePostView { impl ModFeaturePostView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -42,9 +37,9 @@ impl ModFeaturePostView {
.inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(community::table.on(post::community_id.eq(community::id)))
.select(( .select((
mod_feature_post::all_columns, mod_feature_post::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
)) ))
.into_boxed(); .into_boxed();
@ -69,22 +64,19 @@ impl ModFeaturePostView {
.load::<ModFeaturePostViewTuple>(conn) .load::<ModFeaturePostViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModFeaturePostView { impl JoinView for ModFeaturePostView {
type DbTuple = ModFeaturePostViewTuple; type JoinTuple = ModFeaturePostViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_feature_post: a.0,
.map(|a| Self { moderator: a.1,
mod_feature_post: a.0, post: a.2,
moderator: a.1, community: a.3,
post: a.2, }
community: a.3,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,16 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, mod_hide_community, person}, schema::{community, mod_hide_community, person},
source::{ source::{community::Community, moderator::ModHideCommunity, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::ModHideCommunity,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModHideCommunityViewTuple = (ModHideCommunity, Option<PersonSafe>, CommunitySafe); type ModHideCommunityViewTuple = (ModHideCommunity, Option<Person>, Community);
impl ModHideCommunityView { impl ModHideCommunityView {
// Pass in mod_id as admin_id because only admins can do this action // 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))) .inner_join(community::table.on(mod_hide_community::community_id.eq(community::id)))
.select(( .select((
mod_hide_community::all_columns, mod_hide_community::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
Community::safe_columns_tuple(), community::all_columns,
)) ))
.into_boxed(); .into_boxed();
@ -62,21 +58,18 @@ impl ModHideCommunityView {
.load::<ModHideCommunityViewTuple>(conn) .load::<ModHideCommunityViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModHideCommunityView { impl JoinView for ModHideCommunityView {
type DbTuple = ModHideCommunityViewTuple; type JoinTuple = ModHideCommunityViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_hide_community: a.0,
.map(|a| Self { admin: a.1,
mod_hide_community: a.0, community: a.2,
admin: a.1, }
community: a.2,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,17 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, mod_lock_post, person, post}, schema::{community, mod_lock_post, person, post},
source::{ source::{community::Community, moderator::ModLockPost, person::Person, post::Post},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::ModLockPost,
person::{Person, PersonSafe},
post::Post,
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModLockPostViewTuple = (ModLockPost, Option<PersonSafe>, Post, CommunitySafe); type ModLockPostViewTuple = (ModLockPost, Option<Person>, Post, Community);
impl ModLockPostView { impl ModLockPostView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -43,9 +38,9 @@ impl ModLockPostView {
.inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
.select(( .select((
mod_lock_post::all_columns, mod_lock_post::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
)) ))
.into_boxed(); .into_boxed();
@ -70,22 +65,19 @@ impl ModLockPostView {
.load::<ModLockPostViewTuple>(conn) .load::<ModLockPostViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModLockPostView { impl JoinView for ModLockPostView {
type DbTuple = ModLockPostViewTuple; type JoinTuple = ModLockPostViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_lock_post: a.0,
.map(|a| Self { moderator: a.1,
mod_lock_post: a.0, post: a.2,
moderator: a.1, community: a.3,
post: a.2, }
community: a.3,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -14,22 +14,22 @@ use lemmy_db_schema::{
schema::{comment, community, mod_remove_comment, person, post}, schema::{comment, community, mod_remove_comment, person, post},
source::{ source::{
comment::Comment, comment::Comment,
community::{Community, CommunitySafe}, community::Community,
moderator::ModRemoveComment, moderator::ModRemoveComment,
person::{Person, PersonSafe}, person::Person,
post::Post, post::Post,
}, },
traits::{ToSafe, ViewToVec}, traits::JoinView,
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModRemoveCommentViewTuple = ( type ModRemoveCommentViewTuple = (
ModRemoveComment, ModRemoveComment,
Option<PersonSafe>, Option<Person>,
Comment, Comment,
PersonSafe, Person,
Post, Post,
CommunitySafe, Community,
); );
impl ModRemoveCommentView { impl ModRemoveCommentView {
@ -51,11 +51,11 @@ impl ModRemoveCommentView {
.inner_join(community::table.on(post::community_id.eq(community::id))) .inner_join(community::table.on(post::community_id.eq(community::id)))
.select(( .select((
mod_remove_comment::all_columns, mod_remove_comment::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
comment::all_columns, comment::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
)) ))
.into_boxed(); .into_boxed();
@ -80,24 +80,21 @@ impl ModRemoveCommentView {
.load::<ModRemoveCommentViewTuple>(conn) .load::<ModRemoveCommentViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModRemoveCommentView { impl JoinView for ModRemoveCommentView {
type DbTuple = ModRemoveCommentViewTuple; type JoinTuple = ModRemoveCommentViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_remove_comment: a.0,
.map(|a| Self { moderator: a.1,
mod_remove_comment: a.0, comment: a.2,
moderator: a.1, commenter: a.3,
comment: a.2, post: a.4,
commenter: a.3, community: a.5,
post: a.4, }
community: a.5,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,16 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, mod_remove_community, person}, schema::{community, mod_remove_community, person},
source::{ source::{community::Community, moderator::ModRemoveCommunity, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::ModRemoveCommunity,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModRemoveCommunityTuple = (ModRemoveCommunity, Option<PersonSafe>, CommunitySafe); type ModRemoveCommunityTuple = (ModRemoveCommunity, Option<Person>, Community);
impl ModRemoveCommunityView { impl ModRemoveCommunityView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -38,8 +34,8 @@ impl ModRemoveCommunityView {
.inner_join(community::table) .inner_join(community::table)
.select(( .select((
mod_remove_community::all_columns, mod_remove_community::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
Community::safe_columns_tuple(), community::all_columns,
)) ))
.into_boxed(); .into_boxed();
@ -56,21 +52,18 @@ impl ModRemoveCommunityView {
.load::<ModRemoveCommunityTuple>(conn) .load::<ModRemoveCommunityTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModRemoveCommunityView { impl JoinView for ModRemoveCommunityView {
type DbTuple = ModRemoveCommunityTuple; type JoinTuple = ModRemoveCommunityTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_remove_community: a.0,
.map(|a| Self { moderator: a.1,
mod_remove_community: a.0, community: a.2,
moderator: a.1, }
community: a.2,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,17 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, mod_remove_post, person, post}, schema::{community, mod_remove_post, person, post},
source::{ source::{community::Community, moderator::ModRemovePost, person::Person, post::Post},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::ModRemovePost,
person::{Person, PersonSafe},
post::Post,
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModRemovePostViewTuple = (ModRemovePost, Option<PersonSafe>, Post, CommunitySafe); type ModRemovePostViewTuple = (ModRemovePost, Option<Person>, Post, Community);
impl ModRemovePostView { impl ModRemovePostView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -43,9 +38,9 @@ impl ModRemovePostView {
.inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id)))) .inner_join(person_alias_1.on(post::creator_id.eq(person_alias_1.field(person::id))))
.select(( .select((
mod_remove_post::all_columns, mod_remove_post::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
post::all_columns, post::all_columns,
Community::safe_columns_tuple(), community::all_columns,
)) ))
.into_boxed(); .into_boxed();
@ -70,22 +65,19 @@ impl ModRemovePostView {
.load::<ModRemovePostViewTuple>(conn) .load::<ModRemovePostViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModRemovePostView { impl JoinView for ModRemovePostView {
type DbTuple = ModRemovePostViewTuple; type JoinTuple = ModRemovePostViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_remove_post: a.0,
.map(|a| Self { moderator: a.1,
mod_remove_post: a.0, post: a.2,
moderator: a.1, community: a.3,
post: a.2, }
community: a.3,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -12,21 +12,12 @@ use diesel_async::RunQueryDsl;
use lemmy_db_schema::{ use lemmy_db_schema::{
newtypes::PersonId, newtypes::PersonId,
schema::{community, mod_transfer_community, person}, schema::{community, mod_transfer_community, person},
source::{ source::{community::Community, moderator::ModTransferCommunity, person::Person},
community::{Community, CommunitySafe}, traits::JoinView,
moderator::ModTransferCommunity,
person::{Person, PersonSafe},
},
traits::{ToSafe, ViewToVec},
utils::{get_conn, limit_and_offset, DbPool}, utils::{get_conn, limit_and_offset, DbPool},
}; };
type ModTransferCommunityViewTuple = ( type ModTransferCommunityViewTuple = (ModTransferCommunity, Option<Person>, Community, Person);
ModTransferCommunity,
Option<PersonSafe>,
CommunitySafe,
PersonSafe,
);
impl ModTransferCommunityView { impl ModTransferCommunityView {
pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> { pub async fn list(pool: &DbPool, params: ModlogListParams) -> Result<Vec<Self>, Error> {
@ -49,9 +40,9 @@ impl ModTransferCommunityView {
) )
.select(( .select((
mod_transfer_community::all_columns, mod_transfer_community::all_columns,
Person::safe_columns_tuple().nullable(), person::all_columns.nullable(),
Community::safe_columns_tuple(), community::all_columns,
person_alias_1.fields(Person::safe_columns_tuple()), person_alias_1.fields(person::all_columns),
)) ))
.into_boxed(); .into_boxed();
@ -76,22 +67,19 @@ impl ModTransferCommunityView {
.load::<ModTransferCommunityViewTuple>(conn) .load::<ModTransferCommunityViewTuple>(conn)
.await?; .await?;
let results = Self::from_tuple_to_vec(res); let results = res.into_iter().map(Self::from_tuple).collect();
Ok(results) Ok(results)
} }
} }
impl ViewToVec for ModTransferCommunityView { impl JoinView for ModTransferCommunityView {
type DbTuple = ModTransferCommunityViewTuple; type JoinTuple = ModTransferCommunityViewTuple;
fn from_tuple_to_vec(items: Vec<Self::DbTuple>) -> Vec<Self> { fn from_tuple(a: Self::JoinTuple) -> Self {
items Self {
.into_iter() mod_transfer_community: a.0,
.map(|a| Self { moderator: a.1,
mod_transfer_community: a.0, community: a.2,
moderator: a.1, modded_person: a.3,
community: a.2, }
modded_person: a.3,
})
.collect::<Vec<Self>>()
} }
} }

View file

@ -2,7 +2,7 @@ use lemmy_db_schema::{
newtypes::{CommunityId, PersonId}, newtypes::{CommunityId, PersonId},
source::{ source::{
comment::Comment, comment::Comment,
community::CommunitySafe, community::Community,
moderator::{ moderator::{
AdminPurgeComment, AdminPurgeComment,
AdminPurgeCommunity, AdminPurgeCommunity,
@ -20,7 +20,7 @@ use lemmy_db_schema::{
ModRemovePost, ModRemovePost,
ModTransferCommunity, ModTransferCommunity,
}, },
person::PersonSafe, person::Person,
post::Post, post::Post,
}, },
}; };
@ -29,113 +29,113 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModAddCommunityView { pub struct ModAddCommunityView {
pub mod_add_community: ModAddCommunity, pub mod_add_community: ModAddCommunity,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub community: CommunitySafe, pub community: Community,
pub modded_person: PersonSafe, pub modded_person: Person,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModAddView { pub struct ModAddView {
pub mod_add: ModAdd, pub mod_add: ModAdd,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub modded_person: PersonSafe, pub modded_person: Person,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModBanFromCommunityView { pub struct ModBanFromCommunityView {
pub mod_ban_from_community: ModBanFromCommunity, pub mod_ban_from_community: ModBanFromCommunity,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub community: CommunitySafe, pub community: Community,
pub banned_person: PersonSafe, pub banned_person: Person,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModBanView { pub struct ModBanView {
pub mod_ban: ModBan, pub mod_ban: ModBan,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub banned_person: PersonSafe, pub banned_person: Person,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModHideCommunityView { pub struct ModHideCommunityView {
pub mod_hide_community: ModHideCommunity, pub mod_hide_community: ModHideCommunity,
pub admin: Option<PersonSafe>, pub admin: Option<Person>,
pub community: CommunitySafe, pub community: Community,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModLockPostView { pub struct ModLockPostView {
pub mod_lock_post: ModLockPost, pub mod_lock_post: ModLockPost,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModRemoveCommentView { pub struct ModRemoveCommentView {
pub mod_remove_comment: ModRemoveComment, pub mod_remove_comment: ModRemoveComment,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub comment: Comment, pub comment: Comment,
pub commenter: PersonSafe, pub commenter: Person,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModRemoveCommunityView { pub struct ModRemoveCommunityView {
pub mod_remove_community: ModRemoveCommunity, pub mod_remove_community: ModRemoveCommunity,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub community: CommunitySafe, pub community: Community,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModRemovePostView { pub struct ModRemovePostView {
pub mod_remove_post: ModRemovePost, pub mod_remove_post: ModRemovePost,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModFeaturePostView { pub struct ModFeaturePostView {
pub mod_feature_post: ModFeaturePost, pub mod_feature_post: ModFeaturePost,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub post: Post, pub post: Post,
pub community: CommunitySafe, pub community: Community,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ModTransferCommunityView { pub struct ModTransferCommunityView {
pub mod_transfer_community: ModTransferCommunity, pub mod_transfer_community: ModTransferCommunity,
pub moderator: Option<PersonSafe>, pub moderator: Option<Person>,
pub community: CommunitySafe, pub community: Community,
pub modded_person: PersonSafe, pub modded_person: Person,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AdminPurgeCommentView { pub struct AdminPurgeCommentView {
pub admin_purge_comment: AdminPurgeComment, pub admin_purge_comment: AdminPurgeComment,
pub admin: Option<PersonSafe>, pub admin: Option<Person>,
pub post: Post, pub post: Post,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AdminPurgeCommunityView { pub struct AdminPurgeCommunityView {
pub admin_purge_community: AdminPurgeCommunity, pub admin_purge_community: AdminPurgeCommunity,
pub admin: Option<PersonSafe>, pub admin: Option<Person>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AdminPurgePersonView { pub struct AdminPurgePersonView {
pub admin_purge_person: AdminPurgePerson, pub admin_purge_person: AdminPurgePerson,
pub admin: Option<PersonSafe>, pub admin: Option<Person>,
} }
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AdminPurgePostView { pub struct AdminPurgePostView {
pub admin_purge_post: AdminPurgePost, pub admin_purge_post: AdminPurgePost,
pub admin: Option<PersonSafe>, pub admin: Option<Person>,
pub community: CommunitySafe, pub community: Community,
} }
#[derive(Debug, Serialize, Deserialize, Clone, Copy)] #[derive(Debug, Serialize, Deserialize, Clone, Copy)]