Skip to content

Commit

Permalink
Do not forward a direct message if the sender is the receivers virtua…
Browse files Browse the repository at this point in the history
…l user
  • Loading branch information
exul committed May 10, 2018
1 parent c31a007 commit 163abf9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/matrix-rocketchat/handlers/rocketchat/forwarder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ impl<'a> Forwarder<'a> {
}
};

if receiver.rocketchat_user_id.clone().unwrap_or_default() == message.user_id {
info!(self.logger, "Not forwarding direct message, because the sender is the receivers virtual user");
return Ok(None);
}

let room = match self.try_to_find_or_create_direct_message_room(server, &receiver, message)? {
Some(room) => room,
None => return Ok(None),
Expand Down
63 changes: 62 additions & 1 deletion tests/forward_rocketchat_direct_message_to_matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn successfully_forwards_an_image_in_a_direct_message_to_matrix() {
}

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

let mut rocketchat_router = Router::new();
Expand Down Expand Up @@ -414,6 +414,67 @@ fn successfully_forwards_a_direct_message_to_a_matrix_room_that_was_bridged_befo
assert!(message_received_by_matrix.contains("Hey again"));
}

#[test]
fn do_not_forward_a_direct_message_if_the_receiver_is_the_senders_virtual_user() {
let test = Test::new();
let (message_forwarder, receiver) = MessageForwarder::with_path_filter("other_userDMRocketChat_id:localhost");
let mut matrix_router = test.default_matrix_routes();
matrix_router.put(SendMessageEventEndpoint::router_path(), message_forwarder, "send_message_event");
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(DM_LIST_PATH, direct_messages_list_handler, "direct_messages_list");

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 first_direct_message = WebhookMessage {
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 first_direct_message_payload = to_string(&first_direct_message).unwrap();

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

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

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

let message_from_receiver_virtual_user = WebhookMessage {
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: "spec_user_id".to_string(),
user_name: "spec_user".to_string(),
text: "This will not be forwarded".to_string(),
};
let second_direct_message_payload = to_string(&message_from_receiver_virtual_user).unwrap();

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

// message is not forwarded, because the sender is the receivers virtual user
assert!(receiver.recv_timeout(default_timeout()).is_err());
}

#[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();
Expand Down

0 comments on commit 163abf9

Please sign in to comment.