Skip to content

Commit 7324a6c

Browse files
committed
Fix stream urls
1 parent ae3c9ae commit 7324a6c

File tree

2 files changed

+57
-10
lines changed

2 files changed

+57
-10
lines changed

src/macros.rs

+52-1
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ tokio_test::block_on(async {
578578
),
579579
pub async fn $fn_name(&self, $param: $param_type) -> Result<impl TryStream<Ok=(Event, Mastodon), Error=Error> + '_> {
580580
use $crate::event_stream::event_stream;
581-
let mut url: Url = self.route(concat!("/api/v1/streaming/", stringify!($stream))).parse()?;
581+
let mut url: Url = self.route(concat!("/api/v1/streaming/", $stream)).parse()?;
582582
url.query_pairs_mut().append_pair(stringify!($param), $param.as_ref());
583583
let url = url.to_string();
584584
let response = self.authenticated(self.client.get(url.as_str())).header("Accept", "application/json").send().await?;
@@ -598,5 +598,56 @@ tokio_test::block_on(async {
598598
}
599599
streaming! { $($rest)* }
600600
};
601+
($desc:tt $fn_name:ident(flag $param:ident)@$stream:literal, $($rest:tt)*) => {
602+
doc_comment! {
603+
concat!(
604+
$desc,
605+
"\n\nExample:\n\n",
606+
"
607+
use mastodon_async::prelude::*;
608+
use mastodon_async::entities::event::Event;
609+
use futures_util::{pin_mut, StreamExt, TryStreamExt};
610+
611+
tokio_test::block_on(async {
612+
let data = Data::default();
613+
let client = Mastodon::from(data);
614+
let stream = client.",
615+
stringify!($fn_name),
616+
"(false).await.unwrap();
617+
stream.try_for_each(|event| async move {
618+
match event {
619+
Event::Update(ref status) => { /* .. */ },
620+
Event::Notification(ref notification) => { /* .. */ },
621+
Event::Delete(ref id) => { /* .. */ },
622+
Event::FiltersChanged => { /* .. */ },
623+
}
624+
Ok(())
625+
}).await.unwrap();
626+
});"
627+
),
628+
pub async fn $fn_name(&self, $param: bool) -> Result<impl TryStream<Ok=(Event, Mastodon), Error=Error> + '_> {
629+
use $crate::event_stream::event_stream;
630+
let mut url: Url = self.route(concat!("/api/v1/streaming/", $stream)).parse()?;
631+
if $param {
632+
url.query_pairs_mut().append_key_only(stringify!($param));
633+
}
634+
let url = url.to_string();
635+
let response = self.authenticated(self.client.get(url.as_str())).header("Accept", "application/json").send().await?;
636+
debug!(
637+
status = log_serde!(response Status), url = as_debug!(url),
638+
headers = log_serde!(response Headers);
639+
"received API response"
640+
);
641+
let status = response.status();
642+
if status.is_success() {
643+
Ok(event_stream(response, url, self))
644+
} else {
645+
let response = response.json().await?;
646+
Err(Error::Api{ status, response })
647+
}
648+
}
649+
}
650+
streaming! { $($rest)* }
651+
};
601652
() => {}
602653
}

src/mastodon.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,17 @@ impl Mastodon {
130130
"All public posts known to the server. Analogous to the federated timeline."
131131
stream_public@"public",
132132
"All public posts known to the server, filtered for media attachments. Analogous to the federated timeline with 'only media' enabled."
133-
stream_public_media@"public:media",
133+
stream_public_media@"public/media",
134134
"All public posts originating from this server."
135-
stream_local@"public:local",
136-
"All public posts originating from this server, filtered for media attachments. Analogous to the local timeline with 'only media' enabled."
137-
stream_local_media@"public:local:media",
135+
stream_local(flag only_media)@"public/local",
138136
"All public posts originating from other servers."
139-
stream_remote@"public:remote",
140-
"All public posts originating from other servers, filtered for media attachments."
141-
stream_remote_media@"public:remote:media",
137+
stream_remote(flag only_media)@"public/remote",
142138
"All public posts using a certain hashtag."
143139
stream_hashtag(tag: impl AsRef<str>, like "#bots")@"hashtag",
144140
"All public posts using a certain hashtag, originating from this server."
145-
stream_local_hashtag(tag: impl AsRef<str>, like "#bots")@"hashtag:local",
141+
stream_local_hashtag(tag: impl AsRef<str>, like "#bots")@"hashtag/local",
146142
"Notifications for the current user."
147-
stream_notifications@"user:notification",
143+
stream_notifications@"user/notification",
148144
"Updates to a specific list."
149145
stream_list(list: impl AsRef<str>, like "12345")@"list",
150146
"Updates to direct conversations."

0 commit comments

Comments
 (0)