Skip to content

Commit

Permalink
refs #90 Add announcement endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
h3poteto committed May 29, 2023
1 parent 9c370b4 commit 3edba5d
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 1 deletion.
46 changes: 46 additions & 0 deletions src/friendica/friendica.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2582,6 +2582,52 @@ impl megalodon::Megalodon for Friendica {
))
}

async fn get_instance_announcements(
&self,
) -> Result<Response<Vec<MegalodonEntities::Announcement>>, Error> {
Ok(Response::<Vec<MegalodonEntities::Announcement>>::new(
[].to_vec(),
200,
"200".to_string(),
HeaderMap::new(),
))
}

async fn dismiss_instance_announcement(&self, _id: String) -> Result<Response<()>, Error> {
Err(Error::new_own(
"Friendica doest not support".to_string(),
error::Kind::NoImplementedError,
None,
None,
))
}

async fn add_reaction_to_announcement(
&self,
_id: String,
_name: String,
) -> Result<Response<()>, Error> {
Err(Error::new_own(
"Friendica doest not support".to_string(),
error::Kind::NoImplementedError,
None,
None,
))
}

async fn remove_reaction_from_announcement(
&self,
_id: String,
_name: String,
) -> Result<Response<()>, Error> {
Err(Error::new_own(
"Friendica doest not support".to_string(),
error::Kind::NoImplementedError,
None,
None,
))
}

async fn create_emoji_reaction(
&self,
_id: String,
Expand Down
81 changes: 81 additions & 0 deletions src/mastodon/mastodon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2870,6 +2870,87 @@ impl megalodon::Megalodon for Mastodon {
))
}

async fn get_instance_announcements(
&self,
) -> Result<Response<Vec<MegalodonEntities::Announcement>>, Error> {
let res = self
.client
.get::<Vec<entities::Announcement>>("/api/v1/announcements", None)
.await?;

Ok(Response::<Vec<MegalodonEntities::Announcement>>::new(
res.json.into_iter().map(|j| j.into()).collect(),
res.status,
res.status_text,
res.header,
))
}

async fn dismiss_instance_announcement(&self, id: String) -> Result<Response<()>, Error> {
let params = HashMap::<&str, Value>::new();
let res = self
.client
.post::<()>(
format!("/api/v1/announcements/{}/dismiss", id).as_str(),
&params,
None,
)
.await?;

Ok(Response::<()>::new(
(),
res.status,
res.status_text,
res.header,
))
}

async fn add_reaction_to_announcement(
&self,
id: String,
name: String,
) -> Result<Response<()>, Error> {
let params = HashMap::<&str, Value>::new();
let res = self
.client
.put::<()>(
format!("/api/v1/announcements/{}/reactions/{}", id, name).as_str(),
&params,
None,
)
.await?;

Ok(Response::<()>::new(
(),
res.status,
res.status_text,
res.header,
))
}

async fn remove_reaction_from_announcement(
&self,
id: String,
name: String,
) -> Result<Response<()>, Error> {
let params = HashMap::<&str, Value>::new();
let res = self
.client
.delete::<()>(
format!("/api/v1/announcements/{}/reactions/{}", id, name).as_str(),
&params,
None,
)
.await?;

Ok(Response::<()>::new(
(),
res.status,
res.status_text,
res.header,
))
}

