Merge branch 'main' into imp-fixes

This commit is contained in:
Sarah Jamie Lewis 2022-04-26 21:29:36 +00:00
commit 611f803880
1 changed files with 49 additions and 21 deletions

View File

@ -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<String>,
/// 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 => (),