From: Dessalines Date: Tue, 16 Nov 2021 02:07:07 +0000 (-0500) Subject: Use serde_json::to_value X-Git-Url: http://these/git/%7B%60%24%7BrepoUrl%7D/blob/master/docs/static/%7BrepoUrl%7D?a=commitdiff_plain;h=23641fcd06906f1523fd7d2385cd77c511dfa86c;p=lemmy.git Use serde_json::to_value --- diff --git a/crates/apub/src/activities/community/announce.rs b/crates/apub/src/activities/community/announce.rs index 11938184..fc6c5686 100644 --- a/crates/apub/src/activities/community/announce.rs +++ b/crates/apub/src/activities/community/announce.rs @@ -107,14 +107,13 @@ impl ActivityHandler for AnnounceActivity { context: &Data, 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 } } diff --git a/crates/apub/src/activities/community/mod.rs b/crates/apub/src/activities/community/mod.rs index 1b3e305d..c527f0a8 100644 --- a/crates/apub/src/activities/community/mod.rs +++ b/crates/apub/src/activities/community/mod.rs @@ -27,8 +27,9 @@ pub(crate) async fn send_to_community( 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; diff --git a/crates/apub/src/activities/mod.rs b/crates/apub/src/activities/mod.rs index e115769f..ba25d326 100644 --- a/crates/apub/src/activities/mod.rs +++ b/crates/apub/src/activities/mod.rs @@ -171,14 +171,8 @@ async fn send_lemmy_activity( 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, diff --git a/crates/apub/src/context.rs b/crates/apub/src/context.rs index a3a223a3..55486872 100644 --- a/crates/apub/src/context.rs +++ b/crates/apub/src/context.rs @@ -25,19 +25,3 @@ impl WithContext { self.inner } } - -#[derive(Serialize, Deserialize)] -pub(crate) struct WithContextJson { - #[serde(rename = "@context")] - context: OneOrMany, - inner: serde_json::Value, -} - -impl WithContextJson { - pub(crate) fn new(inner: serde_json::Value) -> WithContextJson { - WithContextJson { - context: CONTEXT.clone(), - inner, - } - } -} diff --git a/crates/apub/src/http/mod.rs b/crates/apub/src/http/mod.rs index 3dd068dd..ebb155a8 100644 --- a/crates/apub/src/http/mod.rs +++ b/crates/apub/src/http/mod.rs @@ -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 { HttpResponse::Ok() .content_type(APUB_JSON_CONTENT_TYPE) - .json(WithContextJson::new(data)) + .json(data) } fn create_apub_tombstone_response(data: &T) -> HttpResponse diff --git a/crates/apub/src/lib.rs b/crates/apub/src/lib.rs index 74dfd952..62f84de6 100644 --- a/crates/apub/src/lib.rs +++ b/crates/apub/src/lib.rs @@ -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( +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(())