Add support for blog deletion

fix #181
This commit is contained in:
Trinity Pointard 2018-10-20 15:03:59 +02:00
parent a6e73f4667
commit eca458b0e5
4 changed files with 31 additions and 2 deletions

View file

@ -17,13 +17,14 @@ use webfinger::*;
use {BASE_URL, USE_HTTPS, Connection};
use plume_common::activity_pub::{
ap_accept_header, ApSignature, ActivityStream, Id, IntoId, PublicKey,
inbox::WithInbox,
inbox::{Deletable, WithInbox},
sign
};
use safe_string::SafeString;
use instance::*;
use users::User;
use posts::Post;
use schema::blogs;
use users::User;
pub type CustomGroup = CustomObject<ApSignature, Group>;
@ -273,6 +274,13 @@ impl Blog {
json["fqn"] = json!(self.get_fqn(conn));
json
}
pub fn delete(&self, conn: &Connection) {
for post in Post::get_for_blog(conn, &self) {
post.delete(conn);
}
diesel::delete(self).execute(conn).expect("Blog::delete: blog deletion error");
}
}
impl IntoId for Blog {

View file

@ -66,6 +66,7 @@ fn main() {
routes::blogs::new,
routes::blogs::new_auth,
routes::blogs::create,
routes::blogs::delete,
routes::blogs::atom_feed,
routes::comments::create,

View file

@ -129,6 +129,19 @@ fn create(conn: DbConn, data: LenientForm<NewBlogForm>, user: User) -> Result<Re
}
}
#[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."
}))))
}
}
#[get("/~/<name>/outbox")]
fn outbox(name: String, conn: DbConn) -> Option<ActivityStream<OrderedCollection>> {
let blog = Blog::find_local(&*conn, name)?;

View file

@ -40,4 +40,11 @@
</div>
{{ macros::paginate(page=page, total=n_pages) }}
</section>
{% if is_author %}
<h2>{{ "Danger zone" | _ }}</h2>
<p>{{ "Be very careful, any action taken here can't be cancelled." | _ }}
<form method="post" action="/~/{{ blog.fqn }}/delete">
<input type="submit" class="inline-block button destructive" value="{{ "Delete this blog" | _ }}">
</form>
{% endif %}
{% endblock content %}