Removed more references to websockets

This commit is contained in:
D. Scott Boggs 2022-12-22 12:27:30 -05:00
parent 0f268f7d6f
commit ca7f9c86a6
4 changed files with 22 additions and 34 deletions

View File

@ -45,14 +45,15 @@ use elefren::prelude::*;
use elefren::helpers::toml; // requires `features = ["toml"]` use elefren::helpers::toml; // requires `features = ["toml"]`
use elefren::helpers::cli; use elefren::helpers::cli;
fn main() -> Result<(), Box<dyn Error>> { #[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") { let mastodon = if let Ok(data) = toml::from_file("mastodon-data.toml") {
Mastodon::from(data) Mastodon::from(data)
} else { } else {
register()? register()?
}; };
let you = mastodon.verify_credentials()?; let you = mastodon.verify_credentials().await?;
println!("{:#?}", you); println!("{:#?}", you);
@ -80,25 +81,21 @@ use elefren::entities::event::Event;
use std::error::Error; use std::error::Error;
fn main() -> Result<(), Box<Error>> { #[tokio::main]
let data = Data { async fn main() -> Result<(), Box<Error>> {
base: "".into(), let client = Mastodon::from(Data::default());
client_id: "".into(),
client_secret: "".into(),
redirect: "".into(),
token: "".into(),
};
let client = Mastodon::from(data); client.stream_user()
.await?
for event in client.streaming_user()? { .try_for_each(|event| {
match event { match event {
Event::Update(ref status) => { /* .. */ }, Event::Update(ref status) => { /* .. */ },
Event::Notification(ref notification) => { /* .. */ }, Event::Notification(ref notification) => { /* .. */ },
Event::Delete(ref id) => { /* .. */ }, Event::Delete(ref id) => { /* .. */ },
Event::FiltersChanged => { /* .. */ }, Event::FiltersChanged => { /* .. */ },
} }
} })
.await?;
Ok(()) Ok(())
} }
``` ```

View File

@ -12,7 +12,6 @@ use serde_urlencoded::ser::Error as UrlEncodedError;
use tomlcrate::de::Error as TomlDeError; use tomlcrate::de::Error as TomlDeError;
#[cfg(feature = "toml")] #[cfg(feature = "toml")]
use tomlcrate::ser::Error as TomlSerError; use tomlcrate::ser::Error as TomlSerError;
use tungstenite::{error::Error as WebSocketError, Message as WebSocketMessage};
use url::ParseError as UrlError; use url::ParseError as UrlError;
/// Convience type over `std::result::Result` with `Error` as the error type. /// Convience type over `std::result::Result` with `Error` as the error type.
@ -62,16 +61,12 @@ pub enum Error {
Envy(EnvyError), Envy(EnvyError),
/// Error serializing to a query string /// Error serializing to a query string
SerdeQs(SerdeQsError), SerdeQs(SerdeQsError),
/// WebSocket error
WebSocket(WebSocketError),
/// An integer conversion was attempted, but the value didn't fit into the /// An integer conversion was attempted, but the value didn't fit into the
/// target type. /// target type.
/// ///
/// At the time of writing, this can only be triggered when a file is /// At the time of writing, this can only be triggered when a file is
/// larger than the system's usize allows. /// larger than the system's usize allows.
IntConversion(TryFromIntError), IntConversion(TryFromIntError),
/// A stream message was received that wasn't recognized
UnrecognizedStreamMessage(WebSocketMessage),
/// Other errors /// Other errors
Other(String), Other(String),
} }
@ -100,14 +95,12 @@ impl error::Error for Error {
#[cfg(feature = "env")] #[cfg(feature = "env")]
Error::Envy(ref e) => e, Error::Envy(ref e) => e,
Error::SerdeQs(ref e) => e, Error::SerdeQs(ref e) => e,
Error::WebSocket(ref e) => e,
Error::IntConversion(ref e) => e, Error::IntConversion(ref e) => e,
Error::Client(..) | Error::Server(..) => return None, Error::Client(..) | Error::Server(..) => return None,
Error::ClientIdRequired => return None, Error::ClientIdRequired => return None,
Error::ClientSecretRequired => return None, Error::ClientSecretRequired => return None,
Error::AccessTokenRequired => return None, Error::AccessTokenRequired => return None,
Error::MissingField(_) => return None, Error::MissingField(_) => return None,
Error::UnrecognizedStreamMessage(_) => return None,
Error::Other(..) => return None, Error::Other(..) => return None,
}) })
} }
@ -157,10 +150,8 @@ from! {
HeaderParseError => HeaderParseError, HeaderParseError => HeaderParseError,
#[cfg(feature = "env")] EnvyError => Envy, #[cfg(feature = "env")] EnvyError => Envy,
SerdeQsError => SerdeQs, SerdeQsError => SerdeQs,
WebSocketError => WebSocket,
String => Other, String => Other,
TryFromIntError => IntConversion, TryFromIntError => IntConversion,
WebSocketMessage => UnrecognizedStreamMessage,
} }
#[macro_export] #[macro_export]

View File

@ -14,7 +14,7 @@ use tokio_util::io::StreamReader;
/// Return a stream of events from the given response by parsing Server-Sent /// Return a stream of events from the given response by parsing Server-Sent
/// Events as they come in. /// Events as they come in.
/// ///
/// See https://docs.joinmastodon.org/methods/streaming/ for more info /// See <https://docs.joinmastodon.org/methods/streaming/> for more info
pub fn event_stream( pub fn event_stream(
response: Response, response: Response,
location: String, location: String,
@ -28,14 +28,14 @@ pub fn event_stream(
let (ref mut lines_iter, ref location) = this; let (ref mut lines_iter, ref location) = this;
let mut lines = vec![]; let mut lines = vec![];
while let Some(line) = lines_iter.next_line().await? { while let Some(line) = lines_iter.next_line().await? {
debug!(message = line, location = &location; "received websocket message"); debug!(message = line, location = &location; "received message");
let line = line.trim().to_string(); let line = line.trim().to_string();
if line.starts_with(":") || line.is_empty() { if line.starts_with(":") || line.is_empty() {
continue; continue;
} }
lines.push(line); lines.push(line);
if let Ok(event) = make_event(&lines) { if let Ok(event) = make_event(&lines) {
info!(event = as_serde!(event), location = location; "received websocket event"); info!(event = as_serde!(event), location = location; "received event");
lines.clear(); lines.clear();
return Ok(Some((event, this))); return Ok(Some((event, this)));
} else { } else {
@ -66,7 +66,7 @@ fn make_event(lines: &[String]) -> Result<Event> {
data = message.payload; data = message.payload;
} }
let event: &str = &event; let event: &str = &event;
trace!(event = event, payload = data; "websocket message parsed"); trace!(event = event, payload = data; "SSE message parsed");
Ok(match event { Ok(match event {
"notification" => { "notification" => {
let data = data let data = data

View File

@ -39,7 +39,7 @@
//! let data = Data::default(); //! let data = Data::default();
//! let client = Mastodon::from(data); //! let client = Mastodon::from(data);
//! tokio_test::block_on(async { //! tokio_test::block_on(async {
//! let stream = client.streaming_user().await.unwrap(); //! let stream = client.stream_user().await.unwrap();
//! stream.try_for_each(|event| async move { //! stream.try_for_each(|event| async move {
//! match event { //! match event {
//! Event::Update(ref status) => { /* .. */ }, //! Event::Update(ref status) => { /* .. */ },