Skip to content

Commit

Permalink
Dont allow posts to deleted / removed communities. Fixes #1827
Browse files Browse the repository at this point in the history
  • Loading branch information
dessalines committed Oct 11, 2021
1 parent 454d398 commit f2ed47b
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 1 deletion.
2 changes: 2 additions & 0 deletions crates/api/src/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use anyhow::Context;
use lemmy_api_common::{
blocking,
check_community_ban,
check_community_deleted_or_removed,
community::*,
get_local_user_view_from_jwt,
is_mod_or_admin,
Expand Down Expand Up @@ -70,6 +71,7 @@ impl Perform for FollowCommunity {
if community.local {
if data.follow {
check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
check_community_deleted_or_removed(community_id, context.pool()).await?;

let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
if blocking(context.pool(), follow).await?.is_err() {
Expand Down
4 changes: 4 additions & 0 deletions crates/api/src/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use actix_web::web::Data;
use lemmy_api_common::{
blocking,
check_community_ban,
check_community_deleted_or_removed,
check_downvotes_enabled,
check_person_block,
get_local_user_view_from_jwt,
Expand Down Expand Up @@ -49,6 +50,7 @@ impl Perform for CreatePostLike {
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;

check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
check_community_deleted_or_removed(post.community_id, context.pool()).await?;

check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;

Expand Down Expand Up @@ -133,6 +135,7 @@ impl Perform for LockPost {
context.pool(),
)
.await?;
check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;

// Verify that only the mods can lock
is_mod_or_admin(
Expand Down Expand Up @@ -200,6 +203,7 @@ impl Perform for StickyPost {
context.pool(),
)
.await?;
check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;

// Verify that only the mods can sticky
is_mod_or_admin(
Expand Down
12 changes: 12 additions & 0 deletions crates/api_common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,18 @@ pub async fn check_community_ban(
}
}

pub async fn check_community_deleted_or_removed(
community_id: CommunityId,
pool: &DbPool,
) -> Result<(), LemmyError> {
let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
if community.deleted || community.removed {
Err(ApiError::err("deleted").into())
} else {
Ok(())
}
}

pub async fn check_person_block(
my_id: PersonId,
potential_blocker_id: PersonId,
Expand Down
2 changes: 2 additions & 0 deletions crates/api_crud/src/comment/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use actix_web::web::Data;
use lemmy_api_common::{
blocking,
check_community_ban,
check_community_deleted_or_removed,
check_person_block,
comment::*,
get_local_user_view_from_jwt,
Expand Down Expand Up @@ -56,6 +57,7 @@ impl PerformCrud for CreateComment {
let community_id = post.community_id;

check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
check_community_deleted_or_removed(community_id, context.pool()).await?;

check_person_block(local_user_view.person.id, post.creator_id, context.pool()).await?;

Expand Down
2 changes: 2 additions & 0 deletions crates/api_crud/src/comment/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use actix_web::web::Data;
use lemmy_api_common::{
blocking,
check_community_ban,
check_community_deleted_or_removed,
comment::*,
get_local_user_view_from_jwt,
send_local_notifs,
Expand Down Expand Up @@ -48,6 +49,7 @@ impl PerformCrud for EditComment {
context.pool(),
)
.await?;
check_community_deleted_or_removed(orig_comment.community.id, context.pool()).await?;

// Verify that only the creator can edit
if local_user_view.person.id != orig_comment.creator.id {
Expand Down
2 changes: 2 additions & 0 deletions crates/api_crud/src/post/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use actix_web::web::Data;
use lemmy_api_common::{
blocking,
check_community_ban,
check_community_deleted_or_removed,
get_local_user_view_from_jwt,
honeypot_check,
mark_post_as_read,
Expand Down Expand Up @@ -54,6 +55,7 @@ impl PerformCrud for CreatePost {
}

check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
check_community_deleted_or_removed(data.community_id, context.pool()).await?;

// Fetch post links and pictrs cached image
let data_url = data.url.as_ref();
Expand Down
2 changes: 2 additions & 0 deletions crates/api_crud/src/post/delete.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use actix_web::web::Data;
use lemmy_api_common::{
blocking,
check_community_ban,
check_community_deleted_or_removed,
get_local_user_view_from_jwt,
is_mod_or_admin,
post::*,
Expand Down Expand Up @@ -35,6 +36,7 @@ impl PerformCrud for DeletePost {
context.pool(),
)
.await?;
check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;

// Verify that only the creator can delete
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
Expand Down
9 changes: 8 additions & 1 deletion crates/api_crud/src/post/update.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use crate::PerformCrud;
use actix_web::web::Data;
use lemmy_api_common::{blocking, check_community_ban, get_local_user_view_from_jwt, post::*};
use lemmy_api_common::{
blocking,
check_community_ban,
check_community_deleted_or_removed,
get_local_user_view_from_jwt,
post::*,
};
use lemmy_apub::activities::{post::create_or_update::CreateOrUpdatePost, CreateOrUpdateType};
use lemmy_db_queries::{source::post::Post_, Crud};
use lemmy_db_schema::{naive_now, source::post::*};
Expand Down Expand Up @@ -45,6 +51,7 @@ impl PerformCrud for EditPost {
context.pool(),
)
.await?;
check_community_deleted_or_removed(orig_post.community_id, context.pool()).await?;

// Verify that only the creator can edit
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
Expand Down

0 comments on commit f2ed47b

Please sign in to comment.