implement group policy; group api fixes in experimental libcwtch-rs

This commit is contained in:
Dan Ballard 2023-10-21 10:02:24 -07:00
parent 11485a3d8f
commit 76fa84bd66
2 changed files with 42 additions and 17 deletions

View File

@ -29,6 +29,15 @@ pub enum NewContactPolicy {
AllowList(AllowListMembers)
}
pub enum GroupInvitePolicy {
/// Ignore all group invites
IgnoreAll,
/// Accept all group invites
AcceptAll,
/// Only accept group invites from contacts. Pairs strongly with NewContactPolicy of AllowList
AcceptFromContact,
}
/// Settings for the bot on how it should automatically behave
pub struct Behaviour {
/// The bot will enable experimental feautres (required for any experiments to be used)
@ -47,6 +56,9 @@ pub struct Behaviour {
/// Policy dictacting how the bot should automatically handle ContactCreated events
pub new_contant_policy: NewContactPolicy,
/// Policy dictating how the bot should handle Group Invites
pub group_invite_policy: GroupInvitePolicy,
}
/// intermediary struct for building a Behaviour using builder patern
@ -63,6 +75,7 @@ impl BehaviourBuilder {
proto_experiment_fileshare: false,
proto_experiment_groups: false,
new_contant_policy: NewContactPolicy::Ignore,
group_invite_policy: GroupInvitePolicy::IgnoreAll,
profile_name: "".to_string(),
profile_pic_path: None,
},
@ -107,4 +120,9 @@ impl BehaviourBuilder {
self.behaviour.new_contant_policy = val;
self
}
pub fn group_invite_policy(mut self, val: GroupInvitePolicy) -> Self {
self.behaviour.group_invite_policy = val;
self
}
}

View File

@ -1,9 +1,11 @@
use chrono::{DateTime, FixedOffset};
use libcwtch::structs::*;
use libcwtch::CwtchLib;
use libcwtch::event::{ConversationID, Event};
use libcwtch::event::{ContactIdentity, ConversationID, Event};
use libcwtch::structs::MessageType::InviteGroup;
use crate::behaviour::{Behaviour, NewContactPolicy};
use crate::behaviour::{Behaviour, GroupInvitePolicy, NewContactPolicy};
use crate::behaviour::GroupInvitePolicy::AcceptAll;
use crate::behaviour::NewContactPolicy::AllowList;
/// Trait to be used by implementors of imp bots to supply their custom event handling
@ -20,7 +22,7 @@ pub trait EventHandler {
fn on_new_message_from_contact(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: ConversationID, handle: String, timestamp_received: DateTime<FixedOffset>, message: MessageWrapper) {}
#[allow(unused_variables)]
fn on_new_message_from_group(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: ConversationID, handle: String, timestamp_sent: DateTime<FixedOffset>, message: MessageWrapper) {}
fn on_new_message_from_group(&self, cwtch: &dyn libcwtch::CwtchLib, profile: &Profile, conversation_id: ConversationID, contact: ContactIdentity, timestamp_sent: DateTime<FixedOffset>, message: MessageWrapper) {}
}
/// Cwtch bot
@ -111,14 +113,6 @@ impl Imp {
panic!("error parsing profile: {}", e);
}
// Share profile image
match self.settings.as_ref() {
Some(_settings) => {
self.cwtch.share_file(&profile_id, ConversationID(-1), "build_bot.png");
}
None => (),
};
self.cwtch.set_profile_attribute(
&profile_id,
"profile.name",
@ -139,7 +133,6 @@ impl Imp {
}
}
self.profile = Some(ok_profile.clone());
}
}
@ -190,14 +183,28 @@ impl Imp {
}
}
Event::NewMessageFromPeer {profile_id, conversation_id,contact_id, nick, timestamp_received, message, notification, picture } => {
match self.profile.as_ref() {
Some(profile) => handler.on_new_message_from_contact(self.cwtch.as_ref(), profile, conversation_id.clone(), nick.clone(), timestamp_received.clone(), message.clone()),
None => {},
if message.o == InviteGroup {
if let Some(profile) = self.profile.as_ref() {
match self.behaviour.group_invite_policy {
GroupInvitePolicy::IgnoreAll => (),
GroupInvitePolicy::AcceptAll => self.cwtch.import_bundle(profile_id, message.d.as_str()),
GroupInvitePolicy::AcceptFromContact=> {
if profile.conversations[conversation_id].accepted {
self.cwtch.import_bundle(profile_id, message.d.as_str())
}
}
}
}
} else {
match self.profile.as_ref() {
Some(profile) => handler.on_new_message_from_contact(self.cwtch.as_ref(), profile, conversation_id.clone(), nick.clone(), timestamp_received.clone(), message.clone()),
None => {},
}
}
}
Event::NewMessageFromGroup { profile_id, conversation_id, timestamp_sent, contact_id, index, message, content_hash, picture, nick, notification, } => {
Event::NewMessageFromGroup { profile_id, conversation_id, timestamp_sent, contact_id, index, message, content_hash, picture, notification, } => {
if let Some(profile )= self.profile.as_ref() {
handler.on_new_message_from_group(self.cwtch.as_ref(), profile, conversation_id.clone(), nick.clone(), timestamp_sent.clone(), message.clone())
handler.on_new_message_from_group(self.cwtch.as_ref(), profile, conversation_id.clone(), contact_id.clone(), timestamp_sent.clone(), message.clone())
}
}
Event::ErrUnhandled { name, data } => eprintln!("unhandled event: {}!", name),