2022-11-09 10:05:00 +00:00
|
|
|
use crate::{
|
|
|
|
newtypes::{CommunityId, DbUrl, PersonId},
|
2023-08-01 14:34:10 +00:00
|
|
|
utils::{get_conn, DbPool},
|
2022-11-09 10:05:00 +00:00
|
|
|
};
|
2023-08-01 14:34:10 +00:00
|
|
|
use diesel::{
|
|
|
|
associations::HasTable,
|
|
|
|
dsl,
|
|
|
|
query_builder::{DeleteStatement, IntoUpdateTarget},
|
|
|
|
query_dsl::methods::{FindDsl, LimitDsl},
|
|
|
|
result::Error,
|
|
|
|
Table,
|
|
|
|
};
|
|
|
|
use diesel_async::{
|
|
|
|
methods::{ExecuteDsl, LoadQuery},
|
|
|
|
AsyncPgConnection,
|
|
|
|
RunQueryDsl,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Returned by `diesel::delete`
|
|
|
|
pub type Delete<T> = DeleteStatement<<T as HasTable>::Table, <T as IntoUpdateTarget>::WhereClause>;
|
|
|
|
|
|
|
|
/// Returned by `Self::table().find(id)`
|
|
|
|
pub type Find<T> = dsl::Find<<T as HasTable>::Table, <T as Crud>::IdType>;
|
|
|
|
|
|
|
|
pub type PrimaryKey<T> = <<T as HasTable>::Table as Table>::PrimaryKey;
|
2021-10-16 13:33:38 +00:00
|
|
|
|
2023-08-01 14:34:10 +00:00
|
|
|
// Trying to create default implementations for `create` and `update` results in a lifetime mess and weird compile errors.
|
|
|
|
// https://github.com/rust-lang/rust/issues/102211
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2023-08-01 14:34:10 +00:00
|
|
|
pub trait Crud: HasTable + Sized
|
|
|
|
where
|
|
|
|
Self::Table: FindDsl<Self::IdType>,
|
|
|
|
Find<Self>: LimitDsl + IntoUpdateTarget + Send,
|
|
|
|
Delete<Find<Self>>: ExecuteDsl<AsyncPgConnection> + Send + 'static,
|
|
|
|
|
|
|
|
// Used by `RunQueryDsl::first`
|
|
|
|
dsl::Limit<Find<Self>>: LoadQuery<'static, AsyncPgConnection, Self> + Send + 'static,
|
|
|
|
{
|
2022-10-27 09:24:07 +00:00
|
|
|
type InsertForm;
|
|
|
|
type UpdateForm;
|
2023-08-01 14:34:10 +00:00
|
|
|
type IdType: Send;
|
|
|
|
|
|
|
|
async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result<Self, Error>;
|
|
|
|
|
|
|
|
async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<Self, Error> {
|
|
|
|
let query: Find<Self> = Self::table().find(id);
|
|
|
|
let conn = &mut *get_conn(pool).await?;
|
|
|
|
query.first::<Self>(conn).await
|
|
|
|
}
|
|
|
|
|
2022-10-27 09:24:07 +00:00
|
|
|
/// when you want to null out a column, you have to send Some(None)), since sending None means you just don't want to update that column.
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn update(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
id: Self::IdType,
|
|
|
|
form: &Self::UpdateForm,
|
2023-08-01 14:34:10 +00:00
|
|
|
) -> Result<Self, Error>;
|
|
|
|
|
|
|
|
async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result<usize, Error> {
|
|
|
|
let query: Delete<Find<Self>> = diesel::delete(Self::table().find(id));
|
|
|
|
let conn = &mut *get_conn(pool).await?;
|
|
|
|
query.execute(conn).await
|
2021-10-16 13:33:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-10-16 13:33:38 +00:00
|
|
|
pub trait Followable {
|
|
|
|
type Form;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn follow_accepted(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-10-16 13:33:38 +00:00
|
|
|
community_id: CommunityId,
|
|
|
|
person_id: PersonId,
|
|
|
|
) -> Result<Self, Error>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn unfollow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-10-16 13:33:38 +00:00
|
|
|
pub trait Joinable {
|
|
|
|
type Form;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn join(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn leave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-10-16 13:33:38 +00:00
|
|
|
pub trait Likeable {
|
|
|
|
type Form;
|
|
|
|
type IdType;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn like(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn remove(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-10-16 13:33:38 +00:00
|
|
|
person_id: PersonId,
|
|
|
|
item_id: Self::IdType,
|
|
|
|
) -> Result<usize, Error>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-10-16 13:33:38 +00:00
|
|
|
pub trait Bannable {
|
|
|
|
type Form;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn ban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn unban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-10-16 13:33:38 +00:00
|
|
|
pub trait Saveable {
|
|
|
|
type Form;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn save(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn unsave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-10-16 13:33:38 +00:00
|
|
|
pub trait Blockable {
|
|
|
|
type Form;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn block(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn unblock(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2021-10-16 13:33:38 +00:00
|
|
|
pub trait Reportable {
|
|
|
|
type Form;
|
|
|
|
type IdType;
|
2024-02-15 13:52:04 +00:00
|
|
|
type ObjectIdType;
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn resolve(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-10-16 13:33:38 +00:00
|
|
|
report_id: Self::IdType,
|
|
|
|
resolver_id: PersonId,
|
|
|
|
) -> Result<usize, Error>
|
2024-02-15 13:52:04 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
async fn resolve_all_for_object(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
comment_id_: Self::ObjectIdType,
|
|
|
|
by_resolver_id: PersonId,
|
|
|
|
) -> Result<usize, Error>
|
2021-10-16 13:33:38 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn unresolve(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2021-10-16 13:33:38 +00:00
|
|
|
report_id: Self::IdType,
|
|
|
|
resolver_id: PersonId,
|
|
|
|
) -> Result<usize, Error>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
#[async_trait]
|
2022-01-27 16:39:22 +00:00
|
|
|
pub trait ApubActor {
|
2023-07-11 13:09:59 +00:00
|
|
|
async fn read_from_apub_id(
|
|
|
|
pool: &mut DbPool<'_>,
|
|
|
|
object_id: &DbUrl,
|
|
|
|
) -> Result<Option<Self>, Error>
|
2022-01-27 16:39:22 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2022-07-05 21:40:44 +00:00
|
|
|
/// - actor_name is the name of the community or user to read.
|
|
|
|
/// - include_deleted, if true, will return communities or users that were deleted/removed
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read_from_name(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-07-05 21:40:44 +00:00
|
|
|
actor_name: &str,
|
|
|
|
include_deleted: bool,
|
|
|
|
) -> Result<Self, Error>
|
2022-01-27 16:39:22 +00:00
|
|
|
where
|
|
|
|
Self: Sized;
|
2022-11-09 10:05:00 +00:00
|
|
|
async fn read_from_name_and_domain(
|
2023-07-11 13:09:59 +00:00
|
|
|
pool: &mut DbPool<'_>,
|
2022-01-27 16:39:22 +00:00
|
|
|
actor_name: &str,
|
|
|
|
protocol_domain: &str,
|
|
|
|
) -> Result<Self, Error>
|
|
|
|
where
|
|
|
|
Self: Sized;
|
|
|
|
}
|