Improve the background article fetching code

This commit is contained in:
Bat 2018-07-26 22:59:41 +02:00
parent bd259891f3
commit 0314629d99
3 changed files with 62 additions and 54 deletions

View File

@ -222,7 +222,9 @@ impl Post {
impl FromActivity<Article, PgConnection> for Post { impl FromActivity<Article, PgConnection> for Post {
fn from_activity(conn: &PgConnection, article: Article, _actor: Id) -> Post { fn from_activity(conn: &PgConnection, article: Article, _actor: Id) -> Post {
println!("Article: {:?}", article); if let Some(post) = Post::find_by_ap_url(conn, article.object_props.id_string().unwrap_or(String::new())) {
post
} else {
let (blog, authors) = article.object_props.attributed_to_link_vec::<Id>() let (blog, authors) = article.object_props.attributed_to_link_vec::<Id>()
.expect("Post::from_activity: attributedTo error") .expect("Post::from_activity: attributedTo error")
.into_iter() .into_iter()
@ -266,6 +268,7 @@ impl FromActivity<Article, PgConnection> for Post {
} }
post post
} }
}
} }
impl IntoId for Post { impl IntoId for Post {

View File

@ -159,11 +159,14 @@ impl User {
.send(); .send();
match req { match req {
Ok(mut res) => { Ok(mut res) => {
let text = &res.text().unwrap(); if let Ok(text) = &res.text() {
let ap_sign: ApSignature = serde_json::from_str(text).unwrap(); if let Ok(ap_sign) = serde_json::from_str::<ApSignature>(text) {
let mut json: CustomPerson = serde_json::from_str(text).unwrap(); if let Ok(mut json) = serde_json::from_str::<CustomPerson>(text) {
json.custom_props = ap_sign; // without this workaround, publicKey is not correctly deserialized json.custom_props = ap_sign; // without this workaround, publicKey is not correctly deserialized
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()))
} else { None }
} else { None }
} else { None }
}, },
Err(e) => { Err(e) => {
println!("User fetch error: {:?}", e); println!("User fetch error: {:?}", e);

View File

@ -47,6 +47,7 @@ fn details<'r>(name: String, conn: DbConn, account: Option<User>, worker: State<
let n_followers = user.get_followers(&*conn).len(); let n_followers = user.get_followers(&*conn).len();
// Fetch new articles // Fetch new articles
if !user.get_instance(&*conn).local {
let user_clone = user.clone(); let user_clone = user.clone();
worker.execute(Thunk::of(move || { worker.execute(Thunk::of(move || {
for create_act in user_clone.fetch_outbox::<Create>() { for create_act in user_clone.fetch_outbox::<Create>() {
@ -59,6 +60,7 @@ fn details<'r>(name: String, conn: DbConn, account: Option<User>, worker: State<
} }
} }
})); }));
}
Template::render("users/details", json!({ Template::render("users/details", json!({
"user": user.to_json(&*conn), "user": user.to_json(&*conn),