Implement Post::update_tags07()

This commit is contained in:
Kitaiti Makoto 2022-03-21 10:08:00 +09:00
parent e0390cb105
commit c1562f3868

View file

@ -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<Hashtag07>) -> Result<()> {
let tags_name = tags
.iter()
.filter_map(|t| t.name.as_ref().map(|name| name.as_str().to_string()))
.collect::<HashSet<_>>();
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::<HashSet<_>>();
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<Hashtag>) -> Result<()> {
let tags_name = tags
.iter()