Plume/src/routes/posts.rs

63 lines
1.8 KiB
Rust
Raw Normal View History

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;
use std::collections::HashMap;
2018-05-02 20:44:03 +00:00
use activity_pub::activity::Create;
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-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
#[get("/~/<blog>/<slug>", rank = 3)]
fn details(blog: String, slug: String, conn: DbConn) -> String {
let blog = Blog::find_by_actor_id(&*conn, blog).unwrap();
let post = Post::find_by_slug(&*conn, slug).unwrap();
format!("{} in {}", post.title, blog.title)
}
2018-04-24 09:21:39 +00:00
#[get("/~/<_blog>/new", rank = 1)]
fn new(_blog: String, _user: User) -> Template {
2018-04-23 14:25:39 +00:00
Template::render("posts/new", HashMap::<String, String>::new())
}
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()
}
#[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,
license: form.license.to_string()
});
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);
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())
}