X-Git-Url: http://these/git/?a=blobdiff_plain;f=crates%2Fdb_schema%2Fsrc%2Ftraits.rs;h=cb128339b19f16adc33b001f7f0fb1f7f325c5f1;hb=f7f6766650b9b573a72075e564aed353c0131cd7;hp=d4e0154714aba0920b5cc616ca5537ad0777722d;hpb=985fe24669d3fdeecc0aa76cc74dd6570cbad5c8;p=lemmy.git diff --git a/crates/db_schema/src/traits.rs b/crates/db_schema/src/traits.rs index d4e01547..cb128339 100644 --- a/crates/db_schema/src/traits.rs +++ b/crates/db_schema/src/traits.rs @@ -1,47 +1,81 @@ use crate::{ newtypes::{CommunityId, DbUrl, PersonId}, - utils::DbPool, + utils::{get_conn, DbPool}, }; -use diesel::result::Error; +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 = DeleteStatement<::Table, ::WhereClause>; +/// Returned by `Self::table().find(id)` +pub type Find = dsl::Find<::Table, ::IdType>; + +pub type PrimaryKey = <::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 { +pub trait Crud: HasTable + Sized +where + Self::Table: FindDsl, + Find: LimitDsl + IntoUpdateTarget + Send, + Delete>: ExecuteDsl + Send + 'static, + + // Used by `RunQueryDsl::first` + dsl::Limit>: LoadQuery<'static, AsyncPgConnection, Self> + Send + 'static, +{ type InsertForm; type UpdateForm; - type IdType; - async fn create(pool: &DbPool, form: &Self::InsertForm) -> Result - where - Self: Sized; - async fn read(pool: &DbPool, id: Self::IdType) -> Result - where - Self: Sized; + type IdType: Send; + + async fn create(pool: &mut DbPool<'_>, form: &Self::InsertForm) -> Result; + + async fn read(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { + let query: Find = Self::table().find(id); + let conn = &mut *get_conn(pool).await?; + query.first::(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: &DbPool, id: Self::IdType, form: &Self::UpdateForm) -> Result - where - Self: Sized; - async fn delete(_pool: &DbPool, _id: Self::IdType) -> Result - where - Self: Sized, - Self::IdType: Send, - { - async { Err(Error::NotFound) }.await + async fn update( + pool: &mut DbPool<'_>, + id: Self::IdType, + form: &Self::UpdateForm, + ) -> Result; + + async fn delete(pool: &mut DbPool<'_>, id: Self::IdType) -> Result { + let query: Delete> = 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: &DbPool, form: &Self::Form) -> Result + async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; async fn follow_accepted( - pool: &DbPool, + pool: &mut DbPool<'_>, community_id: CommunityId, person_id: PersonId, ) -> Result where Self: Sized; - async fn unfollow(pool: &DbPool, form: &Self::Form) -> Result + async fn unfollow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -49,10 +83,10 @@ pub trait Followable { #[async_trait] pub trait Joinable { type Form; - async fn join(pool: &DbPool, form: &Self::Form) -> Result + async fn join(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn leave(pool: &DbPool, form: &Self::Form) -> Result + async fn leave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -61,11 +95,11 @@ pub trait Joinable { pub trait Likeable { type Form; type IdType; - async fn like(pool: &DbPool, form: &Self::Form) -> Result + async fn like(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; async fn remove( - pool: &DbPool, + pool: &mut DbPool<'_>, person_id: PersonId, item_id: Self::IdType, ) -> Result @@ -76,10 +110,10 @@ pub trait Likeable { #[async_trait] pub trait Bannable { type Form; - async fn ban(pool: &DbPool, form: &Self::Form) -> Result + async fn ban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn unban(pool: &DbPool, form: &Self::Form) -> Result + async fn unban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -87,10 +121,10 @@ pub trait Bannable { #[async_trait] pub trait Saveable { type Form; - async fn save(pool: &DbPool, form: &Self::Form) -> Result + async fn save(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn unsave(pool: &DbPool, form: &Self::Form) -> Result + async fn unsave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -98,10 +132,10 @@ pub trait Saveable { #[async_trait] pub trait Blockable { type Form; - async fn block(pool: &DbPool, form: &Self::Form) -> Result + async fn block(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn unblock(pool: &DbPool, form: &Self::Form) -> Result + async fn unblock(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -109,10 +143,10 @@ pub trait Blockable { #[async_trait] pub trait Readable { type Form; - async fn mark_as_read(pool: &DbPool, form: &Self::Form) -> Result + async fn mark_as_read(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; - async fn mark_as_unread(pool: &DbPool, form: &Self::Form) -> Result + async fn mark_as_unread(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; } @@ -121,18 +155,26 @@ pub trait Readable { pub trait Reportable { type Form; type IdType; - async fn report(pool: &DbPool, form: &Self::Form) -> Result + type ObjectIdType; + async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result where Self: Sized; async fn resolve( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: Self::IdType, resolver_id: PersonId, ) -> Result + where + Self: Sized; + async fn resolve_all_for_object( + pool: &mut DbPool<'_>, + comment_id_: Self::ObjectIdType, + by_resolver_id: PersonId, + ) -> Result where Self: Sized; async fn unresolve( - pool: &DbPool, + pool: &mut DbPool<'_>, report_id: Self::IdType, resolver_id: PersonId, ) -> Result @@ -149,21 +191,23 @@ pub trait JoinView { #[async_trait] pub trait ApubActor { - // TODO: this should be in a trait ApubObject (and implemented for Post, Comment, PrivateMessage as well) - async fn read_from_apub_id(pool: &DbPool, object_id: &DbUrl) -> Result, Error> + async fn read_from_apub_id( + pool: &mut DbPool<'_>, + object_id: &DbUrl, + ) -> Result, 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: &DbPool, + pool: &mut DbPool<'_>, actor_name: &str, include_deleted: bool, ) -> Result where Self: Sized; async fn read_from_name_and_domain( - pool: &DbPool, + pool: &mut DbPool<'_>, actor_name: &str, protocol_domain: &str, ) -> Result