From 1f5ce8e5049590681d891489e75d7888d85520a4 Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Sun, 6 Mar 2022 22:00:37 +0900 Subject: [PATCH] Add test for Mention::build_activity07() and to_activity07() --- plume-models/src/mentions.rs | 71 ++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/plume-models/src/mentions.rs b/plume-models/src/mentions.rs index 16672781..d6d027d9 100644 --- a/plume-models/src/mentions.rs +++ b/plume-models/src/mentions.rs @@ -3,6 +3,11 @@ use crate::{ users::User, Connection, Error, Result, }; use activitypub::link; +use activitystreams::{ + base::BaseExt, + iri_string::types::IriString, + link::{self as link07, LinkExt}, +}; use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl}; use plume_common::activity_pub::inbox::AsActor; @@ -64,6 +69,14 @@ impl Mention { Ok(mention) } + pub fn build_activity07(conn: &DbConn, ment: &str) -> Result { + let user = User::find_by_fqn(conn, ment)?; + let mut mention = link07::Mention::new(); + mention.set_href(user.ap_url.parse::()?); + mention.set_name(format!("@{}", ment)); + Ok(mention) + } + pub fn to_activity(&self, conn: &Connection) -> Result { let user = self.get_mentioned(conn)?; let mut mention = link::Mention::default(); @@ -74,6 +87,14 @@ impl Mention { Ok(mention) } + pub fn to_activity07(&self, conn: &Connection) -> Result { + let user = self.get_mentioned(conn)?; + let mut mention = link07::Mention::new(); + mention.set_href(user.ap_url.parse::()?); + mention.set_name(format!("@{}", user.fqn)); + Ok(mention) + } + pub fn from_activity( conn: &Connection, ment: &link::Mention, @@ -175,6 +196,27 @@ mod tests { }); } + #[test] + fn build_activity07() { + let conn = db(); + conn.test_transaction::<_, Error, _>(|| { + let (_posts, users, _blogs) = fill_database(&conn); + let user = &users[0]; + let name = &user.username; + let act = Mention::build_activity07(&conn, name)?; + + let expected = json!({ + "href": "https://plu.me/@/admin/", + "name": "@admin", + "type": "Mention", + }); + + assert_json_eq!(to_value(act)?, expected); + + Ok(()) + }); + } + #[test] fn to_activity() { let conn = db(); @@ -203,4 +245,33 @@ mod tests { Ok(()) }); } + + #[test] + fn to_activity07() { + let conn = db(); + conn.test_transaction::<_, Error, _>(|| { + let (posts, users, _blogs) = fill_database(&conn); + let post = &posts[0]; + let user = &users[0]; + let mention = Mention::insert( + &conn, + NewMention { + mentioned_id: user.id, + post_id: Some(post.id), + comment_id: None, + }, + )?; + let act = mention.to_activity07(&conn)?; + + let expected = json!({ + "href": "https://plu.me/@/admin/", + "name": "@admin", + "type": "Mention", + }); + + assert_json_eq!(to_value(act)?, expected); + + Ok(()) + }); + } }