Detect if tags are hashtags when receiving new posts

This commit is contained in:
Trinity Pointard 2018-10-27 23:51:26 +02:00 committed by Baptiste Gelez
parent 9d7ae694a8
commit f147885f08
2 changed files with 18 additions and 8 deletions

View File

@ -6,14 +6,17 @@ use activitypub::{
use canapi::{Error, Provider}; use canapi::{Error, Provider};
use chrono::{NaiveDateTime, TimeZone, Utc}; use chrono::{NaiveDateTime, TimeZone, Utc};
use diesel::{self, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl}; use diesel::{self, RunQueryDsl, QueryDsl, ExpressionMethods, BelongingToDsl};
use heck::KebabCase; use heck::{CamelCase, KebabCase};
use serde_json; use serde_json;
use plume_api::posts::PostEndpoint; use plume_api::posts::PostEndpoint;
use plume_common::activity_pub::{ use plume_common::{
Hashtag, Source, activity_pub::{
PUBLIC_VISIBILTY, Id, IntoId, Hashtag, Source,
inbox::{Deletable, FromActivity} PUBLIC_VISIBILTY, Id, IntoId,
inbox::{Deletable, FromActivity}
},
utils::md_to_html
}; };
use {BASE_URL, ap_url, Connection}; use {BASE_URL, ap_url, Connection};
use blogs::Blog; use blogs::Blog;
@ -26,6 +29,7 @@ use tags::Tag;
use users::User; use users::User;
use schema::posts; use schema::posts;
use safe_string::SafeString; use safe_string::SafeString;
use std::collections::HashSet;
#[derive(Queryable, Identifiable, Serialize, Clone, AsChangeset)] #[derive(Queryable, Identifiable, Serialize, Clone, AsChangeset)]
pub struct Post { pub struct Post {
@ -447,6 +451,8 @@ impl FromActivity<Article, Connection> for Post {
} }
// save mentions and tags // save mentions and tags
let mut hashtags = md_to_html(&post.source).2.into_iter().map(|s| s.to_camel_case()).collect::<HashSet<_>>();
println!("{:?}", hashtags);
if let Some(serde_json::Value::Array(tags)) = article.object_props.tag.clone() { if let Some(serde_json::Value::Array(tags)) = article.object_props.tag.clone() {
for tag in tags.into_iter() { for tag in tags.into_iter() {
serde_json::from_value::<link::Mention>(tag.clone()) serde_json::from_value::<link::Mention>(tag.clone())
@ -454,7 +460,11 @@ impl FromActivity<Article, Connection> for Post {
.ok(); .ok();
serde_json::from_value::<Hashtag>(tag.clone()) serde_json::from_value::<Hashtag>(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(); .ok();
} }
} }

View File

@ -37,10 +37,10 @@ impl Tag {
ht 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::insert(conn, NewTag {
tag: tag.name_string().expect("Tag::from_activity: name error"), tag: tag.name_string().expect("Tag::from_activity: name error"),
is_hashtag: false, is_hashtag,
post_id: post post_id: post
}) })
} }