diff --git a/server/lemmy_db/src/community.rs b/server/lemmy_db/src/community.rs index 3ab8e089e..d7c741642 100644 --- a/server/lemmy_db/src/community.rs +++ b/server/lemmy_db/src/community.rs @@ -162,12 +162,13 @@ impl Community { } 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), - } + use crate::schema::community::dsl::*; + insert_into(community) + .values(community_form) + .on_conflict(actor_id) + .do_update() + .set(community_form) + .get_result::(conn) } } diff --git a/server/lemmy_db/src/post.rs b/server/lemmy_db/src/post.rs index 499847d63..7e8ec13a9 100644 --- a/server/lemmy_db/src/post.rs +++ b/server/lemmy_db/src/post.rs @@ -180,13 +180,11 @@ impl Post { } pub fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result { - let existing = Self::read_from_apub_id(conn, &post_form.ap_id); - match existing { - Err(NotFound {}) => Ok(Self::create(conn, &post_form)?), - // both the old and new comment should be identical so no need to do an update here - Ok(p) => Ok(Self::read(conn, p.id)?), - Err(e) => Err(e), - } + use crate::schema::post::dsl::*; + insert_into(post) + .values(post_form) + .on_conflict_do_nothing() + .get_result::(conn) } } diff --git a/server/lemmy_db/src/user.rs b/server/lemmy_db/src/user.rs index 1a6ff9db2..42cd8e06f 100644 --- a/server/lemmy_db/src/user.rs +++ b/server/lemmy_db/src/user.rs @@ -154,12 +154,12 @@ impl User_ { } 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), - } + insert_into(user_) + .values(user_form) + .on_conflict(actor_id) + .do_update() + .set(user_form) + .get_result::(conn) } }