From 1689813df4b2b26733b5d9853f8d934253827085 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Sat, 27 Oct 2018 20:44:42 +0200 Subject: [PATCH] Deduplicate tags and mentions Use set to work on tags and mentions, allowing deduplication of them, and clearer code May also help with distinguishing tags and hashtags latter --- plume-common/src/utils.rs | 6 +++--- src/routes/posts.rs | 39 +++++++++++++++++++-------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/plume-common/src/utils.rs b/plume-common/src/utils.rs index 36f9558a..e17399d4 100644 --- a/plume-common/src/utils.rs +++ b/plume-common/src/utils.rs @@ -5,6 +5,7 @@ use rocket::{ http::uri::Uri, response::{Redirect, Flash} }; +use std::collections::HashSet; /// Remove non alphanumeric characters and CamelCase a string pub fn make_actor_id(name: String) -> String { @@ -29,7 +30,7 @@ enum State { } /// Returns (HTML, mentions, hashtags) -pub fn md_to_html(md: &str) -> (String, Vec, Vec) { +pub fn md_to_html(md: &str) -> (String, HashSet, HashSet) { let parser = Parser::new_ext(md, Options::all()); let (parser, mentions, hashtags): (Vec>, Vec>, Vec>) = parser.map(|evt| match evt { @@ -129,8 +130,7 @@ pub fn md_to_html(md: &str) -> (String, Vec, Vec) { let mut buf = String::new(); html::push_html(&mut buf, parser); - let hashtags = hashtags.collect(); - (buf, mentions.collect(), hashtags) + (buf, mentions.collect(), hashtags.collect()) } #[cfg(test)] diff --git a/src/routes/posts.rs b/src/routes/posts.rs index d544e4ae..45a55d5b 100644 --- a/src/routes/posts.rs +++ b/src/routes/posts.rs @@ -5,7 +5,7 @@ use rocket::{State, request::LenientForm}; use rocket::response::{Redirect, Flash}; use rocket_contrib::Template; use serde_json; -use std::{collections::HashMap, borrow::Cow}; +use std::{collections::{HashMap, HashSet}, borrow::Cow}; use validator::{Validate, ValidationError, ValidationErrors}; use workerpool::{Pool, thunk::*}; @@ -216,15 +216,16 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor } let old_tags = Tag::for_post(&*conn, post.id).into_iter().collect::>(); - let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0).collect::>(); - for tag in tags.iter() { - if old_tags.iter().all(|ot| &ot.tag!=tag || ot.is_hashtag) { - Tag::insert(&*conn, NewTag { - tag: tag.clone(), - is_hashtag: false, - post_id: post.id - }); - } + 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::>(); + 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 { @@ -232,15 +233,13 @@ fn update(blog: String, slug: String, user: User, conn: DbConn, data: LenientFor } } - let hashtags = hashtags.into_iter().map(|h| h.to_camel_case()).collect::>(); - for hashtag in hashtags.iter() { - if old_tags.iter().all(|ot| &ot.tag!=hashtag || !ot.is_hashtag) { - Tag::insert(&*conn, NewTag { - tag: hashtag.clone(), - is_hashtag: true, - post_id: post.id, - }); - } + 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 { @@ -343,7 +342,7 @@ fn create(blog_name: String, data: LenientForm, user: User, conn: D author_id: user.id }); - let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0); + let tags = form.tags.split(",").map(|t| t.trim().to_camel_case()).filter(|t| t.len() > 0).collect::>(); for tag in tags { Tag::insert(&*conn, NewTag { tag: tag,