add a few common used on_event handlers
This commit is contained in:
parent
7c92039140
commit
b4788b72d7
38
src/imp.rs
38
src/imp.rs
|
@ -1,3 +1,4 @@
|
|||
use chrono::{DateTime, FixedOffset};
|
||||
use crate::event::Event;
|
||||
use libcwtch::structs::*;
|
||||
use libcwtch::CwtchLib;
|
||||
|
@ -9,7 +10,15 @@ use crate::behaviour::NewContactPolicy::AllowList;
|
|||
/// Trait to be used by implementors of imp bots to supply their custom event handling
|
||||
/// the handle function is called after the default imp automatic event handling has run on each new event
|
||||
pub trait EventHandler {
|
||||
fn handle(&mut self, cwtch: &dyn libcwtch::CwtchLib, profile: Option<&Profile>, event: Event);
|
||||
#[allow(unused_variables)]
|
||||
fn handle(&mut self, cwtch: &dyn libcwtch::CwtchLib, profile: Option<&Profile>, event: Event) {}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
fn on_contact_online(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, convo_id: i32) {}
|
||||
#[allow(unused_variables)]
|
||||
fn on_new_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, convo_id: i32) {}
|
||||
#[allow(unused_variables)]
|
||||
fn on_new_message_from_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: i32, handle: String, timestamp_received: DateTime<FixedOffset>, message: String) {}
|
||||
}
|
||||
|
||||
/// Cwtch bot
|
||||
|
@ -56,7 +65,7 @@ impl Imp {
|
|||
let event: CwtchEvent = serde_json::from_str(&event_str).expect("Error parsing Cwtch event");
|
||||
let event = Event::from(&event);
|
||||
match &event {
|
||||
Event::CwtchStarted { data } => {
|
||||
Event::CwtchStarted {data} => {
|
||||
println!("event CwtchStarted!");
|
||||
initialized = true;
|
||||
|
||||
|
@ -159,9 +168,10 @@ impl Imp {
|
|||
|
||||
let acl: ACL = serde_json::from_str(&data["accessControlList"]).expect("Error parsing conversation");
|
||||
|
||||
let conversatio_id: i32 = data["ConversationID"].parse::<i32>().unwrap();
|
||||
let conversation = Conversation {
|
||||
handle: convo_handle.clone(),
|
||||
identifier: data["ConversationID"].parse::<i32>().unwrap(),
|
||||
identifier: conversatio_id,
|
||||
name: data["nick"].to_string(),
|
||||
status: ConnectionState::new(&data["status"]),
|
||||
blocked: data["blocked"] == "true",
|
||||
|
@ -177,11 +187,31 @@ impl Imp {
|
|||
profile
|
||||
.conversations
|
||||
.insert(conversation.identifier, conversation);
|
||||
handler.on_new_contact(self.cwtch.as_ref(), profile, conversatio_id);
|
||||
handler.on_contact_online(self.cwtch.as_ref(), profile, conversatio_id);
|
||||
}
|
||||
None => (),
|
||||
};
|
||||
}
|
||||
Event::PeerStateChange { data } => {}
|
||||
Event::PeerStateChange { data } => {
|
||||
if data["ConnectionState"] == "Authenticated" {
|
||||
match self.profile.as_ref() {
|
||||
Some(profile) => {
|
||||
match profile.find_conversation_id_by_handle(data["RemotePeer"].clone()) {
|
||||
Some(conversation_id) => handler.on_contact_online(self.cwtch.as_ref(), profile,conversation_id),
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
None => (),
|
||||
};
|
||||
}
|
||||
}
|
||||
Event::NewMessageFromPeer { conversation_id, handle, timestamp_received, message } => {
|
||||
match self.profile.as_ref() {
|
||||
Some(profile) => handler.on_new_message_from_contact(self.cwtch.as_ref(), profile, *conversation_id, handle.clone(), *timestamp_received, message.clone()),
|
||||
None => {},
|
||||
}
|
||||
}
|
||||
Event::ErrUnhandled { name, data } => eprintln!("unhandled event: {}!", name),
|
||||
_ => (),
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue