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-12-22 18:27:21 +01:00
|
|
|
use rocket::{
|
|
|
|
data::*,
|
|
|
|
http::Status,
|
|
|
|
Outcome::{self, *},
|
|
|
|
Request,
|
|
|
|
};
|
|
|
|
use rocket_contrib::json::*;
|
|
|
|
use serde::Deserialize;
|
2018-05-01 16:00:29 +02:00
|
|
|
use serde_json;
|
|
|
|
|
2018-12-22 18:27:21 +01:00
|
|
|
use std::io::Read;
|
|
|
|
|
2018-11-24 12:44:17 +01:00
|
|
|
use plume_common::activity_pub::{
|
2018-12-23 11:12:15 +01:00
|
|
|
inbox::{Deletable, FromActivity, InboxError, Notify},
|
2018-12-22 18:27:21 +01:00
|
|
|
Id,request::Digest,
|
2018-11-24 12:44:17 +01:00
|
|
|
};
|
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,
|
2018-12-02 17:37:51 +01:00
|
|
|
users::User, search::Searcher, 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-12-02 17:37:51 +01:00
|
|
|
fn received(&self, conn: &Connection, searcher: &Searcher, 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())?;
|
2018-12-02 17:37:51 +01:00
|
|
|
if Post::try_from_activity(&(conn, searcher), act.clone())
|
2018-11-24 12:44:17 +01:00
|
|
|
|| 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(
|
2018-11-26 10:21:52 +01:00
|
|
|
&act.delete_props
|
2018-11-24 12:44:17 +01:00
|
|
|
.object_object::<Tombstone>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
2018-11-26 10:21:52 +01:00
|
|
|
actor_id.as_ref(),
|
2018-12-02 17:37:51 +01:00
|
|
|
&(conn, searcher),
|
2018-11-24 12:44:17 +01:00
|
|
|
);
|
2018-12-23 11:13:36 +01:00
|
|
|
Comment::delete_id(
|
|
|
|
&act.delete_props
|
|
|
|
.object_object::<Tombstone>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
|
|
|
actor_id.as_ref(),
|
|
|
|
conn,
|
|
|
|
);
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"Follow" => {
|
2018-12-23 11:12:15 +01:00
|
|
|
Follow::from_activity(conn, serde_json::from_value(act.clone())?, actor_id).notify(conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
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())?;
|
2018-12-23 11:12:15 +01:00
|
|
|
if let Some(t) = act.undo_props.object["type"].as_str() {
|
|
|
|
match t {
|
|
|
|
"Like" => {
|
|
|
|
likes::Like::delete_id(
|
|
|
|
&act.undo_props
|
|
|
|
.object_object::<Like>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
|
|
|
actor_id.as_ref(),
|
|
|
|
conn,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"Announce" => {
|
|
|
|
Reshare::delete_id(
|
|
|
|
&act.undo_props
|
|
|
|
.object_object::<Announce>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
|
|
|
actor_id.as_ref(),
|
|
|
|
conn,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
"Follow" => {
|
|
|
|
Follow::delete_id(
|
|
|
|
&act.undo_props
|
|
|
|
.object_object::<FollowAct>()?
|
|
|
|
.object_props
|
|
|
|
.id_string()?,
|
|
|
|
actor_id.as_ref(),
|
|
|
|
conn,
|
|
|
|
);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
_ => Err(InboxError::CantUndo)?,
|
2018-05-16 20:20:44 +02:00
|
|
|
}
|
2018-12-23 11:12:15 +01:00
|
|
|
} else {
|
|
|
|
let link = act.undo_props.object.as_str().expect("Inbox::received: undo don't contain type and isn't Link");
|
|
|
|
if let Some(like) = likes::Like::find_by_ap_url(conn, link) {
|
|
|
|
likes::Like::delete_id(&like.ap_url, actor_id.as_ref(), conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
2018-12-23 11:12:15 +01:00
|
|
|
} else if let Some(reshare) = Reshare::find_by_ap_url(conn, link) {
|
|
|
|
Reshare::delete_id(&reshare.ap_url, actor_id.as_ref(), conn);
|
|
|
|
Ok(())
|
|
|
|
} else if let Some(follow) = Follow::find_by_ap_url(conn, link) {
|
|
|
|
Follow::delete_id(&follow.ap_url, actor_id.as_ref(), conn);
|
2018-11-24 12:44:17 +01:00
|
|
|
Ok(())
|
2018-12-23 11:12:15 +01:00
|
|
|
} else {
|
|
|
|
Err(InboxError::NoType)?
|
2018-11-24 12:44:17 +01:00
|
|
|
}
|
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())?;
|
2018-12-02 17:37:51 +01:00
|
|
|
Post::handle_update(conn, &act.update_props.object_object()?, searcher);
|
2018-11-24 12:44:17 +01:00
|
|
|
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 {}
|
2018-12-22 18:27:21 +01:00
|
|
|
|
|
|
|
const JSON_LIMIT: u64 = 1 << 20;
|
|
|
|
|
|
|
|
pub struct SignedJson<T>(pub Digest, pub Json<T>);
|
|
|
|
|
|
|
|
impl<'a, T: Deserialize<'a>> FromData<'a> for SignedJson<T> {
|
|
|
|
type Error = JsonError<'a>;
|
|
|
|
type Owned = String;
|
|
|
|
type Borrowed = str;
|
|
|
|
|
|
|
|
fn transform(r: &Request, d: Data) -> Transform<Outcome<Self::Owned, (Status, Self::Error), Data>> {
|
|
|
|
let size_limit = r.limits().get("json").unwrap_or(JSON_LIMIT);
|
|
|
|
let mut s = String::with_capacity(512);
|
|
|
|
match d.open().take(size_limit).read_to_string(&mut s) {
|
|
|
|
Ok(_) => Transform::Borrowed(Success(s)),
|
|
|
|
Err(e) => Transform::Borrowed(Failure((Status::BadRequest, JsonError::Io(e))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_data(_: &Request, o: Transformed<'a, Self>) -> Outcome<Self, (Status, Self::Error), Data> {
|
|
|
|
let string = o.borrowed()?;
|
|
|
|
match serde_json::from_str(&string) {
|
|
|
|
Ok(v) => Success(SignedJson(Digest::from_body(&string),Json(v))),
|
|
|
|
Err(e) => {
|
|
|
|
if e.is_data() {
|
|
|
|
Failure((Status::UnprocessableEntity, JsonError::Parse(string, e)))
|
|
|
|
} else {
|
|
|
|
Failure((Status::BadRequest, JsonError::Parse(string, e)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|