2018-11-23 13:17:37 +01:00
|
|
|
use activitypub::{
|
|
|
|
activity::{
|
|
|
|
Announce,
|
|
|
|
Create,
|
|
|
|
Delete,
|
|
|
|
Follow as FollowAct,
|
|
|
|
Like,
|
|
|
|
Undo,
|
|
|
|
Update
|
|
|
|
},
|
|
|
|
object::Tombstone
|
|
|
|
};
|
2018-05-16 20:20:44 +02:00
|
|
|
use failure::Error;
|
2018-05-01 16:00:29 +02:00
|
|
|
use serde_json;
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
use plume_common::activity_pub::{
|
|
|
|
inbox::{Deletable, FromActivity, InboxError},
|
|
|
|
Id,
|
|
|
|
};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_models::{
|
2018-11-24 12:44:17 +01:00
|
|
|
comments::Comment, follows::Follow, instance::Instance, likes, posts::Post, reshares::Reshare,
|
|
|
|
users::User, Connection,
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-05-01 16:00:29 +02:00
|
|
|
|
2018-06-12 21:10:08 +02:00
|
|
|
pub trait Inbox {
|
2018-09-27 23:06:40 +02:00
|
|
|
fn received(&self, conn: &Connection, act: serde_json::Value) -> Result<(), Error> {
|
2018-11-24 12:44:17 +01:00
|
|
|
let actor_id = Id::new(act["actor"].as_str().unwrap_or_else(|| {
|
|
|
|
act["actor"]["id"]
|
|
|
|
.as_str()
|
|
|
|
.expect("Inbox::received: actor_id missing error")
|
|
|
|
}));
|
2018-05-16 20:20:44 +02:00
|
|
|
match act["type"].as_str() {
|
2018-11-24 12:44:17 +01:00
|
|
|
Some(t) => match t {
|
|
|
|
"Announce" => {
|
|
|
|
Reshare::from_activity(conn, serde_json::from_value(act.clone())?, actor_id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"Create" => {
|
|
|
|
let act: Create = serde_json::from_value(act.clone())?;
|
|
|
|
if Post::try_from_activity(conn, act.clone())
|
|
|
|
|| Comment::try_from_activity(conn, act)
|
|
|
|
{
|
2018-06-12 21:10:08 +02:00
|
|
|
Ok(())
|
2018-11-24 12:44:17 +01:00
|
|
|
} else {
|
|
|
|
Err(InboxError::InvalidType)?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"Delete" => {
|
|
|
|
let act: Delete = serde_json::from_value(act.clone())?;
|
|
|
|
Post::delete_id(
|
|
|
|
act.delete_props
|
|
|
|
.object_object::<Tombstone>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
|
|
|
actor_id.into(),
|
|
|
|
conn,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"Follow" => {
|
|
|
|
Follow::from_activity(conn, serde_json::from_value(act.clone())?, actor_id);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"Like" => {
|
|
|
|
likes::Like::from_activity(
|
|
|
|
conn,
|
|
|
|
serde_json::from_value(act.clone())?,
|
|
|
|
actor_id,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"Undo" => {
|
|
|
|
let act: Undo = serde_json::from_value(act.clone())?;
|
|
|
|
match act.undo_props.object["type"]
|
|
|
|
.as_str()
|
|
|
|
.expect("Inbox::received: undo without original type error")
|
|
|
|
{
|
|
|
|
"Like" => {
|
|
|
|
likes::Like::delete_id(
|
|
|
|
act.undo_props
|
|
|
|
.object_object::<Like>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
|
|
|
actor_id.into(),
|
|
|
|
conn,
|
|
|
|
);
|
2018-06-12 21:10:08 +02:00
|
|
|
Ok(())
|
2018-05-16 20:20:44 +02:00
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
"Announce" => {
|
|
|
|
Reshare::delete_id(
|
|
|
|
act.undo_props
|
|
|
|
.object_object::<Announce>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
|
|
|
actor_id.into(),
|
|
|
|
conn,
|
|
|
|
);
|
|
|
|
Ok(())
|
2018-05-16 20:20:44 +02:00
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
"Follow" => {
|
|
|
|
Follow::delete_id(
|
|
|
|
act.undo_props
|
|
|
|
.object_object::<FollowAct>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
|
|
|
actor_id.into(),
|
|
|
|
conn,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(InboxError::CantUndo)?,
|
2018-05-13 12:44:05 +02:00
|
|
|
}
|
|
|
|
}
|
2018-11-24 12:44:17 +01:00
|
|
|
"Update" => {
|
|
|
|
let act: Update = serde_json::from_value(act.clone())?;
|
|
|
|
Post::handle_update(conn, act.update_props.object_object()?);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(InboxError::InvalidType)?,
|
2018-05-13 12:44:05 +02:00
|
|
|
},
|
2018-11-24 12:44:17 +01:00
|
|
|
None => Err(InboxError::NoType)?,
|
2018-05-01 16:00:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-05-18 10:04:40 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
impl Inbox for Instance {}
|
|
|
|
impl Inbox for User {}
|