2018-05-24 10:42:45 +00:00
|
|
|
use comrak::{markdown_to_html, ComrakOptions};
|
2018-04-24 09:21:39 +00:00
|
|
|
use heck::KebabCase;
|
2018-04-23 14:25:39 +00:00
|
|
|
use rocket::request::Form;
|
2018-06-04 19:57:03 +00:00
|
|
|
use rocket::response::{Redirect, Flash};
|
2018-04-23 14:25:39 +00:00
|
|
|
use rocket_contrib::Template;
|
2018-05-10 09:44:57 +00:00
|
|
|
use serde_json;
|
2018-04-23 14:25:39 +00:00
|
|
|
|
2018-05-19 07:39:59 +00:00
|
|
|
use activity_pub::{broadcast, context, activity_pub, ActivityPub, object::Object};
|
2018-04-23 14:25:39 +00:00
|
|
|
use db_conn::DbConn;
|
2018-05-19 07:39:59 +00:00
|
|
|
use models::{
|
|
|
|
blogs::*,
|
|
|
|
comments::Comment,
|
|
|
|
post_authors::*,
|
|
|
|
posts::*,
|
|
|
|
users::User
|
|
|
|
};
|
2018-04-24 09:21:39 +00:00
|
|
|
use utils;
|
2018-06-11 10:21:34 +00:00
|
|
|
use safe_string::SafeString;
|
2018-04-23 14:25:39 +00:00
|
|
|
|
2018-05-04 11:09:08 +00:00
|
|
|
#[get("/~/<blog>/<slug>", rank = 4)]
|
2018-05-10 20:31:52 +00:00
|
|
|
fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Template {
|
2018-06-18 17:28:28 +00:00
|
|
|
may_fail!(Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| {
|
|
|
|
may_fail!(Post::find_by_slug(&*conn, slug), "Couldn't find this post", |post| {
|
|
|
|
let comments = Comment::find_by_post(&*conn, post.id);
|
2018-05-19 04:46:05 +00:00
|
|
|
|
2018-06-18 17:28:28 +00:00
|
|
|
Template::render("posts/details", json!({
|
|
|
|
"author": post.get_authors(&*conn)[0].to_json(&*conn),
|
|
|
|
"post": post,
|
|
|
|
"blog": blog,
|
|
|
|
"comments": comments.into_iter().map(|c| c.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
|
|
|
"n_likes": post.get_likes(&*conn).len(),
|
|
|
|
"has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false),
|
|
|
|
"n_reshares": post.get_reshares(&*conn).len(),
|
|
|
|
"has_reshared": user.clone().map(|u| u.has_reshared(&*conn, &post)).unwrap_or(false),
|
|
|
|
"account": user,
|
|
|
|
"date": &post.creation_date.timestamp()
|
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 11:09:08 +00:00
|
|
|
#[get("/~/<_blog>/<slug>", rank = 3, format = "application/activity+json")]
|
|
|
|
fn activity_details(_blog: String, slug: String, conn: DbConn) -> ActivityPub {
|
2018-05-18 22:04:30 +00:00
|
|
|
// FIXME: posts in different blogs may have the same slug
|
2018-05-04 11:09:08 +00:00
|
|
|
let post = Post::find_by_slug(&*conn, slug).unwrap();
|
|
|
|
|
|
|
|
let mut act = post.serialize(&*conn);
|
|
|
|
act["@context"] = context();
|
|
|
|
activity_pub(act)
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:57:03 +00:00
|
|
|
#[get("/~/<blog>/new", rank = 2)]
|
|
|
|
fn new_auth(blog: String) -> Flash<Redirect> {
|
|
|
|
utils::requires_login("You need to be logged in order to write a new post", &format!("/~/{}/new",blog))
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 11:09:08 +00:00
|
|
|
#[get("/~/<_blog>/new", rank = 1)]
|
2018-05-10 20:31:52 +00:00
|
|
|
fn new(_blog: String, user: User) -> Template {
|
|
|
|
Template::render("posts/new", json!({
|
|
|
|
"account": user
|
|
|
|
}))
|
2018-05-04 11:09:08 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 14:25:39 +00:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct NewPostForm {
|
|
|
|
pub title: String,
|
|
|
|
pub content: String,
|
|
|
|
pub license: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/~/<blog_name>/new", data = "<data>")]
|
2018-04-23 14:39:06 +00:00
|
|
|
fn create(blog_name: String, data: Form<NewPostForm>, user: User, conn: DbConn) -> Redirect {
|
2018-05-13 17:00:47 +00:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap();
|
2018-04-23 14:25:39 +00:00
|
|
|
let form = data.get();
|
|
|
|
let slug = form.title.to_string().to_kebab_case();
|
2018-05-24 10:42:45 +00:00
|
|
|
|
|
|
|
let content = markdown_to_html(form.content.to_string().as_ref(), &ComrakOptions{
|
|
|
|
smart: true,
|
|
|
|
safe: true,
|
|
|
|
ext_strikethrough: true,
|
|
|
|
ext_tagfilter: true,
|
|
|
|
ext_table: true,
|
|
|
|
ext_autolink: true,
|
|
|
|
ext_tasklist: true,
|
|
|
|
ext_superscript: true,
|
|
|
|
ext_header_ids: Some("title".to_string()),
|
|
|
|
ext_footnotes: true,
|
|
|
|
..ComrakOptions::default()
|
|
|
|
});
|
|
|
|
|
2018-04-23 14:39:06 +00:00
|
|
|
let post = Post::insert(&*conn, NewPost {
|
2018-04-23 14:25:39 +00:00
|
|
|
blog_id: blog.id,
|
|
|
|
slug: slug.to_string(),
|
|
|
|
title: form.title.to_string(),
|
2018-06-11 10:21:34 +00:00
|
|
|
content: SafeString::new(&content),
|
2018-04-23 14:25:39 +00:00
|
|
|
published: true,
|
2018-05-10 10:52:56 +00:00
|
|
|
license: form.license.to_string(),
|
|
|
|
ap_url: "".to_string()
|
2018-04-23 14:25:39 +00:00
|
|
|
});
|
2018-05-10 10:52:56 +00:00
|
|
|
post.update_ap_url(&*conn);
|
2018-04-23 14:39:06 +00:00
|
|
|
PostAuthor::insert(&*conn, NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: user.id
|
|
|
|
});
|
2018-05-01 15:51:49 +00:00
|
|
|
|
2018-05-18 22:04:30 +00:00
|
|
|
let act = post.create_activity(&*conn);
|
|
|
|
broadcast(&*conn, &user, act, user.get_followers(&*conn));
|
2018-05-01 15:51:49 +00:00
|
|
|
|
2018-06-16 17:39:22 +00:00
|
|
|
Redirect::to(format!("/~/{}/{}/", blog_name, slug))
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|