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-12-06 18:54:16 +01:00
|
|
|
use rocket_i18n::I18n;
|
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-12-29 09:36:07 +01:00
|
|
|
use routes::{Page, errors::ErrorPage};
|
2018-12-06 18:54:16 +01:00
|
|
|
use template_utils::Ructe;
|
2018-12-02 17:37:51 +01:00
|
|
|
use Searcher;
|
2018-04-23 12:54:37 +02:00
|
|
|
|
2018-07-20 18:42:35 +02:00
|
|
|
#[get("/~/<name>?<page>", rank = 2)]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn details(intl: I18n, name: String, conn: DbConn, user: Option<User>, page: Option<Page>) -> Result<Ructe, ErrorPage> {
|
2018-12-13 22:20:19 +01:00
|
|
|
let page = page.unwrap_or_default();
|
2018-12-29 09:36:07 +01:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, &name)?;
|
|
|
|
let posts = Post::blog_page(&*conn, &blog, page.limits())?;
|
|
|
|
let articles_count = Post::count_for_blog(&*conn, &blog)?;
|
|
|
|
let authors = &blog.list_authors(&*conn)?;
|
2018-12-06 18:54:16 +01:00
|
|
|
|
|
|
|
Ok(render!(blogs::details(
|
|
|
|
&(&*conn, &intl.catalog, user.clone()),
|
|
|
|
blog.clone(),
|
|
|
|
authors,
|
2018-12-14 23:16:18 +01:00
|
|
|
articles_count,
|
2018-12-06 18:54:16 +01:00
|
|
|
page.0,
|
2018-12-14 23:16:18 +01:00
|
|
|
Page::total(articles_count as i32),
|
2018-12-29 09:36:07 +01:00
|
|
|
user.and_then(|x| x.is_author_in(&*conn, &blog).ok()).unwrap_or(false),
|
2018-12-06 18:54:16 +01:00
|
|
|
posts
|
|
|
|
)))
|
2018-04-23 12:54:37 +02:00
|
|
|
}
|
|
|
|
|
2018-07-11 17:30:01 +02:00
|
|
|
#[get("/~/<name>", rank = 1)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn activity_details(name: String, conn: DbConn, _ap: ApRequest) -> Option<ActivityStream<CustomGroup>> {
|
2019-03-06 18:28:10 +01:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, &name).ok()?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Some(ActivityStream::new(blog.to_activity(&*conn).ok()?))
|
2018-04-23 17:09:05 +02:00
|
|
|
}
|
|
|
|
|
2018-04-23 12:54:37 +02:00
|
|
|
#[get("/blogs/new")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn new(user: User, conn: DbConn, intl: I18n) -> Ructe {
|
|
|
|
render!(blogs::new(
|
|
|
|
&(&*conn, &intl.catalog, Some(user)),
|
|
|
|
&NewBlogForm::default(),
|
|
|
|
ValidationErrors::default()
|
|
|
|
))
|
2018-04-23 12:54:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-04 21:57:03 +02:00
|
|
|
#[get("/blogs/new", rank = 2)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn new_auth(i18n: I18n) -> Flash<Redirect>{
|
2018-09-08 01:11:27 +02:00
|
|
|
utils::requires_login(
|
2019-02-02 15:23:50 +01:00
|
|
|
&i18n!(i18n.catalog, "You need to be logged in order to create a new blog"),
|
2018-11-26 10:21:52 +01:00
|
|
|
uri!(new)
|
2018-09-08 01:11:27 +02:00
|
|
|
)
|
2018-06-04 21:57:03 +02:00
|
|
|
}
|
|
|
|
|
2019-03-12 19:40:54 +01:00
|
|
|
#[derive(Default, FromForm, Validate)]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub struct NewBlogForm {
|
2018-07-07 22:51:48 +02:00
|
|
|
#[validate(custom(function = "valid_slug", message = "Invalid name"))]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub title: String,
|
2018-04-23 12:54:37 +02:00
|
|
|
}
|
|
|
|
|
2018-06-29 14:56:00 +02:00
|
|
|
fn valid_slug(title: &str) -> Result<(), ValidationError> {
|
2018-11-26 10:21:52 +01:00
|
|
|
let slug = utils::make_actor_id(title);
|
|
|
|
if slug.is_empty() {
|
2018-06-29 14:56:00 +02:00
|
|
|
Err(ValidationError::new("empty_slug"))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 18:54:16 +01:00
|
|
|
#[post("/blogs/new", data = "<form>")]
|
|
|
|
pub fn create(conn: DbConn, form: LenientForm<NewBlogForm>, user: User, intl: I18n) -> Result<Redirect, Ructe> {
|
2018-11-26 10:21:52 +01:00
|
|
|
let slug = utils::make_actor_id(&form.title);
|
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
|
|
|
|
};
|
2019-03-06 18:28:10 +01:00
|
|
|
if Blog::find_by_fqn(&*conn, &slug).is_ok() {
|
2018-07-07 22:51:48 +02:00
|
|
|
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(""),
|
2018-12-29 09:36:07 +01:00
|
|
|
Instance::get_local(&*conn).expect("blog::create: instance error").id
|
|
|
|
).expect("blog::create: new local error")).expect("blog::create: error");
|
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-12-29 09:36:07 +01:00
|
|
|
}).expect("blog::create: author error");
|
2018-06-19 21:16:18 +02:00
|
|
|
|
2018-12-13 22:20:19 +01:00
|
|
|
Ok(Redirect::to(uri!(details: name = slug.clone(), page = _)))
|
2018-07-06 11:51:19 +02:00
|
|
|
} else {
|
2018-12-29 09:36:07 +01:00
|
|
|
Err(render!(blogs::new(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, Some(user)),
|
|
|
|
&*form,
|
|
|
|
errors
|
|
|
|
)))
|
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")]
|
2018-12-29 09:36:07 +01:00
|
|
|
pub fn delete(conn: DbConn, name: String, user: Option<User>, intl: I18n, searcher: Searcher) -> Result<Redirect, Ructe>{
|
2019-03-06 18:28:10 +01:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, &name).expect("blog::delete: blog not found");
|
2018-12-29 09:36:07 +01:00
|
|
|
if user.clone().and_then(|u| u.is_author_in(&*conn, &blog).ok()).unwrap_or(false) {
|
|
|
|
blog.delete(&conn, &searcher).expect("blog::expect: deletion error");
|
2018-10-20 15:03:59 +02:00
|
|
|
Ok(Redirect::to(uri!(super::instance::index)))
|
|
|
|
} else {
|
2018-12-06 18:54:16 +01:00
|
|
|
// TODO actually return 403 error code
|
2018-12-29 09:36:07 +01:00
|
|
|
Err(render!(errors::not_authorized(
|
2018-12-06 18:54:16 +01:00
|
|
|
&(&*conn, &intl.catalog, user),
|
2019-02-02 15:23:50 +01:00
|
|
|
i18n!(intl.catalog, "You are not allowed to delete this blog.")
|
2018-12-29 09:36:07 +01:00
|
|
|
)))
|
2018-10-20 15:03:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-29 19:49:56 +02:00
|
|
|
#[get("/~/<name>/outbox")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn outbox(name: String, conn: DbConn) -> Option<ActivityStream<OrderedCollection>> {
|
2019-03-06 18:28:10 +01:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, &name).ok()?;
|
2018-12-29 09:36:07 +01:00
|
|
|
Some(blog.outbox(&*conn).ok()?)
|
2018-04-29 19:49:56 +02:00
|
|
|
}
|
2018-09-01 22:08:26 +02:00
|
|
|
|
|
|
|
#[get("/~/<name>/atom.xml")]
|
2018-12-06 18:54:16 +01:00
|
|
|
pub fn atom_feed(name: String, conn: DbConn) -> Option<Content<String>> {
|
2018-12-29 09:36:07 +01:00
|
|
|
let blog = Blog::find_by_fqn(&*conn, &name).ok()?;
|
2018-09-01 22:08:26 +02:00
|
|
|
let feed = FeedBuilder::default()
|
|
|
|
.title(blog.title.clone())
|
2018-12-29 09:36:07 +01:00
|
|
|
.id(Instance::get_local(&*conn).ok()?
|
2018-11-26 10:21:52 +01:00
|
|
|
.compute_box("~", &name, "atom.xml"))
|
2018-12-29 09:36:07 +01:00
|
|
|
.entries(Post::get_recents_for_blog(&*conn, &blog, 15).ok()?
|
2018-09-01 22:08:26 +02:00
|
|
|
.into_iter()
|
|
|
|
.map(|p| super::post_to_atom(p, &*conn))
|
|
|
|
.collect::<Vec<Entry>>())
|
2018-12-29 09:36:07 +01:00
|
|
|
.build().ok()?;
|
2018-10-20 11:04:20 +02:00
|
|
|
Some(Content(ContentType::new("application", "atom+xml"), feed.to_string()))
|
2018-09-01 22:08:26 +02:00
|
|
|
}
|