From 1691db16455a3765674ddddacdc91a4f4691c658 Mon Sep 17 00:00:00 2001 From: Dan Ballard Date: Tue, 26 Apr 2022 14:28:34 -0700 Subject: [PATCH] BehaviourBuilder --- src/imp.rs | 70 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 21 deletions(-) diff --git a/src/imp.rs b/src/imp.rs index 98189d5..9a58dab 100644 --- a/src/imp.rs +++ b/src/imp.rs @@ -3,10 +3,9 @@ use libcwtch::CwtchLib; use crate::event::Event; use serde_json; -use crate::imp::DefaultContactPolicy::Accept; /// How new contacts should be treated -pub enum DefaultContactPolicy { +pub enum NewContactPolicy { /// Do not react, leave it for the custom event handler Ignore, /// Block all new contacts @@ -28,19 +27,48 @@ pub struct Behaviour { pub profile_pic_path: Option, /// Policy dictacting how the bot should automatically handle ContactCreated events - pub default_contant_policy: DefaultContactPolicy, + pub new_contant_policy: NewContactPolicy, } -impl Behaviour { - /// Returns a named, profile pic using, always accepting conversations bot - pub fn new_default_acceptor(name: String, profile_pic_path: String) -> Self { - return Behaviour{ - proto_experiments: true, - proto_experiment_fileshare: true, - default_contant_policy: Accept, - profile_name: name, - profile_pic_path: Some(profile_pic_path), - } +pub struct BehaviourBuilder { + behaviour: Behaviour +} + +impl BehaviourBuilder { + pub fn new() -> Self { + return BehaviourBuilder{ behaviour: Behaviour { + proto_experiments: false, + proto_experiment_fileshare: false, + new_contant_policy: NewContactPolicy::Ignore, + profile_name: "".to_string(), + profile_pic_path: None, + }} + } + + pub fn build(self) -> Behaviour { + self.behaviour + } + + pub fn fileshare(mut self, val: bool) -> Self { + self.behaviour.proto_experiment_fileshare = val; + self + } + + pub fn profile_pic_path(mut self, val: String) -> Self { + self.behaviour.profile_pic_path = Some(val); + self.behaviour.proto_experiment_fileshare = true; + self.behaviour.proto_experiments = true; + self + } + + pub fn name(mut self, val: String) -> Self { + self.behaviour.profile_name = val; + self + } + + pub fn new_contact_policy(mut self, val: NewContactPolicy) -> Self { + self.behaviour.new_contant_policy = val; + self } } @@ -156,13 +184,13 @@ impl Imp { self.cwtch.set_profile_attribute(&profile.handle, "profile.name", &self.behaviour.profile_name); for (_id, conversation) in &profile.conversations { - match self.behaviour.default_contant_policy { - DefaultContactPolicy::Accept => { + match self.behaviour.new_contant_policy { + NewContactPolicy::Accept => { self.cwtch.accept_conversation(&profile.handle.clone(), conversation.identifier); }, - DefaultContactPolicy::Block => + NewContactPolicy::Block => self.cwtch.block_contact(&profile.handle.clone(), conversation.identifier), - DefaultContactPolicy::Ignore => (), + NewContactPolicy::Ignore => (), } } @@ -198,13 +226,13 @@ impl Imp { .conversations .insert(event.data["RemotePeer"].to_string(), conversation); - match self.behaviour.default_contant_policy { - DefaultContactPolicy::Accept => { + match self.behaviour.new_contant_policy { + NewContactPolicy::Accept => { self.cwtch.accept_conversation(&profile.handle.clone(), convo_id); }, - DefaultContactPolicy::Block => + NewContactPolicy::Block => self.cwtch.block_contact(&profile.handle.clone(), convo_id), - DefaultContactPolicy::Ignore => (), + NewContactPolicy::Ignore => (), } } None => (),