2018-04-23 10:54:37 +00:00
|
|
|
use rocket::request::Form;
|
|
|
|
use rocket::response::Redirect;
|
2018-04-24 12:31:02 +00:00
|
|
|
use rocket_contrib::Template;
|
2018-05-12 13:31:09 +00:00
|
|
|
use serde_json;
|
2018-04-23 10:54:37 +00:00
|
|
|
|
2018-04-24 14:52:47 +00:00
|
|
|
use activity_pub::ActivityPub;
|
2018-04-24 09:21:39 +00:00
|
|
|
use activity_pub::actor::Actor;
|
2018-04-29 17:49:56 +00:00
|
|
|
use activity_pub::outbox::Outbox;
|
2018-04-23 10:54:37 +00:00
|
|
|
use db_conn::DbConn;
|
2018-04-23 13:22:07 +00:00
|
|
|
use models::blog_authors::*;
|
2018-04-24 09:21:39 +00:00
|
|
|
use models::blogs::*;
|
2018-04-23 10:54:37 +00:00
|
|
|
use models::instance::Instance;
|
2018-05-12 13:31:09 +00:00
|
|
|
use models::posts::Post;
|
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 10:54:37 +00:00
|
|
|
|
2018-04-23 15:09:05 +00:00
|
|
|
#[get("/~/<name>", rank = 2)]
|
2018-05-10 20:31:52 +00:00
|
|
|
fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
|
2018-05-12 13:31:09 +00:00
|
|
|
let blog = Blog::find_by_actor_id(&*conn, name).unwrap();
|
|
|
|
let recents = Post::get_recents_for_blog(&*conn, &blog, 5);
|
2018-05-09 19:09:52 +00:00
|
|
|
Template::render("blogs/details", json!({
|
2018-05-10 20:31:52 +00:00
|
|
|
"blog": blog,
|
2018-05-12 13:31:09 +00:00
|
|
|
"account": user,
|
|
|
|
"recents": recents.into_iter().map(|p| {
|
|
|
|
json!({
|
|
|
|
"post": p,
|
|
|
|
"author": p.get_authors(&*conn)[0],
|
2018-05-12 17:59:38 +00:00
|
|
|
"url": format!("/~/{}/{}/", p.get_blog(&*conn).actor_id, p.slug),
|
2018-05-12 13:31:09 +00:00
|
|
|
"date": p.creation_date.timestamp()
|
|
|
|
})
|
|
|
|
}).collect::<Vec<serde_json::Value>>()
|
2018-05-09 19:09:52 +00:00
|
|
|
}))
|
2018-04-23 10:54:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 15:09:05 +00:00
|
|
|
#[get("/~/<name>", format = "application/activity+json", rank = 1)]
|
2018-04-24 14:52:47 +00:00
|
|
|
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
|
2018-04-23 15:09:05 +00:00
|
|
|
let blog = Blog::find_by_actor_id(&*conn, name).unwrap();
|
2018-04-24 12:31:02 +00:00
|
|
|
blog.as_activity_pub(&*conn)
|
2018-04-23 15:09:05 +00:00
|
|
|
}
|
|
|
|
|
2018-04-23 10:54:37 +00:00
|
|
|
#[get("/blogs/new")]
|
2018-05-10 20:31:52 +00:00
|
|
|
fn new(user: User) -> Template {
|
|
|
|
Template::render("blogs/new", json!({
|
|
|
|
"account": user
|
|
|
|
}))
|
2018-04-23 10:54:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct NewBlogForm {
|
|
|
|
pub title: String
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/blogs/new", data = "<data>")]
|
2018-04-23 13:22:07 +00:00
|
|
|
fn create(conn: DbConn, data: Form<NewBlogForm>, user: User) -> Redirect {
|
2018-04-23 10:54:37 +00:00
|
|
|
let form = data.get();
|
|
|
|
let slug = utils::make_actor_id(form.title.to_string());
|
|
|
|
|
2018-04-23 13:22:07 +00:00
|
|
|
let blog = Blog::insert(&*conn, NewBlog::new_local(
|
2018-04-23 13:12:59 +00:00
|
|
|
slug.to_string(),
|
|
|
|
form.title.to_string(),
|
|
|
|
String::from(""),
|
2018-05-02 11:53:42 +00:00
|
|
|
Instance::local_id(&*conn)
|
2018-04-23 13:22:07 +00:00
|
|
|
));
|
|
|
|
blog.update_boxes(&*conn);
|
|
|
|
|
|
|
|
BlogAuthor::insert(&*conn, NewBlogAuthor {
|
|
|
|
blog_id: blog.id,
|
|
|
|
author_id: user.id,
|
|
|
|
is_owner: true
|
|
|
|
});
|
2018-04-23 10:54:37 +00:00
|
|
|
|
|
|
|
Redirect::to(format!("/~/{}", slug).as_str())
|
|
|
|
}
|
2018-04-29 17:49:56 +00:00
|
|
|
|
|
|
|
#[get("/~/<name>/outbox")]
|
2018-05-02 21:36:13 +00:00
|
|
|
fn outbox(name: String, conn: DbConn) -> Outbox {
|
2018-04-29 17:49:56 +00:00
|
|
|
let blog = Blog::find_by_actor_id(&*conn, name).unwrap();
|
|
|
|
blog.outbox(&*conn)
|
|
|
|
}
|