]> Untitled Git - lemmy.git/blobdiff - crates/apub/src/http/mod.rs
Adding unique constraint for activity ap_id. Fixes #1878 (#1935)
[lemmy.git] / crates / apub / src / http / mod.rs
index d35815ab2a31141ef5896de7a2e4bcfc8df07bbd..03eb9e5bfe8fe68f17087d94c4c5dc1dd11b2c2d 100644 (file)
@@ -24,7 +24,7 @@ use lemmy_apub_lib::{
   traits::{ActivityHandler, ActorType},
   APUB_JSON_CONTENT_TYPE,
 };
-use lemmy_db_schema::{source::activity::Activity, DbPool};
+use lemmy_db_schema::source::activity::Activity;
 use lemmy_utils::{location_info, LemmyError};
 use lemmy_websocket::LemmyContext;
 use log::info;
@@ -95,12 +95,8 @@ where
   let actor = ObjectId::<UserOrCommunity>::new(activity_data.actor)
     .dereference(context, request_counter)
     .await?;
-  verify_signature(&request, &actor.public_key().context(location_info!())?)?;
+  verify_signature(&request, &actor.public_key())?;
 
-  // Do nothing if we received the same activity before
-  if is_activity_already_known(context.pool(), &activity_data.id).await? {
-    return Ok(HttpResponse::Ok().finish());
-  }
   info!("Verifying activity {}", activity_data.id.to_string());
   activity
     .verify(&Data::new(context.clone()), request_counter)
@@ -109,7 +105,8 @@ where
 
   // Log the activity, so we avoid receiving and parsing it twice. Note that this could still happen
   // if we receive the same activity twice in very quick succession.
-  insert_activity(&activity_data.id, &activity, false, true, context.pool()).await?;
+  let object_value = serde_json::to_value(&activity)?;
+  insert_activity(&activity_data.id, object_value, false, true, context.pool()).await?;
 
   info!("Receiving activity {}", activity_data.id.to_string());
   activity
@@ -129,6 +126,12 @@ where
     .json(WithContext::new(data))
 }
 
+fn create_json_apub_response(data: serde_json::Value) -> HttpResponse<Body> {
+  HttpResponse::Ok()
+    .content_type(APUB_JSON_CONTENT_TYPE)
+    .json(data)
+}
+
 fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body>
 where
   T: Serialize,
@@ -167,22 +170,7 @@ pub(crate) async fn get_activity(
   if !activity.local || sensitive {
     Ok(HttpResponse::NotFound().finish())
   } else {
-    Ok(create_apub_response(&activity.data))
-  }
-}
-
-pub(crate) async fn is_activity_already_known(
-  pool: &DbPool,
-  activity_id: &Url,
-) -> Result<bool, LemmyError> {
-  let activity_id = activity_id.to_owned().into();
-  let existing = blocking(pool, move |conn| {
-    Activity::read_from_apub_id(conn, &activity_id)
-  })
-  .await?;
-  match existing {
-    Ok(_) => Ok(true),
-    Err(_) => Ok(false),
+    Ok(create_json_apub_response(activity.data))
   }
 }