Plume/src/routes/blogs.rs

57 lines
1.4 KiB
Rust
Raw Normal View History

2018-04-23 12:54:37 +02:00
use rocket::request::Form;
use rocket::response::Redirect;
2018-04-23 17:09:05 +02:00
use rocket_contrib::{Json, Template};
2018-04-23 12:54:37 +02:00
use std::collections::HashMap;
use utils;
use db_conn::DbConn;
use models::blogs::*;
2018-04-23 15:22:07 +02:00
use models::blog_authors::*;
2018-04-23 12:54:37 +02:00
use models::instance::Instance;
2018-04-23 17:19:28 +02:00
use models::users::User;
2018-04-23 17:09:05 +02:00
use activity_pub::Actor;
2018-04-23 12:54:37 +02:00
2018-04-23 17:09:05 +02:00
#[get("/~/<name>", rank = 2)]
2018-04-23 12:54:37 +02:00
fn details(name: String) -> String {
format!("Welcome on ~{}", name)
}
2018-04-23 17:09:05 +02:00
#[get("/~/<name>", format = "application/activity+json", rank = 1)]
fn activity(name: String, conn: DbConn) -> Json {
let blog = Blog::find_by_actor_id(&*conn, name).unwrap();
Json(blog.as_activity_pub())
}
2018-04-23 12:54:37 +02:00
#[get("/blogs/new")]
fn new(_user: User) -> Template {
2018-04-23 12:54:37 +02:00
Template::render("blogs/new", HashMap::<String, i32>::new())
}
#[derive(FromForm)]
struct NewBlogForm {
pub title: String
}
#[post("/blogs/new", data = "<data>")]
2018-04-23 15:22:07 +02:00
fn create(conn: DbConn, data: Form<NewBlogForm>, user: User) -> Redirect {
2018-04-23 12:54:37 +02:00
let inst = Instance::get_local(&*conn).unwrap();
let form = data.get();
let slug = utils::make_actor_id(form.title.to_string());
2018-04-23 15:22:07 +02:00
let blog = Blog::insert(&*conn, NewBlog::new_local(
slug.to_string(),
form.title.to_string(),
String::from(""),
inst.id
2018-04-23 15:22:07 +02:00
));
blog.update_boxes(&*conn);
BlogAuthor::insert(&*conn, NewBlogAuthor {
blog_id: blog.id,
author_id: user.id,
is_owner: true
});
2018-04-23 12:54:37 +02:00
Redirect::to(format!("/~/{}", slug).as_str())
}