From c1562f38683f564bca210772285dfbd1858d3019 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Mon, 21 Mar 2022 10:08:00 +0900 Subject: [PATCH] Implement Post::update_tags07() --- plume-models/src/posts.rs | 41 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/plume-models/src/posts.rs b/plume-models/src/posts.rs index e6fb195e..bd87714d 100644 --- a/plume-models/src/posts.rs +++ b/plume-models/src/posts.rs @@ -25,8 +25,8 @@ use plume_common::{ activity_pub::{ inbox::{AsActor, AsObject, FromId}, sign::Signer, - Hashtag, Id, IntoId, Licensed, Licensed07, LicensedArticle as LicensedArticle07, Source, - SourceProperty, PUBLIC_VISIBILITY, + Hashtag, Hashtag07, Id, IntoId, Licensed, Licensed07, LicensedArticle as LicensedArticle07, + Source, SourceProperty, PUBLIC_VISIBILITY, }, utils::{iri_percent_encode_seg, md_to_html}, }; @@ -674,6 +674,43 @@ impl Post { Ok(()) } + pub fn update_tags07(&self, conn: &Connection, tags: Vec) -> Result<()> { + let tags_name = tags + .iter() + .filter_map(|t| t.name.as_ref().map(|name| name.as_str().to_string())) + .collect::>(); + + let old_tags = Tag::for_post(&*conn, self.id)?; + let old_tags_name = old_tags + .iter() + .filter_map(|tag| { + if !tag.is_hashtag { + Some(tag.tag.clone()) + } else { + None + } + }) + .collect::>(); + + for t in tags { + if !t + .name + .as_ref() + .map(|n| old_tags_name.contains(n.as_str())) + .unwrap_or(true) + { + Tag::from_activity07(conn, &t, self.id, false)?; + } + } + + for ot in old_tags.iter().filter(|t| !t.is_hashtag) { + if !tags_name.contains(&ot.tag) { + ot.delete(conn)?; + } + } + Ok(()) + } + pub fn update_hashtags(&self, conn: &Connection, tags: Vec) -> Result<()> { let tags_name = tags .iter()