Make Post::from_activity a bit more complete
This commit is contained in:
parent
3b2ca041d0
commit
6140865660
@ -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
|
||||||
}
|
}
|
||||||
|
@ -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 (blog, authors) = article.object_props.attributed_to_link_vec::<Id>()
|
||||||
|
.expect("Post::from_activity: attributedTo error")
|
||||||
|
.into_iter()
|
||||||
|
.fold((None, vec![]), |(blog, mut authors), link| {
|
||||||
|
let url: String = link.into();
|
||||||
|
match User::from_url(conn, url.clone()) {
|
||||||
|
Some(user) => {
|
||||||
|
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 {
|
let post = Post::insert(conn, NewPost {
|
||||||
blog_id: 0, // TODO
|
blog_id: blog.expect("Received a new Article without a blog").id,
|
||||||
slug: String::from(""), // TODO
|
slug: title.to_kebab_case(),
|
||||||
title: article.object_props.name_string().unwrap(),
|
title: title,
|
||||||
content: SafeString::new(&article.object_props.content_string().unwrap()),
|
content: SafeString::new(&article.object_props.content_string().expect("Post::from_activity: content error")),
|
||||||
published: true,
|
published: true,
|
||||||
license: String::from("CC-0"),
|
license: String::from("CC-0"), // TODO
|
||||||
ap_url: article.object_props.url_string().unwrap_or(String::from(""))
|
// 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() {
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user