lemmy/crates/db_schema/src/traits.rs
dullbananas 91834d0d21
Default imprementations for read and delete in Crud trait (#3707)
* h

* Start doing stuff

* Default impl for Crud::read

* Simplify Crud::read lifetimes

* fmt

* Stuff

* Stuff

* Successfully make default read implementation work

* Restore Person::read

* Clean up default Crud::read and rename 'query2

* Replace filter with find

* Attempt default Crud::create

* Change Crud to Crud<'a> (won't compile)

* Revert "Change Crud to Crud<'a> (won't compile)"

This reverts commit 7ed20f5f71.

* Default Crud::delete

* Remove Crud::delete definitions that match default

* Remove commented Site::read

* Insert trait

* Revert "Insert trait"

This reverts commit 9d780c2403.

* Use non-borrowed forms

* Revert "Use non-borrowed forms"

This reverts commit d2dd442563.

* Revert "Revert "Change Crud to Crud<'a> (won't compile)""

This reverts commit 25a27165a8.

* Fix lifetime for everything except Crud::delete

* Fix Crud::delete

* Add comment about futures

* Attempt Crud::create

* Attempt separate CrudBounds

* Revert "Attempt separate CrudBounds"

This reverts commit 1b4ca321c3.

* Try to fix Crud::create

* Move lifetime parameters to associated types

* Revert "Move lifetime parameters to associated types"

This reverts commit af1bc858ce.

* Revert "Try to fix Crud::create"

This reverts commit eec238496c.

* Revert "Revert "Attempt separate CrudBounds""

This reverts commit 1ec33ce502.

* Revert "Attempt separate CrudBounds"

This reverts commit 1b4ca321c3.

* Revert "Attempt Crud::create"

This reverts commit 47e8071b68.

* Revert "Add comment about futures"

This reverts commit b266b14653.

* Revert "Fix Crud::delete"

This reverts commit 3abcce2eec.

* Revert "Fix lifetime for everything except Crud::delete"

This reverts commit c1ad7a161b.

* Revert "Revert "Revert "Change Crud to Crud<'a> (won't compile)"""

This reverts commit 3129cd0fc3.

* Clean up

* Update site.rs
2023-08-01 10:34:10 -04:00

208 lines
5.3 KiB
Rust

use crate::{
newtypes::{CommunityId, DbUrl, PersonId},
utils::{get_conn, DbPool},
};
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;
// 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
#[async_trait]
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,
{
type InsertForm;
type UpdateForm;
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
}
/// 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.
async fn update(
pool: &mut DbPool<'_>,
id: Self::IdType,
form: &Self::UpdateForm,
) -> 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
}
}
#[async_trait]
pub trait Followable {
type Form;
async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn follow_accepted(
pool: &mut DbPool<'_>,
community_id: CommunityId,
person_id: PersonId,
) -> Result<Self, Error>
where
Self: Sized;
async fn unfollow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
#[async_trait]
pub trait Joinable {
type Form;
async fn join(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn leave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
#[async_trait]
pub trait Likeable {
type Form;
type IdType;
async fn like(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn remove(
pool: &mut DbPool<'_>,
person_id: PersonId,
item_id: Self::IdType,
) -> Result<usize, Error>
where
Self: Sized;
}
#[async_trait]
pub trait Bannable {
type Form;
async fn ban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn unban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
#[async_trait]
pub trait Saveable {
type Form;
async fn save(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn unsave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
#[async_trait]
pub trait Blockable {
type Form;
async fn block(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn unblock(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
#[async_trait]
pub trait Readable {
type Form;
async fn mark_as_read(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn mark_as_unread(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
where
Self: Sized;
}
#[async_trait]
pub trait Reportable {
type Form;
type IdType;
async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
where
Self: Sized;
async fn resolve(
pool: &mut DbPool<'_>,
report_id: Self::IdType,
resolver_id: PersonId,
) -> Result<usize, Error>
where
Self: Sized;
async fn unresolve(
pool: &mut DbPool<'_>,
report_id: Self::IdType,
resolver_id: PersonId,
) -> Result<usize, Error>
where
Self: Sized;
}
pub trait JoinView {
type JoinTuple;
fn from_tuple(tuple: Self::JoinTuple) -> Self
where
Self: Sized;
}
#[async_trait]
pub trait ApubActor {
async fn read_from_apub_id(
pool: &mut DbPool<'_>,
object_id: &DbUrl,
) -> Result<Option<Self>, Error>
where
Self: Sized;
/// - 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
async fn read_from_name(
pool: &mut DbPool<'_>,
actor_name: &str,
include_deleted: bool,
) -> Result<Self, Error>
where
Self: Sized;
async fn read_from_name_and_domain(
pool: &mut DbPool<'_>,
actor_name: &str,
protocol_domain: &str,
) -> Result<Self, Error>
where
Self: Sized;
}