Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse pleroma custom emoji reactions in notifications #189

Merged
merged 3 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/firefish_notifications.rs
Original file line number Diff line number Diff line change
@@ -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<Vec<entities::Notification>, 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())
}
2 changes: 1 addition & 1 deletion src/detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 0 additions & 6 deletions src/entities/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ pub struct Notification {
pub created_at: DateTime<Utc>,
pub id: String,
pub status: Option<Status>,
pub emoji: Option<String>,
pub reaction: Option<Reaction>,
pub target: Option<Account>,
pub r#type: NotificationType,
Expand All @@ -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,
Expand All @@ -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"),
Expand All @@ -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),
Expand Down
1 change: 1 addition & 0 deletions src/entities/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ pub struct Reaction {
pub url: Option<String>,
pub static_url: Option<String>,
pub accounts: Option<Vec<Account>>,
pub account_ids: Option<Vec<String>>,
}
16 changes: 6 additions & 10 deletions src/firefish/entities/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Notification {
// user_id: Option<String>,
user: Option<User>,
note: Option<Note>,
reaction: String,
reaction: Option<String>,
}

#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
Expand Down Expand Up @@ -64,9 +64,6 @@ impl From<MegalodonEntities::notification::NotificationType> 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
}
Expand Down Expand Up @@ -139,11 +136,11 @@ impl Into<MegalodonEntities::Notification> for Notification {
} else {
[].to_vec()
};
let reactions = map_reaction(
emojis,
HashMap::<String, u32>::from([(self.reaction, 1)]),
None,
);
let reactions = if let Some(reaction) = self.reaction {
map_reaction(emojis, HashMap::<String, u32>::from([(reaction, 1)]), None)
} else {
[].to_vec()
};
let reaction = if reactions.len() > 0 {
Some(reactions[0].clone())
} else {
Expand All @@ -154,7 +151,6 @@ impl Into<MegalodonEntities::Notification> 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(),
Expand Down
7 changes: 7 additions & 0 deletions src/firefish/entities/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub(crate) fn map_reaction(
url: url.clone(),
static_url: url,
accounts: None,
account_ids: None,
}
})
.collect()
Expand Down Expand Up @@ -93,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,
},
);

Expand All @@ -114,6 +116,7 @@ mod test {
"https://example.com/proxy/firefishexample/kawaii"
)),
accounts: None,
account_ids: None,
}
);
}
Expand Down Expand Up @@ -143,6 +146,7 @@ mod test {
url: None,
static_url: None,
accounts: None,
account_ids: None,
},
);

Expand All @@ -160,6 +164,7 @@ mod test {
url: None,
static_url: None,
accounts: None,
account_ids: None,
}
);
}
Expand Down Expand Up @@ -210,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,
},
);

Expand All @@ -231,6 +237,7 @@ mod test {
"https://example.com/proxy/firefishexample/kawaii"
)),
accounts: None,
account_ids: None,
}
);
}
Expand Down
1 change: 0 additions & 1 deletion src/friendica/entities/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl Into<MegalodonEntities::Notification> 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(),
Expand Down
1 change: 0 additions & 1 deletion src/mastodon/entities/notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ impl Into<MegalodonEntities::Notification> 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(),
Expand Down
24 changes: 20 additions & 4 deletions src/pleroma/entities/notification.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Account, Status};
use super::{Account, Reaction, Status};
use crate::error::{Error, Kind};

use crate::entities as MegalodonEntities;
Expand All @@ -15,9 +15,25 @@ pub struct Notification {
status: Option<Status>,
r#type: NotificationType,
emoji: Option<String>,
emoji_url: Option<String>,
target: Option<Account>,
}

impl Notification {
fn map_reaction(&self) -> Option<Reaction> {
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,
Expand Down Expand Up @@ -104,7 +120,7 @@ impl Into<MegalodonEntities::notification::NotificationType> 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,
Expand All @@ -114,13 +130,13 @@ impl Into<MegalodonEntities::notification::NotificationType> for NotificationTyp

impl Into<MegalodonEntities::Notification> 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(),
}
Expand Down
16 changes: 10 additions & 6 deletions src/pleroma/entities/reaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use serde::Deserialize;

#[derive(Debug, Deserialize, Clone)]
pub struct Reaction {
count: u32,
me: bool,
name: String,
accounts: Option<Vec<Account>>,
pub count: u32,
pub me: bool,
pub name: String,
pub accounts: Option<Vec<Account>>,
pub account_ids: Option<Vec<String>>,
pub url: Option<String>,
}

impl Into<MegalodonEntities::Reaction> for Reaction {
Expand All @@ -16,10 +18,12 @@ impl Into<MegalodonEntities::Reaction> 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()),
}
}
Expand Down