Rewrite User::from_activity to use the activitypub crate instead of raw JSON

This commit is contained in:
Bat 2018-06-21 21:30:56 +01:00
parent f5f2aa7c59
commit e7e557612e
5 changed files with 51 additions and 13 deletions

2
Cargo.lock generated
View File

@ -949,6 +949,8 @@ name = "plume"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"activitypub 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "activitypub 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"activitystreams-derive 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"activitystreams-traits 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"ammonia 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "ammonia 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"array_tool 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "array_tool 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
"base64 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)",

View File

@ -4,6 +4,8 @@ name = "plume"
version = "0.1.0" version = "0.1.0"
[dependencies] [dependencies]
activitypub = "0.1.1" activitypub = "0.1.1"
activitystreams-derive = "0.1.0"
activitystreams-traits = "0.1.0"
ammonia = "1.1.0" ammonia = "1.1.0"
array_tool = "1.0" array_tool = "1.0"
base64 = "0.9" base64 = "0.9"
@ -18,7 +20,6 @@ hex = "0.3"
hyper = "*" hyper = "*"
lazy_static = "*" lazy_static = "*"
openssl = "0.10.6" openssl = "0.10.6"
pulldown-cmark = { version = "0.1.2", default-features = false }
reqwest = "0.8" reqwest = "0.8"
rpassword = "2.0" rpassword = "2.0"
serde = "*" serde = "*"
@ -36,6 +37,10 @@ version = "0.4"
features = ["postgres", "r2d2", "chrono"] features = ["postgres", "r2d2", "chrono"]
version = "*" version = "*"
[dependencies.pulldown-cmark]
default-features = false
version = "0.1.2"
[dependencies.rocket] [dependencies.rocket]
git = "https://github.com/SergioBenitez/Rocket" git = "https://github.com/SergioBenitez/Rocket"
rev = "df7111143e466c18d1f56377a8d9530a5a306aba" rev = "df7111143e466c18d1f56377a8d9530a5a306aba"

View File

@ -115,3 +115,27 @@ pub trait IntoId {
} }
impl Link for Id {} impl Link for Id {}
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]
#[serde(rename_all = "camelCase")]
pub struct ApSignature {
#[serde(skip_serializing_if = "Option::is_none")]
#[activitystreams(concrete(PublicKey), functional)]
pub public_key: Option<serde_json::Value>
}
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]
#[serde(rename_all = "camelCase")]
pub struct PublicKey {
#[serde(skip_serializing_if = "Option::is_none")]
#[activitystreams(concrete(String), functional)]
pub id: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
#[activitystreams(concrete(String), functional)]
pub owner: Option<serde_json::Value>,
#[serde(skip_serializing_if = "Option::is_none")]
#[activitystreams(concrete(String), functional)]
pub public_key_pem: Option<serde_json::Value>
}

View File

@ -2,6 +2,9 @@
#![plugin(rocket_codegen)] #![plugin(rocket_codegen)]
extern crate activitypub; extern crate activitypub;
#[macro_use]
extern crate activitystreams_derive;
extern crate activitystreams_traits;
extern crate ammonia; extern crate ammonia;
extern crate array_tool; extern crate array_tool;
extern crate base64; extern crate base64;

View File

@ -1,5 +1,5 @@
use activitypub::{ use activitypub::{
Actor, Object, Endpoint, Actor, Object, Endpoint, CustomObject,
actor::Person, actor::Person,
collection::OrderedCollection collection::OrderedCollection
}; };
@ -27,7 +27,7 @@ use webfinger::*;
use BASE_URL; use BASE_URL;
use activity_pub::{ use activity_pub::{
ap_url, ActivityStream, Id, IntoId, ap_url, ActivityStream, Id, IntoId, ApSignature,
inbox::{Inbox, WithInbox}, inbox::{Inbox, WithInbox},
sign::{Signer, gen_keypair} sign::{Signer, gen_keypair}
}; };
@ -45,6 +45,8 @@ use safe_string::SafeString;
pub const AUTH_COOKIE: &'static str = "user_id"; pub const AUTH_COOKIE: &'static str = "user_id";
pub type CustomPerson = CustomObject<ApSignature, Person>;
#[derive(Queryable, Identifiable, Serialize, Deserialize, Clone, Debug)] #[derive(Queryable, Identifiable, Serialize, Deserialize, Clone, Debug)]
pub struct User { pub struct User {
pub id: i32, pub id: i32,
@ -157,14 +159,14 @@ impl User {
.send(); .send();
match req { match req {
Ok(mut res) => { Ok(mut res) => {
let json: serde_json::Value = serde_json::from_str(&res.text().unwrap()).unwrap(); let json: CustomPerson = serde_json::from_str(&res.text().unwrap()).unwrap();
Some(User::from_activity(conn, json, Url::parse(url.as_ref()).unwrap().host_str().unwrap().to_string())) Some(User::from_activity(conn, json, Url::parse(url.as_ref()).unwrap().host_str().unwrap().to_string()))
}, },
Err(_) => None Err(_) => None
} }
} }
fn from_activity(conn: &PgConnection, acct: serde_json::Value, inst: String) -> User { fn from_activity(conn: &PgConnection, acct: CustomPerson, inst: String) -> User {
let instance = match Instance::find_by_domain(conn, inst.clone()) { let instance = match Instance::find_by_domain(conn, inst.clone()) {
Some(instance) => instance, Some(instance) => instance,
None => { None => {
@ -176,19 +178,21 @@ impl User {
} }
}; };
User::insert(conn, NewUser { User::insert(conn, NewUser {
username: acct["preferredUsername"].as_str().unwrap().to_string(), username: acct.object.ap_actor_props.preferred_username_string().expect("User::from_activity: preferredUsername error"),
display_name: acct["name"].as_str().unwrap().to_string(), display_name: acct.object.object_props.name_string().expect("User::from_activity: name error"),
outbox_url: acct["outbox"].as_str().unwrap().to_string(), outbox_url: acct.object.ap_actor_props.outbox_string().expect("User::from_activity: outbox error"),
inbox_url: acct["inbox"].as_str().unwrap().to_string(), inbox_url: acct.object.ap_actor_props.inbox_string().expect("User::from_activity: inbox error"),
is_admin: false, is_admin: false,
summary: SafeString::new(&acct["summary"].as_str().unwrap().to_string()), summary: SafeString::new(&acct.object.object_props.summary_string().expect("User::from_activity: summary error")),
email: None, email: None,
hashed_password: None, hashed_password: None,
instance_id: instance.id, instance_id: instance.id,
ap_url: acct["id"].as_str().unwrap().to_string(), ap_url: acct.object.object_props.id_string().expect("User::from_activity: id error"),
public_key: acct["publicKey"]["publicKeyPem"].as_str().unwrap().to_string(), public_key: acct.custom_props.public_key_publickey().expect("User::from_activity: publicKey error")
.public_key_pem_string().expect("User::from_activity: publicKey.publicKeyPem error"),
private_key: None, private_key: None,
shared_inbox_url: acct["endpoints"]["sharedInbox"].as_str().map(|s| s.to_string()) shared_inbox_url: acct.object.ap_actor_props.endpoints_endpoint()
.and_then(|e| e.shared_inbox_string()).ok()
}) })
} }