Add a fix for double posts / comments. #1080

This commit is contained in:
Dessalines 2020-08-12 13:54:43 -04:00
parent c19dfdae64
commit 85cdcc5f13
2 changed files with 9 additions and 6 deletions

View file

@ -341,7 +341,7 @@ async fn fetch_remote_community(
Ok(community) Ok(community)
} }
fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result<Post, LemmyError> { pub fn upsert_post(post_form: &PostForm, conn: &PgConnection) -> Result<Post, LemmyError> {
let existing = Post::read_from_apub_id(conn, &post_form.ap_id); let existing = Post::read_from_apub_id(conn, &post_form.ap_id);
match existing { match existing {
Err(NotFound {}) => Ok(Post::create(conn, &post_form)?), 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<Comment, LemmyError> { pub fn upsert_comment(
comment_form: &CommentForm,
conn: &PgConnection,
) -> Result<Comment, LemmyError> {
let existing = Comment::read_from_apub_id(conn, &comment_form.ap_id); let existing = Comment::read_from_apub_id(conn, &comment_form.ap_id);
match existing { match existing {
Err(NotFound {}) => Ok(Comment::create(conn, &comment_form)?), Err(NotFound {}) => Ok(Comment::create(conn, &comment_form)?),

View file

@ -4,6 +4,7 @@ use crate::{
post::PostResponse, post::PostResponse,
}, },
apub::{ apub::{
fetcher::{upsert_comment, upsert_post},
inbox::shared_inbox::{ inbox::shared_inbox::{
announce_if_community_is_local, announce_if_community_is_local,
get_user_from_activity, get_user_from_activity,
@ -26,11 +27,10 @@ use activitystreams::{activity::Create, base::AnyBase, object::Note, prelude::*}
use actix_web::{client::Client, HttpResponse}; use actix_web::{client::Client, HttpResponse};
use anyhow::Context; use anyhow::Context;
use lemmy_db::{ use lemmy_db::{
comment::{Comment, CommentForm}, comment::CommentForm,
comment_view::CommentView, comment_view::CommentView,
post::{Post, PostForm}, post::{Post, PostForm},
post_view::PostView, post_view::PostView,
Crud,
}; };
use lemmy_utils::{location_info, scrape_text_for_mentions}; 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 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 // Refetch the view
let inserted_post_id = inserted_post.id; let inserted_post_id = inserted_post.id;
@ -98,7 +98,7 @@ async fn receive_create_comment(
let comment = CommentForm::from_apub(&note, client, pool, Some(user.actor_id()?)).await?; let comment = CommentForm::from_apub(&note, 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_id = inserted_comment.post_id;
let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??; let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;