]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/traits.rs
Automatically resolve report when post/comment is removed (#3850)
[lemmy.git] / crates / db_schema / src / traits.rs
index 6e6d2c700ac493fcf1ab00398ebbf0f4af5b0b37..cb128339b19f16adc33b001f7f0fb1f7f325c5f1 100644 (file)
-use crate::newtypes::{CommunityId, DbUrl, PersonId};
-use diesel::{result::Error, PgConnection};
+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,
+};
 
-pub trait Crud {
+/// 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;
-  fn create(conn: &mut PgConnection, form: &Self::InsertForm) -> Result<Self, Error>
-  where
-    Self: Sized;
-  fn read(conn: &mut PgConnection, id: Self::IdType) -> Result<Self, Error>
-  where
-    Self: Sized;
+  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.
-  fn update(
-    conn: &mut PgConnection,
+  async fn update(
+    pool: &mut DbPool<'_>,
     id: Self::IdType,
     form: &Self::UpdateForm,
-  ) -> Result<Self, Error>
-  where
-    Self: Sized;
-  fn delete(_conn: &mut PgConnection, _id: Self::IdType) -> Result<usize, Error>
-  where
-    Self: Sized,
-  {
-    unimplemented!()
+  ) -> 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;
-  fn follow(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
+  async fn follow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  fn follow_accepted(
-    conn: &mut PgConnection,
+  async fn follow_accepted(
+    pool: &mut DbPool<'_>,
     community_id: CommunityId,
     person_id: PersonId,
   ) -> Result<Self, Error>
   where
     Self: Sized;
-  fn unfollow(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
+  async fn unfollow(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
-  fn has_local_followers(conn: &mut PgConnection, community_id: CommunityId)
-    -> Result<bool, Error>;
 }
 
+#[async_trait]
 pub trait Joinable {
   type Form;
-  fn join(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
+  async fn join(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  fn leave(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
+  async fn leave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
 
+#[async_trait]
 pub trait Likeable {
   type Form;
   type IdType;
-  fn like(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
+  async fn like(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  fn remove(
-    conn: &mut PgConnection,
+  async fn remove(
+    pool: &mut DbPool<'_>,
     person_id: PersonId,
     item_id: Self::IdType,
   ) -> Result<usize, Error>
@@ -71,61 +107,74 @@ pub trait Likeable {
     Self: Sized;
 }
 
+#[async_trait]
 pub trait Bannable {
   type Form;
-  fn ban(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
+  async fn ban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  fn unban(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
+  async fn unban(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
 
+#[async_trait]
 pub trait Saveable {
   type Form;
-  fn save(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
+  async fn save(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  fn unsave(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
+  async fn unsave(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
 
+#[async_trait]
 pub trait Blockable {
   type Form;
-  fn block(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
+  async fn block(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  fn unblock(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
+  async fn unblock(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<usize, Error>
   where
     Self: Sized;
 }
 
+#[async_trait]
 pub trait Readable {
   type Form;
-  fn mark_as_read(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
+  async fn mark_as_read(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  fn mark_as_unread(conn: &mut PgConnection, form: &Self::Form) -> Result<usize, Error>
+  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;
-  fn report(conn: &mut PgConnection, form: &Self::Form) -> Result<Self, Error>
+  type ObjectIdType;
+  async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
-  fn resolve(
-    conn: &mut PgConnection,
+  async fn resolve(
+    pool: &mut DbPool<'_>,
     report_id: Self::IdType,
     resolver_id: PersonId,
   ) -> Result<usize, Error>
   where
     Self: Sized;
-  fn unresolve(
-    conn: &mut PgConnection,
+  async fn resolve_all_for_object(
+    pool: &mut DbPool<'_>,
+    comment_id_: Self::ObjectIdType,
+    by_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>
@@ -133,43 +182,32 @@ pub trait Reportable {
     Self: Sized;
 }
 
-pub trait DeleteableOrRemoveable {
-  fn blank_out_deleted_or_removed_info(self) -> Self;
-}
-
-pub trait ToSafe {
-  type SafeColumns;
-  fn safe_columns_tuple() -> Self::SafeColumns;
-}
-
-pub trait ToSafeSettings {
-  type SafeSettingsColumns;
-  fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns;
-}
-
-pub trait ViewToVec {
-  type DbTuple;
-  fn from_tuple_to_vec(tuple: Vec<Self::DbTuple>) -> Vec<Self>
+pub trait JoinView {
+  type JoinTuple;
+  fn from_tuple(tuple: Self::JoinTuple) -> Self
   where
     Self: Sized;
 }
 
+#[async_trait]
 pub trait ApubActor {
-  // TODO: this should be in a trait ApubObject (and implemented for Post, Comment, PrivateMessage as well)
-  fn read_from_apub_id(conn: &mut PgConnection, object_id: &DbUrl) -> Result<Option<Self>, Error>
+  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
-  fn read_from_name(
-    conn: &mut PgConnection,
+  async fn read_from_name(
+    pool: &mut DbPool<'_>,
     actor_name: &str,
     include_deleted: bool,
   ) -> Result<Self, Error>
   where
     Self: Sized;
-  fn read_from_name_and_domain(
-    conn: &mut PgConnection,
+  async fn read_from_name_and_domain(
+    pool: &mut DbPool<'_>,
     actor_name: &str,
     protocol_domain: &str,
   ) -> Result<Self, Error>