use inner event type for EventEmitter methods

master
Devin R 2020-04-01 09:19:14 -04:00
parent 83bf8bc67c
commit 2ed152708c
4 changed files with 141 additions and 70 deletions

View File

@ -17,12 +17,12 @@ struct EventCallback;
#[async_trait::async_trait]
impl EventEmitter for EventCallback {
async fn on_room_message(&mut self, room: Arc<Mutex<Room>>, event: Arc<Mutex<RoomEvent>>) {
if let RoomEvent::RoomMessage(MessageEvent {
async fn on_room_message(&mut self, room: Arc<Mutex<Room>>, event: Arc<Mutex<MessageEvent>>) {
if let MessageEvent {
content: MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }),
sender,
..
}) = event.lock().await.deref()
} = event.lock().await.deref()
{
let rooms = room.lock().await;
let member = rooms.members.get(&sender.to_string()).unwrap();

View File

@ -367,7 +367,6 @@ impl AsyncClient {
client.get_or_create_room(&room_id_string).clone()
};
// TODO should we determine if anything room state has changed before calling
// re looping is not ideal here
for event in &mut room.state.events {
if let EventResult::Ok(e) = event {
@ -386,7 +385,6 @@ impl AsyncClient {
*event = e;
}
// TODO should we determine if any room state has changed before calling
if let EventResult::Ok(e) = event {
client.emit_timeline_event(room_id, e).await;
}
@ -398,7 +396,6 @@ impl AsyncClient {
if let EventResult::Ok(e) = account_data {
client.receive_account_data(&room_id_string, e).await;
// TODO should we determine if anything room state has changed before calling
client.emit_account_data_event(room_id, e).await;
}
}
@ -414,7 +411,6 @@ impl AsyncClient {
if let EventResult::Ok(e) = presence {
client.receive_presence_event(&room_id_string, e).await;
// TODO should we determine if any room state has changed before calling
client.emit_presence_event(room_id, e).await;
}
}

View File

@ -499,103 +499,106 @@ impl Client {
pub(crate) async fn emit_timeline_event(&mut self, room_id: &RoomId, event: &mut RoomEvent) {
match event {
RoomEvent::RoomMember(_) => {
RoomEvent::RoomMember(mem) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_member(Arc::clone(&room), Arc::new(Mutex::new(event.clone())))
.on_room_member(Arc::clone(&room), Arc::new(Mutex::new(mem.clone())))
.await;
}
}
}
RoomEvent::RoomName(_) => {
RoomEvent::RoomName(name) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_name(Arc::clone(&room), Arc::new(Mutex::new(event.clone())))
.on_room_name(Arc::clone(&room), Arc::new(Mutex::new(name.clone())))
.await;
}
}
}
RoomEvent::RoomCanonicalAlias(_) => {
RoomEvent::RoomCanonicalAlias(canonical) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_canonical_alias(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(canonical.clone())),
)
.await;
}
}
}
RoomEvent::RoomAliases(_) => {
RoomEvent::RoomAliases(aliases) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_aliases(Arc::clone(&room), Arc::new(Mutex::new(event.clone())))
.on_room_aliases(
Arc::clone(&room),
Arc::new(Mutex::new(aliases.clone())),
)
.await;
}
}
}
RoomEvent::RoomAvatar(_) => {
RoomEvent::RoomAvatar(avatar) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_avatar(Arc::clone(&room), Arc::new(Mutex::new(event.clone())))
.on_room_avatar(Arc::clone(&room), Arc::new(Mutex::new(avatar.clone())))
.await;
}
}
}
RoomEvent::RoomMessage(_) => {
RoomEvent::RoomMessage(msg) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_message(Arc::clone(&room), Arc::new(Mutex::new(event.clone())))
.on_room_message(Arc::clone(&room), Arc::new(Mutex::new(msg.clone())))
.await;
}
}
}
RoomEvent::RoomMessageFeedback(_) => {
RoomEvent::RoomMessageFeedback(msg_feedback) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_message_feedback(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(msg_feedback.clone())),
)
.await;
}
}
}
RoomEvent::RoomRedaction(_) => {
RoomEvent::RoomRedaction(redaction) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_redaction(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(redaction.clone())),
)
.await;
}
}
}
RoomEvent::RoomPowerLevels(_) => {
RoomEvent::RoomPowerLevels(power) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_room_power_levels(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(power.clone())),
)
.await;
}
@ -607,83 +610,89 @@ impl Client {
pub(crate) async fn emit_state_event(&mut self, room_id: &RoomId, event: &mut StateEvent) {
match event {
StateEvent::RoomMember(_) => {
StateEvent::RoomMember(member) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_state_member(Arc::clone(&room), Arc::new(Mutex::new(event.clone())))
.on_state_member(
Arc::clone(&room),
Arc::new(Mutex::new(member.clone())),
)
.await;
}
}
}
StateEvent::RoomName(_) => {
StateEvent::RoomName(name) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_state_name(Arc::clone(&room), Arc::new(Mutex::new(event.clone())))
.on_state_name(Arc::clone(&room), Arc::new(Mutex::new(name.clone())))
.await;
}
}
}
StateEvent::RoomCanonicalAlias(_) => {
StateEvent::RoomCanonicalAlias(canonical) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_state_canonical_alias(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(canonical.clone())),
)
.await;
}
}
}
StateEvent::RoomAliases(_) => {
StateEvent::RoomAliases(aliases) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_state_aliases(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(aliases.clone())),
)
.await;
}
}
}
StateEvent::RoomAvatar(_) => {
StateEvent::RoomAvatar(avatar) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_state_avatar(Arc::clone(&room), Arc::new(Mutex::new(event.clone())))
.on_state_avatar(
Arc::clone(&room),
Arc::new(Mutex::new(avatar.clone())),
)
.await;
}
}
}
StateEvent::RoomPowerLevels(_) => {
StateEvent::RoomPowerLevels(power) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_state_power_levels(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(power.clone())),
)
.await;
}
}
}
StateEvent::RoomJoinRules(_) => {
StateEvent::RoomJoinRules(rules) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_state_join_rules(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(rules.clone())),
)
.await;
}
@ -699,53 +708,53 @@ impl Client {
event: &mut NonRoomEvent,
) {
match event {
NonRoomEvent::Presence(_) => {
NonRoomEvent::Presence(presence) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_account_presence(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(presence.clone())),
)
.await;
}
}
}
NonRoomEvent::IgnoredUserList(_) => {
NonRoomEvent::IgnoredUserList(ignored) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_account_ignored_users(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(ignored.clone())),
)
.await;
}
}
}
NonRoomEvent::PushRules(_) => {
NonRoomEvent::PushRules(rules) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_account_push_rules(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(rules.clone())),
)
.await;
}
}
}
NonRoomEvent::FullyRead(_) => {
NonRoomEvent::FullyRead(full_read) => {
if let Some(ee) = &self.event_emitter {
if let Some(room) = self.get_room(&room_id.to_string()) {
ee.lock()
.await
.on_account_data_fully_read(
Arc::clone(&room),
Arc::new(Mutex::new(event.clone())),
Arc::new(Mutex::new(full_read.clone())),
)
.await;
}

View File

@ -15,9 +15,50 @@
use std::sync::Arc;
use crate::events::collections::all::{RoomEvent, StateEvent};
use crate::events::collections::only::Event as NonRoomEvent;
use crate::events::presence::PresenceEvent;
use crate::events::{
call::{
answer::AnswerEvent, candidates::CandidatesEvent, hangup::HangupEvent, invite::InviteEvent,
},
direct::DirectEvent,
dummy::DummyEvent,
forwarded_room_key::ForwardedRoomKeyEvent,
fully_read::FullyReadEvent,
ignored_user_list::IgnoredUserListEvent,
key::verification::{
accept::AcceptEvent, cancel::CancelEvent, key::KeyEvent, mac::MacEvent,
request::RequestEvent, start::StartEvent,
},
presence::PresenceEvent,
push_rules::PushRulesEvent,
receipt::ReceiptEvent,
room::{
aliases::AliasesEvent,
avatar::AvatarEvent,
canonical_alias::CanonicalAliasEvent,
create::CreateEvent,
encrypted::EncryptedEvent,
encryption::EncryptionEvent,
guest_access::GuestAccessEvent,
history_visibility::HistoryVisibilityEvent,
join_rules::JoinRulesEvent,
member::MemberEvent,
message::{feedback::FeedbackEvent, MessageEvent},
name::NameEvent,
pinned_events::PinnedEventsEvent,
power_levels::PowerLevelsEvent,
redaction::RedactionEvent,
server_acl::ServerAclEvent,
third_party_invite::ThirdPartyInviteEvent,
tombstone::TombstoneEvent,
topic::TopicEvent,
},
room_key::RoomKeyEvent,
room_key_request::RoomKeyRequestEvent,
sticker::StickerEvent,
tag::TagEvent,
typing::TypingEvent,
CustomEvent, CustomRoomEvent, CustomStateEvent,
};
use crate::models::Room;
use tokio::sync::Mutex;
@ -26,53 +67,78 @@ use tokio::sync::Mutex;
pub trait EventEmitter: Send + Sync {
// ROOM EVENTS from `IncomingTimeline`
/// Fires when `AsyncClient` receives a `RoomEvent::RoomMember` event.
async fn on_room_member(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_member(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<MemberEvent>>) {}
/// Fires when `AsyncClient` receives a `RoomEvent::RoomName` event.
async fn on_room_name(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_name(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<NameEvent>>) {}
/// Fires when `AsyncClient` receives a `RoomEvent::RoomCanonicalAlias` event.
async fn on_room_canonical_alias(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_canonical_alias(
&mut self,
_: Arc<Mutex<Room>>,
_: Arc<Mutex<CanonicalAliasEvent>>,
) {
}
/// Fires when `AsyncClient` receives a `RoomEvent::RoomAliases` event.
async fn on_room_aliases(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_aliases(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<AliasesEvent>>) {}
/// Fires when `AsyncClient` receives a `RoomEvent::RoomAvatar` event.
async fn on_room_avatar(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_avatar(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<AvatarEvent>>) {}
/// Fires when `AsyncClient` receives a `RoomEvent::RoomMessage` event.
async fn on_room_message(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_message(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<MessageEvent>>) {}
/// Fires when `AsyncClient` receives a `RoomEvent::RoomMessageFeedback` event.
async fn on_room_message_feedback(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_message_feedback(
&mut self,
_: Arc<Mutex<Room>>,
_: Arc<Mutex<FeedbackEvent>>,
) {
}
/// Fires when `AsyncClient` receives a `RoomEvent::RoomRedaction` event.
async fn on_room_redaction(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_redaction(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RedactionEvent>>) {}
/// Fires when `AsyncClient` receives a `RoomEvent::RoomPowerLevels` event.
async fn on_room_power_levels(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<RoomEvent>>) {}
async fn on_room_power_levels(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<PowerLevelsEvent>>) {
}
// `RoomEvent`s from `IncomingState`
/// Fires when `AsyncClient` receives a `StateEvent::RoomMember` event.
async fn on_state_member(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<StateEvent>>) {}
async fn on_state_member(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<MemberEvent>>) {}
/// Fires when `AsyncClient` receives a `StateEvent::RoomName` event.
async fn on_state_name(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<StateEvent>>) {}
async fn on_state_name(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<NameEvent>>) {}
/// Fires when `AsyncClient` receives a `StateEvent::RoomCanonicalAlias` event.
async fn on_state_canonical_alias(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<StateEvent>>) {}
async fn on_state_canonical_alias(
&mut self,
_: Arc<Mutex<Room>>,
_: Arc<Mutex<CanonicalAliasEvent>>,
) {
}
/// Fires when `AsyncClient` receives a `StateEvent::RoomAliases` event.
async fn on_state_aliases(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<StateEvent>>) {}
async fn on_state_aliases(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<AliasesEvent>>) {}
/// Fires when `AsyncClient` receives a `StateEvent::RoomAvatar` event.
async fn on_state_avatar(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<StateEvent>>) {}
async fn on_state_avatar(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<AvatarEvent>>) {}
/// Fires when `AsyncClient` receives a `StateEvent::RoomPowerLevels` event.
async fn on_state_power_levels(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<StateEvent>>) {}
async fn on_state_power_levels(
&mut self,
_: Arc<Mutex<Room>>,
_: Arc<Mutex<PowerLevelsEvent>>,
) {
}
/// Fires when `AsyncClient` receives a `StateEvent::RoomJoinRules` event.
async fn on_state_join_rules(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<StateEvent>>) {}
async fn on_state_join_rules(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<JoinRulesEvent>>) {}
// `NonRoomEvent` (this is a type alias from ruma_events) from `IncomingAccountData`
/// Fires when `AsyncClient` receives a `NonRoomEvent::RoomMember` event.
async fn on_account_presence(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<NonRoomEvent>>) {}
async fn on_account_presence(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<PresenceEvent>>) {}
/// Fires when `AsyncClient` receives a `NonRoomEvent::RoomName` event.
async fn on_account_ignored_users(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<NonRoomEvent>>) {
async fn on_account_ignored_users(
&mut self,
_: Arc<Mutex<Room>>,
_: Arc<Mutex<IgnoredUserListEvent>>,
) {
}
/// Fires when `AsyncClient` receives a `NonRoomEvent::RoomCanonicalAlias` event.
async fn on_account_push_rules(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<NonRoomEvent>>) {}
async fn on_account_push_rules(&mut self, _: Arc<Mutex<Room>>, _: Arc<Mutex<PushRulesEvent>>) {}
/// Fires when `AsyncClient` receives a `NonRoomEvent::RoomAliases` event.
async fn on_account_data_fully_read(
&mut self,
_: Arc<Mutex<Room>>,
_: Arc<Mutex<NonRoomEvent>>,
_: Arc<Mutex<FullyReadEvent>>,
) {
}