From be59d93e67186407c6d75d75b24e944352c711dc Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Tue, 6 Feb 2024 23:03:35 +0900 Subject: [PATCH 1/3] Parse pleroma custom emoji reactions in notifications --- examples/firefish_notifications.rs | 41 ++++++++++++++++++++++++++ src/entities/notification.rs | 6 ---- src/entities/reaction.rs | 1 + src/firefish/entities/notification.rs | 16 ++++------ src/firefish/entities/reaction.rs | 1 + src/friendica/entities/notification.rs | 1 - src/mastodon/entities/notification.rs | 1 - src/pleroma/entities/notification.rs | 24 ++++++++++++--- src/pleroma/entities/reaction.rs | 16 ++++++---- 9 files changed, 79 insertions(+), 28 deletions(-) create mode 100644 examples/firefish_notifications.rs diff --git a/examples/firefish_notifications.rs b/examples/firefish_notifications.rs new file mode 100644 index 0000000..c38a9bd --- /dev/null +++ b/examples/firefish_notifications.rs @@ -0,0 +1,41 @@ +use std::env; + +use megalodon::{entities, error, generator}; + +#[tokio::main] +async fn main() { + env_logger::init(); + + let Ok(url) = env::var("FIREFISH_URL") else { + println!("Specify FIREFISH_URL!!"); + return; + }; + let Ok(token) = env::var("FIREFISH_ACCESS_TOKEN") else { + println!("Specify FIREFISH_ACCESS_TOKEN!!"); + return; + }; + + let res = get_notifications(url.as_str(), token).await; + match res { + Ok(res) => { + println!("{:#?}", res); + } + Err(err) => { + println!("{:#?}", err); + } + } +} + +async fn get_notifications( + url: &str, + access_token: String, +) -> Result, error::Error> { + let client = generator( + megalodon::SNS::Firefish, + url.to_string(), + Some(access_token), + None, + ); + let res = client.get_notifications(None).await?; + Ok(res.json()) +} diff --git a/src/entities/notification.rs b/src/entities/notification.rs index 354fa4f..5f1a2eb 100644 --- a/src/entities/notification.rs +++ b/src/entities/notification.rs @@ -11,7 +11,6 @@ pub struct Notification { pub created_at: DateTime, pub id: String, pub status: Option, - pub emoji: Option, pub reaction: Option, pub target: Option, pub r#type: NotificationType, @@ -28,9 +27,6 @@ pub enum NotificationType { PollVote, PollExpired, Status, - // EmojiReaction contains only emoji as string. - EmojiReaction, - // Reaction contains reaction object instead of emoji. Reaction, Update, Move, @@ -52,7 +48,6 @@ impl fmt::Display for NotificationType { NotificationType::PollExpired => write!(f, "poll_expired"), NotificationType::FollowRequest => write!(f, "follow_request"), NotificationType::Status => write!(f, "status"), - NotificationType::EmojiReaction => write!(f, "emoji_reaction"), NotificationType::Reaction => write!(f, "reaction"), NotificationType::Update => write!(f, "update"), NotificationType::Move => write!(f, "move"), @@ -77,7 +72,6 @@ impl FromStr for NotificationType { "poll_vote" => Ok(NotificationType::PollVote), "follow_request" => Ok(NotificationType::FollowRequest), "status" => Ok(NotificationType::Status), - "emoji_reaction" => Ok(NotificationType::EmojiReaction), "reaction" => Ok(NotificationType::Reaction), "update" => Ok(NotificationType::Update), "move" => Ok(NotificationType::Move), diff --git a/src/entities/reaction.rs b/src/entities/reaction.rs index 9680832..20baa70 100644 --- a/src/entities/reaction.rs +++ b/src/entities/reaction.rs @@ -10,4 +10,5 @@ pub struct Reaction { pub url: Option, pub static_url: Option, pub accounts: Option>, + pub account_ids: Option>, } diff --git a/src/firefish/entities/notification.rs b/src/firefish/entities/notification.rs index 9423593..e524473 100644 --- a/src/firefish/entities/notification.rs +++ b/src/firefish/entities/notification.rs @@ -17,7 +17,7 @@ pub struct Notification { // user_id: Option, user: Option, note: Option, - reaction: String, + reaction: Option, } #[derive(Debug, Clone, Deserialize, Serialize, PartialEq)] @@ -64,9 +64,6 @@ impl From for NotificationTyp MegalodonEntities::notification::NotificationType::Follow => NotificationType::Follow, MegalodonEntities::notification::NotificationType::Mention => NotificationType::Mention, MegalodonEntities::notification::NotificationType::Reblog => NotificationType::Renote, - MegalodonEntities::notification::NotificationType::EmojiReaction => { - NotificationType::Reaction - } MegalodonEntities::notification::NotificationType::Reaction => { NotificationType::Reaction } @@ -139,11 +136,11 @@ impl Into for Notification { } else { [].to_vec() }; - let reactions = map_reaction( - emojis, - HashMap::::from([(self.reaction, 1)]), - None, - ); + let reactions = if let Some(reaction) = self.reaction { + map_reaction(emojis, HashMap::::from([(reaction, 1)]), None) + } else { + [].to_vec() + }; let reaction = if reactions.len() > 0 { Some(reactions[0].clone()) } else { @@ -154,7 +151,6 @@ impl Into for Notification { created_at: self.created_at, id: self.id, status: self.note.map(|n| n.into()), - emoji: None, reaction, target: None, r#type: self.r#type.into(), diff --git a/src/firefish/entities/reaction.rs b/src/firefish/entities/reaction.rs index 5d5661e..fbcd942 100644 --- a/src/firefish/entities/reaction.rs +++ b/src/firefish/entities/reaction.rs @@ -38,6 +38,7 @@ pub(crate) fn map_reaction( url: url.clone(), static_url: url, accounts: None, + account_ids: None, } }) .collect() diff --git a/src/friendica/entities/notification.rs b/src/friendica/entities/notification.rs index fed7480..e163587 100644 --- a/src/friendica/entities/notification.rs +++ b/src/friendica/entities/notification.rs @@ -53,7 +53,6 @@ impl Into for Notification { created_at: self.created_at, id: self.id, status: self.status.map(|i| i.into()), - emoji: None, reaction: None, target: None, r#type: self.r#type.into(), diff --git a/src/mastodon/entities/notification.rs b/src/mastodon/entities/notification.rs index f23f6c7..cf9677a 100644 --- a/src/mastodon/entities/notification.rs +++ b/src/mastodon/entities/notification.rs @@ -63,7 +63,6 @@ impl Into for Notification { created_at: self.created_at, id: self.id, status: self.status.map(|i| i.into()), - emoji: None, reaction: None, target: None, r#type: self.r#type.into(), diff --git a/src/pleroma/entities/notification.rs b/src/pleroma/entities/notification.rs index 9225446..a43a5f5 100644 --- a/src/pleroma/entities/notification.rs +++ b/src/pleroma/entities/notification.rs @@ -1,4 +1,4 @@ -use super::{Account, Status}; +use super::{Account, Reaction, Status}; use crate::error::{Error, Kind}; use crate::entities as MegalodonEntities; @@ -15,9 +15,25 @@ pub struct Notification { status: Option, r#type: NotificationType, emoji: Option, + emoji_url: Option, target: Option, } +impl Notification { + fn map_reaction(&self) -> Option { + let shortcode = self.emoji.clone()?; + let name = shortcode.replace(":", ""); + Some(Reaction { + count: 1, + me: false, + name, + url: self.emoji_url.clone(), + accounts: None, + account_ids: None, + }) + } +} + #[derive(Debug, Clone)] pub enum NotificationType { Follow, @@ -104,7 +120,7 @@ impl Into for NotificationTyp MegalodonEntities::notification::NotificationType::PollExpired } NotificationType::PleromaEmojiReaction => { - MegalodonEntities::notification::NotificationType::EmojiReaction + MegalodonEntities::notification::NotificationType::Reaction } NotificationType::Update => MegalodonEntities::notification::NotificationType::Update, NotificationType::Move => MegalodonEntities::notification::NotificationType::Move, @@ -114,13 +130,13 @@ impl Into for NotificationTyp impl Into for Notification { fn into(self) -> MegalodonEntities::Notification { + let reaction = self.clone().map_reaction(); MegalodonEntities::Notification { account: Some(self.account.into()), created_at: self.created_at, id: self.id, status: self.status.map(|i| i.into()), - emoji: self.emoji, - reaction: None, + reaction: reaction.map(|i| i.into()), target: self.target.map(|i| i.into()), r#type: self.r#type.into(), } diff --git a/src/pleroma/entities/reaction.rs b/src/pleroma/entities/reaction.rs index 320ed7d..e184781 100644 --- a/src/pleroma/entities/reaction.rs +++ b/src/pleroma/entities/reaction.rs @@ -4,10 +4,12 @@ use serde::Deserialize; #[derive(Debug, Deserialize, Clone)] pub struct Reaction { - count: u32, - me: bool, - name: String, - accounts: Option>, + pub count: u32, + pub me: bool, + pub name: String, + pub accounts: Option>, + pub account_ids: Option>, + pub url: Option, } impl Into for Reaction { @@ -16,10 +18,12 @@ impl Into for Reaction { count: self.count, me: self.me, name: self.name, - url: None, - static_url: None, + url: self.url.clone(), + static_url: self.url, + account_ids: self.account_ids, accounts: self .accounts + .clone() .map(|i| i.into_iter().map(|a| a.into()).collect()), } } From a0240516ee3003e871df4334951ab397dca8b044 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Tue, 6 Feb 2024 23:09:46 +0900 Subject: [PATCH 2/3] Fix tests for reaction --- src/firefish/entities/reaction.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/firefish/entities/reaction.rs b/src/firefish/entities/reaction.rs index fbcd942..3a862d8 100644 --- a/src/firefish/entities/reaction.rs +++ b/src/firefish/entities/reaction.rs @@ -94,6 +94,7 @@ mod test { url: Some(String::from("https://example.com/files/ablobcatnodfast")), static_url: Some(String::from("https://example.com/files/ablobcatnodfast")), accounts: None, + account_ids: None, }, ); @@ -115,6 +116,7 @@ mod test { "https://example.com/proxy/firefishexample/kawaii" )), accounts: None, + account_ids: None, } ); } @@ -144,6 +146,7 @@ mod test { url: None, static_url: None, accounts: None, + account_ids: None, }, ); @@ -161,6 +164,7 @@ mod test { url: None, static_url: None, accounts: None, + account_ids: None, } ); } @@ -211,6 +215,7 @@ mod test { url: Some(String::from("https://example.com/files/ablobcatnodfast")), static_url: Some(String::from("https://example.com/files/ablobcatnodfast")), accounts: None, + account_ids: None, }, ); @@ -232,6 +237,7 @@ mod test { "https://example.com/proxy/firefishexample/kawaii" )), accounts: None, + account_ids: None, } ); } From 9d4cba8a0ef7f8968f7da9b9cbb6e74982cecb36 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Tue, 6 Feb 2024 23:24:02 +0900 Subject: [PATCH 3/3] Fix firefish test url --- src/detector.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/detector.rs b/src/detector.rs index b01dff3..24c3975 100644 --- a/src/detector.rs +++ b/src/detector.rs @@ -226,7 +226,7 @@ mod tests { #[tokio::test] async fn test_detector_firefish() { - let sns = detector("https://calckey.jp").await; + let sns = detector("https://calckey.world").await; assert!(sns.is_ok()); assert_eq!(sns.unwrap(), SNS::Firefish);