2018-06-21 23:07:04 +02:00
|
|
|
use activitypub::collection::OrderedCollection;
|
2018-09-01 22:08:26 +02:00
|
|
|
use atom_syndication::{Entry, FeedBuilder};
|
2018-05-19 09:39:59 +02:00
|
|
|
use rocket::{
|
2018-10-20 11:04:20 +02:00
|
|
|
http::ContentType,
|
2018-06-24 18:58:57 +02:00
|
|
|
request::LenientForm,
|
2018-10-20 11:04:20 +02:00
|
|
|
response::{Redirect, Flash, content::Content}
|
2018-05-19 09:39:59 +02:00
|
|
|
};
|
2018-04-24 14:31:02 +02:00
|
|
|
use rocket_contrib::Template;
|
2018-05-12 15:31:09 +02:00
|
|
|
use serde_json;
|
2018-07-07 22:51:48 +02:00
|
|
|
use std::{collections::HashMap, borrow::Cow};
|
2018-07-06 11:51:19 +02:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-04-23 12:54:37 +02:00
|
|
|
|
2018-07-11 17:30:01 +02:00
|
|
|
use plume_common::activity_pub::{ActivityStream, ApRequest};
|
2018-06-23 18:36:11 +02:00
|
|
|
use plume_common::utils;
|
|
|
|
use plume_models::{
|
2018-05-19 09:39:59 +02:00
|
|
|
blog_authors::*,
|
|
|
|
blogs::*,
|
2018-06-23 18:36:11 +02:00
|
|
|
db_conn::DbConn,
|
2018-05-19 09:39:59 +02:00
|
|
|
instance::Instance,
|
|
|
|
posts::Post,
|
|
|
|
users::User
|
|
|
|
};
|
2018-07-20 18:42:35 +02:00
|
|
|
use routes::Page;
|
2018-04-23 12:54:37 +02:00
|
|
|
|
2018-07-20 18:42:35 +02:00
|
|
|
#[get("/~/<name>?<page>", rank = 2)]
|
|
|
|
fn paginated_details(name: String, conn: DbConn, user: Option<User>, page: Page) -> Template {
|
2018-09-03 15:59:02 +02:00
|
|
|
may_fail!(user.map(|u| u.to_json(&*conn)), Blog::find_by_fqn(&*conn, name), "Requested blog couldn't be found", |blog| {
|
2018-07-20 18:42:35 +02:00
|
|
|
let posts = Post::blog_page(&*conn, &blog, page.limits());
|
2018-07-21 16:58:30 +02:00
|
|
|
let articles = Post::get_for_blog(&*conn, &blog);
|
2018-07-18 23:08:49 +02:00
|
|
|
let authors = &blog.list_authors(&*conn);
|
2018-06-18 19:28:28 +02:00
|
|
|
|
|
|
|
Template::render("blogs/details", json!({
|
2018-07-26 19:10:50 +02:00
|
|
|
"blog": &blog.to_json(&*conn),
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": user.clone().map(|u| u.to_json(&*conn)),
|
2018-07-18 23:08:49 +02:00
|
|
|
"is_author": user.map(|x| x.is_author_in(&*conn, blog.clone())),
|
2018-07-20 18:42:35 +02:00
|
|
|
"posts": posts.into_iter().map(|p| p.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
2018-07-18 23:08:49 +02:00
|
|
|
"authors": authors.into_iter().map(|u| u.to_json(&*conn)).collect::<Vec<serde_json::Value>>(),
|
2018-07-21 16:58:30 +02:00
|
|
|
"n_authors": authors.len(),
|
2018-07-25 14:29:34 +02:00
|
|
|
"n_articles": articles.len(),
|
|
|
|
"page": page.page,
|
|
|
|
"n_pages": Page::total(articles.len() as i32)
|
2018-06-18 19:28:28 +02:00
|
|
|
}))
|
2018-07-26 21:35:35 +02:00
|
|
|
})
|
2018-04-23 12:54:37 +02:00
|
|
|
}
|
|
|
|
|
2018-07-20 18:42:35 +02:00
|
|
|
#[get("/~/<name>", rank = 3)]
|
|
|
|
fn details(name: String, conn: DbConn, user: Option<User>) -> Template {
|
|
|
|
paginated_details(name, conn, user, Page::first())
|
|
|
|
}
|
|
|
|
|
2018-07-11 17:30:01 +02:00
|
|
|
#[get("/~/<name>", rank = 1)]
|
2018-10-20 11:04:20 +02:00
|
|
|
fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> Option<ActivityStream<CustomGroup>> {
|
|
|
|
let blog = Blog::find_local(&*conn, name)?;
|
|
|
|
Some(ActivityStream::new(blog.into_activity(&*conn)))
|
2018-04-23 17:09:05 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 12:54:37 +02:00
|
|
|
#[get("/blogs/new")]
|
2018-09-03 15:59:02 +02:00
|
|
|
fn new(user: User, conn: DbConn) -> Template {
|
2018-05-10 22:31:52 +02:00
|
|
|
Template::render("blogs/new", json!({
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": user.to_json(&*conn),
|
2018-07-06 19:29:36 +02:00
|
|
|
"errors": null,
|
|
|
|
"form": null
|
2018-05-10 22:31:52 +02:00
|
|
|
}))
|
2018-04-23 12:54:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:57:03 +02:00
|
|
|
#[get("/blogs/new", rank = 2)]
|
|
|
|
fn new_auth() -> Flash<Redirect>{
|
2018-09-08 01:11:27 +02:00
|
|
|
utils::requires_login(
|
|
|
|
"You need to be logged in order to create a new blog",
|
|
|
|
uri!(new).into()
|
|
|
|
)
|
2018-06-04 21:57:03 +02:00
|
|
|
}
|
|
|
|
|
2018-07-06 19:29:36 +02:00
|
|
|
#[derive(FromForm, Validate, Serialize)]
|
2018-04-23 12:54:37 +02:00
|
|
|
struct NewBlogForm {
|
2018-07-07 22:51:48 +02:00
|
|
|
#[validate(custom(function = "valid_slug", message = "Invalid name"))]
|
2018-04-23 12:54:37 +02:00
|
|
|
pub title: String
|
|
|
|
}
|
|
|
|
|
2018-06-29 14:56:00 +02: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 12:54:37 +02:00
|
|
|
#[post("/blogs/new", data = "<data>")]
|
2018-07-06 11:51:19 +02:00
|
|
|
fn create(conn: DbConn, data: LenientForm<NewBlogForm>, user: User) -> Result<Redirect, Template> {
|
2018-04-23 12:54:37 +02:00
|
|
|
let form = data.get();
|
|
|
|
let slug = utils::make_actor_id(form.title.to_string());
|
2018-06-19 23:20:27 +02:00
|
|
|
|
2018-07-06 11:51:19 +02:00
|
|
|
let mut errors = match form.validate() {
|
|
|
|
Ok(_) => ValidationErrors::new(),
|
|
|
|
Err(e) => e
|
|
|
|
};
|
2018-07-07 22:51:48 +02:00
|
|
|
if let Some(_) = Blog::find_local(&*conn, slug.clone()) {
|
|
|
|
errors.add("title", ValidationError {
|
|
|
|
code: Cow::from("existing_slug"),
|
|
|
|
message: Some(Cow::from("A blog with the same name already exists.")),
|
|
|
|
params: HashMap::new()
|
|
|
|
});
|
2018-07-06 11:51:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if errors.is_empty() {
|
2018-06-19 20:40:20 +02:00
|
|
|
let blog = Blog::insert(&*conn, NewBlog::new_local(
|
2018-07-06 11:51:19 +02:00
|
|
|
slug.clone(),
|
2018-06-19 20:40:20 +02:00
|
|
|
form.title.to_string(),
|
|
|
|
String::from(""),
|
|
|
|
Instance::local_id(&*conn)
|
|
|
|
));
|
|
|
|
blog.update_boxes(&*conn);
|
2018-04-23 15:22:07 +02:00
|
|
|
|
2018-06-19 20:40:20 +02:00
|
|
|
BlogAuthor::insert(&*conn, NewBlogAuthor {
|
|
|
|
blog_id: blog.id,
|
|
|
|
author_id: user.id,
|
|
|
|
is_owner: true
|
|
|
|
});
|
2018-06-19 21:16:18 +02:00
|
|
|
|
2018-07-06 11:51:19 +02:00
|
|
|
Ok(Redirect::to(uri!(details: name = slug.clone())))
|
|
|
|
} else {
|
2018-07-07 22:51:48 +02:00
|
|
|
println!("{:?}", errors);
|
2018-07-06 11:51:19 +02:00
|
|
|
Err(Template::render("blogs/new", json!({
|
2018-09-03 15:59:02 +02:00
|
|
|
"account": user.to_json(&*conn),
|
2018-07-06 19:29:36 +02:00
|
|
|
"errors": errors.inner(),
|
|
|
|
"form": form
|
2018-07-06 11:51:19 +02:00
|
|
|
})))
|
2018-06-19 20:40:20 +02:00
|
|
|
}
|
2018-04-23 12:54:37 +02:00
|
|
|
}
|
2018-04-29 19:49:56 +02:00
|
|
|
|
2018-10-20 15:03:59 +02:00
|
|
|
#[post("/~/<name>/delete")]
|
|
|
|
fn delete(conn: DbConn, name: String, user: Option<User>) -> Result<Redirect, Option<Template>>{
|
|
|
|
let blog = Blog::find_local(&*conn, name).ok_or(None)?;
|
|
|
|
if user.map(|u| u.is_author_in(&*conn, blog.clone())).unwrap_or(false) {
|
|
|
|
blog.delete(&conn);
|
|
|
|
Ok(Redirect::to(uri!(super::instance::index)))
|
|
|
|
} else {
|
|
|
|
Err(Some(Template::render("errors/403", json!({// TODO actually return 403 error code
|
|
|
|
"error_message": "You are not allowed to delete this blog."
|
|
|
|
}))))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:49:56 +02:00
|
|
|
#[get("/~/<name>/outbox")]
|
2018-10-20 11:04:20 +02:00
|
|
|
fn outbox(name: String, conn: DbConn) -> Option<ActivityStream<OrderedCollection>> {
|
|
|
|
let blog = Blog::find_local(&*conn, name)?;
|
|
|
|
Some(blog.outbox(&*conn))
|
2018-04-29 19:49:56 +02:00
|
|
|
}
|
2018-09-01 22:08:26 +02:00
|
|
|
|
|
|
|
#[get("/~/<name>/atom.xml")]
|
2018-10-20 11:04:20 +02:00
|
|
|
fn atom_feed(name: String, conn: DbConn) -> Option<Content<String>> {
|
|
|
|
let blog = Blog::find_by_fqn(&*conn, name.clone())?;
|
2018-09-01 22:08:26 +02:00
|
|
|
let feed = FeedBuilder::default()
|
|
|
|
.title(blog.title.clone())
|
2018-10-20 11:04:20 +02:00
|
|
|
.id(Instance::get_local(&*conn).expect("blogs::atom_feed: local instance not found error")
|
|
|
|
.compute_box("~", name, "atom.xml"))
|
2018-09-01 22:08:26 +02:00
|
|
|
.entries(Post::get_recents_for_blog(&*conn, &blog, 15)
|
|
|
|
.into_iter()
|
|
|
|
.map(|p| super::post_to_atom(p, &*conn))
|
|
|
|
.collect::<Vec<Entry>>())
|
|
|
|
.build()
|
2018-10-20 11:04:20 +02:00
|
|
|
.expect("blogs::atom_feed: feed creation error");
|
|
|
|
Some(Content(ContentType::new("application", "atom+xml"), feed.to_string()))
|
2018-09-01 22:08:26 +02:00
|
|
|
}
|