diff --git a/lemmy_apub/src/objects/mod.rs b/lemmy_apub/src/objects/mod.rs index f7db59c70..fbe89b09a 100644 --- a/lemmy_apub/src/objects/mod.rs +++ b/lemmy_apub/src/objects/mod.rs @@ -13,7 +13,7 @@ use anyhow::{anyhow, Context}; use chrono::NaiveDateTime; use lemmy_db::{ApubObject, Crud, DbPool}; use lemmy_structs::blocking; -use lemmy_utils::{location_info, utils::convert_datetime, LemmyError}; +use lemmy_utils::{location_info, settings::Settings, utils::convert_datetime, LemmyError}; use lemmy_websocket::LemmyContext; use url::Url; @@ -185,18 +185,19 @@ pub(in crate::objects) async fn get_object_from_apub( ) -> Result where From: BaseExt, - To: ApubObject + Crud + Send + 'static, + To: ApubObject + Crud + Send + 'static, ToForm: FromApubToForm + Send + 'static, { let object_id = from.id_unchecked().context(location_info!())?.to_owned(); - let object_in_database = blocking(context.pool(), move |conn| { - To::read_from_apub_id(conn, object_id.as_str()) - }) - .await?; + let domain = object_id.domain().context(location_info!())?; // if we already have the object in our database, return that directly - if let Ok(to) = object_in_database { - Ok(to) + if Settings::get().hostname == domain { + let object = blocking(context.pool(), move |conn| { + To::read_from_apub_id(conn, object_id.as_str()) + }) + .await??; + Ok(object) } // if we dont have it, parse from apub and insert into database // TODO: this is insecure, a `Like/Post` could result in a non-existent post from a different @@ -205,7 +206,7 @@ where else { let to_form = ToForm::from_apub(&from, context, expected_domain, request_counter).await?; - let to = blocking(context.pool(), move |conn| To::create(conn, &to_form)).await??; + let to = blocking(context.pool(), move |conn| To::upsert(conn, &to_form)).await??; Ok(to) } } diff --git a/lemmy_db/src/comment.rs b/lemmy_db/src/comment.rs index e9568bacf..f54ddd10f 100644 --- a/lemmy_db/src/comment.rs +++ b/lemmy_db/src/comment.rs @@ -87,11 +87,21 @@ impl Crud for Comment { } } -impl ApubObject 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) } + + fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result { + use crate::schema::comment::dsl::*; + insert_into(comment) + .values(comment_form) + .on_conflict(ap_id) + .do_update() + .set(comment_form) + .get_result::(conn) + } } impl Comment { @@ -171,16 +181,6 @@ impl Comment { .set((content.eq(new_content), updated.eq(naive_now()))) .get_result::(conn) } - - pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result { - use crate::schema::comment::dsl::*; - insert_into(comment) - .values(comment_form) - .on_conflict(ap_id) - .do_update() - .set(comment_form) - .get_result::(conn) - } } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug, Clone)] diff --git a/lemmy_db/src/community.rs b/lemmy_db/src/community.rs index 5c3ee551d..be40da349 100644 --- a/lemmy_db/src/community.rs +++ b/lemmy_db/src/community.rs @@ -84,13 +84,23 @@ impl Crud for Community { } } -impl ApubObject 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) } + + fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result { + use crate::schema::community::dsl::*; + insert_into(community) + .values(community_form) + .on_conflict(actor_id) + .do_update() + .set(community_form) + .get_result::(conn) + } } impl Community { @@ -168,16 +178,6 @@ impl Community { .unwrap_or_default() .contains(&user_id) } - - pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result { - use crate::schema::community::dsl::*; - insert_into(community) - .values(community_form) - .on_conflict(actor_id) - .do_update() - .set(community_form) - .get_result::(conn) - } } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] diff --git a/lemmy_db/src/lib.rs b/lemmy_db/src/lib.rs index e9c676715..0ca4826a1 100644 --- a/lemmy_db/src/lib.rs +++ b/lemmy_db/src/lib.rs @@ -124,10 +124,13 @@ pub trait Reportable { Self: Sized; } -pub trait ApubObject { +pub trait ApubObject { fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result where Self: Sized; + fn upsert(conn: &PgConnection, user_form: &T) -> Result + where + Self: Sized; } pub trait MaybeOptional { diff --git a/lemmy_db/src/post.rs b/lemmy_db/src/post.rs index 261d5d935..530f475b4 100644 --- a/lemmy_db/src/post.rs +++ b/lemmy_db/src/post.rs @@ -87,11 +87,21 @@ impl Crud for Post { } } -impl ApubObject for Post { +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) } + + fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result { + use crate::schema::post::dsl::*; + insert_into(post) + .values(post_form) + .on_conflict(ap_id) + .do_update() + .set(post_form) + .get_result::(conn) + } } impl Post { @@ -204,16 +214,6 @@ impl Post { pub fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool { user_id == post_creator_id } - - pub fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result { - use crate::schema::post::dsl::*; - insert_into(post) - .values(post_form) - .on_conflict(ap_id) - .do_update() - .set(post_form) - .get_result::(conn) - } } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] diff --git a/lemmy_db/src/private_message.rs b/lemmy_db/src/private_message.rs index e8f021508..0e1aef108 100644 --- a/lemmy_db/src/private_message.rs +++ b/lemmy_db/src/private_message.rs @@ -55,7 +55,7 @@ impl Crud for PrivateMessage { } } -impl ApubObject for PrivateMessage { +impl ApubObject for PrivateMessage { fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result where Self: Sized, @@ -65,6 +65,16 @@ impl ApubObject for PrivateMessage { .filter(ap_id.eq(object_id)) .first::(conn) } + + fn upsert(conn: &PgConnection, private_message_form: &PrivateMessageForm) -> Result { + use crate::schema::private_message::dsl::*; + insert_into(private_message) + .values(private_message_form) + .on_conflict(ap_id) + .do_update() + .set(private_message_form) + .get_result::(conn) + } } impl PrivateMessage { @@ -123,20 +133,6 @@ impl PrivateMessage { .set(read.eq(true)) .get_results::(conn) } - - // TODO use this - pub fn upsert( - conn: &PgConnection, - private_message_form: &PrivateMessageForm, - ) -> Result { - use crate::schema::private_message::dsl::*; - insert_into(private_message) - .values(private_message_form) - .on_conflict(ap_id) - .do_update() - .set(private_message_form) - .get_result::(conn) - } } #[cfg(test)] diff --git a/lemmy_db/src/user.rs b/lemmy_db/src/user.rs index ada535e35..d8e833e6e 100644 --- a/lemmy_db/src/user.rs +++ b/lemmy_db/src/user.rs @@ -90,7 +90,7 @@ impl Crud for User_ { } } -impl ApubObject for User_ { +impl ApubObject for User_ { fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result { use crate::schema::user_::dsl::*; user_ @@ -98,6 +98,15 @@ impl ApubObject for User_ { .filter(actor_id.eq(object_id)) .first::(conn) } + + fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result { + insert_into(user_) + .values(user_form) + .on_conflict(actor_id) + .do_update() + .set(user_form) + .get_result::(conn) + } } impl User_ { @@ -182,15 +191,6 @@ impl User_ { ) } - pub fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result { - insert_into(user_) - .values(user_form) - .on_conflict(actor_id) - .do_update() - .set(user_form) - .get_result::(conn) - } - pub fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result { diesel::update(user_.find(user_id)) .set((last_refreshed_at.eq(naive_now()),))