From 85cdcc5f13c4f02a0119a14a3ffdac315220990e Mon Sep 17 00:00:00 2001 From: Dessalines Date: Wed, 12 Aug 2020 13:54:43 -0400 Subject: [PATCH] Add a fix for double posts / comments. #1080 --- server/src/apub/fetcher.rs | 7 +++++-- server/src/apub/inbox/activities/create.rs | 8 ++++---- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/server/src/apub/fetcher.rs b/server/src/apub/fetcher.rs index 5fcf6ad97..5ae66102c 100644 --- a/server/src/apub/fetcher.rs +++ b/server/src/apub/fetcher.rs @@ -341,7 +341,7 @@ async fn fetch_remote_community( Ok(community) } -fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result { +pub fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result { let existing = Post::read_from_apub_id(conn, &post_form.ap_id); match existing { Err(NotFound {}) => Ok(Post::create(conn, &post_form)?), @@ -376,7 +376,10 @@ pub async fn get_or_fetch_and_insert_post( } } -fn upsert_comment(comment_form: &CommentForm, conn: &PgConnection) -> Result { +pub fn upsert_comment( + comment_form: &CommentForm, + conn: &PgConnection, +) -> Result { let existing = Comment::read_from_apub_id(conn, &comment_form.ap_id); match existing { Err(NotFound {}) => Ok(Comment::create(conn, &comment_form)?), diff --git a/server/src/apub/inbox/activities/create.rs b/server/src/apub/inbox/activities/create.rs index 55bd85fb0..1c9c973eb 100644 --- a/server/src/apub/inbox/activities/create.rs +++ b/server/src/apub/inbox/activities/create.rs @@ -4,6 +4,7 @@ use crate::{ post::PostResponse, }, apub::{ + fetcher::{upsert_comment, upsert_post}, inbox::shared_inbox::{ announce_if_community_is_local, get_user_from_activity, @@ -26,11 +27,10 @@ use activitystreams::{activity::Create, base::AnyBase, object::Note, prelude::*} use actix_web::{client::Client, HttpResponse}; use anyhow::Context; use lemmy_db::{ - comment::{Comment, CommentForm}, + comment::CommentForm, comment_view::CommentView, post::{Post, PostForm}, post_view::PostView, - Crud, }; use lemmy_utils::{location_info, scrape_text_for_mentions}; @@ -65,7 +65,7 @@ async fn receive_create_post( let post = PostForm::from_apub(&page, client, pool, Some(user.actor_id()?)).await?; - let inserted_post = blocking(pool, move |conn| Post::create(conn, &post)).await??; + let inserted_post = blocking(pool, move |conn| upsert_post(&post, conn)).await??; // Refetch the view let inserted_post_id = inserted_post.id; @@ -98,7 +98,7 @@ async fn receive_create_comment( let comment = CommentForm::from_apub(¬e, client, pool, Some(user.actor_id()?)).await?; - let inserted_comment = blocking(pool, move |conn| Comment::create(conn, &comment)).await??; + let inserted_comment = blocking(pool, move |conn| upsert_comment(&comment, conn)).await??; let post_id = inserted_comment.post_id; let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;