2022-10-27 09:24:07 +00:00
|
|
|
use crate::{
|
|
|
|
schema::federation_allowlist,
|
|
|
|
source::{
|
|
|
|
federation_allowlist::{FederationAllowList, FederationAllowListForm},
|
|
|
|
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 FederationAllowList {
|
2023-07-11 13:09:59 +00:00
|
|
|
pub async fn replace(pool: &mut DbPool<'_>, list_opt: Option<Vec<String>>) -> Result<(), Error> {
|
2022-11-09 10:05:00 +00:00
|
|
|
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
|
2023-07-11 13:09:59 +00:00
|
|
|
let instance = Instance::read_or_create(&mut conn.into(), domain).await?;
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let form = FederationAllowListForm {
|
|
|
|
instance_id: instance.id,
|
|
|
|
updated: None,
|
|
|
|
};
|
|
|
|
insert_into(federation_allowlist::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_allowlist::table)
|
|
|
|
.execute(conn)
|
|
|
|
.await
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(test)]
|
2024-03-26 09:17:42 +00:00
|
|
|
#[allow(clippy::unwrap_used)]
|
|
|
|
#[allow(clippy::indexing_slicing)]
|
2022-10-27 09:24:07 +00:00
|
|
|
mod tests {
|
2023-07-17 15:04:14 +00:00
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
use crate::{
|
|
|
|
source::{federation_allowlist::FederationAllowList, instance::Instance},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::build_db_pool_for_tests,
|
2022-10-27 09:24:07 +00:00
|
|
|
};
|
2024-01-04 09:47:18 +00:00
|
|
|
use pretty_assertions::assert_eq;
|
2022-10-27 09:24:07 +00:00
|
|
|
use serial_test::serial;
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[tokio::test]
|
2022-10-27 09:24:07 +00:00
|
|
|
#[serial]
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn test_allowlist_insert_and_clear() {
|
|
|
|
let pool = &build_db_pool_for_tests().await;
|
2023-07-11 13:09:59 +00:00
|
|
|
let pool = &mut pool.into();
|
2023-02-18 14:36:12 +00:00
|
|
|
let domains = vec![
|
2022-10-27 09:24:07 +00:00
|
|
|
"tld1.xyz".to_string(),
|
|
|
|
"tld2.xyz".to_string(),
|
|
|
|
"tld3.xyz".to_string(),
|
2023-02-18 14:36:12 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
let allowed = Some(domains.clone());
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
FederationAllowList::replace(pool, allowed).await.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let allows = Instance::allowlist(pool).await.unwrap();
|
2023-02-18 14:36:12 +00:00
|
|
|
let allows_domains = allows
|
|
|
|
.iter()
|
|
|
|
.map(|i| i.domain.clone())
|
|
|
|
.collect::<Vec<String>>();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
assert_eq!(3, allows.len());
|
2023-02-18 14:36:12 +00:00
|
|
|
assert_eq!(domains, allows_domains);
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
// Now test clearing them via Some(empty vec)
|
|
|
|
let clear_allows = Some(Vec::new());
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
FederationAllowList::replace(pool, clear_allows)
|
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
let allows = Instance::allowlist(pool).await.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
|
|
|
|
assert_eq!(0, allows.len());
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
Instance::delete_all(pool).await.unwrap();
|
2022-10-27 09:24:07 +00:00
|
|
|
}
|
|
|
|
}
|