Plume/src/routes/blogs.rs

88 lines
2.4 KiB
Rust
Raw Normal View History

2018-06-10 11:13:07 +00:00
use activitypub::collection::OrderedCollection;
2018-05-19 07:39:59 +00:00
use rocket::{
request::Form,
2018-06-04 19:57:03 +00:00
response::{Redirect, Flash}
2018-05-19 07:39:59 +00:00
};
2018-04-24 12:31:02 +00:00
use rocket_contrib::Template;
use serde_json;
2018-04-23 10:54:37 +00:00
2018-05-19 07:39:59 +00:00
use activity_pub::{ActivityStream, ActivityPub, actor::Actor};
2018-04-23 10:54:37 +00:00
use db_conn::DbConn;
2018-05-19 07:39:59 +00:00
use models::{
blog_authors::*,
blogs::*,
instance::Instance,
posts::Post,
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 {
may_fail!(Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| {
let recents = Post::get_recents_for_blog(&*conn, &blog, 5);
Template::render("blogs/details", json!({
"blog": blog,
"account": user,
"is_author": user.map(|x| x.is_author_in(&*conn, blog)),
"recents": recents.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>()
}))
})
2018-04-23 10:54:37 +00:00
}
2018-04-23 15:09:05 +00:00
#[get("/~/<name>", format = "application/activity+json", rank = 1)]
fn activity_details(name: String, conn: DbConn) -> ActivityPub {
let blog = Blog::find_local(&*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
}
2018-06-04 19:57:03 +00:00
#[get("/blogs/new", rank = 2)]
fn new_auth() -> Flash<Redirect>{
utils::requires_login("You need to be logged in order to create a new blog", "/blogs/new")
}
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());
if Blog::find_local(&*conn, slug.clone()).is_some() {
Redirect::to(uri!(new))
} else {
let blog = Blog::insert(&*conn, NewBlog::new_local(
slug.to_string(),
form.title.to_string(),
String::from(""),
Instance::local_id(&*conn)
));
blog.update_boxes(&*conn);
2018-04-23 13:22:07 +00:00
BlogAuthor::insert(&*conn, NewBlogAuthor {
blog_id: blog.id,
author_id: user.id,
is_owner: true
});
Redirect::to(format!("/~/{}/", slug))
}
2018-04-23 10:54:37 +00:00
}
2018-04-29 17:49:56 +00:00
#[get("/~/<name>/outbox")]
2018-05-16 18:20:44 +00:00
fn outbox(name: String, conn: DbConn) -> ActivityStream<OrderedCollection> {
let blog = Blog::find_local(&*conn, name).unwrap();
2018-04-29 17:49:56 +00:00
blog.outbox(&*conn)
}