-
-
Notifications
You must be signed in to change notification settings - Fork 882
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Instance blocks with mod log entry and expiration (fixes #2506)
- Loading branch information
Showing
14 changed files
with
178 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use activitypub_federation::config::Data; | ||
use actix_web::web::Json; | ||
use lemmy_api_common::{ | ||
context::LemmyContext, | ||
site::{AdminBlockInstance, BlockInstanceResponse}, utils::is_admin, | ||
}; | ||
use lemmy_db_schema::{ | ||
source::{federation_blocklist::FederationBlockListForm, instance_block::{InstanceBlock, InstanceBlockForm}}, | ||
traits::Blockable, | ||
}; | ||
use lemmy_db_views::structs::LocalUserView; | ||
use lemmy_utils::error::{LemmyErrorExt, LemmyErrorType, LemmyResult}; | ||
|
||
#[tracing::instrument(skip(context))] | ||
pub async fn block_instance( | ||
data: Json<AdminBlockInstance>, | ||
local_user_view: LocalUserView, | ||
context: Data<LemmyContext>, | ||
) -> LemmyResult<Json<BlockInstanceResponse>> { | ||
is_admin(&local_user_view)?; | ||
|
||
let instance_block_form = FederationBlockListForm { | ||
instance_id, | ||
person_id: | ||
reason: data.reason, | ||
expires: data.expires | ||
}; | ||
|
||
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
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 |
---|---|---|
@@ -1,49 +1,21 @@ | ||
use crate::{ | ||
schema::federation_blocklist, | ||
source::{ | ||
federation_blocklist::{FederationBlockList, FederationBlockListForm}, | ||
instance::Instance, | ||
}, | ||
source::federation_blocklist::{FederationBlockList, FederationBlockListForm}, | ||
utils::{get_conn, DbPool}, | ||
}; | ||
use diesel::{dsl::insert_into, result::Error}; | ||
use diesel_async::{AsyncPgConnection, RunQueryDsl}; | ||
use diesel_async::RunQueryDsl; | ||
|
||
impl FederationBlockList { | ||
pub async fn replace(pool: &mut DbPool<'_>, list_opt: Option<Vec<String>>) -> Result<(), Error> { | ||
pub async fn create( | ||
pool: &mut DbPool<'_>, | ||
form: &FederationBlockListForm, | ||
) -> Result<Self, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
conn | ||
.build_transaction() | ||
.run(|conn| { | ||
Box::pin(async move { | ||
if let Some(list) = list_opt { | ||
Self::clear(conn).await?; | ||
|
||
for domain in list { | ||
// Upsert all of these as instances | ||
let instance = Instance::read_or_create(&mut conn.into(), domain).await?; | ||
|
||
let form = FederationBlockListForm { | ||
instance_id: instance.id, | ||
updated: None, | ||
}; | ||
insert_into(federation_blocklist::table) | ||
.values(form) | ||
.get_result::<Self>(conn) | ||
.await?; | ||
} | ||
Ok(()) | ||
} else { | ||
Ok(()) | ||
} | ||
}) as _ | ||
}) | ||
.await | ||
} | ||
|
||
async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> { | ||
diesel::delete(federation_blocklist::table) | ||
.execute(conn) | ||
insert_into(federation_blocklist::table) | ||
.values(form) | ||
.get_result::<Self>(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
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,57 @@ | ||
use crate::structs::{AdminBlockInstance, ModlogListParams}; | ||
use diesel::{ | ||
result::Error, | ||
BoolExpressionMethods, | ||
ExpressionMethods, | ||
IntoSql, | ||
JoinOnDsl, | ||
NullableExpressionMethods, | ||
QueryDsl, | ||
}; | ||
use diesel_async::RunQueryDsl; | ||
use lemmy_db_schema::{ | ||
newtypes::PersonId, | ||
schema::{federation_blocklist, instance, person}, | ||
utils::{functions::coalesce, get_conn, limit_and_offset, DbPool}, | ||
}; | ||
|
||
impl AdminBlockInstance { | ||
pub async fn list(pool: &mut DbPool<'_>, params: ModlogListParams) -> Result<Vec<Self>, Error> { | ||
let conn = &mut get_conn(pool).await?; | ||
|
||
let admin_person_id_join = params.mod_person_id.unwrap_or(PersonId(-1)); | ||
let show_mod_names = !params.hide_modlog_names; | ||
let show_mod_names_expr = show_mod_names.as_sql::<diesel::sql_types::Bool>(); | ||
|
||
let admin_names_join = coalesce(federation_blocklist::admin_person_id, 0) | ||
.eq(person::id) | ||
.and(show_mod_names_expr.or(person::id.eq(admin_person_id_join))); | ||
let mut query = federation_blocklist::table | ||
.left_join(person::table.on(admin_names_join)) | ||
.inner_join(instance::table) | ||
.select(( | ||
federation_blocklist::all_columns, | ||
instance::all_columns, | ||
person::all_columns.nullable(), | ||
)) | ||
.into_boxed(); | ||
|
||
if let Some(admin_person_id) = params.mod_person_id { | ||
query = query.filter(federation_blocklist::admin_person_id.eq(admin_person_id)); | ||
}; | ||
|
||
// If a post or comment ID is given, then don't find any results | ||
if params.post_id.is_some() || params.comment_id.is_some() { | ||
return Ok(vec![]); | ||
} | ||
|
||
let (limit, offset) = limit_and_offset(params.page, params.limit)?; | ||
|
||
query | ||
.limit(limit) | ||
.offset(offset) | ||
.order_by(federation_blocklist::published.desc()) | ||
.load::<AdminBlockInstance>(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,3 @@ | ||
alter table federation_blocklist drop column reason; | ||
alter table federation_blocklist drop column expires; | ||
alter table federation_blocklist drop column admin_person_id; |
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,3 @@ | ||
alter table federation_blocklist add column admin_person_id int REFERENCES person(id) ON UPDATE CASCADE ON DELETE CASCADE; | ||
alter table federation_blocklist add column reason text; | ||
alter table federation_blocklist add column expires timestamptz; |