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> { - 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> { + 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> { + 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, like "#bots")@stream_hashtag ("All public posts using a certain hashtag."), + "hashtag:local"(tag: impl AsRef, 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, like "12345")@stream_list ("Updates to a specific list."), + "direct"@stream_direct ("Updates to direct conversations."), } /// Create a new Mastodon Client