Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Implement part #20

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,26 @@ impl<IS: Io> Bridge<IS> {
warn!(self.ctx.logger, "Unknown channel"; "channel" => channel.as_str());
}
}
// TODO: Handle JOIN
// TODO: Handle PART
IrcCommand::Join { channel } => {
info!(self.ctx.logger, "Joining channel"; "channel" => channel);
let logger = self.ctx.logger.clone();
self.handle.spawn(
self.matrix_client.join_room(channel.as_str())
.map(|_| ()).map_err(move |err| warn!(logger, "Failed to join: {}", err))
);
}
IrcCommand::Part { channel } => {
if let Some(room_id) = self.mappings.channel_to_room_id(&channel) {
info!(self.ctx.logger, "Leaving channel"; "channel" => room_id.as_str());
let logger = self.ctx.logger.clone();
self.handle.spawn(
self.matrix_client.leave_room(room_id.as_str())
.map(|_| ()).map_err(move |err| warn!(logger, "Failed to part: {}", err))
);
} else {
warn!(self.ctx.logger, "Unknown channel"; "channel" => channel.as_str());
}
}
c => {
warn!(self.ctx.logger, "Ignoring IRC command"; "command" => c.command());
}
Expand Down
20 changes: 20 additions & 0 deletions src/matrix/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use std::io;
use tokio_core::reactor::Handle;

use url::Url;
use url::percent_encoding::{percent_encode, PATH_SEGMENT_ENCODE_SET};

use serde_json;
use serde::{Serialize, Deserialize};
Expand Down Expand Up @@ -116,6 +117,25 @@ impl MatrixClient {
.map_err(JsonPostError::into_io_error)
}

pub fn join_room(&mut self, room_id: &str) -> impl Future<Item=protocol::RoomJoinResponse, Error=io::Error> {
let roomid_encoded = percent_encode(room_id.as_bytes(), PATH_SEGMENT_ENCODE_SET);
let mut url = self.url.join(&format!("/_matrix/client/r0/join/{}", roomid_encoded)).unwrap();
url.query_pairs_mut()
.clear()
.append_pair("access_token", &self.access_token);
do_json_post("POST", &mut self.http_stream, &url, &protocol::RoomJoinInput { }).map_err(JsonPostError::into_io_error)
}

pub fn leave_room(&mut self, room_id: &str) -> impl Future<Item=protocol::RoomLeaveResponse, Error=io::Error> {
let roomid_encoded = percent_encode(room_id.as_bytes(), PATH_SEGMENT_ENCODE_SET);
let mut url = self.url.join(&format!("/_matrix/client/r0/rooms/{}/leave", roomid_encoded))
.expect("Unable to construct a valid API url");
url.query_pairs_mut()
.clear()
.append_pair("access_token", &self.access_token);
do_json_post("POST", &mut self.http_stream, &url, &protocol::RoomLeaveInput { }).map_err(JsonPostError::into_io_error)
}

pub fn get_room(&self, room_id: &str) -> Option<&Room> {
self.rooms.get(room_id)
}
Expand Down
17 changes: 17 additions & 0 deletions src/matrix/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ pub struct LoginResponse {
pub user_id: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct RoomJoinInput {
// third_party_signed at some point
}

#[derive(Debug, Clone, Deserialize)]
pub struct RoomJoinResponse {
pub room_id: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct RoomLeaveInput {
}

#[derive(Debug, Clone, Deserialize)]
pub struct RoomLeaveResponse {
}

#[derive(Debug, Clone, Serialize)]
pub struct RoomSendInput {
Expand Down