diff --git a/lemmy_api/src/community.rs b/lemmy_api/src/community.rs index 762420202..d7de0e6bd 100644 --- a/lemmy_api/src/community.rs +++ b/lemmy_api/src/community.rs @@ -20,6 +20,7 @@ use lemmy_db::{ post::Post, site::*, user_view::*, + ApubObject, Bannable, Crud, Followable, @@ -129,7 +130,7 @@ impl Perform for CreateCommunity { let actor_id = make_apub_endpoint(EndpointType::Community, &data.name).to_string(); let actor_id_cloned = actor_id.to_owned(); let community_dupe = blocking(context.pool(), move |conn| { - Community::read_from_actor_id(conn, &actor_id_cloned) + Community::read_from_apub_id(conn, &actor_id_cloned) }) .await?; if community_dupe.is_ok() { diff --git a/lemmy_apub/src/activities/receive/community.rs b/lemmy_apub/src/activities/receive/community.rs index ed43b33e3..b1866283b 100644 --- a/lemmy_apub/src/activities/receive/community.rs +++ b/lemmy_apub/src/activities/receive/community.rs @@ -4,7 +4,7 @@ use activitystreams::{ base::{AnyBase, ExtendsExt}, }; use anyhow::Context; -use lemmy_db::{community::Community, community_view::CommunityView}; +use lemmy_db::{community::Community, community_view::CommunityView, ApubObject}; use lemmy_structs::{blocking, community::CommunityResponse}; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation}; @@ -53,7 +53,7 @@ pub(crate) async fn receive_remove_community( .single_xsd_any_uri() .context(location_info!())?; let community = blocking(context.pool(), move |conn| { - Community::read_from_actor_id(conn, community_uri.as_str()) + Community::read_from_apub_id(conn, community_uri.as_str()) }) .await??; @@ -135,7 +135,7 @@ pub(crate) async fn receive_undo_remove_community( .single_xsd_any_uri() .context(location_info!())?; let community = blocking(context.pool(), move |conn| { - Community::read_from_actor_id(conn, community_uri.as_str()) + Community::read_from_apub_id(conn, community_uri.as_str()) }) .await??; diff --git a/lemmy_apub/src/activities/send/user.rs b/lemmy_apub/src/activities/send/user.rs index a94f241da..8c539c4b0 100644 --- a/lemmy_apub/src/activities/send/user.rs +++ b/lemmy_apub/src/activities/send/user.rs @@ -16,6 +16,7 @@ use activitystreams::{ use lemmy_db::{ community::{Community, CommunityFollower, CommunityFollowerForm}, user::User_, + ApubObject, DbPool, Followable, }; @@ -46,7 +47,7 @@ impl ActorType for User_ { ) -> Result<(), LemmyError> { let follow_actor_id = follow_actor_id.to_string(); let community = blocking(context.pool(), move |conn| { - Community::read_from_actor_id(conn, &follow_actor_id) + Community::read_from_apub_id(conn, &follow_actor_id) }) .await??; @@ -77,7 +78,7 @@ impl ActorType for User_ { ) -> Result<(), LemmyError> { let follow_actor_id = follow_actor_id.to_string(); let community = blocking(context.pool(), move |conn| { - Community::read_from_actor_id(conn, &follow_actor_id) + Community::read_from_apub_id(conn, &follow_actor_id) }) .await??; diff --git a/lemmy_apub/src/fetcher.rs b/lemmy_apub/src/fetcher.rs index 0fb0cb10c..a29c1fe97 100644 --- a/lemmy_apub/src/fetcher.rs +++ b/lemmy_apub/src/fetcher.rs @@ -22,6 +22,7 @@ use lemmy_db::{ post_view::PostView, user::User_, user_view::UserView, + ApubObject, Joinable, SearchType, }; @@ -236,7 +237,7 @@ pub(crate) async fn get_or_fetch_and_upsert_user( ) -> Result { let apub_id_owned = apub_id.to_owned(); let user = blocking(context.pool(), move |conn| { - User_::read_from_actor_id(conn, apub_id_owned.as_ref()) + User_::read_from_apub_id(conn, apub_id_owned.as_ref()) }) .await?; @@ -314,7 +315,7 @@ pub(crate) async fn get_or_fetch_and_upsert_community( ) -> Result { let apub_id_owned = apub_id.to_owned(); let community = blocking(context.pool(), move |conn| { - Community::read_from_actor_id(conn, apub_id_owned.as_str()) + Community::read_from_apub_id(conn, apub_id_owned.as_str()) }) .await?; diff --git a/lemmy_apub/src/inbox/community_inbox.rs b/lemmy_apub/src/inbox/community_inbox.rs index 137f3fea4..6acdfa0d8 100644 --- a/lemmy_apub/src/inbox/community_inbox.rs +++ b/lemmy_apub/src/inbox/community_inbox.rs @@ -29,6 +29,7 @@ use lemmy_db::{ community::{Community, CommunityFollower, CommunityFollowerForm}, community_view::CommunityUserBanView, user::User_, + ApubObject, DbPool, Followable, }; @@ -116,7 +117,7 @@ pub(crate) async fn community_receive_message( // unconditionally. let actor_id = actor.actor_id_str(); let user = blocking(&context.pool(), move |conn| { - User_::read_from_actor_id(&conn, &actor_id) + User_::read_from_apub_id(&conn, &actor_id) }) .await??; check_community_or_site_ban(&user, &to_community, context.pool()).await?; @@ -240,7 +241,7 @@ async fn handle_undo_follow( verify_activity_domains_valid(&follow, &user_url, false)?; let user = blocking(&context.pool(), move |conn| { - User_::read_from_actor_id(&conn, user_url.as_str()) + User_::read_from_apub_id(&conn, user_url.as_str()) }) .await??; let community_follower_form = CommunityFollowerForm { diff --git a/lemmy_apub/src/inbox/mod.rs b/lemmy_apub/src/inbox/mod.rs index 4fdbb7a53..8b37f5be9 100644 --- a/lemmy_apub/src/inbox/mod.rs +++ b/lemmy_apub/src/inbox/mod.rs @@ -12,7 +12,7 @@ use activitystreams::{ }; use actix_web::HttpRequest; use anyhow::{anyhow, Context}; -use lemmy_db::{activity::Activity, community::Community, user::User_, DbPool}; +use lemmy_db::{activity::Activity, community::Community, user::User_, ApubObject, DbPool}; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; @@ -119,7 +119,7 @@ pub(crate) async fn is_addressed_to_local_user( ) -> Result { for url in to_and_cc { let url = url.to_string(); - let user = blocking(&pool, move |conn| User_::read_from_actor_id(&conn, &url)).await?; + let user = blocking(&pool, move |conn| User_::read_from_apub_id(&conn, &url)).await?; if let Ok(u) = user { if u.local { return Ok(true); @@ -141,7 +141,7 @@ pub(crate) async fn is_addressed_to_community_followers( if url.ends_with("/followers") { let community_url = url.replace("/followers", ""); let community = blocking(&pool, move |conn| { - Community::read_from_actor_id(&conn, &community_url) + Community::read_from_apub_id(&conn, &community_url) }) .await??; if !community.local { diff --git a/lemmy_apub/src/inbox/receive_for_community.rs b/lemmy_apub/src/inbox/receive_for_community.rs index b6dfa1e42..710cf17bf 100644 --- a/lemmy_apub/src/inbox/receive_for_community.rs +++ b/lemmy_apub/src/inbox/receive_for_community.rs @@ -40,7 +40,7 @@ use activitystreams::{ }; use anyhow::Context; use diesel::result::Error::NotFound; -use lemmy_db::{comment::Comment, post::Post, site::Site, Crud}; +use lemmy_db::{comment::Comment, post::Post, site::Site, ApubObject, Crud}; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; diff --git a/lemmy_apub/src/inbox/shared_inbox.rs b/lemmy_apub/src/inbox/shared_inbox.rs index dfd583662..9b5bdd6f4 100644 --- a/lemmy_apub/src/inbox/shared_inbox.rs +++ b/lemmy_apub/src/inbox/shared_inbox.rs @@ -14,7 +14,7 @@ use crate::{ use activitystreams::{activity::ActorAndObject, prelude::*}; use actix_web::{web, HttpRequest, HttpResponse}; use anyhow::Context; -use lemmy_db::{community::Community, DbPool}; +use lemmy_db::{community::Community, ApubObject, DbPool}; use lemmy_structs::blocking; use lemmy_utils::{location_info, LemmyError}; use lemmy_websocket::LemmyContext; @@ -135,10 +135,7 @@ async fn extract_local_community_from_destinations( ) -> Result, LemmyError> { for url in to_and_cc { let url = url.to_string(); - let community = blocking(&pool, move |conn| { - Community::read_from_actor_id(&conn, &url) - }) - .await?; + let community = blocking(&pool, move |conn| Community::read_from_apub_id(&conn, &url)).await?; if let Ok(c) = community { if c.local { return Ok(Some(c)); diff --git a/lemmy_apub/src/inbox/user_inbox.rs b/lemmy_apub/src/inbox/user_inbox.rs index dfcb2d618..76fa7d623 100644 --- a/lemmy_apub/src/inbox/user_inbox.rs +++ b/lemmy_apub/src/inbox/user_inbox.rs @@ -51,6 +51,7 @@ use lemmy_db::{ community::{Community, CommunityFollower}, private_message::PrivateMessage, user::User_, + ApubObject, Followable, }; use lemmy_structs::blocking; @@ -375,7 +376,7 @@ async fn find_community_or_private_message_by_id( ) -> Result { let ap_id = apub_id.to_string(); let community = blocking(context.pool(), move |conn| { - Community::read_from_actor_id(conn, &ap_id) + Community::read_from_apub_id(conn, &ap_id) }) .await?; if let Ok(c) = community { diff --git a/lemmy_apub/src/objects/comment.rs b/lemmy_apub/src/objects/comment.rs index 1454a738c..52e9320ef 100644 --- a/lemmy_apub/src/objects/comment.rs +++ b/lemmy_apub/src/objects/comment.rs @@ -26,6 +26,7 @@ use lemmy_db::{ community::Community, post::Post, user::User_, + ApubObject, Crud, DbPool, }; diff --git a/lemmy_apub/src/objects/community.rs b/lemmy_apub/src/objects/community.rs index 57cc47373..eababf515 100644 --- a/lemmy_apub/src/objects/community.rs +++ b/lemmy_apub/src/objects/community.rs @@ -25,6 +25,7 @@ use lemmy_db::{ community::{Community, CommunityForm}, community_view::CommunityModeratorView, naive_now, + ApubObject, DbPool, }; use lemmy_structs::blocking; @@ -124,7 +125,7 @@ impl FromApub for Community { let domain = community_id.domain().context(location_info!())?; if domain == Settings::get().hostname { let community = blocking(context.pool(), move |conn| { - Community::read_from_actor_id(conn, community_id.as_str()) + Community::read_from_apub_id(conn, community_id.as_str()) }) .await??; Ok(community) diff --git a/lemmy_apub/src/objects/post.rs b/lemmy_apub/src/objects/post.rs index 7151ee332..98a9c2eef 100644 --- a/lemmy_apub/src/objects/post.rs +++ b/lemmy_apub/src/objects/post.rs @@ -23,6 +23,7 @@ use lemmy_db::{ community::Community, post::{Post, PostForm}, user::User_, + ApubObject, Crud, DbPool, }; diff --git a/lemmy_apub/src/objects/user.rs b/lemmy_apub/src/objects/user.rs index 7162a1170..4fce30a62 100644 --- a/lemmy_apub/src/objects/user.rs +++ b/lemmy_apub/src/objects/user.rs @@ -21,6 +21,7 @@ use anyhow::Context; use lemmy_db::{ naive_now, user::{UserForm, User_}, + ApubObject, DbPool, }; use lemmy_structs::blocking; @@ -101,7 +102,7 @@ impl FromApub for User_ { let domain = user_id.domain().context(location_info!())?; if domain == Settings::get().hostname { let user = blocking(context.pool(), move |conn| { - User_::read_from_actor_id(conn, user_id.as_str()) + User_::read_from_apub_id(conn, user_id.as_str()) }) .await??; Ok(user) diff --git a/lemmy_db/src/comment.rs b/lemmy_db/src/comment.rs index 9b0928257..e9568bacf 100644 --- a/lemmy_db/src/comment.rs +++ b/lemmy_db/src/comment.rs @@ -2,6 +2,7 @@ use super::post::Post; use crate::{ naive_now, schema::{comment, comment_like, comment_saved}, + ApubObject, Crud, Likeable, Saveable, @@ -86,6 +87,13 @@ impl Crud for Comment { } } +impl ApubObject for Comment { + fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { + use crate::schema::comment::dsl::*; + comment.filter(ap_id.eq(object_id)).first::(conn) + } +} + impl Comment { pub fn update_ap_id( conn: &PgConnection, @@ -99,11 +107,6 @@ impl Comment { .get_result::(conn) } - pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { - use crate::schema::comment::dsl::*; - comment.filter(ap_id.eq(object_id)).first::(conn) - } - pub fn permadelete_for_creator( conn: &PgConnection, for_creator_id: i32, diff --git a/lemmy_db/src/community.rs b/lemmy_db/src/community.rs index 5f76d5143..5c3ee551d 100644 --- a/lemmy_db/src/community.rs +++ b/lemmy_db/src/community.rs @@ -1,6 +1,7 @@ use crate::{ naive_now, schema::{community, community_follower, community_moderator, community_user_ban}, + ApubObject, Bannable, Crud, Followable, @@ -83,6 +84,15 @@ impl Crud for Community { } } +impl ApubObject for Community { + fn read_from_apub_id(conn: &PgConnection, for_actor_id: &str) -> Result { + use crate::schema::community::dsl::*; + community + .filter(actor_id.eq(for_actor_id)) + .first::(conn) + } +} + impl Community { pub fn read_from_name(conn: &PgConnection, community_name: &str) -> Result { use crate::schema::community::dsl::*; @@ -92,13 +102,6 @@ impl Community { .first::(conn) } - pub fn read_from_actor_id(conn: &PgConnection, for_actor_id: &str) -> Result { - use crate::schema::community::dsl::*; - community - .filter(actor_id.eq(for_actor_id)) - .first::(conn) - } - pub fn update_deleted( conn: &PgConnection, community_id: i32, diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs index bad646d14..e9c676715 100644 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db/src/lib.rs @@ -124,6 +124,12 @@ pub trait Reportable { Self: Sized; } +pub trait ApubObject { + fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result + where + Self: Sized; +} + pub trait MaybeOptional { fn get_optional(self) -> Option; } diff --git a/lemmy_db/src/post.rs b/lemmy_db/src/post.rs index 787d5e6c4..261d5d935 100644 --- a/lemmy_db/src/post.rs +++ b/lemmy_db/src/post.rs @@ -1,6 +1,7 @@ use crate::{ naive_now, schema::{post, post_like, post_read, post_saved}, + ApubObject, Crud, Likeable, Readable, @@ -62,6 +63,37 @@ impl PostForm { } } +impl Crud for Post { + fn read(conn: &PgConnection, post_id: i32) -> Result { + use crate::schema::post::dsl::*; + post.find(post_id).first::(conn) + } + + fn delete(conn: &PgConnection, post_id: i32) -> Result { + use crate::schema::post::dsl::*; + diesel::delete(post.find(post_id)).execute(conn) + } + + fn create(conn: &PgConnection, new_post: &PostForm) -> Result { + use crate::schema::post::dsl::*; + insert_into(post).values(new_post).get_result::(conn) + } + + fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result { + use crate::schema::post::dsl::*; + diesel::update(post.find(post_id)) + .set(new_post) + .get_result::(conn) + } +} + +impl ApubObject for Post { + fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { + use crate::schema::post::dsl::*; + post.filter(ap_id.eq(object_id)).first::(conn) + } +} + impl Post { pub fn read(conn: &PgConnection, post_id: i32) -> Result { use crate::schema::post::dsl::*; @@ -81,11 +113,6 @@ impl Post { .load::(conn) } - pub fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { - use crate::schema::post::dsl::*; - post.filter(ap_id.eq(object_id)).first::(conn) - } - pub fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: String) -> Result { use crate::schema::post::dsl::*; @@ -189,30 +216,6 @@ impl Post { } } -impl Crud for Post { - fn read(conn: &PgConnection, post_id: i32) -> Result { - use crate::schema::post::dsl::*; - post.find(post_id).first::(conn) - } - - fn delete(conn: &PgConnection, post_id: i32) -> Result { - use crate::schema::post::dsl::*; - diesel::delete(post.find(post_id)).execute(conn) - } - - fn create(conn: &PgConnection, new_post: &PostForm) -> Result { - use crate::schema::post::dsl::*; - insert_into(post).values(new_post).get_result::(conn) - } - - fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result { - use crate::schema::post::dsl::*; - diesel::update(post.find(post_id)) - .set(new_post) - .get_result::(conn) - } -} - #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] #[belongs_to(Post)] #[table_name = "post_like"] diff --git a/lemmy_db/src/user.rs b/lemmy_db/src/user.rs index 4733fdfea..ada535e35 100644 --- a/lemmy_db/src/user.rs +++ b/lemmy_db/src/user.rs @@ -2,6 +2,7 @@ use crate::{ is_email_regex, naive_now, schema::{user_, user_::dsl::*}, + ApubObject, Crud, }; use bcrypt::{hash, DEFAULT_COST}; @@ -89,6 +90,16 @@ impl Crud for User_ { } } +impl ApubObject for User_ { + fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { + use crate::schema::user_::dsl::*; + user_ + .filter(deleted.eq(false)) + .filter(actor_id.eq(object_id)) + .first::(conn) + } +} + impl User_ { pub fn register(conn: &PgConnection, form: &UserForm) -> Result { let mut edited_user = form.clone(); @@ -135,14 +146,6 @@ impl User_ { .get_result::(conn) } - pub fn read_from_actor_id(conn: &PgConnection, object_id: &str) -> Result { - use crate::schema::user_::dsl::*; - user_ - .filter(deleted.eq(false)) - .filter(actor_id.eq(object_id)) - .first::(conn) - } - pub fn find_by_email_or_username( conn: &PgConnection, username_or_email: &str,