Skip to content

Commit

Permalink
Ignore multiple join events for the same user
Browse files Browse the repository at this point in the history
Because name changes are sent as join events as well and there seems to be no way to differentiate them from normal join events
  • Loading branch information
exul committed Apr 15, 2017
1 parent 89e388e commit 8793fe4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/matrix-rocketchat/handlers/events/room_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,23 @@ impl<'a> RoomHandler<'a> {

fn handle_user_join(&self, matrix_user_id: UserId, matrix_room_id: RoomId) -> Result<()> {
let room = Room::find(self.connection, &matrix_room_id)?;

if UserInRoom::find_by_matrix_user_id_and_matrix_room_id(self.connection, &matrix_user_id, &matrix_room_id)?.is_some() {
let msg = format!("Skipping because the user {} is already in the room {} (join event triggered due to name change)",
&matrix_user_id,
&matrix_room_id);
debug!(self.logger, msg);
return Ok(());
}

if room.is_admin_room {
info!(self.logger, "Another user join the admin room {}, bot user is leaving", matrix_room_id);
let admin_room_language = self.admin_room_language(&room)?;
let body = t!(["errors", "other_user_joined"]).l(&admin_room_language);
self.matrix_api.send_text_message_event(matrix_room_id, self.config.matrix_bot_user_id()?, body)?;
self.leave_and_forget_room(&room)?;
} else {
debug!(self.logger, format!("Adding user {} to room {}", &matrix_user_id, &matrix_room_id));
let new_user_in_room = NewUserInRoom {
matrix_user_id: matrix_user_id,
matrix_room_id: matrix_room_id,
Expand Down
19 changes: 19 additions & 0 deletions tests/admin_room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,3 +457,22 @@ fn ignore_messages_from_the_bot_user() {
// no command is executed, so we get a timeout
assert!(receiver.recv_timeout(default_timeout()).is_err());
}

#[test]
fn ignore_multiple_join_events_for_the_same_user() {
let (message_forwarder, receiver) = MessageForwarder::new();
let mut matrix_router = Router::new();
matrix_router.put(SendMessageEventEndpoint::router_path(), message_forwarder, "send_message_event");

let test = Test::new().with_admin_room().with_matrix_routes(matrix_router).run();

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

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

// no message, because the join is ignored
assert!(receiver.recv_timeout(default_timeout()).is_err());
}

0 comments on commit 8793fe4

Please sign in to comment.