]> Untitled Git - lemmy.git/commitdiff
Use serde_json::to_value
authorDessalines <tyhou13@gmx.com>
Tue, 16 Nov 2021 02:07:07 +0000 (21:07 -0500)
committerDessalines <tyhou13@gmx.com>
Tue, 16 Nov 2021 02:11:44 +0000 (21:11 -0500)
crates/apub/src/activities/community/announce.rs
crates/apub/src/activities/community/mod.rs
crates/apub/src/activities/mod.rs
crates/apub/src/context.rs
crates/apub/src/http/mod.rs
crates/apub/src/lib.rs

index 11938184f2b0c41e69e4a5f1b8dd6aae3492b82d..fc6c5686cc0abb021e6f270e2c5cf77f89a0873c 100644 (file)
@@ -107,14 +107,13 @@ impl ActivityHandler for AnnounceActivity {
     context: &Data<LemmyContext>,
     request_counter: &mut i32,
   ) -> Result<(), LemmyError> {
-    // TODO: this is pretty ugly, but i cant think of a much better way
-    let object = serde_json::to_string(&self.object)?;
-    let object_data: ActivityCommonFields = serde_json::from_str(&object)?;
+    let object_value = serde_json::to_value(&self.object)?;
+    let object_data: ActivityCommonFields = serde_json::from_value(object_value.to_owned())?;
 
     if is_activity_already_known(context.pool(), &object_data.id).await? {
       return Ok(());
     }
-    insert_activity(&object_data.id, &self.object, false, true, context.pool()).await?;
+    insert_activity(&object_data.id, object_value, false, true, context.pool()).await?;
     self.object.receive(context, request_counter).await
   }
 }
index 1b3e305dc7695906f541db0677dc1a7cd9758e83..c527f0a81523505e89597134b9a8574f1a409490 100644 (file)
@@ -27,8 +27,9 @@ pub(crate) async fn send_to_community<T: ActorType>(
   context: &LemmyContext,
 ) -> Result<(), LemmyError> {
   // if this is a local community, we need to do an announce from the community instead
+  let object_value = serde_json::to_value(&activity)?;
   if community.local {
-    insert_activity(activity_id, &activity, true, false, context.pool()).await?;
+    insert_activity(activity_id, object_value, true, false, context.pool()).await?;
     AnnounceActivity::send(activity, community, additional_inboxes, context).await?;
   } else {
     let mut inboxes = additional_inboxes;
index e115769f380ad8185b8d31aec010571a8a902297..ba25d32655621fe9885f38c3a8c4ff920dc4ac32 100644 (file)
@@ -171,14 +171,8 @@ async fn send_lemmy_activity<T: Serialize>(
 
   let serialised_activity = serde_json::to_string(&activity)?;
 
-  insert_activity(
-    activity_id,
-    &serialised_activity,
-    true,
-    sensitive,
-    context.pool(),
-  )
-  .await?;
+  let object_value = serde_json::to_value(&activity)?;
+  insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?;
 
   send_activity(
     serialised_activity,
index a3a223a36f22bfa57956407e631698effde33440..55486872ff592b714c038fcebaf683b28a38be50 100644 (file)
@@ -25,19 +25,3 @@ impl<T> WithContext<T> {
     self.inner
   }
 }
-
-#[derive(Serialize, Deserialize)]
-pub(crate) struct WithContextJson {
-  #[serde(rename = "@context")]
-  context: OneOrMany<AnyBase>,
-  inner: serde_json::Value,
-}
-
-impl WithContextJson {
-  pub(crate) fn new(inner: serde_json::Value) -> WithContextJson {
-    WithContextJson {
-      context: CONTEXT.clone(),
-      inner,
-    }
-  }
-}
index 3dd068dd136af89455440c96446a6f8e6e522789..ebb155a80dc2e4581e6985884de46e86c9ee0dea 100644 (file)
@@ -1,7 +1,7 @@
 use crate::{
   activity_lists::SharedInboxActivities,
   check_is_apub_id_valid,
-  context::{WithContext, WithContextJson},
+  context::WithContext,
   fetcher::user_or_community::UserOrCommunity,
   http::{community::receive_group_inbox, person::receive_person_inbox},
   insert_activity,
@@ -109,7 +109,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
@@ -132,7 +133,7 @@ where
 fn create_json_apub_response(data: serde_json::Value) -> HttpResponse<Body> {
   HttpResponse::Ok()
     .content_type(APUB_JSON_CONTENT_TYPE)
-    .json(WithContextJson::new(data))
+    .json(data)
 }
 
 fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body>
index 74dfd95256a89e0e20a136e028fac33f705cf443..62f84de61ddfe29c9a9462ba8e0762c3286b9be4 100644 (file)
@@ -18,7 +18,6 @@ use lemmy_apub_lib::webfinger::{webfinger_resolve_actor, WebfingerType};
 use lemmy_db_schema::{newtypes::DbUrl, source::activity::Activity, DbPool};
 use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
 use lemmy_websocket::LemmyContext;
-use serde::Serialize;
 use std::net::IpAddr;
 use url::{ParseError, Url};
 
@@ -177,20 +176,16 @@ pub async fn get_actor_id_from_name(
 
 /// Store a sent or received activity in the database, for logging purposes. These records are not
 /// persistent.
-async fn insert_activity<T>(
+async fn insert_activity(
   ap_id: &Url,
-  activity: &T,
+  activity: serde_json::Value,
   local: bool,
   sensitive: bool,
   pool: &DbPool,
-) -> Result<(), LemmyError>
-where
-  T: Serialize + std::fmt::Debug + Send + 'static,
-{
-  let data = serde_json::to_value(activity)?;
+) -> Result<(), LemmyError> {
   let ap_id = ap_id.to_owned().into();
   blocking(pool, move |conn| {
-    Activity::insert(conn, ap_id, data, local, sensitive)
+    Activity::insert(conn, ap_id, activity, local, sensitive)
   })
   .await??;
   Ok(())