3 schema::{federation_allowlist, federation_blocklist, instance},
4 source::instance::{Instance, InstanceForm},
5 utils::{get_conn, naive_now, DbPool},
7 use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl};
8 use diesel_async::{AsyncPgConnection, RunQueryDsl};
11 pub(crate) async fn read_or_create_with_conn(
12 conn: &mut AsyncPgConnection,
14 ) -> Result<Self, Error> {
15 use crate::schema::instance::domain;
16 // First try to read the instance row and return directly if found
17 let instance = instance::table
18 .filter(domain.eq(&domain_))
23 Err(diesel::NotFound) => {
24 // Instance not in database yet, insert it
25 let form = InstanceForm::builder()
27 .updated(Some(naive_now()))
29 insert_into(instance::table)
31 // Necessary because this method may be called concurrently for the same domain. This
32 // could be handled with a transaction, but nested transactions arent allowed
33 .on_conflict(instance::domain)
36 .get_result::<Self>(conn)
43 /// Attempt to read Instance column for the given domain. If it doesnt exist, insert a new one.
44 /// There is no need for update as the domain of an existing instance cant change.
45 pub async fn read_or_create(pool: &DbPool, domain: String) -> Result<Self, Error> {
46 let conn = &mut get_conn(pool).await?;
47 Self::read_or_create_with_conn(conn, domain).await
49 pub async fn delete(pool: &DbPool, instance_id: InstanceId) -> Result<usize, Error> {
50 let conn = &mut get_conn(pool).await?;
51 diesel::delete(instance::table.find(instance_id))
56 pub async fn delete_all(pool: &DbPool) -> Result<usize, Error> {
57 let conn = &mut get_conn(pool).await?;
58 diesel::delete(instance::table).execute(conn).await
60 pub async fn allowlist(pool: &DbPool) -> Result<Vec<Self>, Error> {
61 let conn = &mut get_conn(pool).await?;
63 .inner_join(federation_allowlist::table)
64 .select(instance::all_columns)
69 pub async fn blocklist(pool: &DbPool) -> Result<Vec<Self>, Error> {
70 let conn = &mut get_conn(pool).await?;
72 .inner_join(federation_blocklist::table)
73 .select(instance::all_columns)
78 pub async fn linked(pool: &DbPool) -> Result<Vec<Self>, Error> {
79 let conn = &mut get_conn(pool).await?;
81 .left_join(federation_blocklist::table)
82 .filter(federation_blocklist::id.is_null())
83 .select(instance::all_columns)