From 2b1ddc71ac69af11b56e40e9a02c90ed9bc7345d Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sun, 6 Mar 2022 20:04:42 +0900 Subject: [PATCH] Implement Tag::to_activity07() and Tag::build_activity07() --- plume-models/src/tags.rs | 73 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/plume-models/src/tags.rs b/plume-models/src/tags.rs index 0460efc2..18072929 100644 --- a/plume-models/src/tags.rs +++ b/plume-models/src/tags.rs @@ -1,6 +1,7 @@ use crate::{ap_url, instance::Instance, schema::tags, Connection, Error, Result}; +use activitystreams::iri_string::types::IriString; use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; -use plume_common::activity_pub::Hashtag; +use plume_common::activity_pub::{Hashtag, Hashtag07, HashtagExt}; #[derive(Clone, Identifiable, Queryable)] pub struct Tag { @@ -35,6 +36,20 @@ impl Tag { Ok(ht) } + pub fn to_activity07(&self) -> Result { + let mut ht = Hashtag07::new(); + ht.set_href( + ap_url(&format!( + "{}/tag/{}", + Instance::get_local()?.public_domain, + self.tag + )) + .parse::()?, + ); + ht.set_name(self.tag.clone()); + Ok(ht) + } + pub fn from_activity( conn: &Connection, tag: &Hashtag, @@ -62,6 +77,20 @@ impl Tag { Ok(ht) } + pub fn build_activity07(tag: String) -> Result { + let mut ht = Hashtag07::new(); + ht.set_href( + ap_url(&format!( + "{}/tag/{}", + Instance::get_local()?.public_domain, + tag + )) + .parse::()?, + ); + ht.set_name(tag); + Ok(ht) + } + pub fn delete(&self, conn: &Connection) -> Result<()> { diesel::delete(self) .execute(conn) @@ -137,4 +166,46 @@ mod tests { Ok(()) }); } + + #[test] + fn to_activity07() { + let conn = &db(); + conn.test_transaction::<_, Error, _>(|| { + fill_database(conn); + let tag = Tag { + id: 0, + tag: "a_tag".into(), + is_hashtag: false, + post_id: 0, + }; + let act = tag.to_activity07()?; + let expected = json!({ + "href": "https://plu.me/tag/a_tag", + "name": "a_tag", + "type": "Hashtag" + }); + + assert_json_eq!(to_value(&act)?, expected); + + Ok(()) + }) + } + + #[test] + fn build_activity07() { + let conn = &db(); + conn.test_transaction::<_, Error, _>(|| { + fill_database(conn); + let act = Tag::build_activity07("a_tag".into())?; + let expected = json!({ + "href": "https://plu.me/tag/a_tag", + "name": "a_tag", + "type": "Hashtag" + }); + + assert_json_eq!(to_value(&act)?, expected); + + Ok(()) + }); + } }