Skip to content

Commit

Permalink
Get rid of Safe Views, use serde_skip (#2767)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
dessalines and Nutomic committed Apr 26, 2023
1 parent a1bd2c2 commit 2c67fce
Show file tree
Hide file tree
Showing 55 changed files with 827 additions and 1,329 deletions.
6 changes: 3 additions & 3 deletions crates/api/src/community/ban.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use lemmy_db_schema::{
},
traits::{Bannable, Crud, Followable},
};
use lemmy_db_views_actor::structs::PersonViewSafe;
use lemmy_utils::{error::LemmyError, utils::naive_from_unix, ConnectionId};
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId};

#[async_trait::async_trait(?Send)]
impl Perform for BanFromCommunity {
Expand Down Expand Up @@ -88,7 +88,7 @@ impl Perform for BanFromCommunity {
ModBanFromCommunity::create(context.pool(), &form).await?;

let person_id = data.person_id;
let person_view = PersonViewSafe::read(context.pool(), person_id).await?;
let person_view = PersonView::read(context.pool(), person_id).await?;

let res = BanFromCommunityResponse {
person_view,
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/add_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use lemmy_db_schema::{
},
traits::Crud,
};
use lemmy_db_views_actor::structs::PersonViewSafe;
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, ConnectionId};

#[async_trait::async_trait(?Send)]
Expand Down Expand Up @@ -52,7 +52,7 @@ impl Perform for AddAdmin {

ModAdd::create(context.pool(), &form).await?;

let admins = PersonViewSafe::admins(context.pool()).await?;
let admins = PersonView::admins(context.pool()).await?;

let res = AddAdminResponse { admins };

Expand Down
6 changes: 3 additions & 3 deletions crates/api/src/local_user/ban_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use lemmy_db_schema::{
},
traits::Crud,
};
use lemmy_db_views_actor::structs::PersonViewSafe;
use lemmy_utils::{error::LemmyError, utils::naive_from_unix, ConnectionId};
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, utils::time::naive_from_unix, ConnectionId};

