2018-04-24 09:21:39 +00:00
|
|
|
use heck::KebabCase;
|
2018-04-23 14:25:39 +00:00
|
|
|
use rocket::request::Form;
|
2018-04-24 09:21:39 +00:00
|
|
|
use rocket::response::Redirect;
|
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-18 22:04:30 +00:00
|
|
|
use activity_pub::{broadcast, context, activity_pub, ActivityPub};
|
2018-05-04 11:09:08 +00:00
|
|
|
use activity_pub::object::Object;
|
2018-04-23 14:25:39 +00:00
|
|
|
use db_conn::DbConn;
|
|
|
|
use models::blogs::*;
|
2018-05-10 09:44:57 +00:00
|
|
|
use models::comments::Comment;
|
2018-04-23 14:39:06 +00:00
|
|
|
use models::post_authors::*;
|
2018-04-24 09:21:39 +00:00
|
|
|
use models::posts::*;
|
2018-04-23 15:19:28 +00:00
|
|
|
use models::users::User;
|
2018-04-24 09:21:39 +00:00
|
|
|
use utils;
|
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-05-13 17:00:47 +00:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, blog).unwrap();
|
2018-04-23 14:25:39 +00:00
|
|
|
let post = Post::find_by_slug(&*conn, slug).unwrap();
|
2018-05-13 11:53:58 +00:00
|
|
|
let comments = Comment::find_by_post(&*conn, post.id);
|
2018-05-09 19:09:52 +00:00
|
|
|
Template::render("posts/details", json!({
|
|
|
|
"post": post,
|
2018-05-10 09:44:57 +00:00
|
|
|
"blog": blog,
|
|
|
|
"comments": comments.into_iter().map(|c| {
|
|
|
|
json!({
|
2018-05-10 13:58:17 +00:00
|
|
|
"id": c.id,
|
2018-05-10 09:44:57 +00:00
|
|
|
"content": c.content,
|
|
|
|
"author": c.get_author(&*conn)
|
|
|
|
})
|
2018-05-10 16:38:03 +00:00
|
|
|
}).collect::<Vec<serde_json::Value>>(),
|
2018-05-10 20:31:52 +00:00
|
|
|
"n_likes": post.get_likes(&*conn).len(),
|
2018-05-12 20:56:57 +00:00
|
|
|
"has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false),
|
2018-05-10 20:31:52 +00:00
|
|
|
"account": user
|
2018-05-09 19:09:52 +00:00
|
|
|
}))
|
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-04-24 09:21:39 +00:00
|
|
|
#[get("/~/<_blog>/new", rank = 2)]
|
|
|
|
fn new_auth(_blog: String) -> Redirect {
|
2018-04-23 14:25:39 +00:00
|
|
|
utils::requires_login()
|
|
|
|
}
|
|
|
|
|
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-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(),
|
|
|
|
content: form.content.to_string(),
|
|
|
|
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-04-23 14:25:39 +00:00
|
|
|
Redirect::to(format!("/~/{}/{}", blog_name, slug).as_str())
|
|
|
|
}
|