mirror of
https://github.com/LemmyNet/lemmy.git
synced 2025-09-04 12:14:31 +00:00
Fix API tests (#5934)
* api test fix 1 * Only test post. * Try cloning the data. * Only test post * Rest of API tests. * Removing unecessary post id. * Revert "Try cloning the data." This reverts commit7f3d67b1f0
. * Comment plugin hook before. * Remove another before * Rest of api tests * Fix user spec. * Revert "Fix user spec." This reverts commit1a0bf39916
. * Using localuserview for admin check * Fixing site_view import * Try to add back in plugin hooks * Commenting again, adding lints. * Adding back tests. * Forgot one * Fixing shear.
This commit is contained in:
parent
29254b1e45
commit
16aab76c5a
9 changed files with 30 additions and 75 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -3229,7 +3229,6 @@ dependencies = [
|
|||
"lemmy_db_views_local_image",
|
||||
"lemmy_db_views_local_user",
|
||||
"lemmy_db_views_notification",
|
||||
"lemmy_db_views_person",
|
||||
"lemmy_db_views_post",
|
||||
"lemmy_db_views_private_message",
|
||||
"lemmy_db_views_site",
|
||||
|
|
|
@ -42,13 +42,7 @@ pub async fn mod_update_post(
|
|||
let community = orig_post.community;
|
||||
|
||||
check_community_user_action(&local_user_view, &community, &mut context.pool()).await?;
|
||||
check_is_mod_or_admin(
|
||||
&mut context.pool(),
|
||||
local_user_view.person.id,
|
||||
community.id,
|
||||
local_instance_id,
|
||||
)
|
||||
.await?;
|
||||
check_is_mod_or_admin(&mut context.pool(), local_user_view.person.id, community.id).await?;
|
||||
|
||||
let mut post_form = PostUpdateForm {
|
||||
nsfw: data.nsfw,
|
||||
|
|
|
@ -41,7 +41,7 @@ pub async fn delete_post(
|
|||
// Update the post
|
||||
let post = Post::update(
|
||||
&mut context.pool(),
|
||||
data.post_id,
|
||||
post_id,
|
||||
&PostUpdateForm {
|
||||
deleted: Some(data.deleted),
|
||||
..Default::default()
|
||||
|
@ -54,11 +54,5 @@ pub async fn delete_post(
|
|||
&context,
|
||||
)?;
|
||||
|
||||
build_post_response(
|
||||
&context,
|
||||
orig_post.community_id,
|
||||
local_user_view,
|
||||
data.post_id,
|
||||
)
|
||||
.await
|
||||
build_post_response(&context, orig_post.community_id, local_user_view, post_id).await
|
||||
}
|
||||
|
|
|
@ -6,7 +6,7 @@ use lemmy_api_utils::{
|
|||
build_response::build_post_response,
|
||||
context::LemmyContext,
|
||||
notify::NotifyData,
|
||||
plugins::{plugin_hook_after, plugin_hook_before},
|
||||
plugins::plugin_hook_after,
|
||||
request::generate_post_link_metadata,
|
||||
send_activity::SendActivityData,
|
||||
utils::{
|
||||
|
@ -130,7 +130,7 @@ pub async fn update_post(
|
|||
(_, _) => None,
|
||||
};
|
||||
|
||||
let mut post_form = PostUpdateForm {
|
||||
let post_form = PostUpdateForm {
|
||||
name: data.name.clone(),
|
||||
url,
|
||||
body,
|
||||
|
@ -141,7 +141,8 @@ pub async fn update_post(
|
|||
scheduled_publish_time_at,
|
||||
..Default::default()
|
||||
};
|
||||
post_form = plugin_hook_before("before_update_local_post", post_form).await?;
|
||||
// TODO this is currently broken
|
||||
// post_form = plugin_hook_before("before_update_local_post", post_form).await?;
|
||||
|
||||
let post_id = data.post_id;
|
||||
let updated_post = Post::update(&mut context.pool(), post_id, &post_form).await?;
|
||||
|
|
|
@ -26,7 +26,6 @@ full = [
|
|||
"lemmy_db_views_community_person_ban/full",
|
||||
"lemmy_db_views_local_image/full",
|
||||
"lemmy_db_views_local_user/full",
|
||||
"lemmy_db_views_person/full",
|
||||
"lemmy_db_views_site/full",
|
||||
"lemmy_db_views_private_message/full",
|
||||
"lemmy_db_views_comment/full",
|
||||
|
@ -42,7 +41,6 @@ lemmy_db_views_community_moderator = { workspace = true }
|
|||
lemmy_db_views_community_person_ban = { workspace = true }
|
||||
lemmy_db_views_local_image = { workspace = true }
|
||||
lemmy_db_views_local_user = { workspace = true }
|
||||
lemmy_db_views_person = { workspace = true }
|
||||
lemmy_db_views_site = { workspace = true }
|
||||
lemmy_db_views_private_message = { workspace = true }
|
||||
lemmy_db_views_comment = { workspace = true }
|
||||
|
|
|
@ -40,7 +40,6 @@ use lemmy_db_views_community_moderator::CommunityModeratorView;
|
|||
use lemmy_db_views_community_person_ban::CommunityPersonBanView;
|
||||
use lemmy_db_views_local_image::LocalImageView;
|
||||
use lemmy_db_views_local_user::LocalUserView;
|
||||
use lemmy_db_views_person::PersonView;
|
||||
use lemmy_db_views_site::{
|
||||
api::{FederatedInstances, InstanceWithFederationState},
|
||||
SiteView,
|
||||
|
@ -73,15 +72,15 @@ pub async fn check_is_mod_or_admin(
|
|||
pool: &mut DbPool<'_>,
|
||||
person_id: PersonId,
|
||||
community_id: CommunityId,
|
||||
local_instance_id: InstanceId,
|
||||
) -> LemmyResult<()> {
|
||||
let is_mod =
|
||||
CommunityModeratorView::check_is_community_moderator(pool, community_id, person_id).await;
|
||||
if is_mod.is_ok()
|
||||
|| PersonView::read(pool, person_id, None, local_instance_id, false)
|
||||
.await
|
||||
.is_ok_and(|t| t.is_admin)
|
||||
{
|
||||
let is_mod = CommunityModeratorView::check_is_community_moderator(pool, community_id, person_id)
|
||||
.await
|
||||
.is_ok();
|
||||
let is_admin = LocalUserView::read_person(pool, person_id)
|
||||
.await
|
||||
.is_ok_and(|t| t.local_user.admin);
|
||||
|
||||
if is_mod || is_admin {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(LemmyErrorType::NotAModOrAdmin)?
|
||||
|
@ -92,14 +91,15 @@ pub async fn check_is_mod_or_admin(
|
|||
pub(crate) async fn check_is_mod_of_any_or_admin(
|
||||
pool: &mut DbPool<'_>,
|
||||
person_id: PersonId,
|
||||
local_instance_id: InstanceId,
|
||||
) -> LemmyResult<()> {
|
||||
let is_mod_of_any = CommunityModeratorView::is_community_moderator_of_any(pool, person_id).await;
|
||||
if is_mod_of_any.is_ok()
|
||||
|| PersonView::read(pool, person_id, None, local_instance_id, false)
|
||||
.await
|
||||
.is_ok_and(|t| t.is_admin)
|
||||
{
|
||||
let is_mod_of_any = CommunityModeratorView::is_community_moderator_of_any(pool, person_id)
|
||||
.await
|
||||
.is_ok();
|
||||
let is_admin = LocalUserView::read_person(pool, person_id)
|
||||
.await
|
||||
.is_ok_and(|t| t.local_user.admin);
|
||||
|
||||
if is_mod_of_any || is_admin {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(LemmyErrorType::NotAModOrAdmin)?
|
||||
|
@ -112,13 +112,7 @@ pub async fn is_mod_or_admin(
|
|||
community_id: CommunityId,
|
||||
) -> LemmyResult<()> {
|
||||
check_local_user_valid(local_user_view)?;
|
||||
check_is_mod_or_admin(
|
||||
pool,
|
||||
local_user_view.person.id,
|
||||
community_id,
|
||||
local_user_view.person.instance_id,
|
||||
)
|
||||
.await
|
||||
check_is_mod_or_admin(pool, local_user_view.person.id, community_id).await
|
||||
}
|
||||
|
||||
pub async fn is_mod_or_admin_opt(
|
||||
|
@ -127,7 +121,6 @@ pub async fn is_mod_or_admin_opt(
|
|||
community_id: Option<CommunityId>,
|
||||
) -> LemmyResult<()> {
|
||||
if let Some(local_user_view) = local_user_view {
|
||||
check_local_user_valid(local_user_view)?;
|
||||
if let Some(community_id) = community_id {
|
||||
is_mod_or_admin(pool, local_user_view, community_id).await
|
||||
} else {
|
||||
|
@ -148,7 +141,7 @@ pub async fn check_community_mod_of_any_or_admin_action(
|
|||
let person = &local_user_view.person;
|
||||
|
||||
check_local_user_valid(local_user_view)?;
|
||||
check_is_mod_of_any_or_admin(pool, person.id, person.instance_id).await
|
||||
check_is_mod_of_any_or_admin(pool, person.id).await
|
||||
}
|
||||
|
||||
pub fn is_admin(local_user_view: &LocalUserView) -> LemmyResult<()> {
|
||||
|
|
|
@ -129,7 +129,6 @@ impl Activity for CreateOrUpdateNote {
|
|||
|
||||
async fn receive(self, context: &Data<Self::DataType>) -> LemmyResult<()> {
|
||||
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
||||
let local_instance_id = site_view.site.instance_id;
|
||||
|
||||
// Need to do this check here instead of Note::from_json because we need the person who
|
||||
// send the activity, not the comment author.
|
||||
|
@ -140,13 +139,7 @@ impl Activity for CreateOrUpdateNote {
|
|||
{
|
||||
if distinguished != existing_comment.distinguished {
|
||||
let creator = self.actor.dereference(context).await?;
|
||||
check_is_mod_or_admin(
|
||||
&mut context.pool(),
|
||||
creator.id,
|
||||
post.community_id,
|
||||
local_instance_id,
|
||||
)
|
||||
.await?;
|
||||
check_is_mod_or_admin(&mut context.pool(), creator.id, post.community_id).await?;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -44,7 +44,6 @@ use lemmy_db_schema::{
|
|||
},
|
||||
traits::Crud,
|
||||
};
|
||||
use lemmy_db_views_site::SiteView;
|
||||
use lemmy_utils::{
|
||||
error::{FederationError, LemmyError, LemmyResult},
|
||||
utils::markdown::markdown_to_html,
|
||||
|
@ -176,17 +175,10 @@ impl Object for ApubComment {
|
|||
|
||||
let (post, _) = Box::pin(note.get_parents(context)).await?;
|
||||
let creator = Box::pin(note.attributed_to.dereference(context)).await?;
|
||||
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
||||
let local_instance_id = site_view.site.instance_id;
|
||||
|
||||
let is_mod_or_admin = check_is_mod_or_admin(
|
||||
&mut context.pool(),
|
||||
creator.id,
|
||||
community.id,
|
||||
local_instance_id,
|
||||
)
|
||||
.await
|
||||
.is_ok();
|
||||
let is_mod_or_admin = check_is_mod_or_admin(&mut context.pool(), creator.id, community.id)
|
||||
.await
|
||||
.is_ok();
|
||||
if post.locked && !is_mod_or_admin {
|
||||
Err(FederationError::PostIsLocked)?
|
||||
} else {
|
||||
|
|
|
@ -340,15 +340,6 @@ pub async fn verify_mod_action(
|
|||
return Ok(());
|
||||
}
|
||||
|
||||
let site_view = SiteView::read_local(&mut context.pool()).await?;
|
||||
let local_instance_id = site_view.site.instance_id;
|
||||
|
||||
let mod_ = mod_id.dereference(context).await?;
|
||||
check_is_mod_or_admin(
|
||||
&mut context.pool(),
|
||||
mod_.id,
|
||||
community.id,
|
||||
local_instance_id,
|
||||
)
|
||||
.await
|
||||
check_is_mod_or_admin(&mut context.pool(), mod_.id, community.id).await
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue