Use serde_json::to_value

This commit is contained in:
Dessalines 2021-11-15 21:07:07 -05:00
parent 09cd32034b
commit 23641fcd06
6 changed files with 15 additions and 41 deletions

View 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
}
}

View 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;

View 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,

View 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,
}
}
}

View 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>

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_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(())