}
pub fn upsert(conn: &PgConnection, comment_form: &CommentForm) -> Result<Self, Error> {
- use crate::schema::comment::dsl::*;
- insert_into(comment)
- .values(comment_form)
- .on_conflict(ap_id)
- .do_update()
- .set(comment_form)
- .get_result::<Self>(conn)
+ let existing = Self::read_from_apub_id(
+ conn,
+ comment_form.ap_id.as_ref().unwrap_or(&"none".to_string()),
+ );
+ match existing {
+ Err(NotFound {}) => Ok(Self::create(conn, &comment_form)?),
+ Ok(p) => Ok(Self::update(conn, p.id, &comment_form)?),
+ Err(e) => Err(e),
+ }
}
}
}
pub fn upsert(conn: &PgConnection, community_form: &CommunityForm) -> Result<Community, Error> {
- use crate::schema::community::dsl::*;
- insert_into(community)
- .values(community_form)
- .on_conflict(actor_id)
- .do_update()
- .set(community_form)
- .get_result::<Self>(conn)
+ let existing = Self::read_from_actor_id(
+ conn,
+ community_form
+ .actor_id
+ .as_ref()
+ .unwrap_or(&"none".to_string()),
+ );
+ match existing {
+ Err(NotFound {}) => Ok(Self::create(conn, &community_form)?),
+ Ok(p) => Ok(Self::update(conn, p.id, &community_form)?),
+ Err(e) => Err(e),
+ }
}
}
}
pub fn upsert(conn: &PgConnection, post_form: &PostForm) -> Result<Post, Error> {
- use crate::schema::post::dsl::*;
- insert_into(post)
- .values(post_form)
- .on_conflict(ap_id)
- .do_update()
- .set(post_form)
- .get_result::<Self>(conn)
+ let existing = Self::read_from_apub_id(
+ conn,
+ post_form.ap_id.as_ref().unwrap_or(&"none".to_string()),
+ );
+ match existing {
+ Err(NotFound {}) => Ok(Self::create(conn, &post_form)?),
+ Ok(p) => Ok(Self::update(conn, p.id, &post_form)?),
+ Err(e) => Err(e),
+ }
}
}
conn: &PgConnection,
private_message_form: &PrivateMessageForm,
) -> Result<Self, Error> {
- 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::<Self>(conn)
+ let existing = Self::read_from_apub_id(
+ conn,
+ private_message_form
+ .ap_id
+ .as_ref()
+ .unwrap_or(&"none".to_string()),
+ );
+ match existing {
+ Err(NotFound {}) => Ok(Self::create(conn, &private_message_form)?),
+ Ok(p) => Ok(Self::update(conn, p.id, &private_message_form)?),
+ Err(e) => Err(e),
+ }
}
}
}
pub fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
- insert_into(user_)
- .values(user_form)
- .on_conflict(actor_id)
- .do_update()
- .set(user_form)
- .get_result::<Self>(conn)
+ let existing = Self::read_from_actor_id(
+ conn,
+ user_form.actor_id.as_ref().unwrap_or(&"none".to_string()),
+ );
+ match existing {
+ Err(NotFound {}) => Ok(Self::create(conn, &user_form)?),
+ Ok(p) => Ok(Self::update(conn, p.id, &user_form)?),
+ Err(e) => Err(e),
+ }
}
}