2018-06-21 21:07:04 +00:00
|
|
|
use activitypub::collection::OrderedCollection;
|
2018-05-19 07:39:59 +00:00
|
|
|
use rocket::{
|
2018-06-24 16:58:57 +00:00
|
|
|
request::LenientForm,
|
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;
|
2018-05-12 13:31:09 +00:00
|
|
|
use serde_json;
|
2018-07-06 09:51:19 +00:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-04-23 10:54:37 +00:00
|
|
|
|
2018-06-23 16:36:11 +00:00
|
|
|
use plume_common::activity_pub::ActivityStream;
|
|
|
|
use plume_common::utils;
|
|
|
|
use plume_models::{
|
2018-05-19 07:39:59 +00:00
|
|
|
blog_authors::*,
|
|
|
|
blogs::*,
|
2018-06-23 16:36:11 +00:00
|
|
|
db_conn::DbConn,
|
2018-05-19 07:39:59 +00:00
|
|
|
instance::Instance,
|
|
|
|
posts::Post,
|
|
|
|
users::User
|
|
|
|
};
|
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-06-21 22:50:06 +00:00
|
|
|
may_fail!(user, Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| {
|
2018-06-18 17:28:28 +00:00
|
|
|
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)]
|
2018-06-21 21:07:04 +00:00
|
|
|
fn activity_details(name: String, conn: DbConn) -> ActivityStream<CustomGroup> {
|
2018-05-13 17:00:47 +00:00
|
|
|
let blog = Blog::find_local(&*conn, name).unwrap();
|
2018-06-21 17:09:18 +00:00
|
|
|
ActivityStream::new(blog.into_activity(&*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>{
|
2018-06-19 21:20:27 +00:00
|
|
|
utils::requires_login("You need to be logged in order to create a new blog", uri!(new))
|
2018-06-04 19:57:03 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 12:56:00 +00:00
|
|
|
#[derive(FromForm, Validate)]
|
2018-04-23 10:54:37 +00:00
|
|
|
struct NewBlogForm {
|
2018-06-29 12:56:00 +00:00
|
|
|
#[validate(custom = "valid_slug")]
|
2018-04-23 10:54:37 +00:00
|
|
|
pub title: String
|
|
|
|
}
|
|
|
|
|
2018-06-29 12:56:00 +00:00
|
|
|
fn valid_slug(title: &str) -> Result<(), ValidationError> {
|
|
|
|
let slug = utils::make_actor_id(title.to_string());
|
|
|
|
if slug.len() == 0 {
|
|
|
|
Err(ValidationError::new("empty_slug"))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 10:54:37 +00:00
|
|
|
#[post("/blogs/new", data = "<data>")]
|
2018-07-06 09:51:19 +00:00
|
|
|
fn create(conn: DbConn, data: LenientForm<NewBlogForm>, user: User) -> Result<Redirect, Template> {
|
2018-04-23 10:54:37 +00:00
|
|
|
let form = data.get();
|
|
|
|
let slug = utils::make_actor_id(form.title.to_string());
|
2018-07-06 09:51:19 +00:00
|
|
|
let slug_taken_err = Blog::find_local(&*conn, slug.clone()).ok_or(ValidationError::new("existing_slug"));
|
2018-06-19 21:20:27 +00:00
|
|
|
|
2018-07-06 09:51:19 +00:00
|
|
|
let mut errors = match form.validate() {
|
|
|
|
Ok(_) => ValidationErrors::new(),
|
|
|
|
Err(e) => e
|
|
|
|
};
|
|
|
|
if let Err(e) = slug_taken_err {
|
|
|
|
errors.add("title", e)
|
|
|
|
}
|
|
|
|
|
|
|
|
if errors.is_empty() {
|
2018-06-19 18:40:20 +00:00
|
|
|
let blog = Blog::insert(&*conn, NewBlog::new_local(
|
2018-07-06 09:51:19 +00:00
|
|
|
slug.clone(),
|
2018-06-19 18:40:20 +00:00
|
|
|
form.title.to_string(),
|
|
|
|
String::from(""),
|
|
|
|
Instance::local_id(&*conn)
|
|
|
|
));
|
|
|
|
blog.update_boxes(&*conn);
|
2018-04-23 13:22:07 +00:00
|
|
|
|
2018-06-19 18:40:20 +00:00
|
|
|
BlogAuthor::insert(&*conn, NewBlogAuthor {
|
|
|
|
blog_id: blog.id,
|
|
|
|
author_id: user.id,
|
|
|
|
is_owner: true
|
|
|
|
});
|
2018-06-19 19:16:18 +00:00
|
|
|
|
2018-07-06 09:51:19 +00:00
|
|
|
Ok(Redirect::to(uri!(details: name = slug.clone())))
|
|
|
|
} else {
|
|
|
|
Err(Template::render("blogs/new", json!({
|
|
|
|
"account": user,
|
|
|
|
"errors": errors.inner()
|
|
|
|
})))
|
2018-06-19 18:40:20 +00:00
|
|
|
}
|
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> {
|
2018-05-13 17:00:47 +00:00
|
|
|
let blog = Blog::find_local(&*conn, name).unwrap();
|
2018-04-29 17:49:56 +00:00
|
|
|
blog.outbox(&*conn)
|
|
|
|
}
|