diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index 8eb1c418..22aab769 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -389,10 +389,11 @@ impl Post { post.source = source.content; } - let mut hashtags = md_to_html(&post.source).2.into_iter().map(|s| s.to_camel_case()).collect::>(); + let mut txt_hashtags = md_to_html(&post.source).2.into_iter().map(|s| s.to_camel_case()).collect::>(); if let Some(serde_json::Value::Array(mention_tags)) = updated.object_props.tag.clone() { let mut mentions = vec![]; let mut tags = vec![]; + let mut hashtags = vec![]; for tag in mention_tags.into_iter() { serde_json::from_value::(tag.clone()) .map(|m| mentions.push(m)) @@ -401,12 +402,16 @@ impl Post { serde_json::from_value::(tag.clone()) .map(|t| { let tag_name = t.name_string().expect("Post::from_activity: tag name error"); - tags.push((t, hashtags.remove(&tag_name))); + if txt_hashtags.remove(&tag_name) { + hashtags.push(t); + } else { + tags.push(t); + } }).ok(); } - // Tag::from_activity(conn, t, post.id, bool); - // Mention::from_activity(conn, m, post.id, true, true) post.update_mentions(conn, mentions); + post.update_tags(conn, tags); + post.update_hashtags(conn, hashtags); } post.update(conn); @@ -433,14 +438,44 @@ impl Post { } } -/* pub fn update_hashtags_from_activity(&self, conn: &Connection, Vec/*create a build_activity for Tag, as in Mention,*/) { - unimplemented!(); + pub fn update_tags(&self, conn: &Connection, tags: Vec) { + let tags_name = tags.iter().filter_map(|t| t.name_string().ok()).collect::>(); + + let old_tags = Tag::for_post(&*conn, self.id).into_iter().collect::>(); + let old_tags_name = old_tags.iter().filter_map(|tag| if !tag.is_hashtag {Some(tag.tag.clone())} else {None}).collect::>(); + + for t in tags.into_iter() { + if !t.name_string().map(|n| old_tags_name.contains(&n)).unwrap_or(true) { + Tag::from_activity(conn, t, self.id, false); + } + } + + for ot in old_tags { + if !tags_name.contains(&ot.tag) { + ot.delete(conn); + } + } } - pub fn update_tags_from_activity(&self, conn: &Connection, Vec) { - unimplemented!(); + pub fn update_hashtags(&self, conn: &Connection, tags: Vec) { + let tags_name = tags.iter().filter_map(|t| t.name_string().ok()).collect::>(); + + let old_tags = Tag::for_post(&*conn, self.id).into_iter().collect::>(); + let old_tags_name = old_tags.iter().filter_map(|tag| if tag.is_hashtag {Some(tag.tag.clone())} else {None}).collect::>(); + + for t in tags.into_iter() { + if !t.name_string().map(|n| old_tags_name.contains(&n)).unwrap_or(true) { + Tag::from_activity(conn, t, self.id, true); + } + } + + for ot in old_tags { + if !tags_name.contains(&ot.tag) { + ot.delete(conn); + } + } } -*/ + pub fn to_json(&self, conn: &Connection) -> serde_json::Value { let blog = self.get_blog(conn); json!({ diff --git a/plume-models/src/tags.rs b/plume-models/src/tags.rs index 44bf98b0..ed704bd1 100644 --- a/plume-models/src/tags.rs +++ b/plume-models/src/tags.rs @@ -45,6 +45,16 @@ impl Tag { }) } + pub fn build_activity(conn: &Connection, tag: String) -> Hashtag { + let mut ht = Hashtag::default(); + ht.set_href_string(ap_url(format!("{}/tag/{}", + Instance::get_local(conn).expect("Tag::into_activity: local instance not found error").public_domain, + tag) + )).expect("Tag::into_activity: href error"); + ht.set_name_string(tag).expect("Tag::into_activity: name error"); + ht + } + pub fn delete(&self, conn: &Connection) { diesel::delete(self).execute(conn).expect("Tag::delete: database error"); } diff --git a/src/routes/posts.rs b/src/routes/posts.rs index e25f71fb..48486a5e 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -213,37 +213,13 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor post.update_mentions(&conn, mentions.into_iter().map(|m| Mention::build_activity(&conn, m)).collect()); } - let old_tags = Tag::for_post(&*conn, post.id).into_iter().collect::>(); - let old_non_hashtags = old_tags.iter().filter_map(|tag| if !tag.is_hashtag {Some(tag.tag.clone())} else {None}).collect(); - let old_hashtags = old_tags.iter().filter_map(|tag| if tag.is_hashtag {Some(tag.tag.clone())} else {None}).collect(); + let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0) + .collect::>().into_iter().map(|t| Tag::build_activity(&conn, t)).collect::>(); + post.update_tags(&conn, tags); - let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0).collect::>(); - for tag in tags.difference(&old_non_hashtags) { - Tag::insert(&*conn, NewTag { - tag: tag.clone(), - is_hashtag: false, - post_id: post.id - }); - } - 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::>(); - for hashtag in hashtags.difference(&old_hashtags) { - Tag::insert(&*conn, NewTag { - tag: hashtag.clone(), - is_hashtag: true, - post_id: post.id, - }); - } - for ot in old_tags { - if !hashtags.contains(&ot.tag) && ot.is_hashtag { - ot.delete(&conn); - } - } + let hashtags = hashtags.into_iter().map(|h| h.to_camel_case()).collect::>() + .into_iter().map(|t| Tag::build_activity(&conn, t)).collect::>(); + post.update_tags(&conn, hashtags); if post.published { if newly_published {