Skip to content

Commit

Permalink
Improve help message
Browse files Browse the repository at this point in the history
  • Loading branch information
exul committed Mar 31, 2017
1 parent 873d527 commit fdc2c0e
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 13 deletions.
13 changes: 11 additions & 2 deletions assets/translations.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,16 @@ en:
**Warning**: This will store your password *unecrypted* in the database of your homeserver
1. Login via curl: `curl AS_URL/login -d '{"rocketchat_server_url": "${rocketchat_url}", "matrix_user_id": "${matrix_user_id}", "username": "rocketchat_user", "password": "mysecret"}'`
bridge_instructions: "You are logged in."
usage_instructions: |
You are logged in.
You are connected to ${rocketchat_url}.
`list` Lists all public rooms from the Rocket.Chat server
`bridge rocketchatroomnname` Bridge a Rocket.Chat room
`unbridge rocketchatroomnname` Unbridge a Rocket.Chat room (messages are no longer forwarded)
list_channels: |
Rooms are written in *italic* when you joined them on the Rocket.Chat server, you can `bridge` such a room.
Expand All @@ -60,7 +69,7 @@ en:
rocketchat_join_first: "You have to join the channel ${channel_name} on the Rocket.Chat server before you can bridge it."
room_already_connected: "This room is already connected"
room_not_connected: "This room is not connected to a Rocket.Chat server, you have to connect it first to be able to execute the command, type `help` for further instructions on how to connect this room"
room_not_empty: "Cannot unbdrige room ${channel_name}, because Matrix users (${users}) are still using the room."
room_not_empty: "Cannot unbdrige room ${channel_name}, because Matrix users (${users}) are still using the room. All Matrix users have to leave a room before it can be unbridged."
token_already_in_use: "The token ${token} is already in use, please use another token."
too_many_members_in_room: "Admin rooms must only contain the user that invites the bot. Too many members in the room, leaving."
unsupported_rocketchat_api_version: "No supported API version (>= ${min_version}) found for the Rocket.Chat server, found version: ${version}"
Expand Down
15 changes: 9 additions & 6 deletions src/matrix-rocketchat/db/user_on_rocketchat_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl UserOnRocketchatServer {
match users_on_rocketchat_server.into_iter().next() {
Some(existing_user_on_rocketchat_server) => {
existing_user_on_rocketchat_server.set_credentials(connection,
user_on_rocketchat_server.rocketchat_user_id.clone(),
user_on_rocketchat_server.rocketchat_auth_token.clone())?;
user_on_rocketchat_server.rocketchat_user_id.clone(),
user_on_rocketchat_server.rocketchat_auth_token.clone())?;
}
None => {
diesel::insert(user_on_rocketchat_server).into(users_on_rocketchat_servers::table)
Expand Down Expand Up @@ -117,10 +117,7 @@ impl UserOnRocketchatServer {
}

/// Update the users Rocket.Chat username.
pub fn set_rocketchat_username(&self,
connection: &SqliteConnection,
rocketchat_username: Option<String>)
-> Result<()> {
pub fn set_rocketchat_username(&self, connection: &SqliteConnection, rocketchat_username: Option<String>) -> Result<()> {
diesel::update(users_on_rocketchat_servers::table.find((&self.matrix_user_id, self.rocketchat_server_id)))
.set(users_on_rocketchat_servers::rocketchat_username.eq(rocketchat_username)).execute(connection)
.chain_err(|| ErrorKind::DBUpdateError)?;
Expand All @@ -131,4 +128,10 @@ impl UserOnRocketchatServer {
pub fn user(&self, connection: &SqliteConnection) -> Result<User> {
User::find(connection, &self.matrix_user_id)
}

/// Returns true if the user is logged in on the Rocket.Chat server via the application
/// serivce, and false otherwise.
pub fn is_logged_in(&self) -> bool {
self.rocketchat_auth_token.is_some()
}
}
16 changes: 11 additions & 5 deletions src/matrix-rocketchat/handlers/events/command_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,17 +164,23 @@ impl<'a> CommandHandler<'a> {
let room = Room::find(self.connection, &event.room_id)?;
let user = User::find(self.connection, &event.user_id)?;

let body = match room.rocketchat_url(self.connection)? {
Some(rocketchat_url) => {
t!(["admin_room", "login_instructions"]).with_vars(vec![("rocketchat_url", rocketchat_url)])
let body = match room.rocketchat_server(self.connection)? {
Some(rocketchat_server) => {
if UserOnRocketchatServer::find(self.connection, &user.matrix_user_id, rocketchat_server.id)?.is_logged_in() {
t!(["admin_room", "usage_instructions"]).with_vars(vec![("rocketchat_url",
rocketchat_server.rocketchat_url)])
} else {
t!(["admin_room", "login_instructions"]).with_vars(vec![("rocketchat_url",
rocketchat_server.rocketchat_url)])
}
}
None => t!(["admin_room", "connection_instructions"]).with_vars(vec![("as_url", self.config.as_url.clone())]),
};

let bot_matrix_user_id = self.config.matrix_bot_user_id()?;
self.matrix_api.send_text_message_event(event.room_id.clone(), bot_matrix_user_id, body.l(&user.language))?;

Ok(info!(self.logger, "Successfully executed login command for user {}", user.matrix_user_id))
Ok(info!(self.logger, "Successfully executed help command for user {}", user.matrix_user_id))
}

fn login(&self, event: &MessageEvent, rocketchat_server: &RocketchatServer, message: &str) -> Result<()> {
Expand All @@ -199,7 +205,7 @@ impl<'a> CommandHandler<'a> {
user_on_rocketchat_server.set_rocketchat_username(self.connection, Some(username.clone()))?;

let bot_matrix_user_id = self.config.matrix_bot_user_id()?;
let message = t!(["admin_room", "bridge_instructions"]);
let message = t!(["admin_room", "usage_instructions"]);
self.matrix_api.send_text_message_event(event.room_id.clone(), bot_matrix_user_id, message.l(&user.language))?;

Ok(info!(self.logger,
Expand Down
30 changes: 30 additions & 0 deletions tests/admin_commands_help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,33 @@ fn help_command_when_connected() {
let message_received_by_matrix = receiver.recv_timeout(default_timeout()).unwrap();
assert!(message_received_by_matrix.contains("You have to login before you can use the application service, there are two ways to do that"));
}

#[test]
fn help_command_when_logged_in() {
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_matrix_routes(matrix_router)
.with_rocketchat_mock()
.with_connected_admin_room()
.with_logged_in_user()
.run();

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

// 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("`list` Lists all public rooms from the Rocket.Chat server"));
assert!(message_received_by_matrix.contains("`bridge rocketchatroomnname` Bridge a Rocket.Chat room"));
assert!(message_received_by_matrix.contains("`unbridge rocketchatroomnname` Unbridge a Rocket.Chat room (messages are no longer forwarded)"));
}

0 comments on commit fdc2c0e

Please sign in to comment.