Skip to content

Commit

Permalink
Do not re-bridge direct message rooms when the user is not logged in …
Browse files Browse the repository at this point in the history
…anymore on the Rocket.Chat server.
  • Loading branch information
exul committed Jul 21, 2017
1 parent 8661fde commit aeb0386
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 9 deletions.
5 changes: 3 additions & 2 deletions src/matrix-rocketchat/handlers/rocketchat/forwarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ impl<'a> Forwarder<'a> {
rocketchat_server.id.clone(),
message.channel_id.clone(),
)? {
Some(ref room) if room.is_bridged => room.matrix_room_id.clone(),
Some(ref mut room) if room.is_direct_message_room => {
debug!(self.logger, "Got a message for a direct message room (channel_id `{}`)", &message.channel_id);
let bot_matrix_user_id = self.config.matrix_bot_user_id()?;
let receiver_matrix_user_id = match self.find_matching_user_for_direct_message(rocketchat_server, message)? {
Some(user_on_rocketchat_server) => user_on_rocketchat_server.matrix_user_id.clone(),
None => {
debug!(
self.logger,
"No matching user found. Not bridging channel {} automatically",
"No matching user found. Not re-bridging channel {} automatically",
message.channel_id
);
return Ok(());
Expand All @@ -77,6 +77,7 @@ impl<'a> Forwarder<'a> {
room.set_is_bridged(self.connection, true)?;
room.matrix_room_id.clone()
}
Some(ref room) if room.is_bridged => room.matrix_room_id.clone(),
Some(ref room) => {
debug!(
self.logger,
Expand Down
87 changes: 86 additions & 1 deletion tests/forward_rocketchat_direct_message_to_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::convert::TryFrom;
use iron::status;
use matrix_rocketchat::api::rocketchat::Message;
use matrix_rocketchat::api::rocketchat::v1::DIRECT_MESSAGES_LIST_PATH;
use matrix_rocketchat::db::Room;
use matrix_rocketchat::db::{Room, UserOnRocketchatServer};
use matrix_rocketchat_test::{MessageForwarder, RS_TOKEN, Test, default_timeout, handlers, helpers};
use router::Router;
use ruma_client_api::Endpoint;
Expand Down Expand Up @@ -296,6 +296,91 @@ fn successfully_forwards_a_direct_message_to_a_room_that_was_bridged_before() {
assert!(room.is_bridged);
}

#[test]
fn do_not_forwards_a_direct_message_to_a_room_if_the_user_is_no_longer_logged_in_on_the_rocketchat_server() {
let test = Test::new();

let mut rocketchat_router = Router::new();
let mut direct_messages = HashMap::new();
direct_messages.insert("spec_user_id_other_user_id", vec!["spec_user", "other_user"]);
let direct_messages_list_handler = handlers::RocketchatDirectMessagesList {
direct_messages: direct_messages,
status: status::Ok,
};
rocketchat_router.get(DIRECT_MESSAGES_LIST_PATH, direct_messages_list_handler, "direct_messages_list");

let mut matrix_router = test.default_matrix_routes();
let (message_forwarder, receiver) = MessageForwarder::new();
let (invite_forwarder, invite_receiver) = MessageForwarder::new();
matrix_router.put(SendMessageEventEndpoint::router_path(), message_forwarder, "send_message_event");
matrix_router.post(InviteEndpoint::router_path(), invite_forwarder, "invite_user");

let test = test.with_matrix_routes(matrix_router)
.with_rocketchat_mock()
.with_custom_rocketchat_routes(rocketchat_router)
.with_connected_admin_room()
.with_logged_in_user()
.run();

let direct_message = Message {
message_id: "spec_id_1".to_string(),
token: Some(RS_TOKEN.to_string()),
channel_id: "spec_user_id_other_user_id".to_string(),
channel_name: None,
user_id: "other_user_id".to_string(),
user_name: "other_user".to_string(),
text: "Hey there".to_string(),
};
let direct_message_payload = to_string(&direct_message).unwrap();

helpers::simulate_message_from_rocketchat(&test.config.as_url, &direct_message_payload);

// discard welcome message
receiver.recv_timeout(default_timeout()).unwrap();
// discard connect message
receiver.recv_timeout(default_timeout()).unwrap();
// discard login message
receiver.recv_timeout(default_timeout()).unwrap();

let message_received_by_matrix = receiver.recv_timeout(default_timeout()).unwrap();
assert!(message_received_by_matrix.contains("Hey there"));

let initial_invite = invite_receiver.recv_timeout(default_timeout()).unwrap();
assert!(initial_invite.contains("@spec_user:localhost"));

helpers::join(
&test.config.as_url,
RoomId::try_from("!1234_id:localhost").unwrap(),
UserId::try_from("@spec_user:localhost").unwrap(),
);

helpers::leave_room(
&test.config.as_url,
RoomId::try_from("!1234_id:localhost").unwrap(),
UserId::try_from("@spec_user:localhost").unwrap(),
);

let connection = test.connection_pool.get().unwrap();
let receier_user_id = UserId::try_from("@spec_user:localhost").unwrap();
let user = UserOnRocketchatServer::find(&connection, &receier_user_id, "rc_id".to_string()).unwrap();
helpers::logout_user_from_rocketchat_server_on_bridge(&connection, "rc_id".to_string(), &user.matrix_user_id);

let direct_message = Message {
message_id: "spec_id_2".to_string(),
token: Some(RS_TOKEN.to_string()),
channel_id: "spec_user_id_other_user_id".to_string(),
channel_name: None,
user_id: "other_user_id".to_string(),
user_name: "other_user".to_string(),
text: "Hey again".to_string(),
};
let direct_message_payload = to_string(&direct_message).unwrap();

helpers::simulate_message_from_rocketchat(&test.config.as_url, &direct_message_payload);

assert!(receiver.recv_timeout(default_timeout()).is_err());
}

#[test]
fn no_room_is_created_when_the_user_doesn_not_have_access_to_the_matching_direct_message_channel() {
let test = Test::new();
Expand Down
20 changes: 14 additions & 6 deletions tests/matrix-rocketchat-test/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::collections::HashMap;

use diesel::sqlite::SqliteConnection;
use matrix_rocketchat::api::RestApi;
use matrix_rocketchat::models::Events;
use matrix_rocketchat::db::UserOnRocketchatServer;
use reqwest::{Method, StatusCode};
use ruma_events::EventType;
use ruma_events::collections::all::Event;
Expand Down Expand Up @@ -88,9 +90,9 @@ pub fn leave_room(as_url: &str, room_id: RoomId, user_id: UserId) {
pub fn send_room_message_from_matrix(as_url: &str, room_id: RoomId, user_id: UserId, body: String) {
let message_event = MessageEvent {
content: MessageEventContent::Text(TextMessageEventContent {
body: body,
msgtype: MessageType::Text,
}),
body: body,
msgtype: MessageType::Text,
}),
event_id: EventId::new("localhost").unwrap(),
event_type: EventType::RoomMessage,
room_id: room_id,
Expand All @@ -107,9 +109,9 @@ pub fn send_room_message_from_matrix(as_url: &str, room_id: RoomId, user_id: Use
pub fn send_emote_message_from_matrix(as_url: &str, room_id: RoomId, user_id: UserId, body: String) {
let message_event = MessageEvent {
content: MessageEventContent::Text(TextMessageEventContent {
body: body,
msgtype: MessageType::Emote,
}),
body: body,
msgtype: MessageType::Emote,
}),
event_id: EventId::new("localhost").unwrap(),
event_type: EventType::RoomMessage,
room_id: room_id,
Expand All @@ -135,3 +137,9 @@ pub fn simulate_message_from_rocketchat(as_url: &str, payload: &str) -> (String,
let params = HashMap::new();
RestApi::call(Method::Post, &url, payload, &params, None).unwrap()
}

pub fn logout_user_from_rocketchat_server_on_bridge(connection: &SqliteConnection, rocketchat_server_id: String, matrix_user_id: &UserId) {
let mut user_on_rocketchat_server = UserOnRocketchatServer::find(connection, &matrix_user_id, rocketchat_server_id).unwrap();
let rocketchat_user_id = user_on_rocketchat_server.rocketchat_user_id.clone();
user_on_rocketchat_server.set_credentials(connection, rocketchat_user_id, None).unwrap();
}

0 comments on commit aeb0386

Please sign in to comment.