2018-04-30 19:46:27 +02:00
|
|
|
use chrono::NaiveDateTime;
|
2018-04-30 18:50:35 +02:00
|
|
|
use diesel::{self, PgConnection, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl};
|
|
|
|
use diesel::dsl::any;
|
2018-04-29 22:23:44 +02:00
|
|
|
use serde_json;
|
2018-04-24 11:21:39 +02:00
|
|
|
|
2018-05-03 17:22:40 +02:00
|
|
|
use BASE_URL;
|
|
|
|
use activity_pub::{PUBLIC_VISIBILTY, ap_url};
|
2018-04-30 18:50:35 +02:00
|
|
|
use activity_pub::actor::Actor;
|
2018-04-29 22:23:44 +02:00
|
|
|
use activity_pub::object::Object;
|
2018-05-03 17:22:40 +02:00
|
|
|
use models::blogs::Blog;
|
2018-04-30 18:50:35 +02:00
|
|
|
use models::users::User;
|
|
|
|
use models::post_authors::PostAuthor;
|
2018-04-23 15:41:43 +02:00
|
|
|
use schema::posts;
|
|
|
|
|
2018-05-09 21:09:52 +02:00
|
|
|
#[derive(Queryable, Identifiable, Serialize)]
|
2018-04-23 15:41:43 +02:00
|
|
|
pub struct Post {
|
|
|
|
pub id: i32,
|
|
|
|
pub blog_id: i32,
|
|
|
|
pub slug: String,
|
|
|
|
pub title: String,
|
|
|
|
pub content: String,
|
|
|
|
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,
|
|
|
|
pub content: String,
|
|
|
|
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 {
|
|
|
|
pub fn insert (conn: &PgConnection, new: NewPost) -> Post {
|
|
|
|
diesel::insert_into(posts::table)
|
|
|
|
.values(new)
|
|
|
|
.get_result(conn)
|
|
|
|
.expect("Error saving new post")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(conn: &PgConnection, id: i32) -> Option<Post> {
|
|
|
|
posts::table.filter(posts::id.eq(id))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("Error loading post by id")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
2018-04-23 16:25:39 +02:00
|
|
|
|
|
|
|
pub fn find_by_slug(conn: &PgConnection, slug: String) -> Option<Post> {
|
|
|
|
posts::table.filter(posts::slug.eq(slug))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("Error loading post by slug")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
2018-04-30 18:50:35 +02:00
|
|
|
|
2018-05-10 12:52:56 +02:00
|
|
|
pub fn get_by_ap_url(conn: &PgConnection, ap_url: String) -> Option<Post> {
|
|
|
|
posts::table.filter(posts::ap_url.eq(ap_url))
|
|
|
|
.limit(1)
|
|
|
|
.load::<Post>(conn)
|
|
|
|
.expect("Error loading post by AP URL")
|
|
|
|
.into_iter().nth(0)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
pub fn update_ap_url(&self, conn: &PgConnection) {
|
|
|
|
if self.ap_url.len() == 0 {
|
|
|
|
diesel::update(self)
|
|
|
|
.set(posts::ap_url.eq(self.compute_id(conn)))
|
|
|
|
.get_result::<Post>(conn).expect("Couldn't update AP URL");
|
|
|
|
}
|
|
|
|
}
|
2018-04-23 15:41:43 +02:00
|
|
|
}
|
2018-04-29 22:23:44 +02:00
|
|
|
|
|
|
|
impl Object for Post {
|
2018-05-03 17:22:40 +02:00
|
|
|
fn compute_id(&self, conn: &PgConnection) -> String {
|
2018-05-04 12:46:02 +02:00
|
|
|
ap_url(format!("{}/~/{}/{}", BASE_URL.as_str(), self.get_blog(conn).actor_id, self.slug))
|
2018-05-03 17:22:40 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:50:35 +02:00
|
|
|
fn serialize(&self, conn: &PgConnection) -> serde_json::Value {
|
2018-05-03 23:36:59 +02:00
|
|
|
let followers = self.get_authors(conn).into_iter().map(|a| a.get_followers(conn)).collect::<Vec<Vec<User>>>();
|
2018-05-03 23:19:47 +02:00
|
|
|
let mut to = followers.into_iter().fold(vec![], |mut acc, f| {
|
|
|
|
for x in f {
|
|
|
|
acc.push(x.ap_url);
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
});
|
|
|
|
to.push(PUBLIC_VISIBILTY.to_string());
|
2018-05-03 23:36:59 +02:00
|
|
|
|
2018-05-03 21:27:17 +02:00
|
|
|
json!({
|
2018-05-03 23:36:59 +02:00
|
|
|
"type": "Article",
|
2018-05-03 23:46:40 +02:00
|
|
|
"id": self.compute_id(conn),
|
2018-05-04 13:39:11 +02:00
|
|
|
"attributedTo": self.get_authors(conn)[0].compute_id(conn),
|
2018-05-04 14:12:02 +02:00
|
|
|
"name": self.title,
|
2018-05-03 23:36:59 +02:00
|
|
|
"content": self.content,
|
2018-05-04 00:12:28 +02:00
|
|
|
"actor": self.get_authors(conn)[0].compute_id(conn),
|
|
|
|
"published": self.creation_date,
|
2018-05-03 21:27:17 +02:00
|
|
|
// TODO: "image": "image",
|
|
|
|
// TODO: "preview": "preview",
|
|
|
|
// TODO: "replies": "replies",
|
|
|
|
// TODO: "summary": "summary",
|
|
|
|
"tag": [],
|
|
|
|
// TODO: "updated": "updated",
|
2018-05-04 12:47:04 +02:00
|
|
|
"url": self.compute_id(conn),
|
2018-05-04 00:12:28 +02:00
|
|
|
"to": to,
|
|
|
|
"cc": []
|
2018-05-03 21:27:17 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|