2018-06-21 17:09:18 +00:00
|
|
|
use activitypub::object::Article;
|
2018-04-24 09:21:39 +00:00
|
|
|
use heck::KebabCase;
|
2018-06-24 16:58:57 +00:00
|
|
|
use rocket::request::LenientForm;
|
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-07-07 20:51:48 +00:00
|
|
|
use std::{collections::HashMap, borrow::Cow};
|
2018-07-06 09:51:19 +00:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-04-23 14:25:39 +00:00
|
|
|
|
2018-07-11 15:30:01 +00:00
|
|
|
use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest};
|
2018-06-23 16:36:11 +00:00
|
|
|
use plume_common::utils;
|
|
|
|
use plume_models::{
|
2018-05-19 07:39:59 +00:00
|
|
|
blogs::*,
|
2018-06-23 16:36:11 +00:00
|
|
|
db_conn::DbConn,
|
2018-05-19 07:39:59 +00:00
|
|
|
comments::Comment,
|
2018-06-20 20:58:11 +00:00
|
|
|
mentions::Mention,
|
2018-05-19 07:39:59 +00:00
|
|
|
post_authors::*,
|
|
|
|
posts::*,
|
2018-06-23 16:36:11 +00:00
|
|
|
safe_string::SafeString,
|
2018-05-19 07:39:59 +00:00
|
|
|
users::User
|
|
|
|
};
|
2018-06-26 22:19:18 +00:00
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct CommentQuery {
|
|
|
|
responding_to: Option<i32>
|
|
|
|
}
|
2018-04-23 14:25:39 +00:00
|
|
|
|
2018-06-21 14:00:25 +00:00
|
|
|
// See: https://github.com/SergioBenitez/Rocket/pull/454
|
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-21 14:00:25 +00: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-21 22:50:06 +00: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 10:28:42 +00:00
|
|
|
let comments = Comment::list_by_post(&*conn, post.id);
|
2018-07-25 16:18:41 +00:00
|
|
|
let comms = comments.clone();
|
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,
|
2018-07-25 16:18:41 +00:00
|
|
|
"comments": &comments.into_iter().filter_map(|c| if c.in_response_to_id.is_none() {
|
|
|
|
Some(c.to_json(&*conn, &comms))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}).collect::<Vec<serde_json::Value>>(),
|
2018-06-18 17:28:28 +00:00
|
|
|
"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 14:00:25 +00:00
|
|
|
"date": &post.creation_date.timestamp(),
|
2018-07-25 16:18:41 +00:00
|
|
|
"previous": query.and_then(|q| q.responding_to.map(|r| Comment::get(&*conn, r).expect("Error retrieving previous comment").to_json(&*conn, &vec![]))),
|
2018-06-21 14:00:25 +00:00
|
|
|
"user_fqn": user.map(|u| u.get_fqn(&*conn)).unwrap_or(String::new())
|
2018-06-18 17:28:28 +00:00
|
|
|
}))
|
|
|
|
})
|
|
|
|
})
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 15:30:01 +00:00
|
|
|
#[get("/~/<blog>/<slug>", rank = 3)]
|
|
|
|
fn activity_details(blog: String, slug: String, conn: DbConn, _ap: ApRequest) -> ActivityStream<Article> {
|
2018-06-19 19:16:18 +00:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, blog).unwrap();
|
|
|
|
let post = Post::find_by_slug(&*conn, slug, blog.id).unwrap();
|
2018-05-04 11:09:08 +00:00
|
|
|
|
2018-06-21 17:09:18 +00:00
|
|
|
ActivityStream::new(post.into_activity(&*conn))
|
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> {
|
2018-06-19 21:20:27 +00:00
|
|
|
utils::requires_login("You need to be logged in order to write a new post", uri!(new: blog = blog))
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 19:16:18 +00:00
|
|
|
#[get("/~/<blog>/new", rank = 1)]
|
2018-06-20 08:44:56 +00: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!({
|
2018-07-06 17:29:36 +00:00
|
|
|
"account": user,
|
|
|
|
"errors": null,
|
|
|
|
"form": null
|
2018-06-20 08:44:56 +00:00
|
|
|
}))
|
|
|
|
}
|
2018-05-04 11:09:08 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 17:29:36 +00:00
|
|
|
#[derive(FromForm, Validate, Serialize)]
|
2018-04-23 14:25:39 +00:00
|
|
|
struct NewPostForm {
|
2018-07-07 20:51:48 +00:00
|
|
|
#[validate(custom(function = "valid_slug", message = "Invalid title"))]
|
2018-04-23 14:25:39 +00:00
|
|
|
pub title: String,
|
|
|
|
pub content: String,
|
|
|
|
pub license: String
|
|
|
|
}
|
|
|
|
|
2018-06-29 12:56:00 +00:00
|
|
|
fn valid_slug(title: &str) -> Result<(), ValidationError> {
|
|
|
|
let slug = title.to_string().to_kebab_case();
|
|
|
|
if slug.len() == 0 {
|
|
|
|
Err(ValidationError::new("empty_slug"))
|
2018-07-06 09:51:19 +00:00
|
|
|
} else if slug == "new" {
|
|
|
|
Err(ValidationError::new("invalid_slug"))
|
2018-06-29 12:56:00 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 14:25:39 +00:00
|
|
|
#[post("/~/<blog_name>/new", data = "<data>")]
|
2018-07-06 09:51:19 +00:00
|
|
|
fn create(blog_name: String, data: LenientForm<NewPostForm>, user: User, conn: DbConn) -> Result<Redirect, Template> {
|
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-07-06 09:51:19 +00:00
|
|
|
|
|
|
|
let mut errors = match form.validate() {
|
|
|
|
Ok(_) => ValidationErrors::new(),
|
|
|
|
Err(e) => e
|
|
|
|
};
|
2018-07-07 20:51:48 +00:00
|
|
|
if let Some(_) = Post::find_by_slug(&*conn, slug.clone(), blog.id) {
|
|
|
|
errors.add("title", ValidationError {
|
|
|
|
code: Cow::from("existing_slug"),
|
|
|
|
message: Some(Cow::from("A post with the same title already exists.")),
|
|
|
|
params: HashMap::new()
|
|
|
|
});
|
2018-07-06 09:51:19 +00:00
|
|
|
}
|
2018-05-24 10:42:45 +00:00
|
|
|
|
2018-07-06 09:51:19 +00:00
|
|
|
if errors.is_empty() {
|
|
|
|
if !user.is_author_in(&*conn, blog.clone()) {
|
|
|
|
// actually it's not "Ok"…
|
|
|
|
Ok(Redirect::to(uri!(super::blogs::details: name = blog_name)))
|
2018-06-20 08:44:56 +00:00
|
|
|
} else {
|
2018-06-20 20:58:11 +00:00
|
|
|
let (content, mentions) = utils::md_to_html(form.content.to_string().as_ref());
|
2018-05-24 10:42:45 +00:00
|
|
|
|
2018-06-20 08:44:56 +00: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 15:17:53 +00:00
|
|
|
let post = post.update_ap_url(&*conn);
|
2018-06-20 08:44:56 +00:00
|
|
|
PostAuthor::insert(&*conn, NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: user.id
|
|
|
|
});
|
2018-05-01 15:51:49 +00:00
|
|
|
|
2018-06-20 20:58:11 +00:00
|
|
|
for m in mentions.into_iter() {
|
2018-06-21 13:05:35 +00:00
|
|
|
Mention::from_activity(&*conn, Mention::build_activity(&*conn, m), post.id, true);
|
2018-06-20 20:58:11 +00:00
|
|
|
}
|
|
|
|
|
2018-06-20 08:44:56 +00:00
|
|
|
let act = post.create_activity(&*conn);
|
2018-06-21 15:31:42 +00:00
|
|
|
broadcast(&user, act, user.get_followers(&*conn));
|
2018-05-01 15:51:49 +00:00
|
|
|
|
2018-07-06 09:51:19 +00:00
|
|
|
Ok(Redirect::to(uri!(details: blog = blog_name, slug = slug)))
|
2018-06-20 08:44:56 +00:00
|
|
|
}
|
2018-07-06 09:51:19 +00:00
|
|
|
} else {
|
|
|
|
Err(Template::render("posts/new", json!({
|
|
|
|
"account": user,
|
2018-07-06 17:29:36 +00:00
|
|
|
"errors": errors.inner(),
|
|
|
|
"form": form
|
2018-07-06 09:51:19 +00:00
|
|
|
})))
|
2018-06-19 19:16:18 +00:00
|
|
|
}
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|