#[async_trait::async_trait(?Send)]
impl Perform for BanPerson {
Expand Down Expand Up @@ -72,7 +72,7 @@ impl Perform for BanPerson {
ModBan::create(context.pool(), &form).await?;

let person_id = data.person_id;
let person_view = PersonViewSafe::read(context.pool(), person_id).await?;
let person_view = PersonView::read(context.pool(), person_id).await?;

let res = BanPersonResponse {
person_view,
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use lemmy_db_schema::{
source::person_block::{PersonBlock, PersonBlockForm},
traits::Blockable,
};
use lemmy_db_views_actor::structs::PersonViewSafe;
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, ConnectionId};

#[async_trait::async_trait(?Send)]
Expand Down Expand Up @@ -39,7 +39,7 @@ impl Perform for BlockPerson {
target_id,
};

let target_person_view = PersonViewSafe::read(context.pool(), target_id).await?;
let target_person_view = PersonView::read(context.pool(), target_id).await?;

if target_person_view.person.admin {
return Err(LemmyError::from_message("cant_block_admin"));
Expand Down
4 changes: 2 additions & 2 deletions crates/api/src/local_user/list_banned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lemmy_api_common::{
person::{BannedPersonsResponse, GetBannedPersons},
utils::{get_local_user_view_from_jwt, is_admin},
};
use lemmy_db_views_actor::structs::PersonViewSafe;
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, ConnectionId};

#[async_trait::async_trait(?Send)]
Expand All @@ -24,7 +24,7 @@ impl Perform for GetBannedPersons {
// Make sure user is an admin
is_admin(&local_user_view)?;

let banned = PersonViewSafe::banned(context.pool()).await?;
let banned = PersonView::banned(context.pool()).await?;

let res = Self::Response { banned };

Expand Down
6 changes: 3 additions & 3 deletions crates/api/src/site/leave_admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use lemmy_db_schema::{
traits::Crud,
};
use lemmy_db_views::structs::SiteView;
use lemmy_db_views_actor::structs::PersonViewSafe;
use lemmy_db_views_actor::structs::PersonView;
use lemmy_utils::{error::LemmyError, version, ConnectionId};

#[async_trait::async_trait(?Send)]
Expand All @@ -36,7 +36,7 @@ impl Perform for LeaveAdmin {
is_admin(&local_user_view)?;

// Make sure there isn't just one admin (so if one leaves, there will still be one left)
let admins = PersonViewSafe::admins(context.pool()).await?;
let admins = PersonView::admins(context.pool()).await?;
if admins.len() == 1 {
return Err(LemmyError::from_message("cannot_leave_admin"));
}
Expand All @@ -60,7 +60,7 @@ impl Perform for LeaveAdmin {

// Reread site and admins
let site_view = SiteView::read_local(context.pool()).await?;
let admins = PersonViewSafe::admins(context.pool()).await?;
let admins = PersonView::admins(context.pool()).await?;

let all_languages = Language::read_all(context.pool()).await?;
let discussion_languages = SiteLanguage::read_local(context.pool()).await?;
Expand Down
4 changes: 2 additions & 2 deletions crates/api_common/src/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use lemmy_db_schema::{
ListingType,
SortType,
};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonViewSafe};
use lemmy_db_views_actor::structs::{CommunityModeratorView, CommunityView, PersonView};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -74,7 +74,7 @@ pub struct BanFromCommunity {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BanFromCommunityResponse {
pub person_view: PersonViewSafe,
pub person_view: PersonView,
pub banned: bool,
}

Expand Down
12 changes: 6 additions & 6 deletions crates/api_common/src/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use lemmy_db_views_actor::structs::{
CommentReplyView,
CommunityModeratorView,
PersonMentionView,
PersonViewSafe,
PersonView,
};
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -104,7 +104,7 @@ pub struct GetPersonDetails {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GetPersonDetailsResponse {
pub person_view: PersonViewSafe,
pub person_view: PersonView,
pub comments: Vec<CommentView>,
pub posts: Vec<PostView>,
pub moderates: Vec<CommunityModeratorView>,
Expand Down Expand Up @@ -134,7 +134,7 @@ pub struct AddAdmin {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AddAdminResponse {
pub admins: Vec<PersonViewSafe>,
pub admins: Vec<PersonView>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand All @@ -154,12 +154,12 @@ pub struct GetBannedPersons {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BannedPersonsResponse {
pub banned: Vec<PersonViewSafe>,
pub banned: Vec<PersonView>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BanPersonResponse {
pub person_view: PersonViewSafe,
pub person_view: PersonView,
pub banned: bool,
}

Expand All @@ -172,7 +172,7 @@ pub struct BlockPerson {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BlockPersonResponse {
pub person_view: PersonViewSafe,
pub person_view: PersonView,
pub blocked: bool,
}

Expand Down
12 changes: 6 additions & 6 deletions crates/api_common/src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use lemmy_db_schema::{
};
use lemmy_db_views::structs::{
CommentView,
LocalUserSettingsView,
LocalUserView,
PostView,
RegistrationApplicationView,
SiteView,
Expand All @@ -20,7 +20,7 @@ use lemmy_db_views_actor::structs::{
CommunityModeratorView,
CommunityView,
PersonBlockView,
PersonViewSafe,
PersonView,
};
use lemmy_db_views_moderator::structs::{
AdminPurgeCommentView,
Expand Down Expand Up @@ -61,7 +61,7 @@ pub struct SearchResponse {
pub comments: Vec<CommentView>,
pub posts: Vec<PostView>,
pub communities: Vec<CommunityView>,
pub users: Vec<PersonViewSafe>,
pub users: Vec<PersonView>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand All @@ -75,7 +75,7 @@ pub struct ResolveObjectResponse {
pub comment: Option<CommentView>,
pub post: Option<PostView>,
pub community: Option<CommunityView>,
pub person: Option<PersonViewSafe>,
pub person: Option<PersonView>,
}

#[derive(Debug, Serialize, Deserialize, Clone, Default)]
Expand Down Expand Up @@ -212,7 +212,7 @@ pub struct SiteResponse {
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct GetSiteResponse {
pub site_view: SiteView,
pub admins: Vec<PersonViewSafe>,
pub admins: Vec<PersonView>,
pub online: usize,
pub version: String,
pub my_user: Option<MyUserInfo>,
Expand All @@ -224,7 +224,7 @@ pub struct GetSiteResponse {

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MyUserInfo {
pub local_user_view: LocalUserSettingsView,
pub local_user_view: LocalUserView,
pub follows: Vec<CommunityFollowerView>,
pub moderates: Vec<CommunityModeratorView>,
pub community_blocks: Vec<CommunityBlockView>,
Expand Down
19 changes: 8 additions & 11 deletions crates/api_common/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,12 @@ use lemmy_db_schema::{
utils::DbPool,
ListingType,
};
use lemmy_db_views::{
comment_view::CommentQuery,
structs::{LocalUserSettingsView, LocalUserView},
};
use lemmy_db_views::{comment_view::CommentQuery, structs::LocalUserView};
use lemmy_db_views_actor::structs::{
CommunityModeratorView,
CommunityPersonBanView,
CommunityView,
PersonViewSafe,
PersonView,
};
use lemmy_utils::{
claims::Claims,
Expand Down Expand Up @@ -62,7 +59,7 @@ pub async fn is_mod_or_admin(
}

pub async fn is_top_admin(pool: &DbPool, person_id: PersonId) -> Result<(), LemmyError> {
let admins = PersonViewSafe::admins(pool).await?;
let admins = PersonView::admins(pool).await?;
let top_admin = admins
.first()
.ok_or_else(|| LemmyError::from_message("no admins"))?;
Expand Down Expand Up @@ -180,14 +177,14 @@ pub async fn get_local_user_settings_view_from_jwt_opt(
jwt: Option<&Sensitive<String>>,
pool: &DbPool,
secret: &Secret,
) -> Result<Option<LocalUserSettingsView>, LemmyError> {
) -> Result<Option<LocalUserView>, LemmyError> {
match jwt {
Some(jwt) => {
let claims = Claims::decode(jwt.as_ref(), &secret.jwt_secret)
.map_err(|e| e.with_message("not_logged_in"))?
.claims;
let local_user_id = LocalUserId(claims.sub);
let local_user_view = LocalUserSettingsView::read(pool, local_user_id).await?;
let local_user_view = LocalUserView::read(pool, local_user_id).await?;
check_user_valid(
local_user_view.person.banned,
local_user_view.person.ban_expires,
Expand Down Expand Up @@ -433,7 +430,7 @@ pub fn get_interface_language(user: &LocalUserView) -> Lang {
lang_str_to_lang(&user.local_user.interface_language)
}

pub fn get_interface_language_from_settings(user: &LocalUserSettingsView) -> Lang {
pub fn get_interface_language_from_settings(user: &LocalUserView) -> Lang {
lang_str_to_lang(&user.local_user.interface_language)
}

Expand Down Expand Up @@ -494,7 +491,7 @@ pub async fn send_new_applicant_email_to_admins(
settings: &Settings,
) -> Result<(), LemmyError> {
// Collect the admins with emails
let admins = LocalUserSettingsView::list_admins_with_emails(pool).await?;
let admins = LocalUserView::list_admins_with_emails(pool).await?;

let applications_link = &format!(
"{}/registration_applications",
Expand All @@ -519,7 +516,7 @@ pub async fn send_new_report_email_to_admins(
settings: &Settings,
) -> Result<(), LemmyError> {
// Collect the admins with emails
let admins = LocalUserSettingsView::list_admins_with_emails(pool).await?;
let admins = LocalUserView::list_admins_with_emails(pool).await?;

let reports_link = &format!("{}/reports", settings.get_protocol_and_hostname(),);

Expand Down
4 changes: 2 additions & 2 deletions crates/api_crud/src/site/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use lemmy_db_views_actor::structs::{
CommunityFollowerView,
CommunityModeratorView,
PersonBlockView,
PersonViewSafe,
PersonView,
};
use lemmy_utils::{error::LemmyError, version, ConnectionId};

Expand All @@ -34,7 +34,7 @@ impl PerformCrud for GetSite {

let site_view = SiteView::read_local(context.pool()).await?;

let admins = PersonViewSafe::admins(context.pool()).await?;
let admins = PersonView::admins(context.pool()).await?;

let online = context.chat_server().get_users_online()?;

Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/api/read_person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use lemmy_db_schema::{
utils::post_to_comment_sort_type,
};
use lemmy_db_views::{comment_view::CommentQuery, post_view::PostQuery};
use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonViewSafe};
use lemmy_db_views_actor::structs::{CommunityModeratorView, PersonView};
use lemmy_utils::{error::LemmyError, ConnectionId};

#[async_trait::async_trait(?Send)]
Expand Down Expand Up @@ -55,7 +55,7 @@ impl PerformApub for GetPersonDetails {

// You don't need to return settings for the user, since this comes back with GetSite
// `my_user`
let person_view = PersonViewSafe::read(context.pool(), person_details_id).await?;
let person_view = PersonView::read(context.pool(), person_details_id).await?;

let sort = data.sort;
let page = data.page;
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/api/resolve_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use lemmy_api_common::{
};
use lemmy_db_schema::{newtypes::PersonId, source::local_site::LocalSite, utils::DbPool};
use lemmy_db_views::structs::{CommentView, PostView};
use lemmy_db_views_actor::structs::{CommunityView, PersonViewSafe};
use lemmy_db_views_actor::structs::{CommunityView, PersonView};
use lemmy_utils::{error::LemmyError, ConnectionId};

#[async_trait::async_trait(?Send)]
Expand Down Expand Up @@ -52,7 +52,7 @@ async fn convert_response(
match object {
Person(p) => {
removed_or_deleted = p.deleted;
res.person = Some(PersonViewSafe::read(pool, p.id).await?)
res.person = Some(PersonView::read(pool, p.id).await?)
}
Community(c) => {
removed_or_deleted = c.deleted || c.removed;
Expand Down

0 comments on commit 2c67fce

Please sign in to comment.