diff --git a/crates/db_queries/src/lib.rs b/crates/db_queries/src/lib.rs index adc0717ac..fb5b8cd5d 100644 --- a/crates/db_queries/src/lib.rs +++ b/crates/db_queries/src/lib.rs @@ -25,17 +25,19 @@ pub mod source; pub type DbPool = diesel::r2d2::Pool>; -pub trait Crud { - fn create(conn: &PgConnection, form: &Form) -> Result +pub trait Crud { + type Form; + type IdType; + fn create(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; - fn read(conn: &PgConnection, id: IdType) -> Result + fn read(conn: &PgConnection, id: Self::IdType) -> Result where Self: Sized; - fn update(conn: &PgConnection, id: IdType, form: &Form) -> Result + fn update(conn: &PgConnection, id: Self::IdType, form: &Self::Form) -> Result where Self: Sized; - fn delete(_conn: &PgConnection, _id: IdType) -> Result + fn delete(_conn: &PgConnection, _id: Self::IdType) -> Result where Self: Sized, { @@ -43,8 +45,9 @@ pub trait Crud { } } -pub trait Followable
{ - fn follow(conn: &PgConnection, form: &Form) -> Result +pub trait Followable { + type Form; + fn follow(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; fn follow_accepted( @@ -54,59 +57,70 @@ pub trait Followable { ) -> Result where Self: Sized; - fn unfollow(conn: &PgConnection, form: &Form) -> Result + fn unfollow(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; fn has_local_followers(conn: &PgConnection, community_id: CommunityId) -> Result; } -pub trait Joinable { - fn join(conn: &PgConnection, form: &Form) -> Result +pub trait Joinable { + type Form; + fn join(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; - fn leave(conn: &PgConnection, form: &Form) -> Result + fn leave(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; } -pub trait Likeable { - fn like(conn: &PgConnection, form: &Form) -> Result +pub trait Likeable { + type Form; + type IdType; + fn like(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; - fn remove(conn: &PgConnection, person_id: PersonId, item_id: IdType) -> Result + fn remove( + conn: &PgConnection, + person_id: PersonId, + item_id: Self::IdType, + ) -> Result where Self: Sized; } -pub trait Bannable { - fn ban(conn: &PgConnection, form: &Form) -> Result +pub trait Bannable { + type Form; + fn ban(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; - fn unban(conn: &PgConnection, form: &Form) -> Result + fn unban(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; } -pub trait Saveable { - fn save(conn: &PgConnection, form: &Form) -> Result +pub trait Saveable { + type Form; + fn save(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; - fn unsave(conn: &PgConnection, form: &Form) -> Result + fn unsave(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; } -pub trait Readable { - fn mark_as_read(conn: &PgConnection, form: &Form) -> Result +pub trait Readable { + type Form; + fn mark_as_read(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; - fn mark_as_unread(conn: &PgConnection, form: &Form) -> Result + fn mark_as_unread(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; } -pub trait Reportable { - fn report(conn: &PgConnection, form: &Form) -> Result +pub trait Reportable { + type Form; + fn report(conn: &PgConnection, form: &Self::Form) -> Result where Self: Sized; fn resolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result @@ -121,11 +135,12 @@ pub trait DeleteableOrRemoveable { fn blank_out_deleted_or_removed_info(self) -> Self; } -pub trait ApubObject { +pub trait ApubObject { + type Form; fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result where Self: Sized; - fn upsert(conn: &PgConnection, user_form: &Form) -> Result + fn upsert(conn: &PgConnection, user_form: &Self::Form) -> Result where Self: Sized; } diff --git a/crates/db_queries/src/source/activity.rs b/crates/db_queries/src/source/activity.rs index f87cf7330..1f158370e 100644 --- a/crates/db_queries/src/source/activity.rs +++ b/crates/db_queries/src/source/activity.rs @@ -9,7 +9,9 @@ use std::{ io::{Error as IoError, ErrorKind}, }; -impl Crud for Activity { +impl Crud for Activity { + type Form = ActivityForm; + type IdType = i32; fn read(conn: &PgConnection, activity_id: i32) -> Result { use lemmy_db_schema::schema::activity::dsl::*; activity.find(activity_id).first::(conn) diff --git a/crates/db_queries/src/source/comment.rs b/crates/db_queries/src/source/comment.rs index d0171cb8d..1e9722714 100644 --- a/crates/db_queries/src/source/comment.rs +++ b/crates/db_queries/src/source/comment.rs @@ -135,7 +135,9 @@ impl Comment_ for Comment { } } -impl Crud for Comment { +impl Crud for Comment { + type Form = CommentForm; + type IdType = CommentId; fn read(conn: &PgConnection, comment_id: CommentId) -> Result { use lemmy_db_schema::schema::comment::dsl::*; comment.find(comment_id).first::(conn) @@ -165,7 +167,8 @@ impl Crud for Comment { } } -impl ApubObject for Comment { +impl ApubObject for Comment { + type Form = CommentForm; fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result { use lemmy_db_schema::schema::comment::dsl::*; comment.filter(ap_id.eq(object_id)).first::(conn) @@ -182,7 +185,9 @@ impl ApubObject for Comment { } } -impl Likeable for CommentLike { +impl Likeable for CommentLike { + type Form = CommentLikeForm; + type IdType = CommentId; fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result { use lemmy_db_schema::schema::comment_like::dsl::*; insert_into(comment_like) @@ -207,7 +212,8 @@ impl Likeable for CommentLike { } } -impl Saveable for CommentSaved { +impl Saveable for CommentSaved { + type Form = CommentSavedForm; fn save(conn: &PgConnection, comment_saved_form: &CommentSavedForm) -> Result { use lemmy_db_schema::schema::comment_saved::dsl::*; insert_into(comment_saved) diff --git a/crates/db_queries/src/source/comment_report.rs b/crates/db_queries/src/source/comment_report.rs index 87b936062..738ec3e29 100644 --- a/crates/db_queries/src/source/comment_report.rs +++ b/crates/db_queries/src/source/comment_report.rs @@ -6,7 +6,8 @@ use lemmy_db_schema::{ PersonId, }; -impl Reportable for CommentReport { +impl Reportable for CommentReport { + type Form = CommentReportForm; /// creates a comment report and returns it /// /// * `conn` - the postgres connection diff --git a/crates/db_queries/src/source/community.rs b/crates/db_queries/src/source/community.rs index 2a6219339..eaaa2ba29 100644 --- a/crates/db_queries/src/source/community.rs +++ b/crates/db_queries/src/source/community.rs @@ -60,7 +60,9 @@ mod safe_type { } } -impl Crud for Community { +impl Crud for Community { + type Form = CommunityForm; + type IdType = CommunityId; fn read(conn: &PgConnection, community_id: CommunityId) -> Result { use lemmy_db_schema::schema::community::dsl::*; community.find(community_id).first::(conn) @@ -90,7 +92,8 @@ impl Crud for Community { } } -impl ApubObject for Community { +impl ApubObject for Community { + type Form = CommunityForm; fn read_from_apub_id(conn: &PgConnection, for_actor_id: &DbUrl) -> Result { use lemmy_db_schema::schema::community::dsl::*; community @@ -175,7 +178,8 @@ impl Community_ for Community { } } -impl Joinable for CommunityModerator { +impl Joinable for CommunityModerator { + type Form = CommunityModeratorForm; fn join( conn: &PgConnection, community_moderator_form: &CommunityModeratorForm, @@ -252,7 +256,8 @@ impl CommunityModerator_ for CommunityModerator { } } -impl Bannable for CommunityPersonBan { +impl Bannable for CommunityPersonBan { + type Form = CommunityPersonBanForm; fn ban( conn: &PgConnection, community_person_ban_form: &CommunityPersonBanForm, @@ -277,7 +282,8 @@ impl Bannable for CommunityPersonBan { } } -impl Followable for CommunityFollower { +impl Followable for CommunityFollower { + type Form = CommunityFollowerForm; fn follow( conn: &PgConnection, community_follower_form: &CommunityFollowerForm, diff --git a/crates/db_queries/src/source/local_user.rs b/crates/db_queries/src/source/local_user.rs index b711c82aa..91eac3308 100644 --- a/crates/db_queries/src/source/local_user.rs +++ b/crates/db_queries/src/source/local_user.rs @@ -91,7 +91,9 @@ impl LocalUser_ for LocalUser { } } -impl Crud for LocalUser { +impl Crud for LocalUser { + type Form = LocalUserForm; + type IdType = LocalUserId; fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result { local_user.find(local_user_id).first::(conn) } diff --git a/crates/db_queries/src/source/moderator.rs b/crates/db_queries/src/source/moderator.rs index 14bb07f1c..25ef1416d 100644 --- a/crates/db_queries/src/source/moderator.rs +++ b/crates/db_queries/src/source/moderator.rs @@ -2,7 +2,9 @@ use crate::Crud; use diesel::{dsl::*, result::Error, *}; use lemmy_db_schema::source::moderator::*; -impl Crud for ModRemovePost { +impl Crud for ModRemovePost { + type Form = ModRemovePostForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_remove_post::dsl::*; mod_remove_post.find(from_id).first::(conn) @@ -23,7 +25,9 @@ impl Crud for ModRemovePost { } } -impl Crud for ModLockPost { +impl Crud for ModLockPost { + type Form = ModLockPostForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_lock_post::dsl::*; mod_lock_post.find(from_id).first::(conn) @@ -44,7 +48,9 @@ impl Crud for ModLockPost { } } -impl Crud for ModStickyPost { +impl Crud for ModStickyPost { + type Form = ModStickyPostForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_sticky_post::dsl::*; mod_sticky_post.find(from_id).first::(conn) @@ -65,7 +71,9 @@ impl Crud for ModStickyPost { } } -impl Crud for ModRemoveComment { +impl Crud for ModRemoveComment { + type Form = ModRemoveCommentForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_remove_comment::dsl::*; mod_remove_comment.find(from_id).first::(conn) @@ -86,7 +94,9 @@ impl Crud for ModRemoveComment { } } -impl Crud for ModRemoveCommunity { +impl Crud for ModRemoveCommunity { + type Form = ModRemoveCommunityForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_remove_community::dsl::*; mod_remove_community.find(from_id).first::(conn) @@ -111,7 +121,9 @@ impl Crud for ModRemoveCommunity { } } -impl Crud for ModBanFromCommunity { +impl Crud for ModBanFromCommunity { + type Form = ModBanFromCommunityForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_ban_from_community::dsl::*; mod_ban_from_community.find(from_id).first::(conn) @@ -136,7 +148,9 @@ impl Crud for ModBanFromCommunity { } } -impl Crud for ModBan { +impl Crud for ModBan { + type Form = ModBanForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_ban::dsl::*; mod_ban.find(from_id).first::(conn) @@ -155,7 +169,9 @@ impl Crud for ModBan { } } -impl Crud for ModAddCommunity { +impl Crud for ModAddCommunity { + type Form = ModAddCommunityForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_add_community::dsl::*; mod_add_community.find(from_id).first::(conn) @@ -176,7 +192,9 @@ impl Crud for ModAddCommunity { } } -impl Crud for ModAdd { +impl Crud for ModAdd { + type Form = ModAddForm; + type IdType = i32; fn read(conn: &PgConnection, from_id: i32) -> Result { use lemmy_db_schema::schema::mod_add::dsl::*; mod_add.find(from_id).first::(conn) diff --git a/crates/db_queries/src/source/password_reset_request.rs b/crates/db_queries/src/source/password_reset_request.rs index d04a1ac4b..a71cdae26 100644 --- a/crates/db_queries/src/source/password_reset_request.rs +++ b/crates/db_queries/src/source/password_reset_request.rs @@ -7,7 +7,9 @@ use lemmy_db_schema::{ }; use sha2::{Digest, Sha256}; -impl Crud for PasswordResetRequest { +impl Crud for PasswordResetRequest { + type Form = PasswordResetRequestForm; + type IdType = i32; fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result { password_reset_request .find(password_reset_request_id) diff --git a/crates/db_queries/src/source/person.rs b/crates/db_queries/src/source/person.rs index a7266aa27..6172b4e1e 100644 --- a/crates/db_queries/src/source/person.rs +++ b/crates/db_queries/src/source/person.rs @@ -158,7 +158,9 @@ mod safe_type_alias_2 { } } -impl Crud for Person { +impl Crud for Person { + type Form = PersonForm; + type IdType = PersonId; fn read(conn: &PgConnection, person_id: PersonId) -> Result { person .filter(deleted.eq(false)) @@ -178,7 +180,8 @@ impl Crud for Person { } } -impl ApubObject for Person { +impl ApubObject for Person { + type Form = PersonForm; fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result { use lemmy_db_schema::schema::person::dsl::*; person diff --git a/crates/db_queries/src/source/person_mention.rs b/crates/db_queries/src/source/person_mention.rs index 456894200..774f79185 100644 --- a/crates/db_queries/src/source/person_mention.rs +++ b/crates/db_queries/src/source/person_mention.rs @@ -2,7 +2,9 @@ use crate::Crud; use diesel::{dsl::*, result::Error, *}; use lemmy_db_schema::{source::person_mention::*, PersonId, PersonMentionId}; -impl Crud for PersonMention { +impl Crud for PersonMention { + type Form = PersonMentionForm; + type IdType = PersonMentionId; fn read(conn: &PgConnection, person_mention_id: PersonMentionId) -> Result { use lemmy_db_schema::schema::person_mention::dsl::*; person_mention.find(person_mention_id).first::(conn) diff --git a/crates/db_queries/src/source/post.rs b/crates/db_queries/src/source/post.rs index f0cf10935..02ae4d6e2 100644 --- a/crates/db_queries/src/source/post.rs +++ b/crates/db_queries/src/source/post.rs @@ -18,7 +18,9 @@ use lemmy_db_schema::{ PostId, }; -impl Crud for Post { +impl Crud for Post { + type Form = PostForm; + type IdType = PostId; fn read(conn: &PgConnection, post_id: PostId) -> Result { use lemmy_db_schema::schema::post::dsl::*; post.find(post_id).first::(conn) @@ -179,7 +181,8 @@ impl Post_ for Post { } } -impl ApubObject for Post { +impl ApubObject for Post { + type Form = PostForm; fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result { use lemmy_db_schema::schema::post::dsl::*; post.filter(ap_id.eq(object_id)).first::(conn) @@ -196,7 +199,9 @@ impl ApubObject for Post { } } -impl Likeable for PostLike { +impl Likeable for PostLike { + type Form = PostLikeForm; + type IdType = PostId; fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result { use lemmy_db_schema::schema::post_like::dsl::*; insert_into(post_like) @@ -217,7 +222,8 @@ impl Likeable for PostLike { } } -impl Saveable for PostSaved { +impl Saveable for PostSaved { + type Form = PostSavedForm; fn save(conn: &PgConnection, post_saved_form: &PostSavedForm) -> Result { use lemmy_db_schema::schema::post_saved::dsl::*; insert_into(post_saved) @@ -238,7 +244,8 @@ impl Saveable for PostSaved { } } -impl Readable for PostRead { +impl Readable for PostRead { + type Form = PostReadForm; fn mark_as_read(conn: &PgConnection, post_read_form: &PostReadForm) -> Result { use lemmy_db_schema::schema::post_read::dsl::*; insert_into(post_read) diff --git a/crates/db_queries/src/source/post_report.rs b/crates/db_queries/src/source/post_report.rs index b4e855731..19cd5dfea 100644 --- a/crates/db_queries/src/source/post_report.rs +++ b/crates/db_queries/src/source/post_report.rs @@ -2,7 +2,8 @@ use crate::Reportable; use diesel::{dsl::*, result::Error, *}; use lemmy_db_schema::{naive_now, source::post_report::*, PersonId}; -impl Reportable for PostReport { +impl Reportable for PostReport { + type Form = PostReportForm; /// creates a post report and returns it /// /// * `conn` - the postgres connection diff --git a/crates/db_queries/src/source/private_message.rs b/crates/db_queries/src/source/private_message.rs index da1c5abd5..c1138b979 100644 --- a/crates/db_queries/src/source/private_message.rs +++ b/crates/db_queries/src/source/private_message.rs @@ -2,7 +2,9 @@ use crate::{ApubObject, Crud, DeleteableOrRemoveable}; use diesel::{dsl::*, result::Error, *}; use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl, PersonId, PrivateMessageId}; -impl Crud for PrivateMessage { +impl Crud for PrivateMessage { + type Form = PrivateMessageForm; + type IdType = PrivateMessageId; fn read(conn: &PgConnection, private_message_id: PrivateMessageId) -> Result { use lemmy_db_schema::schema::private_message::dsl::*; private_message.find(private_message_id).first::(conn) @@ -27,7 +29,8 @@ impl Crud for PrivateMessage { } } -impl ApubObject for PrivateMessage { +impl ApubObject for PrivateMessage { + type Form = PrivateMessageForm; fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result where Self: Sized, diff --git a/crates/db_queries/src/source/site.rs b/crates/db_queries/src/source/site.rs index d688546f8..b2bad7138 100644 --- a/crates/db_queries/src/source/site.rs +++ b/crates/db_queries/src/source/site.rs @@ -2,7 +2,9 @@ use crate::Crud; use diesel::{dsl::*, result::Error, *}; use lemmy_db_schema::{naive_now, source::site::*, PersonId}; -impl Crud for Site { +impl Crud for Site { + type Form = SiteForm; + type IdType = i32; fn read(conn: &PgConnection, _site_id: i32) -> Result { use lemmy_db_schema::schema::site::dsl::*; site.first::(conn)