2018-06-11 12:30:14 +00:00
|
|
|
use activitypub::{
|
|
|
|
activity::Create,
|
2018-06-20 19:42:16 +00:00
|
|
|
link,
|
2018-06-11 12:30:14 +00:00
|
|
|
object::{Article, properties::ObjectProperties}
|
|
|
|
};
|
2018-04-30 17:46:27 +00:00
|
|
|
use chrono::NaiveDateTime;
|
2018-06-10 19:33:42 +00:00
|
|
|
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl, dsl::any};
|
2018-04-29 20:23:44 +00:00
|
|
|
use serde_json;
|
2018-04-24 09:21:39 +00:00
|
|
|
|
2018-05-03 15:22:40 +00:00
|
|
|
use BASE_URL;
|
2018-05-19 07:39:59 +00:00
|
|
|
use activity_pub::{
|
|
|
|
PUBLIC_VISIBILTY, ap_url, Id, IntoId,
|
2018-06-20 09:01:25 +00:00
|
|
|
inbox::FromActivity
|
2018-05-19 07:39:59 +00:00
|
|
|
};
|
|
|
|
use models::{
|
|
|
|
blogs::Blog,
|
2018-06-10 19:33:42 +00:00
|
|
|
instance::Instance,
|
2018-05-19 07:39:59 +00:00
|
|
|
likes::Like,
|
2018-06-20 19:42:16 +00:00
|
|
|
mentions::Mention,
|
2018-05-19 07:39:59 +00:00
|
|
|
post_authors::PostAuthor,
|
2018-05-19 09:57:39 +00:00
|
|
|
reshares::Reshare,
|
2018-05-19 07:39:59 +00:00
|
|
|
users::User
|
|
|
|
};
|
2018-04-23 13:41:43 +00:00
|
|
|
use schema::posts;
|
2018-06-11 10:21:34 +00:00
|
|
|
use safe_string::SafeString;
|
2018-04-23 13:41:43 +00:00
|
|
|
|
2018-06-17 19:37:10 +00:00
|
|
|
#[derive(Queryable, Identifiable, Serialize, Clone)]
|
2018-04-23 13:41:43 +00:00
|
|
|
pub struct Post {
|
|
|
|
pub id: i32,
|
|
|
|
pub blog_id: i32,
|
|
|
|
pub slug: String,
|
|
|
|
pub title: String,
|
2018-06-11 10:21:34 +00:00
|
|
|
pub content: SafeString,
|
2018-04-23 13:41:43 +00:00
|
|
|
pub published: bool,
|
2018-04-30 17:46:27 +00:00
|
|
|
pub license: String,
|
2018-05-10 10:52:56 +00:00
|
|
|
pub creation_date: NaiveDateTime,
|
|
|
|
pub ap_url: String
|
2018-04-23 13:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Insertable)]
|
|
|
|
#[table_name = "posts"]
|
|
|
|
pub struct NewPost {
|
|
|
|
pub blog_id: i32,
|
|
|
|
pub slug: String,
|
|
|
|
pub title: String,
|
2018-06-11 10:21:34 +00:00
|
|
|
pub content: SafeString,
|
2018-04-23 13:41:43 +00:00
|
|
|
pub published: bool,
|
2018-05-10 10:52:56 +00:00
|
|
|
pub license: String,
|
|
|
|
pub ap_url: String
|
2018-04-23 13:41:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Post {
|
2018-06-18 13:57:38 +00:00
|
|
|
insert!(posts, NewPost);
|
2018-06-18 13:44:23 +00:00
|
|
|
get!(posts);
|
2018-06-19 19:16:18 +00:00
|
|
|
find_by!(posts, find_by_slug, slug as String, blog_id as i32);
|
2018-06-18 15:13:09 +00:00
|
|
|
find_by!(posts, find_by_ap_url, ap_url as String);
|
2018-04-23 14:25:39 +00:00
|
|
|
|
2018-06-10 19:33:42 +00: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 12:56:38 +00: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 13:31:09 +00: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-04-30 16:50:35 +00: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 15:22:40 +00: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 10:52:56 +00:00
|
|
|
|
2018-05-10 16:38:03 +00: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 09:57:39 +00: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 15:17:53 +00:00
|
|
|
pub fn update_ap_url(&self, conn: &PgConnection) -> Post {
|
2018-05-10 10:52:56 +00:00
|
|
|
if self.ap_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(posts::ap_url.eq(self.compute_id(conn)))
|
2018-06-22 15:17:53 +00:00
|
|
|
.get_result::<Post>(conn).expect("Couldn't update AP URL")
|
|
|
|
} else {
|
|
|
|
self.clone()
|
2018-05-10 10:52:56 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-10 15:36:32 +00: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-18 22:04:30 +00: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 20:58:11 +00:00
|
|
|
let mentions = Mention::list_for_post(conn, self.id).into_iter().map(|m| m.to_activity(conn)).collect::<Vec<link::Mention>>();
|
|
|
|
|
2018-05-18 22:04:30 +00:00
|
|
|
let mut article = Article::default();
|
|
|
|
article.object_props = ObjectProperties {
|
|
|
|
name: Some(serde_json::to_value(self.title.clone()).unwrap()),
|
|
|
|
id: Some(serde_json::to_value(self.ap_url.clone()).unwrap()),
|
|
|
|
attributed_to: Some(serde_json::to_value(self.get_authors(conn).into_iter().map(|x| x.ap_url).collect::<Vec<String>>()).unwrap()),
|
|
|
|
content: Some(serde_json::to_value(self.content.clone()).unwrap()),
|
|
|
|
published: Some(serde_json::to_value(self.creation_date).unwrap()),
|
2018-06-20 20:58:11 +00:00
|
|
|
tag: Some(serde_json::to_value(mentions).unwrap()),
|
2018-06-21 14:48:54 +00:00
|
|
|
url: Some(serde_json::to_value(self.ap_url.clone()).unwrap()),
|
2018-05-18 22:04:30 +00:00
|
|
|
to: Some(serde_json::to_value(to).unwrap()),
|
|
|
|
cc: Some(serde_json::to_value(Vec::<serde_json::Value>::new()).unwrap()),
|
2018-06-20 20:58:11 +00:00
|
|
|
..ObjectProperties::default()
|
2018-05-18 22:04:30 +00:00
|
|
|
};
|
|
|
|
article
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn create_activity(&self, conn: &PgConnection) -> Create {
|
|
|
|
let mut act = Create::default();
|
|
|
|
act.object_props.set_id_string(format!("{}/activity", self.ap_url)).unwrap();
|
2018-06-10 11:13:07 +00:00
|
|
|
act.create_props.set_actor_link(Id::new(self.get_authors(conn)[0].clone().ap_url)).unwrap();
|
|
|
|
act.create_props.set_object_object(self.into_activity(conn)).unwrap();
|
2018-05-18 22:04:30 +00:00
|
|
|
act
|
|
|
|
}
|
2018-06-18 16:34:29 +00:00
|
|
|
|
|
|
|
pub fn to_json(&self, conn: &PgConnection) -> serde_json::Value {
|
|
|
|
json!({
|
|
|
|
"post": self,
|
|
|
|
"author": self.get_authors(conn)[0].to_json(conn),
|
|
|
|
"url": format!("/~/{}/{}/", self.get_blog(conn).actor_id, self.slug),
|
|
|
|
"date": self.creation_date.timestamp()
|
|
|
|
})
|
|
|
|
}
|
2018-06-20 09:01:25 +00: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-18 22:04:30 +00:00
|
|
|
}
|
|
|
|
|
2018-06-12 19:10:08 +00:00
|
|
|
impl FromActivity<Article> for Post {
|
|
|
|
fn from_activity(conn: &PgConnection, article: Article, _actor: Id) -> Post {
|
2018-06-21 13:05:35 +00:00
|
|
|
let post = Post::insert(conn, NewPost {
|
2018-06-12 19:10:08 +00:00
|
|
|
blog_id: 0, // TODO
|
|
|
|
slug: String::from(""), // TODO
|
|
|
|
title: article.object_props.name_string().unwrap(),
|
|
|
|
content: SafeString::new(&article.object_props.content_string().unwrap()),
|
|
|
|
published: true,
|
|
|
|
license: String::from("CC-0"),
|
|
|
|
ap_url: article.object_props.url_string().unwrap_or(String::from(""))
|
2018-06-21 13:05:35 +00: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 19:10:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-18 22:04:30 +00:00
|
|
|
impl IntoId for Post {
|
|
|
|
fn into_id(self) -> Id {
|
|
|
|
Id::new(self.ap_url.clone())
|
|
|
|
}
|
2018-04-23 13:41:43 +00:00
|
|
|
}
|