2018-06-21 19:09:18 +02:00
|
|
|
use activitypub::object::Article;
|
2018-04-24 11:21:39 +02:00
|
|
|
use heck::KebabCase;
|
2018-04-23 16:25:39 +02:00
|
|
|
use rocket::request::Form;
|
2018-06-04 21:57:03 +02:00
|
|
|
use rocket::response::{Redirect, Flash};
|
2018-04-23 16:25:39 +02:00
|
|
|
use rocket_contrib::Template;
|
2018-05-10 11:44:57 +02:00
|
|
|
use serde_json;
|
2018-04-23 16:25:39 +02:00
|
|
|
|
2018-06-21 19:09:18 +02:00
|
|
|
use activity_pub::{broadcast, ActivityStream};
|
2018-04-23 16:25:39 +02:00
|
|
|
use db_conn::DbConn;
|
2018-05-19 09:39:59 +02:00
|
|
|
use models::{
|
|
|
|
blogs::*,
|
|
|
|
comments::Comment,
|
2018-06-20 22:58:11 +02:00
|
|
|
mentions::Mention,
|
2018-05-19 09:39:59 +02:00
|
|
|
post_authors::*,
|
|
|
|
posts::*,
|
|
|
|
users::User
|
|
|
|
};
|
2018-06-21 16:00:25 +02:00
|
|
|
use routes::comments::CommentQuery;
|
2018-06-11 12:21:34 +02:00
|
|
|
use safe_string::SafeString;
|
2018-06-20 20:22:34 +02:00
|
|
|
use utils;
|
2018-04-23 16:25:39 +02:00
|
|
|
|
2018-06-21 16:00:25 +02:00
|
|
|
// See: https://github.com/SergioBenitez/Rocket/pull/454
|
2018-05-04 13:09:08 +02:00
|
|
|
#[get("/~/<blog>/<slug>", rank = 4)]
|
2018-05-10 22:31:52 +02:00
|
|
|
fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Template {
|
2018-06-21 16:00:25 +02:00
|
|
|
details_response(blog, slug, conn, user, None)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/~/<blog>/<slug>?<query>")]
|
|
|
|
fn details_response(blog: String, slug: String, conn: DbConn, user: Option<User>, query: Option<CommentQuery>) -> Template {
|
2018-06-22 00:50:06 +02:00
|
|
|
may_fail!(user, Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| {
|
|
|
|
may_fail!(user, Post::find_by_slug(&*conn, slug, blog.id), "Couldn't find this post", |post| {
|
2018-06-21 12:28:42 +02:00
|
|
|
let comments = Comment::list_by_post(&*conn, post.id);
|
2018-05-19 06:46:05 +02:00
|
|
|
|
2018-06-18 19:28:28 +02: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,
|
2018-06-21 16:00:25 +02:00
|
|
|
"date": &post.creation_date.timestamp(),
|
|
|
|
"previous": query.and_then(|q| q.responding_to.map(|r| Comment::get(&*conn, r).expect("Error retrieving previous comment").to_json(&*conn))),
|
|
|
|
"user_fqn": user.map(|u| u.get_fqn(&*conn)).unwrap_or(String::new())
|
2018-06-18 19:28:28 +02:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
2018-04-23 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 21:16:18 +02:00
|
|
|
#[get("/~/<blog>/<slug>", rank = 3, format = "application/activity+json")]
|
2018-06-21 19:09:18 +02:00
|
|
|
fn activity_details(blog: String, slug: String, conn: DbConn) -> ActivityStream<Article> {
|
2018-06-19 21:16:18 +02:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, blog).unwrap();
|
|
|
|
let post = Post::find_by_slug(&*conn, slug, blog.id).unwrap();
|
2018-05-04 13:09:08 +02:00
|
|
|
|
2018-06-21 19:09:18 +02:00
|
|
|
ActivityStream::new(post.into_activity(&*conn))
|
2018-04-23 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:57:03 +02:00
|
|
|
#[get("/~/<blog>/new", rank = 2)]
|
|
|
|
fn new_auth(blog: String) -> Flash<Redirect> {
|
2018-06-19 23:20:27 +02:00
|
|
|
utils::requires_login("You need to be logged in order to write a new post", uri!(new: blog = blog))
|
2018-04-23 16:25:39 +02:00
|
|
|
}
|
|
|
|
|
2018-06-19 21:16:18 +02:00
|
|
|
#[get("/~/<blog>/new", rank = 1)]
|
2018-06-20 10:44:56 +02:00
|
|
|
fn new(blog: String, user: User, conn: DbConn) -> Template {
|
|
|
|
let b = Blog::find_by_fqn(&*conn, blog.to_string()).unwrap();
|
|
|
|
|
|
|
|
if !user.is_author_in(&*conn, b.clone()) {
|
|
|
|
Template::render("errors/403", json!({
|
|
|
|
"error_message": "You are not author in this blog."
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Template::render("posts/new", json!({
|
|
|
|
"account": user
|
|
|
|
}))
|
|
|
|
}
|
2018-05-04 13:09:08 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 16:25:39 +02:00
|
|
|
#[derive(FromForm)]
|
|
|
|
struct NewPostForm {
|
|
|
|
pub title: String,
|
|
|
|
pub content: String,
|
|
|
|
pub license: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/~/<blog_name>/new", data = "<data>")]
|
2018-04-23 16:39:06 +02:00
|
|
|
fn create(blog_name: String, data: Form<NewPostForm>, user: User, conn: DbConn) -> Redirect {
|
2018-05-13 19:00:47 +02:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).unwrap();
|
2018-04-23 16:25:39 +02:00
|
|
|
let form = data.get();
|
|
|
|
let slug = form.title.to_string().to_kebab_case();
|
2018-05-24 12:42:45 +02:00
|
|
|
|
2018-06-20 10:44:56 +02:00
|
|
|
if !user.is_author_in(&*conn, blog.clone()) {
|
|
|
|
Redirect::to(uri!(super::blogs::details: name = blog_name))
|
2018-06-19 21:16:18 +02:00
|
|
|
} else {
|
2018-06-20 10:44:56 +02:00
|
|
|
if slug == "new" || Post::find_by_slug(&*conn, slug.clone(), blog.id).is_some() {
|
|
|
|
Redirect::to(uri!(new: blog = blog_name))
|
|
|
|
} else {
|
2018-06-20 22:58:11 +02:00
|
|
|
let (content, mentions) = utils::md_to_html(form.content.to_string().as_ref());
|
2018-05-24 12:42:45 +02:00
|
|
|
|
2018-06-20 10:44:56 +02:00
|
|
|
let post = Post::insert(&*conn, NewPost {
|
|
|
|
blog_id: blog.id,
|
|
|
|
slug: slug.to_string(),
|
|
|
|
title: form.title.to_string(),
|
|
|
|
content: SafeString::new(&content),
|
|
|
|
published: true,
|
|
|
|
license: form.license.to_string(),
|
|
|
|
ap_url: "".to_string()
|
|
|
|
});
|
2018-06-22 17:17:53 +02:00
|
|
|
let post = post.update_ap_url(&*conn);
|
2018-06-20 10:44:56 +02:00
|
|
|
PostAuthor::insert(&*conn, NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: user.id
|
|
|
|
});
|
2018-05-01 17:51:49 +02:00
|
|
|
|
2018-06-20 22:58:11 +02:00
|
|
|
for m in mentions.into_iter() {
|
2018-06-21 15:05:35 +02:00
|
|
|
Mention::from_activity(&*conn, Mention::build_activity(&*conn, m), post.id, true);
|
2018-06-20 22:58:11 +02:00
|
|
|
}
|
|
|
|
|
2018-06-20 10:44:56 +02:00
|
|
|
let act = post.create_activity(&*conn);
|
2018-06-21 17:31:42 +02:00
|
|
|
broadcast(&user, act, user.get_followers(&*conn));
|
2018-05-01 17:51:49 +02:00
|
|
|
|
2018-06-20 10:44:56 +02:00
|
|
|
Redirect::to(uri!(details: blog = blog_name, slug = slug))
|
|
|
|
}
|
2018-06-19 21:16:18 +02:00
|
|
|
}
|
2018-04-23 16:25:39 +02:00
|
|
|
}
|