2018-06-11 14:30:14 +02:00
|
|
|
use activitypub::{
|
|
|
|
activity::Create,
|
2018-06-20 21:42:16 +02:00
|
|
|
link,
|
2018-06-22 22:45:37 +02:00
|
|
|
object::Article
|
2018-06-11 14:30:14 +02:00
|
|
|
};
|
2018-06-22 22:45:37 +02:00
|
|
|
use chrono::{NaiveDateTime, TimeZone, Utc};
|
2018-06-10 21:33:42 +02:00
|
|
|
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any};
|
2018-06-23 13:14:03 +02:00
|
|
|
use heck::KebabCase;
|
2018-04-29 22:23:44 +02:00
|
|
|
use serde_json;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::activity_pub::{
|
2018-06-26 16:21:58 +02:00
|
|
|
PUBLIC_VISIBILTY, Id, IntoId,
|
2018-06-20 11:01:25 +02:00
|
|
|
inbox::FromActivity
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-06-26 16:21:58 +02:00
|
|
|
use {BASE_URL, ap_url};
|
2018-06-23 18:36:11 +02:00
|
|
|
use blogs::Blog;
|
|
|
|
use instance::Instance;
|
|
|
|
use likes::Like;
|
|
|
|
use mentions::Mention;
|
|
|
|
use post_authors::*;
|
|
|
|
use reshares::Reshare;
|
|
|
|
use users::User;
|
2018-04-23 15:41:43 +02:00
|
|
|
use schema::posts;
|
2018-06-11 12:21:34 +02:00
|
|
|
use safe_string::SafeString;
|
2018-04-23 15:41:43 +02:00
|
|
|
|
2018-06-17 21:37:10 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize, Clone)]
|
2018-04-23 15:41:43 +02:00
|
|
|
pub struct Post {
|
|
|
|
pub id: i32,
|
|
|
|
pub blog_id: i32,
|
|
|
|
pub slug: String,
|
|
|
|
pub title: String,
|
2018-06-11 12:21:34 +02:00
|
|
|
pub content: SafeString,
|
2018-04-23 15:41:43 +02:00
|
|
|
pub published: bool,
|
2018-04-30 19:46:27 +02:00
|
|
|
pub license: String,
|
2018-05-10 12:52:56 +02:00
|
|
|
pub creation_date: NaiveDateTime,
|
|
|
|
pub ap_url: String
|
2018-04-23 15:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "posts"]
|
|
|
|
pub struct NewPost {
|
|
|
|
pub blog_id: i32,
|
|
|
|
pub slug: String,
|
|
|
|
pub title: String,
|
2018-06-11 12:21:34 +02:00
|
|
|
pub content: SafeString,
|
2018-04-23 15:41:43 +02:00
|
|
|
pub published: bool,
|
2018-05-10 12:52:56 +02:00
|
|
|
pub license: String,
|
|
|
|
pub ap_url: String
|
2018-04-23 15:41:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Post {
|
2018-06-18 15:57:38 +02:00
|
|
|
insert!(posts, NewPost);
|
2018-06-18 15:44:23 +02:00
|
|
|
get!(posts);
|
2018-06-19 21:16:18 +02:00
|
|
|
find_by!(posts, find_by_slug, slug as String, blog_id as i32);
|
2018-06-18 17:13:09 +02:00
|
|
|
find_by!(posts, find_by_ap_url, ap_url as String);
|
2018-04-23 16:25:39 +02:00
|
|
|
|
2018-06-10 21:33:42 +02:00
|
|
|
pub fn count_local(conn: &PgConnection) -> usize {
|
|
|
|
use schema::post_authors;
|
|
|
|
use schema::users;
|
|
|
|
let local_authors = users::table.filter(users::instance_id.eq(Instance::local_id(conn))).select(users::id);
|
|
|
|
let local_posts_id = post_authors::table.filter(post_authors::author_id.eq(any(local_authors))).select(post_authors::post_id);
|
|
|
|
posts::table.filter(posts::id.eq(any(local_posts_id)))
|
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("Couldn't load local posts")
|
|
|
|
.len()
|
|
|
|
}
|
|
|
|
|
2018-05-12 14:56:38 +02:00
|
|
|
pub fn get_recents(conn: &PgConnection, limit: i64) -> Vec<Post> {
|
|
|
|
posts::table.order(posts::creation_date.desc())
|
|
|
|
.limit(limit)
|
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("Error loading recent posts")
|
|
|
|
}
|
|
|
|
|
2018-05-12 15:31:09 +02:00
|
|
|
pub fn get_recents_for_author(conn: &PgConnection, author: &User, limit: i64) -> Vec<Post> {
|
|
|
|
use schema::post_authors;
|
|
|
|
|
|
|
|
let posts = PostAuthor::belonging_to(author).select(post_authors::post_id);
|
|
|
|
posts::table.filter(posts::id.eq(any(posts)))
|
|
|
|
.order(posts::creation_date.desc())
|
|
|
|
.limit(limit)
|
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("Error loading recent posts for author")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_recents_for_blog(conn: &PgConnection, blog: &Blog, limit: i64) -> Vec<Post> {
|
|
|
|
posts::table.filter(posts::blog_id.eq(blog.id))
|
|
|
|
.order(posts::creation_date.desc())
|
|
|
|
.limit(limit)
|
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("Error loading recent posts for blog")
|
|
|
|
}
|
|
|
|
|
2018-07-21 16:58:30 +02:00
|
|
|
pub fn get_for_blog(conn: &PgConnection, blog:&Blog) -> Vec<Post> {
|
|
|
|
posts::table.filter(posts::blog_id.eq(blog.id))
|
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("Error loading posts for blog")
|
|
|
|
}
|
|
|
|
|
2018-04-30 18:50:35 +02:00
|
|
|
pub fn get_authors(&self, conn: &PgConnection) -> Vec<User> {
|
|
|
|
use schema::users;
|
|
|
|
use schema::post_authors;
|
|
|
|
let author_list = PostAuthor::belonging_to(self).select(post_authors::author_id);
|
|
|
|
users::table.filter(users::id.eq(any(author_list))).load::<User>(conn).unwrap()
|
|
|
|
}
|
2018-05-03 17:22:40 +02:00
|
|
|
|
|
|
|
pub fn get_blog(&self, conn: &PgConnection) -> Blog {
|
|
|
|
use schema::blogs;
|
|
|
|
blogs::table.filter(blogs::id.eq(self.blog_id))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Blog>(conn)
|
|
|
|
.expect("Couldn't load blog associted to post")
|
|
|
|
.into_iter().nth(0).unwrap()
|
|
|
|
}
|
2018-05-10 12:52:56 +02:00
|
|
|
|
2018-05-10 18:38:03 +02:00
|
|
|
pub fn get_likes(&self, conn: &PgConnection) -> Vec<Like> {
|
|
|
|
use schema::likes;
|
|
|
|
likes::table.filter(likes::post_id.eq(self.id))
|
|
|
|
.load::<Like>(conn)
|
|
|
|
.expect("Couldn't load likes associted to post")
|
|
|
|
}
|
|
|
|
|
2018-05-19 11:57:39 +02:00
|
|
|
pub fn get_reshares(&self, conn: &PgConnection) -> Vec<Reshare> {
|
|
|
|
use schema::reshares;
|
|
|
|
reshares::table.filter(reshares::post_id.eq(self.id))
|
|
|
|
.load::<Reshare>(conn)
|
|
|
|
.expect("Couldn't load reshares associted to post")
|
|
|
|
}
|
|
|
|
|
2018-06-22 17:17:53 +02:00
|
|
|
pub fn update_ap_url(&self, conn: &PgConnection) -> Post {
|
2018-05-10 12:52:56 +02:00
|
|
|
if self.ap_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(posts::ap_url.eq(self.compute_id(conn)))
|
2018-06-22 17:17:53 +02:00
|
|
|
.get_result::<Post>(conn).expect("Couldn't update AP URL")
|
|
|
|
} else {
|
|
|
|
self.clone()
|
2018-05-10 12:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 17:36:32 +02:00
|
|
|
|
|
|
|
pub fn get_receivers_urls(&self, conn: &PgConnection) -> Vec<String> {
|
|
|
|
let followers = self.get_authors(conn).into_iter().map(|a| a.get_followers(conn)).collect::<Vec<Vec<User>>>();
|
|
|
|
let to = followers.into_iter().fold(vec![], |mut acc, f| {
|
|
|
|
for x in f {
|
|
|
|
acc.push(x.ap_url);
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
});
|
|
|
|
to
|
|
|
|
}
|
2018-05-19 00:04:30 +02:00
|
|
|
|
|
|
|
pub fn into_activity(&self, conn: &PgConnection) -> Article {
|
|
|
|
let mut to = self.get_receivers_urls(conn);
|
|
|
|
to.push(PUBLIC_VISIBILTY.to_string());
|
|
|
|
|
2018-06-20 22:58:11 +02:00
|
|
|
let mentions = Mention::list_for_post(conn, self.id).into_iter().map(|m| m.to_activity(conn)).collect::<Vec<link::Mention>>();
|
|
|
|
|
2018-05-19 00:04:30 +02:00
|
|
|
let mut article = Article::default();
|
2018-06-23 14:29:41 +02:00
|
|
|
article.object_props.set_name_string(self.title.clone()).expect("Article::into_activity: name error");
|
|
|
|
article.object_props.set_id_string(self.ap_url.clone()).expect("Article::into_activity: id error");
|
|
|
|
article.object_props.set_attributed_to_link_vec::<Id>(self.get_authors(conn).into_iter().map(|x| Id::new(x.ap_url)).collect()).expect("Article::into_activity: attributedTo error");
|
|
|
|
article.object_props.set_content_string(self.content.get().clone()).expect("Article::into_activity: content error");
|
|
|
|
article.object_props.set_published_utctime(Utc.from_utc_datetime(&self.creation_date)).expect("Article::into_activity: published error");
|
|
|
|
article.object_props.set_tag_link_vec(mentions).expect("Article::into_activity: tag error");
|
|
|
|
article.object_props.set_url_string(self.ap_url.clone()).expect("Article::into_activity: url error");
|
|
|
|
article.object_props.set_to_link_vec::<Id>(to.into_iter().map(Id::new).collect()).expect("Article::into_activity: to error");
|
|
|
|
article.object_props.set_cc_link_vec::<Id>(vec![]).expect("Article::into_activity: cc error");
|
2018-05-19 00:04:30 +02:00
|
|
|
article
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_activity(&self, conn: &PgConnection) -> Create {
|
2018-06-23 14:29:41 +02:00
|
|
|
let article = self.into_activity(conn);
|
2018-05-19 00:04:30 +02:00
|
|
|
let mut act = Create::default();
|
2018-06-23 14:29:41 +02:00
|
|
|
act.object_props.set_id_string(format!("{}/activity", self.ap_url)).expect("Article::create_activity: id error");
|
|
|
|
act.object_props.set_to_link_vec::<Id>(article.object_props.to_link_vec().expect("Article::create_activity: Couldn't copy 'to'"))
|
|
|
|
.expect("Article::create_activity: to error");
|
|
|
|
act.object_props.set_cc_link_vec::<Id>(article.object_props.cc_link_vec().expect("Article::create_activity: Couldn't copy 'cc'"))
|
|
|
|
.expect("Article::create_activity: cc error");
|
|
|
|
act.create_props.set_actor_link(Id::new(self.get_authors(conn)[0].clone().ap_url)).expect("Article::create_activity: actor error");
|
|
|
|
act.create_props.set_object_object(article).expect("Article::create_activity: object error");
|
2018-05-19 00:04:30 +02:00
|
|
|
act
|
|
|
|
}
|
2018-06-18 18:34:29 +02:00
|
|
|
|
|
|
|
pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value {
|
2018-07-22 01:42:27 +02:00
|
|
|
let blog = self.get_blog(conn);
|
2018-06-18 18:34:29 +02:00
|
|
|
json!({
|
|
|
|
"post": self,
|
|
|
|
"author": self.get_authors(conn)[0].to_json(conn),
|
2018-07-22 01:42:27 +02:00
|
|
|
"url": format!("/~/{}/{}/", blog.actor_id, self.slug),
|
|
|
|
"date": self.creation_date.timestamp(),
|
|
|
|
"blog": blog,
|
|
|
|
"blog_url": format!("/~/{}", blog.actor_id)
|
2018-06-18 18:34:29 +02:00
|
|
|
})
|
|
|
|
}
|
2018-06-20 11:01:25 +02:00
|
|
|
|
|
|
|
pub fn compute_id(&self, conn: &PgConnection) -> String {
|
|
|
|
ap_url(format!("{}/~/{}/{}/", BASE_URL.as_str(), self.get_blog(conn).actor_id, self.slug))
|
|
|
|
}
|
2018-05-19 00:04:30 +02:00
|
|
|
}
|
|
|
|
|
2018-06-23 18:36:11 +02:00
|
|
|
impl FromActivity<Article, PgConnection> for Post {
|
2018-06-12 21:10:08 +02:00
|
|
|
fn from_activity(conn: &PgConnection, article: Article, _actor: Id) -> Post {
|
2018-06-23 13:14:03 +02:00
|
|
|
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");
|
2018-06-21 15:05:35 +02:00
|
|
|
let post = Post::insert(conn, NewPost {
|
2018-06-23 13:14:03 +02:00
|
|
|
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")),
|
2018-06-12 21:10:08 +02:00
|
|
|
published: true,
|
2018-06-23 13:14:03 +02:00
|
|
|
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"))
|
2018-06-21 15:05:35 +02:00
|
|
|
});
|
|
|
|
|
2018-06-23 13:14:03 +02:00
|
|
|
for author in authors.into_iter() {
|
|
|
|
PostAuthor::insert(conn, NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: author.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-06-21 15:05:35 +02:00
|
|
|
// save mentions
|
|
|
|
if let Some(serde_json::Value::Array(tags)) = article.object_props.tag.clone() {
|
|
|
|
for tag in tags.into_iter() {
|
|
|
|
serde_json::from_value::<link::Mention>(tag)
|
|
|
|
.map(|m| Mention::from_activity(conn, m, post.id, true))
|
|
|
|
.ok();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
post
|
2018-06-12 21:10:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-19 00:04:30 +02:00
|
|
|
impl IntoId for Post {
|
|
|
|
fn into_id(self) -> Id {
|
|
|
|
Id::new(self.ap_url.clone())
|
|
|
|
}
|
2018-04-23 15:41:43 +02:00
|
|
|
}
|