Room State Management #44

Manually merged
projectmoon merged 11 commits from room-state-management into master 2020-11-10 20:58:50 +00:00
1 changed files with 23 additions and 12 deletions
Showing only changes of commit 9349dd5f00 - Show all commits

View File

@ -41,7 +41,13 @@ fn check_message_age(
} }
} }
async fn should_process<'a>( /// Determine whether or not to process a received message. This check
/// is necessary in addition to the event processing check because we
/// may receive message events when entering a room for the first
/// time, and we don't want to respond to things before the bot was in
/// the channel, but we do want to respond to things that were sent if
/// the bot left and rejoined quickly.
async fn should_process_message<'a>(
bot: &DiceBot, bot: &DiceBot,
event: &SyncMessageEvent<MessageEventContent>, event: &SyncMessageEvent<MessageEventContent>,
) -> Result<(String, String), BotError> { ) -> Result<(String, String), BotError> {
@ -138,12 +144,12 @@ impl EventEmitter for DiceBot {
async fn on_stripped_state_member( async fn on_stripped_state_member(
&self, &self,
room: SyncRoom, room: SyncRoom,
room_member: &StrippedStateEvent<MemberEventContent>, event: &StrippedStateEvent<MemberEventContent>,
_: Option<MemberEventContent>, _: Option<MemberEventContent>,
) { ) {
if let SyncRoom::Invited(room) = room { if let SyncRoom::Invited(room) = room {
if let Some(user_id) = self.client.user_id().await { if let Some(user_id) = self.client.user_id().await {
if room_member.state_key != user_id { if event.state_key != user_id {
return; return;
} }
} }
@ -160,17 +166,22 @@ impl EventEmitter for DiceBot {
async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) { async fn on_room_message(&self, room: SyncRoom, event: &SyncMessageEvent<MessageEventContent>) {
if let SyncRoom::Joined(room) = room { if let SyncRoom::Joined(room) = room {
let (msg_body, sender_username) = //Clone to avoid holding lock.
if let Ok((msg_body, sender_username)) = should_process(self, &event).await { let room = room.read().await.clone();
let room_id = room.room_id.as_str();
if !should_process_event(&self.db, room_id, event.event_id.as_str()) {
return;
}
let (msg_body, sender_username) = if let Ok((msg_body, sender_username)) =
should_process_message(self, &event).await
{
(msg_body, sender_username) (msg_body, sender_username)
} else { } else {
return; return;
}; };
//we clone here to hold the lock for as little time as possible. self.execute_commands(&room, &sender_username, &msg_body)
let real_room = room.read().await.clone();
self.execute_commands(&real_room, &sender_username, &msg_body)
.await; .await;
} }
} }