-
Notifications
You must be signed in to change notification settings - Fork 5
/
message_handler.rs
54 lines (48 loc) · 1.77 KB
/
message_handler.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use diesel::sqlite::SqliteConnection;
use ruma_events::room::message::MessageEvent;
use slog::Logger;
use api::MatrixApi;
use config::Config;
use db::Room;
use errors::*;
use super::{CommandHandler, Forwarder};
/// Handles message events
pub struct MessageHandler<'a> {
config: &'a Config,
connection: &'a SqliteConnection,
logger: &'a Logger,
matrix_api: Box<MatrixApi>,
}
impl<'a> MessageHandler<'a> {
/// Create a new `MessageHandler`.
pub fn new(
config: &'a Config,
connection: &'a SqliteConnection,
logger: &'a Logger,
matrix_api: Box<MatrixApi>,
) -> MessageHandler<'a> {
MessageHandler {
config: config,
connection: connection,
logger: logger,
matrix_api: matrix_api,
}
}
/// Handles messages that are sent in a room
pub fn process(&self, event: &MessageEvent) -> Result<()> {
if event.user_id == self.config.matrix_bot_user_id()? {
debug!(self.logger, "Skipping event, because it was sent by the bot user");
return Ok(());
}
let matrix_room_id = event.room_id.clone();
let matrix_api = self.matrix_api.as_ref();
if Room::is_admin_room(self.matrix_api.as_ref(), self.config, matrix_room_id.clone())? {
CommandHandler::new(self.config, self.connection, self.logger, matrix_api).process(event, matrix_room_id)?;
} else if let Some(channel_id) = Room::rocketchat_channel_id(matrix_api, matrix_room_id.clone())? {
Forwarder::new(self.connection, self.logger, matrix_api).process(event, matrix_room_id, channel_id)?;
} else {
debug!(self.logger, "Skipping event, because the room {} is not bridged", matrix_room_id);
}
Ok(())
}
}