Merge pull request #1903 from LemmyNet/fix_direct_string_apub_serialize_error

Use serde_json::to_value
This commit is contained in:
Nutomic 2021-11-16 17:00:07 +00:00 committed by GitHub
commit 3914c6c875
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 41 deletions

View file

@ -107,14 +107,13 @@ impl ActivityHandler for AnnounceActivity {
context: &Data<LemmyContext>, context: &Data<LemmyContext>,
request_counter: &mut i32, request_counter: &mut i32,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// TODO: this is pretty ugly, but i cant think of a much better way let object_value = serde_json::to_value(&self.object)?;
let object = serde_json::to_string(&self.object)?; let object_data: ActivityCommonFields = serde_json::from_value(object_value.to_owned())?;
let object_data: ActivityCommonFields = serde_json::from_str(&object)?;
if is_activity_already_known(context.pool(), &object_data.id).await? { if is_activity_already_known(context.pool(), &object_data.id).await? {
return Ok(()); 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 self.object.receive(context, request_counter).await
} }
} }

View file

@ -27,8 +27,9 @@ pub(crate) async fn send_to_community<T: ActorType>(
context: &LemmyContext, context: &LemmyContext,
) -> Result<(), LemmyError> { ) -> Result<(), LemmyError> {
// if this is a local community, we need to do an announce from the community instead // 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 { 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?; AnnounceActivity::send(activity, community, additional_inboxes, context).await?;
} else { } else {
let mut inboxes = additional_inboxes; let mut inboxes = additional_inboxes;

View file

@ -171,14 +171,8 @@ async fn send_lemmy_activity<T: Serialize>(
let serialised_activity = serde_json::to_string(&activity)?; let serialised_activity = serde_json::to_string(&activity)?;
insert_activity( let object_value = serde_json::to_value(&activity)?;
activity_id, insert_activity(activity_id, object_value, true, sensitive, context.pool()).await?;
&serialised_activity,
true,
sensitive,
context.pool(),
)
.await?;
send_activity( send_activity(
serialised_activity, serialised_activity,

View file

@ -25,19 +25,3 @@ impl<T> WithContext<T> {
self.inner 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,
}
}
}

View file

@ -1,7 +1,7 @@
use crate::{ use crate::{
activity_lists::SharedInboxActivities, activity_lists::SharedInboxActivities,
check_is_apub_id_valid, check_is_apub_id_valid,
context::{WithContext, WithContextJson}, context::WithContext,
fetcher::user_or_community::UserOrCommunity, fetcher::user_or_community::UserOrCommunity,
http::{community::receive_group_inbox, person::receive_person_inbox}, http::{community::receive_group_inbox, person::receive_person_inbox},
insert_activity, insert_activity,
@ -109,7 +109,8 @@ where
// Log the activity, so we avoid receiving and parsing it twice. Note that this could still happen // 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. // 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()); info!("Receiving activity {}", activity_data.id.to_string());
activity activity
@ -132,7 +133,7 @@ where
fn create_json_apub_response(data: serde_json::Value) -> HttpResponse<Body> { fn create_json_apub_response(data: serde_json::Value) -> HttpResponse<Body> {
HttpResponse::Ok() HttpResponse::Ok()
.content_type(APUB_JSON_CONTENT_TYPE) .content_type(APUB_JSON_CONTENT_TYPE)
.json(WithContextJson::new(data)) .json(data)
} }
fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body> fn create_apub_tombstone_response<T>(data: &T) -> HttpResponse<Body>

View 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_db_schema::{newtypes::DbUrl, source::activity::Activity, DbPool};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError}; use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_websocket::LemmyContext; use lemmy_websocket::LemmyContext;
use serde::Serialize;
use std::net::IpAddr; use std::net::IpAddr;
use url::{ParseError, Url}; 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 /// Store a sent or received activity in the database, for logging purposes. These records are not
/// persistent. /// persistent.
async fn insert_activity<T>( async fn insert_activity(
ap_id: &Url, ap_id: &Url,
activity: &T, activity: serde_json::Value,
local: bool, local: bool,
sensitive: bool, sensitive: bool,
pool: &DbPool, pool: &DbPool,
) -> Result<(), LemmyError> ) -> Result<(), LemmyError> {
where
T: Serialize + std::fmt::Debug + Send + 'static,
{
let data = serde_json::to_value(activity)?;
let ap_id = ap_id.to_owned().into(); let ap_id = ap_id.to_owned().into();
blocking(pool, move |conn| { blocking(pool, move |conn| {
Activity::insert(conn, ap_id, data, local, sensitive) Activity::insert(conn, ap_id, activity, local, sensitive)
}) })
.await??; .await??;
Ok(()) Ok(())