2022-10-27 09:24:07 +00:00
|
|
|
use crate::{
|
|
|
|
schema::federation_blocklist,
|
|
|
|
source::{
|
|
|
|
federation_blocklist::{FederationBlockList, FederationBlockListForm},
|
|
|
|
instance::Instance,
|
|
|
|
},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2022-10-27 09:24:07 +00:00
|
|
|
};
|
2022-11-19 04:33:54 +00:00
|
|
|
use diesel::{dsl::insert_into, result::Error};
|
2022-11-09 10:05:00 +00:00
|
|
|
use diesel_async::{AsyncPgConnection, RunQueryDsl};
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
impl FederationBlockList {
|
2022-11-09 10:05:00 +00:00
|
|
|
pub async fn replace(pool: &DbPool, list_opt: Option<Vec<String>>) -> Result<(), 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?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
for domain in list {
|
|
|
|
// Upsert all of these as instances
|
|
|
|
let instance = Instance::create_conn(conn, &domain).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
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
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> {
|
|
|
|
diesel::delete(federation_blocklist::table)
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
}
|