moving to new imp event on_handlers

This commit is contained in:
Dan Ballard 2022-05-19 14:05:04 -07:00
parent 25a40cd5cf
commit 24dae6f3dc
3 changed files with 71 additions and 96 deletions

1
Cargo.lock generated
View File

@ -321,6 +321,7 @@ checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
name = "update_bot"
version = "0.1.0"
dependencies = [
"chrono",
"imp",
"libcwtch",
"serde_json",

View File

@ -9,4 +9,5 @@ edition = "2021"
libcwtch = "0.3.2"
imp = {path = "../imp"}
serde_json = "1.0"
chrono = "0.4.19"

View File

@ -5,12 +5,12 @@ use std::fs::read_dir;
use std::path::{Path, PathBuf};
use std::thread;
use ::imp::event::Event;
use ::imp::imp;
use ::imp::behaviour;
use ::imp::behaviour::BehaviourBuilder;
use ::imp::behaviour::Behaviour;
use ::imp::imp::Imp;
use chrono::{DateTime, FixedOffset};
use libcwtch;
use libcwtch::CwtchLib;
use libcwtch::structs::*;
@ -55,7 +55,7 @@ impl UpdateBot {
.to_str()
.unwrap()
.to_string();
let mut bot = UpdateBot {
let bot = UpdateBot {
versions_dirs: versions_dirs,
latest_version: latest_version,
version: version,
@ -84,8 +84,7 @@ fn main() {
impl UpdateBot {
pub fn greet(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, convo_id: i32) {
if let Some(profile) = profile_opt {
pub fn greet(&self, cwtch: &dyn CwtchLib, profile: &Profile, convo_id: i32) {
let do_offer = match cwtch.get_conversation_attribute(
&profile.handle,
convo_id,
@ -98,7 +97,7 @@ impl UpdateBot {
Err(_) => false
};
if do_offer {
self.offer(cwtch, profile_opt, convo_id);
self.offer(cwtch, profile, convo_id);
cwtch.set_conversation_attribute(
&profile.handle,
convo_id,
@ -107,10 +106,8 @@ impl UpdateBot {
);
}
}
}
pub fn offer(&self, cwtch: &dyn CwtchLib, profile_opt: Option<&Profile>, convo_id: i32) {
if let Some(profile) = profile_opt {
pub fn offer(&self, cwtch: &dyn CwtchLib, profile: &Profile, convo_id: i32) {
let resp_message = format!(
"Currently offering Cwtch {}\nPlease respond with the OS you would like a package for:\n- Windows\n- Android\n- MacOS\n- Linux",
self.version
@ -123,33 +120,16 @@ impl UpdateBot {
Ok(response_json) => cwtch.send_message(&profile.handle, convo_id, &response_json),
Err(e) => { println!("Error parsing json response: {}", e.to_string()); "".to_string() }
};
}
}
}
}
impl imp::EventHandler for UpdateBot {
fn handle(&mut self, cwtch: &dyn libcwtch::CwtchLib, profile_opt: Option<&Profile>, event: Event) {
match event {
Event::ContactCreated { data } => {
let convo_id = data["ConversationID"].parse::<i32>().unwrap();
self.greet(cwtch, profile_opt, convo_id);
fn on_contact_online(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, convo_id: i32) {
self.greet(cwtch, profile, convo_id);
}
Event::PeerStateChange { data } => {
if data["ConnectionState"] == "Authenticated" {
match profile_opt.as_ref() {
Some(profile) => {
match profile.find_conversation_id_by_handle(data["RemotePeer"].clone()) {
Some(conversation_id) => self.greet(cwtch, profile_opt, conversation_id),
None => {}
}
}
None => (),
};
}
}
Event::NewMessageFromPeer { conversation_id, handle: _, timestamp_received: _, message } => {
match profile_opt {
Some(profile) => {
fn on_new_message_from_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: i32, _handle: String, _timestamp_received: DateTime<FixedOffset>, message: String) {
let from = profile.handle.as_str();
let message_wrapper: Message =
serde_json::from_str(&message).expect("Error parsing message");
@ -178,15 +158,8 @@ impl imp::EventHandler for UpdateBot {
cwtch.share_file(&from, conversation_id, android_path.to_str().unwrap());
}
_ => {
self.offer(cwtch, profile_opt, conversation_id);
self.offer(cwtch, profile, conversation_id);
}
}
}
_ => {}
}
}
Event::ErrUnhandled { name, data: _ } => eprintln!("unhandled event: {}!", name),
_ => ()
};
}
}