Update tags and hashtags on remote post edition
This commit is contained in:
committed by
Baptiste Gelez
parent
c4fc656809
commit
76ca76f068
@@ -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::<HashSet<_>>();
|
||||
let mut txt_hashtags = md_to_html(&post.source).2.into_iter().map(|s| s.to_camel_case()).collect::<HashSet<_>>();
|
||||
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::<link::Mention>(tag.clone())
|
||||
.map(|m| mentions.push(m))
|
||||
@@ -401,12 +402,16 @@ impl Post {
|
||||
serde_json::from_value::<Hashtag>(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<Hashtag>/*create a build_activity for Tag, as in Mention,*/) {
|
||||
unimplemented!();
|
||||
pub fn update_tags(&self, conn: &Connection, tags: Vec<Hashtag>) {
|
||||
let tags_name = tags.iter().filter_map(|t| t.name_string().ok()).collect::<HashSet<_>>();
|
||||
|
||||
let old_tags = Tag::for_post(&*conn, self.id).into_iter().collect::<Vec<_>>();
|
||||
let old_tags_name = old_tags.iter().filter_map(|tag| if !tag.is_hashtag {Some(tag.tag.clone())} else {None}).collect::<HashSet<_>>();
|
||||
|
||||
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<Hashtag>) {
|
||||
unimplemented!();
|
||||
pub fn update_hashtags(&self, conn: &Connection, tags: Vec<Hashtag>) {
|
||||
let tags_name = tags.iter().filter_map(|t| t.name_string().ok()).collect::<HashSet<_>>();
|
||||
|
||||
let old_tags = Tag::for_post(&*conn, self.id).into_iter().collect::<Vec<_>>();
|
||||
let old_tags_name = old_tags.iter().filter_map(|tag| if tag.is_hashtag {Some(tag.tag.clone())} else {None}).collect::<HashSet<_>>();
|
||||
|
||||
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!({
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user