From e135e7b466c0e0b7b1c8126242e6de791fa46a0c Mon Sep 17 00:00:00 2001 From: Felix Ableitner Date: Mon, 24 Aug 2020 15:03:22 +0200 Subject: [PATCH] implement upsert for user/community --- server/lemmy_db/src/community.rs | 9 +++++++++ server/lemmy_db/src/user.rs | 9 +++++++++ server/src/apub/fetcher.rs | 11 ++--------- server/src/apub/inbox/shared_inbox.rs | 2 ++ 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/server/lemmy_db/src/community.rs b/server/lemmy_db/src/community.rs index df5f12941..3ab8e089e 100644 --- a/server/lemmy_db/src/community.rs +++ b/server/lemmy_db/src/community.rs @@ -160,6 +160,15 @@ impl Community { .unwrap_or_default() .contains(&user_id) } + + pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result { + let existing = Self::read_from_actor_id(conn, &community_form.actor_id); + match existing { + Err(NotFound {}) => Ok(Self::create(conn, &community_form)?), + Ok(c) => Ok(Self::update(conn, c.id, &community_form)?), + Err(e) => Err(e), + } + } } #[derive(Identifiable, Queryable, Associations, PartialEq, Debug)] diff --git a/server/lemmy_db/src/user.rs b/server/lemmy_db/src/user.rs index 73da1c244..1a6ff9db2 100644 --- a/server/lemmy_db/src/user.rs +++ b/server/lemmy_db/src/user.rs @@ -152,6 +152,15 @@ impl User_ { pub fn get_profile_url(&self, hostname: &str) -> String { format!("https://{}/u/{}", hostname, self.name) } + + pub fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result { + let existing = Self::read_from_actor_id(conn, &user_form.actor_id); + match existing { + Err(NotFound {}) => Ok(Self::create(conn, &user_form)?), + Ok(u) => Ok(Self::update(conn, u.id, &user_form)?), + Err(e) => Err(e), + } + } } #[cfg(test)] diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 0c42aa14e..0e1afc7b0 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -230,7 +230,7 @@ pub async fn get_or_fetch_and_upsert_user( let person = fetch_remote_object::(context.client(), apub_id).await?; let uf = UserForm::from_apub(&person, context, Some(apub_id.to_owned())).await?; - let user = blocking(context.pool(), move |conn| User_::create(conn, &uf)).await??; + let user = blocking(context.pool(), move |conn| User_::upsert(conn, &uf)).await??; Ok(user) } @@ -286,14 +286,7 @@ async fn fetch_remote_community( let group = fetch_remote_object::(context.client(), apub_id).await?; let cf = CommunityForm::from_apub(&group, context, Some(apub_id.to_owned())).await?; - let community = blocking(context.pool(), move |conn| { - if let Some(cid) = community_id { - Community::update(conn, cid, &cf) - } else { - Community::create(conn, &cf) - } - }) - .await??; + let community = blocking(context.pool(), move |conn| Community::upsert(conn, &cf)).await??; // Also add the community moderators too let attributed_to = group.inner.attributed_to().context(location_info!())?; diff --git a/server/src/apub/inbox/shared_inbox.rs b/server/src/apub/inbox/shared_inbox.rs index c9f9324dc..0f8cc8ed8 100644 --- a/server/src/apub/inbox/shared_inbox.rs +++ b/server/src/apub/inbox/shared_inbox.rs @@ -66,6 +66,8 @@ pub async fn shared_inbox( let json = serde_json::to_string(&activity)?; debug!("Shared inbox received activity: {}", json); + // TODO: if we already received an activity with identical ID, then ignore this (same in other inboxes) + let sender = &activity .actor()? .to_owned()