Add methods for remaining streams

This commit is contained in:
D. Scott Boggs 2022-12-18 18:00:58 -05:00
parent c5141972e4
commit 334e620d3c
2 changed files with 75 additions and 20 deletions

View File

@ -440,13 +440,12 @@ macro_rules! paged_routes_with_id {
} }
macro_rules! streaming { macro_rules! streaming {
{$($stream:ident@$fn_name:ident ($desc:tt),)*} => { ($stream:literal@$fn_name:ident ($desc:tt), $($rest:tt)*) => {
$( doc_comment! {
doc_comment! { concat!(
concat!( $desc,
$desc, "\n\nExample:\n\n",
"\n\nExample:\n\n", "
"
use elefren::prelude::*; use elefren::prelude::*;
use elefren::entities::event::Event; use elefren::entities::event::Event;
use futures_util::{pin_mut, StreamExt, TryStreamExt}; use futures_util::{pin_mut, StreamExt, TryStreamExt};
@ -467,18 +466,63 @@ tokio_test::block_on(async {
Ok(()) Ok(())
}).await.unwrap(); }).await.unwrap();
});" });"
), ),
pub async fn $fn_name(&self) -> Result<impl TryStream<Ok=Event, Error=Error>> { pub async fn $fn_name(&self) -> Result<impl TryStream<Ok=Event, Error=Error>> {
let url = self.route(concat!("/api/v1/streaming/", stringify!($stream))); let url = self.route(concat!("/api/v1/streaming/", stringify!($stream)));
let response = self.authenticated(self.client.get(&url)).send().await?; let response = self.authenticated(self.client.get(&url)).send().await?;
debug!( debug!(
status = log_serde!(response Status), url = &url, status = log_serde!(response Status), url = &url,
headers = log_serde!(response Headers); headers = log_serde!(response Headers);
"received API response" "received API response"
); );
Ok(event_stream(response.error_for_status()?, url)) 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)* }
};
() => {}
} }

View File

@ -130,7 +130,18 @@ impl Mastodon {
} }
streaming! { 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 /// Create a new Mastodon Client