Update tags and hashtags on remote post edition

This commit is contained in:
Trinity Pointard 2018-10-28 10:57:10 +01:00 committed by Baptiste Gelez
parent c4fc656809
commit 76ca76f068
3 changed files with 60 additions and 39 deletions

View File

@ -389,10 +389,11 @@ impl Post {
post.source = source.content; 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() { if let Some(serde_json::Value::Array(mention_tags)) = updated.object_props.tag.clone() {
let mut mentions = vec![]; let mut mentions = vec![];
let mut tags = vec![]; let mut tags = vec![];
let mut hashtags = vec![];
for tag in mention_tags.into_iter() { for tag in mention_tags.into_iter() {
serde_json::from_value::<link::Mention>(tag.clone()) serde_json::from_value::<link::Mention>(tag.clone())
.map(|m| mentions.push(m)) .map(|m| mentions.push(m))
@ -401,12 +402,16 @@ impl Post {
serde_json::from_value::<Hashtag>(tag.clone()) serde_json::from_value::<Hashtag>(tag.clone())
.map(|t| { .map(|t| {
let tag_name = t.name_string().expect("Post::from_activity: tag name error"); 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(); }).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_mentions(conn, mentions);
post.update_tags(conn, tags);
post.update_hashtags(conn, hashtags);
} }
post.update(conn); 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,*/) { pub fn update_tags(&self, conn: &Connection, tags: Vec<Hashtag>) {
unimplemented!(); 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>) { pub fn update_hashtags(&self, conn: &Connection, tags: Vec<Hashtag>) {
unimplemented!(); 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 { pub fn to_json(&self, conn: &Connection) -> serde_json::Value {
let blog = self.get_blog(conn); let blog = self.get_blog(conn);
json!({ json!({

View File

@ -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) { pub fn delete(&self, conn: &Connection) {
diesel::delete(self).execute(conn).expect("Tag::delete: database error"); diesel::delete(self).execute(conn).expect("Tag::delete: database error");
} }

View File

@ -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()); 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::<Vec<_>>(); let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0)
let old_non_hashtags = old_tags.iter().filter_map(|tag| if !tag.is_hashtag {Some(tag.tag.clone())} else {None}).collect(); .collect::<HashSet<_>>().into_iter().map(|t| Tag::build_activity(&conn, t)).collect::<Vec<_>>();
let old_hashtags = old_tags.iter().filter_map(|tag| if tag.is_hashtag {Some(tag.tag.clone())} else {None}).collect(); post.update_tags(&conn, tags);
let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0).collect::<HashSet<_>>(); let hashtags = hashtags.into_iter().map(|h| h.to_camel_case()).collect::<HashSet<_>>()
for tag in tags.difference(&old_non_hashtags) { .into_iter().map(|t| Tag::build_activity(&conn, t)).collect::<Vec<_>>();
Tag::insert(&*conn, NewTag { post.update_tags(&conn, hashtags);
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::<HashSet<_>>();
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);
}
}
if post.published { if post.published {
if newly_published { if newly_published {