2018-06-21 17:09:18 +00:00
|
|
|
use activitypub::object::Article;
|
2018-09-10 18:38:19 +00:00
|
|
|
use chrono::Utc;
|
2018-09-05 20:18:27 +00:00
|
|
|
use heck::{CamelCase, KebabCase};
|
2018-07-26 15:38:22 +00:00
|
|
|
use rocket::{State, request::LenientForm};
|
2018-06-04 19:57:03 +00:00
|
|
|
use rocket::response::{Redirect, Flash};
|
2018-04-23 14:25:39 +00:00
|
|
|
use rocket_contrib::Template;
|
2018-05-10 09:44:57 +00:00
|
|
|
use serde_json;
|
2018-07-07 20:51:48 +00:00
|
|
|
use std::{collections::HashMap, borrow::Cow};
|
2018-07-06 09:51:19 +00:00
|
|
|
use validator::{Validate, ValidationError, ValidationErrors};
|
2018-07-26 15:38:22 +00:00
|
|
|
use workerpool::{Pool, thunk::*};
|
2018-04-23 14:25:39 +00:00
|
|
|
|
2018-09-01 15:28:47 +00:00
|
|
|
use plume_common::activity_pub::{broadcast, ActivityStream, ApRequest, inbox::Deletable};
|
2018-06-23 16:36:11 +00:00
|
|
|
use plume_common::utils;
|
|
|
|
use plume_models::{
|
2018-05-19 07:39:59 +00:00
|
|
|
blogs::*,
|
2018-06-23 16:36:11 +00:00
|
|
|
db_conn::DbConn,
|
2018-05-19 07:39:59 +00:00
|
|
|
comments::Comment,
|
2018-07-27 18:31:47 +00:00
|
|
|
instance::Instance,
|
2018-06-20 20:58:11 +00:00
|
|
|
mentions::Mention,
|
2018-05-19 07:39:59 +00:00
|
|
|
post_authors::*,
|
|
|
|
posts::*,
|
2018-06-23 16:36:11 +00:00
|
|
|
safe_string::SafeString,
|
2018-09-05 20:18:27 +00:00
|
|
|
tags::*,
|
2018-05-19 07:39:59 +00:00
|
|
|
users::User
|
|
|
|
};
|
2018-06-26 22:19:18 +00:00
|
|
|
|
|
|
|
#[derive(FromForm)]
|
|
|
|
struct CommentQuery {
|
|
|
|
responding_to: Option<i32>
|
|
|
|
}
|
2018-04-23 14:25:39 +00:00
|
|
|
|
2018-06-21 14:00:25 +00:00
|
|
|
// See: https://github.com/SergioBenitez/Rocket/pull/454
|
2018-05-04 11:09:08 +00:00
|
|
|
#[get("/~/<blog>/<slug>", rank = 4)]
|
2018-05-10 20:31:52 +00:00
|
|
|
fn details(blog: String, slug: String, conn: DbConn, user: Option<User>) -> Template {
|
2018-06-21 14:00:25 +00:00
|
|
|
details_response(blog, slug, conn, user, None)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[get("/~/<blog>/<slug>?<query>")]
|
|
|
|
fn details_response(blog: String, slug: String, conn: DbConn, user: Option<User>, query: Option<CommentQuery>) -> Template {
|
2018-09-03 13:59:02 +00:00
|
|
|
may_fail!(user.map(|u| u.to_json(&*conn)), Blog::find_by_fqn(&*conn, blog), "Couldn't find this blog", |blog| {
|
|
|
|
may_fail!(user.map(|u| u.to_json(&*conn)), Post::find_by_slug(&*conn, slug, blog.id), "Couldn't find this post", |post| {
|
2018-09-12 15:58:38 +00:00
|
|
|
if post.published || post.get_authors(&*conn).into_iter().any(|a| a.id == user.clone().map(|u| u.id).unwrap_or(0)) {
|
|
|
|
let comments = Comment::list_by_post(&*conn, post.id);
|
|
|
|
let comms = comments.clone();
|
|
|
|
|
|
|
|
Template::render("posts/details", json!({
|
|
|
|
"author": post.get_authors(&*conn)[0].to_json(&*conn),
|
|
|
|
"article": post.to_json(&*conn),
|
|
|
|
"blog": blog.to_json(&*conn),
|
|
|
|
"comments": &comments.into_iter().filter_map(|c| if c.in_response_to_id.is_none() {
|
|
|
|
Some(c.to_json(&*conn, &comms))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}).collect::<Vec<serde_json::Value>>(),
|
|
|
|
"n_likes": post.get_likes(&*conn).len(),
|
|
|
|
"has_liked": user.clone().map(|u| u.has_liked(&*conn, &post)).unwrap_or(false),
|
|
|
|
"n_reshares": post.get_reshares(&*conn).len(),
|
|
|
|
"has_reshared": user.clone().map(|u| u.has_reshared(&*conn, &post)).unwrap_or(false),
|
|
|
|
"account": &user.clone().map(|u| u.to_json(&*conn)),
|
|
|
|
"date": &post.creation_date.timestamp(),
|
2018-10-20 09:04:20 +00:00
|
|
|
"previous": query.and_then(|q| q.responding_to.map(|r| Comment::get(&*conn, r)
|
|
|
|
.expect("posts::details_reponse: Error retrieving previous comment").to_json(&*conn, &vec![]))),
|
2018-09-12 15:58:38 +00:00
|
|
|
"user_fqn": user.clone().map(|u| u.get_fqn(&*conn)).unwrap_or(String::new()),
|
|
|
|
"is_author": user.clone().map(|u| post.get_authors(&*conn).into_iter().any(|a| u.id == a.id)).unwrap_or(false),
|
|
|
|
"is_following": user.map(|u| u.is_following(&*conn, post.get_authors(&*conn)[0].id)).unwrap_or(false)
|
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
Template::render("errors/403", json!({
|
|
|
|
"error_message": "This post isn't published yet."
|
|
|
|
}))
|
|
|
|
}
|
2018-06-18 17:28:28 +00:00
|
|
|
})
|
|
|
|
})
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 15:30:01 +00:00
|
|
|
#[get("/~/<blog>/<slug>", rank = 3)]
|
2018-10-20 09:04:20 +00:00
|
|
|
fn activity_details(blog: String, slug: String, conn: DbConn, _ap: ApRequest) -> Result<ActivityStream<Article>, Option<String>> {
|
|
|
|
let blog = Blog::find_by_fqn(&*conn, blog).ok_or(None)?;
|
|
|
|
let post = Post::find_by_slug(&*conn, slug, blog.id).ok_or(None)?;
|
2018-09-12 15:58:38 +00:00
|
|
|
if post.published {
|
|
|
|
Ok(ActivityStream::new(post.into_activity(&*conn)))
|
|
|
|
} else {
|
2018-10-20 09:04:20 +00:00
|
|
|
Err(Some(String::from("Not published yet.")))
|
2018-09-12 15:58:38 +00:00
|
|
|
}
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-04 19:57:03 +00:00
|
|
|
#[get("/~/<blog>/new", rank = 2)]
|
|
|
|
fn new_auth(blog: String) -> Flash<Redirect> {
|
2018-09-07 23:11:27 +00:00
|
|
|
utils::requires_login(
|
|
|
|
"You need to be logged in order to write a new post",
|
|
|
|
uri!(new: blog = blog).into()
|
|
|
|
)
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 19:16:18 +00:00
|
|
|
#[get("/~/<blog>/new", rank = 1)]
|
2018-10-20 09:04:20 +00:00
|
|
|
fn new(blog: String, user: User, conn: DbConn) -> Option<Template> {
|
|
|
|
let b = Blog::find_by_fqn(&*conn, blog.to_string())?;
|
2018-06-20 08:44:56 +00:00
|
|
|
|
|
|
|
if !user.is_author_in(&*conn, b.clone()) {
|
2018-10-20 09:04:20 +00:00
|
|
|
Some(Template::render("errors/403", json!({// TODO actually return 403 error code
|
2018-06-20 08:44:56 +00:00
|
|
|
"error_message": "You are not author in this blog."
|
2018-10-20 09:04:20 +00:00
|
|
|
})))
|
2018-06-20 08:44:56 +00:00
|
|
|
} else {
|
2018-10-20 09:04:20 +00:00
|
|
|
Some(Template::render("posts/new", json!({
|
2018-09-03 13:59:02 +00:00
|
|
|
"account": user.to_json(&*conn),
|
2018-07-27 18:31:47 +00:00
|
|
|
"instance": Instance::get_local(&*conn),
|
2018-09-06 21:39:22 +00:00
|
|
|
"editing": false,
|
2018-07-06 17:29:36 +00:00
|
|
|
"errors": null,
|
2018-09-10 18:38:19 +00:00
|
|
|
"form": null,
|
|
|
|
"is_draft": true,
|
2018-10-20 09:04:20 +00:00
|
|
|
})))
|
2018-06-20 08:44:56 +00:00
|
|
|
}
|
2018-05-04 11:09:08 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 21:39:22 +00:00
|
|
|
#[get("/~/<blog>/<slug>/edit")]
|
2018-10-20 09:04:20 +00:00
|
|
|
fn edit(blog: String, slug: String, user: User, conn: DbConn) -> Option<Template> {
|
|
|
|
let b = Blog::find_by_fqn(&*conn, blog.to_string())?;
|
|
|
|
let post = Post::find_by_slug(&*conn, slug, b.id)?;
|
2018-09-06 21:39:22 +00:00
|
|
|
|
2018-10-20 09:04:20 +00:00
|
|
|
if !user.is_author_in(&*conn, b) {
|
|
|
|
Some(Template::render("errors/403", json!({// TODO actually return 403 error code
|
2018-09-06 21:39:22 +00:00
|
|
|
"error_message": "You are not author in this blog."
|
2018-10-20 09:04:20 +00:00
|
|
|
})))
|
2018-09-06 21:39:22 +00:00
|
|
|
} else {
|
2018-10-20 09:04:20 +00:00
|
|
|
let source = if post.source.len() > 0 {
|
|
|
|
post.source
|
2018-09-08 11:05:22 +00:00
|
|
|
} else {
|
2018-10-20 09:04:20 +00:00
|
|
|
post.content.get().clone() // fallback to HTML if the markdown was not stored
|
2018-09-08 11:05:22 +00:00
|
|
|
};
|
|
|
|
|
2018-10-20 09:04:20 +00:00
|
|
|
Some(Template::render("posts/new", json!({
|
2018-09-06 21:39:22 +00:00
|
|
|
"account": user.to_json(&*conn),
|
|
|
|
"instance": Instance::get_local(&*conn),
|
|
|
|
"editing": true,
|
|
|
|
"errors": null,
|
|
|
|
"form": NewPostForm {
|
|
|
|
title: post.title.clone(),
|
|
|
|
subtitle: post.subtitle.clone(),
|
2018-09-08 11:05:22 +00:00
|
|
|
content: source,
|
2018-09-06 21:39:22 +00:00
|
|
|
tags: Tag::for_post(&*conn, post.id)
|
|
|
|
.into_iter()
|
2018-10-20 17:27:49 +00:00
|
|
|
.filter_map(|t| if !t.is_hashtag {Some(t.tag)} else {None})
|
2018-09-06 21:39:22 +00:00
|
|
|
.collect::<Vec<String>>()
|
|
|
|
.join(", "),
|
|
|
|
license: post.license.clone(),
|
2018-09-10 18:38:19 +00:00
|
|
|
draft: true,
|
|
|
|
},
|
|
|
|
"is_draft": !post.published
|
2018-10-20 09:04:20 +00:00
|
|
|
})))
|
2018-09-06 21:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/~/<blog>/<slug>/edit", data = "<data>")]
|
2018-10-20 09:04:20 +00:00
|
|
|
fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientForm<NewPostForm>, worker: State<Pool<ThunkWorker<()>>>)
|
|
|
|
-> Result<Redirect, Option<Template>> {
|
|
|
|
let b = Blog::find_by_fqn(&*conn, blog.to_string()).ok_or(None)?;
|
|
|
|
let mut post = Post::find_by_slug(&*conn, slug.clone(), b.id).ok_or(None)?;
|
2018-09-06 21:39:22 +00:00
|
|
|
|
|
|
|
let form = data.get();
|
2018-10-11 12:23:23 +00:00
|
|
|
let new_slug = if !post.published {
|
|
|
|
form.title.to_string().to_kebab_case()
|
|
|
|
} else {
|
|
|
|
post.slug
|
|
|
|
};
|
2018-09-06 21:39:22 +00:00
|
|
|
|
|
|
|
let mut errors = match form.validate() {
|
|
|
|
Ok(_) => ValidationErrors::new(),
|
|
|
|
Err(e) => e
|
|
|
|
};
|
2018-09-07 17:51:53 +00:00
|
|
|
|
|
|
|
if new_slug != slug {
|
2018-10-20 09:04:20 +00:00
|
|
|
if let Some(_) = Post::find_by_slug(&*conn, new_slug.clone(), b.id) {
|
2018-09-07 17:51:53 +00:00
|
|
|
errors.add("title", ValidationError {
|
|
|
|
code: Cow::from("existing_slug"),
|
|
|
|
message: Some(Cow::from("A post with the same title already exists.")),
|
|
|
|
params: HashMap::new()
|
|
|
|
});
|
|
|
|
}
|
2018-09-06 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if errors.is_empty() {
|
2018-10-20 09:04:20 +00:00
|
|
|
if !user.is_author_in(&*conn, b) {
|
2018-09-06 21:39:22 +00:00
|
|
|
// actually it's not "Ok"…
|
|
|
|
Ok(Redirect::to(uri!(super::blogs::details: name = blog)))
|
|
|
|
} else {
|
2018-10-20 17:27:49 +00:00
|
|
|
let (content, mentions, hashtags) = utils::md_to_html(form.content.to_string().as_ref());
|
2018-09-06 21:39:22 +00:00
|
|
|
|
|
|
|
let license = if form.license.len() > 0 {
|
|
|
|
form.license.to_string()
|
|
|
|
} else {
|
2018-10-06 17:19:45 +00:00
|
|
|
Instance::get_local(&*conn).map(|i| i.default_license).unwrap_or(String::from("CC-BY-SA"))
|
2018-09-06 21:39:22 +00:00
|
|
|
};
|
|
|
|
|
2018-09-10 18:38:19 +00:00
|
|
|
// update publication date if when this article is no longer a draft
|
2018-10-28 10:42:01 +00:00
|
|
|
let newly_published = if !post.published && !form.draft {
|
2018-09-10 18:38:19 +00:00
|
|
|
post.published = true;
|
|
|
|
post.creation_date = Utc::now().naive_utc();
|
2018-10-28 10:42:01 +00:00
|
|
|
true
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
};
|
2018-09-10 18:38:19 +00:00
|
|
|
|
2018-09-06 21:39:22 +00:00
|
|
|
post.slug = new_slug.clone();
|
|
|
|
post.title = form.title.clone();
|
|
|
|
post.subtitle = form.subtitle.clone();
|
|
|
|
post.content = SafeString::new(&content);
|
|
|
|
post.source = form.content.clone();
|
|
|
|
post.license = license;
|
|
|
|
post.update(&*conn);
|
|
|
|
let post = post.update_ap_url(&*conn);
|
|
|
|
|
2018-09-12 16:00:00 +00:00
|
|
|
if post.published {
|
|
|
|
for m in mentions.into_iter() {
|
|
|
|
Mention::from_activity(&*conn, Mention::build_activity(&*conn, m), post.id, true, true);
|
|
|
|
}
|
2018-09-06 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
2018-10-20 12:05:41 +00:00
|
|
|
let old_tags = Tag::for_post(&*conn, post.id).into_iter().collect::<Vec<_>>();
|
|
|
|
let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0).collect::<Vec<_>>();
|
|
|
|
for tag in tags.iter() {
|
2018-10-20 17:27:49 +00:00
|
|
|
if old_tags.iter().all(|ot| &ot.tag!=tag || ot.is_hashtag) {
|
2018-10-20 12:05:41 +00:00
|
|
|
Tag::insert(&*conn, NewTag {
|
|
|
|
tag: tag.clone(),
|
2018-10-20 17:27:49 +00:00
|
|
|
is_hashtag: false,
|
2018-10-20 12:05:41 +00:00
|
|
|
post_id: post.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 17:27:49 +00:00
|
|
|
for ot in old_tags.iter() {
|
|
|
|
if !tags.contains(&ot.tag) && !ot.is_hashtag {
|
|
|
|
ot.delete(&conn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let hashtags = hashtags.into_iter().map(|h| h.to_camel_case()).collect::<Vec<_>>();
|
|
|
|
for hashtag in hashtags.iter() {
|
|
|
|
if old_tags.iter().all(|ot| &ot.tag!=hashtag || !ot.is_hashtag) {
|
|
|
|
Tag::insert(&*conn, NewTag {
|
|
|
|
tag: hashtag.clone(),
|
|
|
|
is_hashtag: true,
|
|
|
|
post_id: post.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2018-10-20 12:05:41 +00:00
|
|
|
for ot in old_tags {
|
2018-10-20 17:27:49 +00:00
|
|
|
if !hashtags.contains(&ot.tag) && ot.is_hashtag {
|
2018-10-20 12:05:41 +00:00
|
|
|
ot.delete(&conn);
|
|
|
|
}
|
2018-09-06 21:39:22 +00:00
|
|
|
}
|
|
|
|
|
2018-09-10 18:38:19 +00:00
|
|
|
if post.published {
|
2018-10-28 10:42:01 +00:00
|
|
|
if newly_published {
|
|
|
|
let act = post.create_activity(&conn);
|
|
|
|
let dest = User::one_by_instance(&*conn);
|
|
|
|
worker.execute(Thunk::of(move || broadcast(&user, act, dest)));
|
|
|
|
} else {
|
|
|
|
let act = post.update_activity(&*conn);
|
|
|
|
let dest = User::one_by_instance(&*conn);
|
|
|
|
worker.execute(Thunk::of(move || broadcast(&user, act, dest)));
|
|
|
|
}
|
2018-09-10 18:38:19 +00:00
|
|
|
}
|
2018-09-06 21:39:22 +00:00
|
|
|
|
2018-09-07 17:51:53 +00:00
|
|
|
Ok(Redirect::to(uri!(details: blog = blog, slug = new_slug)))
|
2018-09-06 21:39:22 +00:00
|
|
|
}
|
|
|
|
} else {
|
2018-10-20 09:04:20 +00:00
|
|
|
Err(Some(Template::render("posts/new", json!({
|
2018-09-06 21:39:22 +00:00
|
|
|
"account": user.to_json(&*conn),
|
|
|
|
"instance": Instance::get_local(&*conn),
|
2018-09-07 17:51:53 +00:00
|
|
|
"editing": true,
|
2018-09-06 21:39:22 +00:00
|
|
|
"errors": errors.inner(),
|
2018-09-10 18:38:19 +00:00
|
|
|
"form": form,
|
|
|
|
"is_draft": form.draft,
|
2018-10-20 09:04:20 +00:00
|
|
|
}))))
|
2018-09-06 21:39:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-06 17:29:36 +00:00
|
|
|
#[derive(FromForm, Validate, Serialize)]
|
2018-04-23 14:25:39 +00:00
|
|
|
struct NewPostForm {
|
2018-07-07 20:51:48 +00:00
|
|
|
#[validate(custom(function = "valid_slug", message = "Invalid title"))]
|
2018-04-23 14:25:39 +00:00
|
|
|
pub title: String,
|
2018-09-04 11:26:13 +00:00
|
|
|
pub subtitle: String,
|
2018-04-23 14:25:39 +00:00
|
|
|
pub content: String,
|
2018-09-05 20:18:27 +00:00
|
|
|
pub tags: String,
|
2018-09-10 18:38:19 +00:00
|
|
|
pub license: String,
|
|
|
|
pub draft: bool,
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
|
|
|
|
2018-06-29 12:56:00 +00:00
|
|
|
fn valid_slug(title: &str) -> Result<(), ValidationError> {
|
|
|
|
let slug = title.to_string().to_kebab_case();
|
|
|
|
if slug.len() == 0 {
|
|
|
|
Err(ValidationError::new("empty_slug"))
|
2018-07-06 09:51:19 +00:00
|
|
|
} else if slug == "new" {
|
|
|
|
Err(ValidationError::new("invalid_slug"))
|
2018-06-29 12:56:00 +00:00
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-23 14:25:39 +00:00
|
|
|
#[post("/~/<blog_name>/new", data = "<data>")]
|
2018-10-20 09:04:20 +00:00
|
|
|
fn create(blog_name: String, data: LenientForm<NewPostForm>, user: User, conn: DbConn, worker: State<Pool<ThunkWorker<()>>>) -> Result<Redirect, Option<Template>> {
|
|
|
|
let blog = Blog::find_by_fqn(&*conn, blog_name.to_string()).ok_or(None)?;
|
2018-04-23 14:25:39 +00:00
|
|
|
let form = data.get();
|
|
|
|
let slug = form.title.to_string().to_kebab_case();
|
2018-09-03 13:59:02 +00:00
|
|
|
|
2018-07-06 09:51:19 +00:00
|
|
|
let mut errors = match form.validate() {
|
|
|
|
Ok(_) => ValidationErrors::new(),
|
|
|
|
Err(e) => e
|
|
|
|
};
|
2018-07-07 20:51:48 +00:00
|
|
|
if let Some(_) = Post::find_by_slug(&*conn, slug.clone(), blog.id) {
|
|
|
|
errors.add("title", ValidationError {
|
|
|
|
code: Cow::from("existing_slug"),
|
|
|
|
message: Some(Cow::from("A post with the same title already exists.")),
|
|
|
|
params: HashMap::new()
|
|
|
|
});
|
2018-07-06 09:51:19 +00:00
|
|
|
}
|
2018-05-24 10:42:45 +00:00
|
|
|
|
2018-07-06 09:51:19 +00:00
|
|
|
if errors.is_empty() {
|
|
|
|
if !user.is_author_in(&*conn, blog.clone()) {
|
|
|
|
// actually it's not "Ok"…
|
|
|
|
Ok(Redirect::to(uri!(super::blogs::details: name = blog_name)))
|
2018-06-20 08:44:56 +00:00
|
|
|
} else {
|
2018-10-20 17:27:49 +00:00
|
|
|
let (content, mentions, hashtags) = utils::md_to_html(form.content.to_string().as_ref());
|
2018-05-24 10:42:45 +00:00
|
|
|
|
2018-06-20 08:44:56 +00:00
|
|
|
let post = Post::insert(&*conn, NewPost {
|
|
|
|
blog_id: blog.id,
|
|
|
|
slug: slug.to_string(),
|
|
|
|
title: form.title.to_string(),
|
|
|
|
content: SafeString::new(&content),
|
2018-09-10 18:38:19 +00:00
|
|
|
published: !form.draft,
|
2018-07-27 18:31:47 +00:00
|
|
|
license: if form.license.len() > 0 {
|
|
|
|
form.license.to_string()
|
|
|
|
} else {
|
2018-10-06 17:19:45 +00:00
|
|
|
Instance::get_local(&*conn).map(|i| i.default_license).unwrap_or(String::from("CC-BY-SA"))
|
2018-07-27 18:31:47 +00:00
|
|
|
},
|
2018-07-26 22:29:21 +00:00
|
|
|
ap_url: "".to_string(),
|
2018-09-04 11:26:13 +00:00
|
|
|
creation_date: None,
|
2018-09-06 19:00:55 +00:00
|
|
|
subtitle: form.subtitle.clone(),
|
|
|
|
source: form.content.clone(),
|
2018-06-20 08:44:56 +00:00
|
|
|
});
|
2018-06-22 15:17:53 +00:00
|
|
|
let post = post.update_ap_url(&*conn);
|
2018-06-20 08:44:56 +00:00
|
|
|
PostAuthor::insert(&*conn, NewPostAuthor {
|
|
|
|
post_id: post.id,
|
|
|
|
author_id: user.id
|
|
|
|
});
|
2018-05-01 15:51:49 +00:00
|
|
|
|
2018-09-05 20:18:27 +00:00
|
|
|
let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0);
|
|
|
|
for tag in tags {
|
|
|
|
Tag::insert(&*conn, NewTag {
|
|
|
|
tag: tag,
|
2018-10-20 17:27:49 +00:00
|
|
|
is_hashtag: false,
|
|
|
|
post_id: post.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for hashtag in hashtags {
|
|
|
|
Tag::insert(&*conn, NewTag {
|
|
|
|
tag: hashtag.to_camel_case(),
|
|
|
|
is_hashtag: true,
|
2018-09-05 20:18:27 +00:00
|
|
|
post_id: post.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-09-10 18:38:19 +00:00
|
|
|
if post.published {
|
|
|
|
for m in mentions.into_iter() {
|
|
|
|
Mention::from_activity(&*conn, Mention::build_activity(&*conn, m), post.id, true, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
let act = post.create_activity(&*conn);
|
|
|
|
let dest = User::one_by_instance(&*conn);
|
|
|
|
worker.execute(Thunk::of(move || broadcast(&user, act, dest)));
|
|
|
|
}
|
2018-05-01 15:51:49 +00:00
|
|
|
|
2018-07-06 09:51:19 +00:00
|
|
|
Ok(Redirect::to(uri!(details: blog = blog_name, slug = slug)))
|
2018-06-20 08:44:56 +00:00
|
|
|
}
|
2018-07-06 09:51:19 +00:00
|
|
|
} else {
|
2018-10-20 09:04:20 +00:00
|
|
|
Err(Some(Template::render("posts/new", json!({
|
2018-09-03 13:59:02 +00:00
|
|
|
"account": user.to_json(&*conn),
|
|
|
|
"instance": Instance::get_local(&*conn),
|
2018-09-06 21:39:22 +00:00
|
|
|
"editing": false,
|
2018-07-06 17:29:36 +00:00
|
|
|
"errors": errors.inner(),
|
2018-09-10 18:38:19 +00:00
|
|
|
"form": form,
|
|
|
|
"is_draft": form.draft
|
2018-10-20 09:04:20 +00:00
|
|
|
}))))
|
2018-06-19 19:16:18 +00:00
|
|
|
}
|
2018-04-23 14:25:39 +00:00
|
|
|
}
|
2018-09-01 15:28:47 +00:00
|
|
|
|
2018-09-19 17:13:07 +00:00
|
|
|
#[post("/~/<blog_name>/<slug>/delete")]
|
2018-09-01 15:28:47 +00:00
|
|
|
fn delete(blog_name: String, slug: String, conn: DbConn, user: User, worker: State<Pool<ThunkWorker<()>>>) -> Redirect {
|
|
|
|
let post = Blog::find_by_fqn(&*conn, blog_name.clone())
|
|
|
|
.and_then(|blog| Post::find_by_slug(&*conn, slug.clone(), blog.id));
|
|
|
|
|
|
|
|
if let Some(post) = post {
|
|
|
|
if !post.get_authors(&*conn).into_iter().any(|a| a.id == user.id) {
|
|
|
|
Redirect::to(uri!(details: blog = blog_name.clone(), slug = slug.clone()))
|
|
|
|
} else {
|
2018-09-09 11:19:11 +00:00
|
|
|
let dest = User::one_by_instance(&*conn);
|
2018-09-01 15:28:47 +00:00
|
|
|
let delete_activity = post.delete(&*conn);
|
2018-09-09 11:19:11 +00:00
|
|
|
worker.execute(Thunk::of(move || broadcast(&user, delete_activity, dest)));
|
2018-09-01 15:28:47 +00:00
|
|
|
|
|
|
|
Redirect::to(uri!(super::blogs::details: name = blog_name))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Redirect::to(uri!(super::blogs::details: name = blog_name))
|
|
|
|
}
|
|
|
|
}
|