Allow tag deletion

Fix #232
This commit is contained in:
Trinity Pointard 2018-10-20 14:05:41 +02:00
parent 879fde81ba
commit a6e73f4667
2 changed files with 20 additions and 9 deletions

View File

@ -5,7 +5,7 @@ use {ap_url, Connection};
use instance::Instance; use instance::Instance;
use schema::tags; use schema::tags;
#[derive(Serialize, Queryable, Clone)] #[derive(Clone, Identifiable, Serialize, Queryable)]
pub struct Tag { pub struct Tag {
pub id: i32, pub id: i32,
pub tag: String, pub tag: String,
@ -44,4 +44,8 @@ impl Tag {
post_id: post post_id: post
}) })
} }
pub fn delete(&self, conn: &Connection) {
diesel::delete(self).execute(conn).expect("Tag::delete: database error");
}
} }

View File

@ -212,15 +212,22 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor
} }
} }
let old_tags = Tag::for_post(&*conn, post.id).into_iter().map(|t| t.tag).collect::<Vec<_>>(); 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 && !old_tags.contains(t)); let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0).collect::<Vec<_>>();
for tag in tags { for tag in tags.iter() {
if old_tags.iter().all(|ot| &ot.tag!=tag) {
Tag::insert(&*conn, NewTag { Tag::insert(&*conn, NewTag {
tag: tag, tag: tag.clone(),
is_hastag: false, is_hastag: false,
post_id: post.id post_id: post.id
}); });
} }
}
for ot in old_tags {
if !tags.contains(&ot.tag) {
ot.delete(&conn);
}
}
if post.published { if post.published {
let act = post.update_activity(&*conn); let act = post.update_activity(&*conn);