2 schema::federation_allowlist,
4 federation_allowlist::{FederationAllowList, FederationAllowListForm},
7 utils::{get_conn, DbPool},
9 use diesel::{dsl::insert_into, result::Error};
10 use diesel_async::{AsyncPgConnection, RunQueryDsl};
12 impl FederationAllowList {
13 pub async fn replace(pool: &mut DbPool<'_>, list_opt: Option<Vec<String>>) -> Result<(), Error> {
14 let conn = &mut get_conn(pool).await?;
19 if let Some(list) = list_opt {
20 Self::clear(conn).await?;
23 // Upsert all of these as instances
24 let instance = Instance::read_or_create(&mut conn.into(), domain).await?;
26 let form = FederationAllowListForm {
27 instance_id: instance.id,
30 insert_into(federation_allowlist::table)
32 .get_result::<Self>(conn)
44 async fn clear(conn: &mut AsyncPgConnection) -> Result<usize, Error> {
45 diesel::delete(federation_allowlist::table)
52 #![allow(clippy::unwrap_used)]
53 #![allow(clippy::indexing_slicing)]
56 source::{federation_allowlist::FederationAllowList, instance::Instance},
57 utils::build_db_pool_for_tests,
59 use serial_test::serial;
63 async fn test_allowlist_insert_and_clear() {
64 let pool = &build_db_pool_for_tests().await;
65 let pool = &mut pool.into();
67 "tld1.xyz".to_string(),
68 "tld2.xyz".to_string(),
69 "tld3.xyz".to_string(),
72 let allowed = Some(domains.clone());
74 FederationAllowList::replace(pool, allowed).await.unwrap();
76 let allows = Instance::allowlist(pool).await.unwrap();
77 let allows_domains = allows
79 .map(|i| i.domain.clone())
80 .collect::<Vec<String>>();
82 assert_eq!(3, allows.len());
83 assert_eq!(domains, allows_domains);
85 // Now test clearing them via Some(empty vec)
86 let clear_allows = Some(Vec::new());
88 FederationAllowList::replace(pool, clear_allows)
91 let allows = Instance::allowlist(pool).await.unwrap();
93 assert_eq!(0, allows.len());
95 Instance::delete_all(pool).await.unwrap();