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

This commit is contained in:
Bat 2018-06-21 21:39:44 +01:00
parent e7e557612e
commit a0f680a9b6

View file

@ -1,4 +1,4 @@
use activitypub::{Actor, Object, actor::Group, collection::OrderedCollection}; use activitypub::{Actor, Object, CustomObject, actor::Group, collection::OrderedCollection};
use reqwest::{ use reqwest::{
Client, Client,
header::{Accept, qitem}, header::{Accept, qitem},
@ -17,13 +17,14 @@ use openssl::{
use webfinger::*; use webfinger::*;
use activity_pub::{ use activity_pub::{
ActivityStream, Id, IntoId, ApSignature, ActivityStream, Id, IntoId,
inbox::WithInbox, inbox::WithInbox,
sign sign
}; };
use models::instance::*; use models::instance::*;
use schema::blogs; use schema::blogs;
pub type CustomGroup = CustomObject<ApSignature, Group>;
#[derive(Queryable, Identifiable, Serialize, Deserialize, Clone)] #[derive(Queryable, Identifiable, Serialize, Deserialize, Clone)]
pub struct Blog { pub struct Blog {
@ -111,14 +112,14 @@ impl Blog {
.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 = serde_json::from_str(&res.text().unwrap()).unwrap();
Some(Blog::from_activity(conn, json, Url::parse(url.as_ref()).unwrap().host_str().unwrap().to_string())) Some(Blog::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) -> Blog { fn from_activity(conn: &PgConnection, acct: CustomGroup, inst: String) -> Blog {
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 => {
@ -130,14 +131,15 @@ impl Blog {
} }
}; };
Blog::insert(conn, NewBlog { Blog::insert(conn, NewBlog {
actor_id: acct["preferredUsername"].as_str().unwrap().to_string(), actor_id: acct.object.ap_actor_props.preferred_username_string().expect("Blog::from_activity: preferredUsername error"),
title: acct["name"].as_str().unwrap().to_string(), title: acct.object.object_props.name_string().expect("Blog::from_activity: name error"),
outbox_url: acct["outbox"].as_str().unwrap().to_string(), outbox_url: acct.object.ap_actor_props.outbox_string().expect("Blog::from_activity: outbox error"),
inbox_url: acct["inbox"].as_str().unwrap().to_string(), inbox_url: acct.object.ap_actor_props.inbox_string().expect("Blog::from_activity: inbox error"),
summary: acct["summary"].as_str().unwrap().to_string(), summary: acct.object.object_props.summary_string().expect("Blog::from_activity: summary error"),
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("Blog::from_activity: id error"),
public_key: acct["publicKey"]["publicKeyPem"].as_str().unwrap_or("").to_string(), public_key: acct.custom_props.public_key_publickey().expect("Blog::from_activity: publicKey error")
.public_key_pem_string().expect("Blog::from_activity: publicKey.publicKeyPem error"),
private_key: None private_key: None
}) })
} }