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
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2018-05-04 11:09:08 +00:00
|
|
|
use activity_pub::{context, activity_pub, ActivityPub};
|
2018-05-02 20:44:03 +00:00
|
|
|
use activity_pub::activity::Create;
|
2018-05-04 11:09:08 +00:00
|
|
|
use activity_pub::object::Object;
|
2018-05-01 15:51:49 +00:00
|
|
|
use activity_pub::outbox::broadcast;
|
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-09 19:09:52 +00:00
|
|
|
fn details(blog: String, slug: String, conn: DbConn) -> Template {
|
2018-04-23 14:25:39 +00:00
|
|
|
let blog = Blog::find_by_actor_id(&*conn, blog).unwrap();
|
|
|
|
let post = Post::find_by_slug(&*conn, slug).unwrap();
|
2018-05-10 09:44:57 +00:00
|
|
|
let comments = Comment::for_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!({
|
|
|
|
"content": c.content,
|
|
|
|
"author": c.get_author(&*conn)
|
|
|
|
})
|
|
|
|
}).collect::<Vec<serde_json::Value>>()
|
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 {
|
|
|
|
// TODO: posts in different blogs may have the same slug
|
|
|
|
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)]
|
|
|
|
fn new(_blog: String, _user: User) -> Template {
|
|
|
|
Template::render("posts/new", HashMap::<String, String>::new())
|
|
|
|
}
|
|
|
|
|
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-04-23 14:25:39 +00:00
|
|
|
let blog = Blog::find_by_actor_id(&*conn, blog_name.to_string()).unwrap();
|
|
|
|
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-02 20:44:03 +00:00
|
|
|
let act = Create::new(&user, &post, &*conn);
|
2018-05-03 19:11:04 +00:00
|
|
|
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())
|
|
|
|
}
|