-
-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* User can block instances (fixes #2397) * update comments * review comments * use route * update * add api test * update tests * fix * fix test * ci --------- Co-authored-by: Dessalines <dessalines@users.noreply.github.com>
- Loading branch information
1 parent
89b7c98
commit 50f81cf
Showing
24 changed files
with
441 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
use activitypub_federation::config::Data; | ||
use actix_web::web::Json; | ||
use lemmy_api_common::{ | ||
context::LemmyContext, | ||
site::{BlockInstance, BlockInstanceResponse}, | ||
utils::local_user_view_from_jwt, | ||
}; | ||
use lemmy_db_schema::{ | ||
source::instance_block::{InstanceBlock, InstanceBlockForm}, | ||
traits::Blockable, | ||
}; | ||
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType}; | ||
|
||
#[tracing::instrument(skip(context))] | ||
pub async fn block_instance( | ||
data: Json<BlockInstance>, | ||
context: Data<LemmyContext>, | ||
) -> Result<Json<BlockInstanceResponse>, LemmyError> { | ||
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?; | ||
|
||
let instance_id = data.instance_id; | ||
let person_id = local_user_view.person.id; | ||
let instance_block_form = InstanceBlockForm { | ||
person_id, | ||
instance_id, | ||
}; | ||
|
||
if data.block { | ||
InstanceBlock::block(&mut context.pool(), &instance_block_form) | ||
.await | ||
.with_lemmy_type(LemmyErrorType::InstanceBlockAlreadyExists)?; | ||
} else { | ||
InstanceBlock::unblock(&mut context.pool(), &instance_block_form) | ||
.await | ||
.with_lemmy_type(LemmyErrorType::InstanceBlockAlreadyExists)?; | ||
} | ||
|
||
Ok(Json(BlockInstanceResponse { | ||
blocked: data.block, | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod block; | ||
pub mod federated_instances; | ||
pub mod leave_admin; | ||
pub mod mod_log; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use crate::{ | ||
schema::instance_block::dsl::{instance_block, instance_id, person_id}, | ||
source::instance_block::{InstanceBlock, InstanceBlockForm}, | ||
traits::Blockable, | ||
utils::{get_conn, DbPool}, | ||
}; | ||
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl}; | ||
use diesel_async::RunQueryDsl; | ||
|
||
#[async_trait] | ||
impl Blockable for InstanceBlock { | ||
type Form = InstanceBlockForm; | ||
async fn block(pool: &mut DbPool<'_>, instance_block_form: &Self::Form) -> Result<Self, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
insert_into(instance_block) | ||
.values(instance_block_form) | ||
.on_conflict((person_id, instance_id)) | ||
.do_update() | ||
.set(instance_block_form) | ||
.get_result::<Self>(conn) | ||
.await | ||
} | ||
async fn unblock( | ||
pool: &mut DbPool<'_>, | ||
instance_block_form: &Self::Form, | ||
) -> Result<usize, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
diesel::delete( | ||
instance_block | ||
.filter(person_id.eq(instance_block_form.person_id)) | ||
.filter(instance_id.eq(instance_block_form.instance_id)), | ||
) | ||
.execute(conn) | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use crate::newtypes::{InstanceId, PersonId}; | ||
#[cfg(feature = "full")] | ||
use crate::schema::instance_block; | ||
use chrono::{DateTime, Utc}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, PartialEq, Eq, Debug, Serialize, Deserialize)] | ||
#[cfg_attr(feature = "full", derive(Queryable, Associations, Identifiable))] | ||
#[cfg_attr( | ||
feature = "full", | ||
diesel(belongs_to(crate::source::instance::Instance)) | ||
)] | ||
#[cfg_attr(feature = "full", diesel(table_name = instance_block))] | ||
pub struct InstanceBlock { | ||
pub id: i32, | ||
pub person_id: PersonId, | ||
pub instance_id: InstanceId, | ||
pub published: DateTime<Utc>, | ||
} | ||
|
||
#[cfg_attr(feature = "full", derive(Insertable, AsChangeset))] | ||
#[cfg_attr(feature = "full", diesel(table_name = instance_block))] | ||
pub struct InstanceBlockForm { | ||
pub person_id: PersonId, | ||
pub instance_id: InstanceId, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.