summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorD. Scott Boggs <scott@tams.tech>2022-12-18 18:00:58 -0500
committerD. Scott Boggs <scott@tams.tech>2022-12-18 18:00:58 -0500
commit334e620d3cb08963b84e3e2895479e707827356f (patch)
tree299e6c23336ef0e73e6409041b8d58b8a6a9e878
parentc5141972e405e30ec333a7a8768a6a33f4e242a1 (diff)
Add methods for remaining streams
-rw-r--r--src/macros.rs82
-rw-r--r--src/mastodon.rs13
2 files changed, 75 insertions, 20 deletions
diff --git a/src/macros.rs b/src/macros.rs
index 2446143..18f14c7 100644
--- a/src/macros.rs
+++ b/src/macros.rs
@@ -440,13 +440,12 @@ macro_rules! paged_routes_with_id {
}
macro_rules! streaming {
- {$($stream:ident@$fn_name:ident ($desc:tt),)*} => {
- $(
- doc_comment! {
- concat!(
- $desc,
- "\n\nExample:\n\n",
- "
+ ($stream:literal@$fn_name:ident ($desc:tt), $($rest:tt)*) => {
+ doc_comment! {
+ concat!(
+ $desc,
+ "\n\nExample:\n\n",
+ "
use elefren::prelude::*;
use elefren::entities::event::Event;
use futures_util::{pin_mut, StreamExt, TryStreamExt};
@@ -467,18 +466,63 @@ tokio_test::block_on(async {
Ok(())
}).await.unwrap();
});"
- ),
- pub async fn $fn_name(&self) -> Result<impl TryStream<Ok=Event, Error=Error>> {
- let url = self.route(concat!("/api/v1/streaming/", stringify!($stream)));
- let response = self.authenticated(self.client.get(&url)).send().await?;
- debug!(
- status = log_serde!(response Status), url = &url,
- headers = log_serde!(response Headers);
- "received API response"
- );
- Ok(event_stream(response.error_for_status()?, url))
- }
+ ),
+ pub async fn $fn_name(&self) -> Result<impl TryStream<Ok=Event, Error=Error>> {
+ let url = self.route(concat!("/api/v1/streaming/", stringify!($stream)));
+ let response = self.authenticated(self.client.get(&url)).send().await?;
+ debug!(
+ status = log_serde!(response Status), url = &url,
+ headers = log_serde!(response Headers);
+ "received API response"
+ );
+ Ok(event_stream(response.error_for_status()?, url))
+ }
+ }
+ streaming! { $($rest)* }
+ };
+ ($stream:literal($param:ident: $param_type:ty, like $param_doc_val:literal)@$fn_name:ident ($desc:tt), $($rest:tt)*) => {
+ doc_comment! {
+ concat!(
+ $desc,
+ "\n\nExample:\n\n",
+ "
+use elefren::prelude::*;
+use elefren::entities::event::Event;
+use futures_util::{pin_mut, StreamExt, TryStreamExt};
+
+tokio_test::block_on(async {
+ let data = Data::default();
+ let client = Mastodon::from(data);
+ let stream = client.",
+ stringify!($fn_name),
+ "(",
+ $param_doc_val,
+ ").await.unwrap();
+ stream.try_for_each(|event| async move {
+ match event {
+ Event::Update(ref status) => { /* .. */ },
+ Event::Notification(ref notification) => { /* .. */ },
+ Event::Delete(ref id) => { /* .. */ },
+ Event::FiltersChanged => { /* .. */ },
+ }
+ Ok(())
+ }).await.unwrap();
+});"
+ ),
+ pub async fn $fn_name(&self, $param: $param_type) -> Result<impl TryStream<Ok=Event, Error=Error>> {
+ let mut url: Url = self.route(concat!("/api/v1/streaming/", stringify!($stream))).parse()?;
+ url.query_pairs_mut().append_pair(stringify!($param), $param.as_ref());
+ let url = url.to_string();
+ let response = self.authenticated(self.client.get(url.as_str())).send().await?;
+ debug!(
+ status = log_serde!(response Status), url = as_debug!(url),
+ headers = log_serde!(response Headers);
+ "received API response"
+ );
+ Ok(event_stream(response.error_for_status()?, url))
}
- )*
+ }
+ streaming! { $($rest)* }
};
+ () => {}
}
diff --git a/src/mastodon.rs b/src/mastodon.rs
index 83152f1..5182281 100644
--- a/src/mastodon.rs
+++ b/src/mastodon.rs
@@ -130,7 +130,18 @@ impl Mastodon {
}
streaming! {
- user@stream_user ("returns events that are relevant to the authorized user, i.e. home timeline & notifications"),
+ "user"@stream_user ("returns events that are relevant to the authorized user, i.e. home timeline & notifications"),
+ "public"@stream_public ("All public posts known to the server. Analogous to the federated timeline."),
+ "public:media"@stream_public_media ("All public posts known to the server, filtered for media attachments. Analogous to the federated timeline with 'only media' enabled."),
+ "public:local"@stream_local ("All public posts originating from this server."),
+ "public:local:media"@stream_local_media ("All public posts originating from this server, filtered for media attachments. Analogous to the local timeline with 'only media' enabled."),
+ "public:remote"@stream_remote ("All public posts originating from other servers."),
+ "public:remote:media"@stream_remote_media ("All public posts originating from other servers, filtered for media attachments."),
+ "hashtag"(tag: impl AsRef<str>, like "#bots")@stream_hashtag ("All public posts using a certain hashtag."),
+ "hashtag:local"(tag: impl AsRef<str>, like "#bots")@stream_local_hashtag ("All public posts using a certain hashtag, originating from this server."),
+ "user:notification"@stream_notifications ("Notifications for the current user."),
+ "list"(list: impl AsRef<str>, like "12345")@stream_list ("Updates to a specific list."),
+ "direct"@stream_direct ("Updates to direct conversations."),
}
/// Create a new Mastodon Client