async fn create_emoji_reaction(
&self,
_id: String,
Expand Down
25 changes: 25 additions & 0 deletions src/megalodon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,31 @@ pub trait Megalodon {
/// Returns custom emojis that are available on the server.
async fn get_instance_custom_emojis(&self) -> Result<Response<Vec<entities::Emoji>>, Error>;

// ======================================
// instance/announcements
// ======================================
/// Get all currently active announcements set by admins.
async fn get_instance_announcements(
&self,
) -> Result<Response<Vec<entities::Announcement>>, Error>;

/// Dismiss an announcement
async fn dismiss_instance_announcement(&self, id: String) -> Result<Response<()>, Error>;

/// Add a reaction to an announcement.
async fn add_reaction_to_announcement(
&self,
id: String,
name: String,
) -> Result<Response<()>, Error>;

/// Remove a reaction from an announcement.
async fn remove_reaction_from_announcement(
&self,
id: String,
name: String,
) -> Result<Response<()>, Error>;

// ======================================
// Emoji reactions
// ======================================
Expand Down
98 changes: 98 additions & 0 deletions src/pleroma/entities/announcement.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use super::{status, Emoji};
use crate::entities as MegalodonEntities;
use chrono::{DateTime, Utc};
use serde::Deserialize;

#[derive(Debug, Clone, Deserialize)]
pub struct Announcement {
id: String,
conent: String,
starts_at: Option<DateTime<Utc>>,
ends_at: Option<DateTime<Utc>>,
published: bool,
all_day: bool,
published_at: DateTime<Utc>,
updated_at: DateTime<Utc>,
read: Option<bool>,
mentions: Vec<Account>,
statuses: Vec<Status>,
tags: Vec<status::Tag>,
emojis: Vec<Emoji>,
reactions: Vec<Reaction>,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Account {
id: String,
username: String,
url: String,
acct: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Status {
id: String,
url: String,
}

#[derive(Debug, Clone, Deserialize)]
pub struct Reaction {
name: String,
count: i64,
me: Option<bool>,
url: Option<String>,
static_url: Option<String>,
}

impl Into<MegalodonEntities::Announcement> for Announcement {
fn into(self) -> MegalodonEntities::Announcement {
MegalodonEntities::Announcement {
id: self.id,
conent: self.conent,
starts_at: self.starts_at,
ends_at: self.ends_at,
published: self.published,
all_day: self.all_day,
published_at: self.published_at,
updated_at: self.updated_at,
read: self.read,
mentions: self.mentions.into_iter().map(|i| i.into()).collect(),
statuses: self.statuses.into_iter().map(|i| i.into()).collect(),
tags: self.tags.into_iter().map(|i| i.into()).collect(),
emojis: self.emojis.into_iter().map(|i| i.into()).collect(),
reactions: self.reactions.into_iter().map(|i| i.into()).collect(),
}
}
}

impl Into<MegalodonEntities::announcement::Account> for Account {
fn into(self) -> MegalodonEntities::announcement::Account {
MegalodonEntities::announcement::Account {
id: self.id,
username: self.username,
url: self.url,
acct: self.acct,
}
}
}

impl Into<MegalodonEntities::announcement::Status> for Status {
fn into(self) -> MegalodonEntities::announcement::Status {
MegalodonEntities::announcement::Status {
id: self.id,
url: self.url,
}
}
}

impl Into<MegalodonEntities::announcement::Reaction> for Reaction {
fn into(self) -> MegalodonEntities::announcement::Reaction {
MegalodonEntities::announcement::Reaction {
name: self.name,
count: self.count,
me: self.me,
url: self.url,
static_url: self.static_url,
}
}
}
2 changes: 2 additions & 0 deletions src/pleroma/entities/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod account;
pub mod activity;
pub mod announcement;
pub mod application;
pub mod attachment;
pub mod card;
Expand Down Expand Up @@ -35,6 +36,7 @@ pub mod urls;

pub use account::Account;
pub use activity::Activity;
pub use announcement::Announcement;
pub use application::Application;
pub use attachment::Attachment;
pub use card::Card;
Expand Down
63 changes: 62 additions & 1 deletion src/pleroma/pleroma.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use super::entities;
use super::oauth;
use super::web_socket::WebSocket;
use crate::megalodon::FollowRequestOutput;
use crate::Streaming;
use crate::{
default, entities as MegalodonEntities, error::Error, megalodon, oauth as MegalodonOAuth,
response::Response,
};
use crate::{error, Streaming};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use oauth2::basic::BasicClient;
Expand Down Expand Up @@ -2824,6 +2824,67 @@ impl megalodon::Megalodon for Pleroma {
))
}

async fn get_instance_announcements(
&self,
) -> Result<Response<Vec<MegalodonEntities::Announcement>>, Error> {
let res = self
.client
.get::<Vec<entities::Announcement>>("/api/v1/announcements", None)
.await?;

Ok(Response::<Vec<MegalodonEntities::Announcement>>::new(
res.json.into_iter().map(|j| j.into()).collect(),
res.status,
res.status_text,
res.header,
))
}

async fn dismiss_instance_announcement(&self, id: String) -> Result<Response<()>, Error> {
let params = HashMap::<&str, Value>::new();
let res = self
.client
.post::<()>(
format!("/api/v1/announcements/{}/dismiss", id).as_str(),
&params,
None,
)
.await?;

Ok(Response::<()>::new(
(),
res.status,
res.status_text,
res.header,
))
}

async fn add_reaction_to_announcement(
&self,
_id: String,
_name: String,
) -> Result<Response<()>, Error> {
Err(Error::new_own(
"Pleroma doest not support".to_string(),
error::Kind::NoImplementedError,
None,
None,
))
}

async fn remove_reaction_from_announcement(
&self,
_id: String,
_name: String,
) -> Result<Response<()>, Error> {
Err(Error::new_own(
"Pleroma doest not support".to_string(),
error::Kind::NoImplementedError,
None,
None,
))
}

async fn create_emoji_reaction(
&self,
id: String,
Expand Down

0 comments on commit 3edba5d

Please sign in to comment.