Skip to content
This repository has been archived by the owner on Sep 8, 2022. It is now read-only.

Commit

Permalink
mahoosive refactoring, yay
Browse files Browse the repository at this point in the history
  • Loading branch information
eeeeeta committed Mar 17, 2018
1 parent 2d932a7 commit 47901f2
Show file tree
Hide file tree
Showing 15 changed files with 292 additions and 333 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[workspace]
members = ["./glitch-in-the-matrix", "./gm-types", "./gm-boilerplate"]
members = ["./glitch-in-the-matrix", "./gm-types"]
18 changes: 11 additions & 7 deletions glitch-in-the-matrix/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
[package]
authors = [
"eeeeeta <[email protected]>",
"digital <[email protected]>"
]
authors = ["eeeeeta <[email protected]>", "digital <[email protected]>"]
description = "A set of matrix.org bindings for Rust."
documentation = "http://docs.rs/glitch-in-the-matrix"
homepage = "https://github.com/eeeeeta/glitch-in-the-matrix"
Expand All @@ -11,19 +8,26 @@ license = "CC0-1.0"
name = "glitch-in-the-matrix"
readme = "../README.md"
repository = "https://github.com/eeeeeta/glitch-in-the-matrix"
version = "0.13.1"
version = "0.14.0"

[dependencies]
failure = "0.1"
failure_derive = "0.1"
futures = "0.1"
hyper = "0.11"
hyper-openssl = "0.3"
percent-encoding = "1.0"
serde = "1.0"
serde_json = "1.0"
tokio-core = "0.1"
percent-encoding = "1.0"
gm-types = { path = "../gm-types", version = "0.3.1" }

[dependencies.uuid]
version = "0.6"
features = ["v4"]

[dependencies.gm-types]
path = "../gm-types"
version = "0.4.0"

[dev-dependencies]
rpassword = "0.4.2"
Expand Down
15 changes: 8 additions & 7 deletions glitch-in-the-matrix/examples/echo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ extern crate rpassword;

use futures::{Future, Stream};
use tokio_core::reactor::Core;
use gm::{MatrixClient, MatrixFuture};
use gm::MatrixClient;
use gm::room::RoomExt;
use gm::types::messages::{Message};
use gm::types::content::{Content};
use gm::types::events::Event;
use gm::sync::SyncStream;
use gm::request::MatrixRequestable;
use rpassword::prompt_password_stdout;
use std::env;

Expand All @@ -27,28 +29,27 @@ fn main() {
let hdl = core.handle();
let mut mx = core.run(MatrixClient::login(username, password, server, &hdl)).unwrap();
println!("[+] Connected to {} as {}", server, username);
let ss = mx.get_sync_stream();
let ss = SyncStream::new(mx.clone());
// We discard the results of the initial `/sync`, because we only want to echo
// new requests.
let fut = ss.skip(1).for_each(|sync| {
let mut futs: Vec<MatrixFuture<()>> = vec![];
for (room, evt) in sync.iter_events() {
if let Event::Full(ref meta, ref content) = *evt {
// only echo messages from other users
if meta.sender == mx.user_id() {
if meta.sender == mx.get_user_id() {
continue;
}
// tell the server we have read the event
let mut rc = room.cli(&mut mx);
futs.push(Box::new(rc.read_receipt(&meta.event_id).map(|_| ())));
hdl.spawn(rc.read_receipt(&meta.event_id).map(|_| ()).map_err(|_| ()));
if let Content::RoomMessage(ref m) = *content {
if let Message::Text { ref body, .. } = *m {
futs.push(Box::new(rc.send_simple(body.to_owned()).map(|_| ())));
hdl.spawn(rc.send_simple(body.to_owned()).map(|_| ()).map_err(|_| ()));
}
}
}
}
futures::future::join_all(futs.into_iter()).map(|_| ())
Ok(())
});
core.run(fut).unwrap();
}
40 changes: 40 additions & 0 deletions glitch-in-the-matrix/src/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//! Error handling.
macro_rules! derive_from {
($err:ident, $($var:ident, $ty:ty),*) => {
$(
impl From<$ty> for $err {
fn from(e: $ty) -> $err {
$err::$var(e)
}
}
)*
}
}
/// Something Matrixy that can go wrong.
#[derive(Fail, Debug)]
pub enum MatrixError {
#[fail(display = "HTTP error: {}", _0)]
Hyper(#[cause] ::hyper::error::Error),
#[fail(display = "Serialization error: {}", _0)]
Serde(#[cause] ::serde_json::Error),
#[fail(display = "Error decoding URI: {}", _0)]
UriError(#[cause] ::hyper::error::UriError),
#[fail(display = "I/O error: {}", _0)]
Io(#[cause] ::std::io::Error),
#[fail(display = "OpenSSL error: {}", _0)]
Openssl(#[cause] ::hyper_openssl::openssl::error::ErrorStack),
#[fail(display = "Request failed with HTTP status: {}", _0)]
HttpCode(::hyper::StatusCode),
#[fail(display = "Error from homeserver: {:?}", _0)]
BadRequest(super::types::replies::BadRequestReply)
}
derive_from!(MatrixError,
Hyper, ::hyper::error::Error,
Serde, ::serde_json::Error,
UriError, ::hyper::error::UriError,
Io, ::std::io::Error,
Openssl, ::hyper_openssl::openssl::error::ErrorStack
);
/// Bog-standard result newtype. You know the drill.
pub type MatrixResult<T> = Result<T, MatrixError>;

9 changes: 9 additions & 0 deletions glitch-in-the-matrix/src/http.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! Types reexported from `hyper`.
pub use hyper::Method;
pub use hyper::Body;
pub use hyper::header::ContentType;
pub use hyper::StatusCode;
pub use hyper::Client;
pub use hyper_openssl::HttpsConnector;
pub use hyper::client::HttpConnector;
pub type MatrixHyper = Client<HttpsConnector<HttpConnector>>;
Loading

0 comments on commit 47901f2

Please sign in to comment.