From f147885f08f191e63968ba464b5d6abd9345e94d Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Sat, 27 Oct 2018 23:51:26 +0200 Subject: [PATCH] Detect if tags are hashtags when receiving new posts --- plume-models/src/posts.rs | 22 ++++++++++++++++------ plume-models/src/tags.rs | 4 ++-- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index 13af02d6..71c9a6a9 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -6,14 +6,17 @@ use activitypub::{ use canapi::{Error, Provider}; use chrono::{NaiveDateTime, TimeZone, Utc}; use diesel::{self, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl}; -use heck::KebabCase; +use heck::{CamelCase, KebabCase}; use serde_json; use plume_api::posts::PostEndpoint; -use plume_common::activity_pub::{ - Hashtag, Source, - PUBLIC_VISIBILTY, Id, IntoId, - inbox::{Deletable, FromActivity} +use plume_common::{ + activity_pub::{ + Hashtag, Source, + PUBLIC_VISIBILTY, Id, IntoId, + inbox::{Deletable, FromActivity} + }, + utils::md_to_html }; use {BASE_URL, ap_url, Connection}; use blogs::Blog; @@ -26,6 +29,7 @@ use tags::Tag; use users::User; use schema::posts; use safe_string::SafeString; +use std::collections::HashSet; #[derive(Queryable, Identifiable, Serialize, Clone, AsChangeset)] pub struct Post { @@ -447,6 +451,8 @@ impl FromActivity for Post { } // save mentions and tags + let mut hashtags = md_to_html(&post.source).2.into_iter().map(|s| s.to_camel_case()).collect::>(); + println!("{:?}", hashtags); if let Some(serde_json::Value::Array(tags)) = article.object_props.tag.clone() { for tag in tags.into_iter() { serde_json::from_value::(tag.clone()) @@ -454,7 +460,11 @@ impl FromActivity for Post { .ok(); serde_json::from_value::(tag.clone()) - .map(|t| Tag::from_activity(conn, t, post.id)) + .map(|t| { + let tag_name = t.name_string().expect("Post::from_activity: tag name error"); + println!("{} : {}", tag_name, hashtags.contains(&tag_name)); + Tag::from_activity(conn, t, post.id, hashtags.remove(&tag_name)); + }) .ok(); } } diff --git a/plume-models/src/tags.rs b/plume-models/src/tags.rs index 99e6cb6c..44bf98b0 100644 --- a/plume-models/src/tags.rs +++ b/plume-models/src/tags.rs @@ -37,10 +37,10 @@ impl Tag { ht } - pub fn from_activity(conn: &Connection, tag: Hashtag, post: i32) -> Tag { + pub fn from_activity(conn: &Connection, tag: Hashtag, post: i32, is_hashtag: bool) -> Tag { Tag::insert(conn, NewTag { tag: tag.name_string().expect("Tag::from_activity: name error"), - is_hashtag: false, + is_hashtag, post_id: post }) }