Make Post::from_activity a bit more complete

This commit is contained in:
Bat 2018-06-23 12:14:03 +01:00
parent 3b2ca041d0
commit 6140865660
3 changed files with 33 additions and 9 deletions

View File

@ -229,7 +229,7 @@ impl Blog {
// The requested user was not in the DB // The requested user was not in the DB
// We try to fetch it if it is remote // We try to fetch it if it is remote
if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() { if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() {
Some(Blog::fetch_from_url(conn, url).unwrap()) Blog::fetch_from_url(conn, url)
} else { } else {
None None
} }

View File

@ -5,6 +5,7 @@ use activitypub::{
}; };
use chrono::{NaiveDateTime, TimeZone, Utc}; use chrono::{NaiveDateTime, TimeZone, Utc};
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any}; use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any};
use heck::KebabCase;
use serde_json; use serde_json;
use BASE_URL; use BASE_URL;
@ -17,7 +18,7 @@ use models::{
instance::Instance, instance::Instance,
likes::Like, likes::Like,
mentions::Mention, mentions::Mention,
post_authors::PostAuthor, post_authors::*,
reshares::Reshare, reshares::Reshare,
users::User users::User
}; };
@ -185,16 +186,39 @@ impl Post {
impl FromActivity<Article> for Post { impl FromActivity<Article> for Post {
fn from_activity(conn: &PgConnection, article: Article, _actor: Id) -> Post { fn from_activity(conn: &PgConnection, article: Article, _actor: Id) -> Post {
let post = Post::insert(conn, NewPost { let (blog, authors) = article.object_props.attributed_to_link_vec::<Id>()
blog_id: 0, // TODO .expect("Post::from_activity: attributedTo error")
slug: String::from(""), // TODO .into_iter()
title: article.object_props.name_string().unwrap(), .fold((None, vec![]), |(blog, mut authors), link| {
content: SafeString::new(&article.object_props.content_string().unwrap()), let url: String = link.into();
published: true, match User::from_url(conn, url.clone()) {
license: String::from("CC-0"), Some(user) => {
ap_url: article.object_props.url_string().unwrap_or(String::from("")) authors.push(user);
(blog, authors)
},
None => (blog.or_else(|| Blog::from_url(conn, url)), authors)
}
}); });
let title = article.object_props.name_string().expect("Post::from_activity: title error");
let post = Post::insert(conn, NewPost {
blog_id: blog.expect("Received a new Article without a blog").id,
slug: title.to_kebab_case(),
title: title,
content: SafeString::new(&article.object_props.content_string().expect("Post::from_activity: content error")),
published: true,
license: String::from("CC-0"), // TODO
// FIXME: This is wrong: with this logic, we may use the display URL as the AP ID. We need two different fields
ap_url: article.object_props.url_string().unwrap_or(article.object_props.id_string().expect("Post::from_activity: url + id error"))
});
for author in authors.into_iter() {
PostAuthor::insert(conn, NewPostAuthor {
post_id: post.id,
author_id: author.id
});
}
// save mentions // save mentions
if let Some(serde_json::Value::Array(tags)) = article.object_props.tag.clone() { if let Some(serde_json::Value::Array(tags)) = article.object_props.tag.clone() {
for tag in tags.into_iter() { for tag in tags.into_iter() {

View File

@ -378,7 +378,7 @@ impl User {
// The requested user was not in the DB // The requested user was not in the DB
// We try to fetch it if it is remote // We try to fetch it if it is remote
if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() { if Url::parse(url.as_ref()).unwrap().host_str().unwrap() != BASE_URL.as_str() {
Some(User::fetch_from_url(conn, url).unwrap()) User::fetch_from_url(conn, url)
} else { } else {
None None
} }