add contacts from allow list

This commit is contained in:
Dan Ballard 2022-05-02 10:23:05 -07:00
parent 8fdb2b2059
commit eafe7a0ae5
1 changed files with 15 additions and 5 deletions

View File

@ -3,19 +3,20 @@ use libcwtch::structs::*;
use libcwtch::CwtchLib; use libcwtch::CwtchLib;
use serde_json; use serde_json;
use crate::imp::NewContactPolicy::AllowList;
// Todo: move Behaviour + building to seperate file // Todo: move Behaviour + building to seperate file
pub struct AllowList { pub struct AllowListMembers {
// list of peers to allow by handle // list of peers to allow by handle
peers: Vec<String>, peers: Vec<String>,
// list of groups to join and listen for peers in peer list from // list of groups to join and listen for peers in peer list from
groups: Vec<String>, groups: Vec<String>,
} }
impl AllowList { impl AllowListMembers {
pub fn new(peers: Vec<String>, groups: Vec<String>) -> Self { pub fn new(peers: Vec<String>, groups: Vec<String>) -> Self {
AllowList{peers: peers, groups: groups} AllowListMembers {peers: peers, groups: groups}
} }
} }
@ -29,7 +30,7 @@ pub enum NewContactPolicy {
Accept, Accept,
/// AllowList is a list of handles that connections will be allowed from and connected to, and will be accepted /// AllowList is a list of handles that connections will be allowed from and connected to, and will be accepted
/// everything else will be ignored /// everything else will be ignored
AllowList(AllowList) AllowList(AllowListMembers)
} }
/// Settings for the bot on how it should automatically behave /// Settings for the bot on how it should automatically behave
@ -234,10 +235,19 @@ impl Imp {
self.process_contact(conversation.identifier); self.process_contact(conversation.identifier);
} }
// Allow list should add all people in the list
if let AllowList(allow_list) = &self.behaviour.new_contant_policy {
for handle in &allow_list.peers {
if let None = profile.find_conversation_id_by_handle(handle.clone()) {
self.cwtch.import_bundle(&profile.handle.clone(), &handle.clone());
}
}
}
self.profile = Some(profile); self.profile = Some(profile);
} }
Event::AppError { data } => { Event::AppError { data } => {
if initialized && data["Error"] == "Loaded 0 profiles" { if initialized && data.contains_key("Error") && data["Error"] == "Loaded 0 profiles" {
self.cwtch self.cwtch
.create_profile(&self.behaviour.profile_name, &self.password); .create_profile(&self.behaviour.profile_name, &self.password);
} }