fixing contact struct to match accepted/blocked; adding Settings struct and save fn; adding Experiments enums; adding ConnectionSate::new(String)

This commit is contained in:
Dan Ballard 2022-04-04 18:10:02 -07:00
parent 5bed265440
commit 70c043461b
1 changed files with 120 additions and 2 deletions

View File

@ -1,4 +1,5 @@
use crate::structs::ConnectionState::Disconnected;
use crate::CwtchLib;
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, DefaultOnError};
use std::collections::HashMap;
@ -28,6 +29,22 @@ impl Default for ConnectionState {
}
}
impl ConnectionState {
/// Creates a ConnectionState from a string sent from libcwtch-go
pub fn new(name: &str) -> Self {
match name {
"Disconnected" => ConnectionState::Disconnected,
"Connecting" => ConnectionState::Connecting,
"Connected" => ConnectionState::Connected,
"Authenticated" => ConnectionState::Authenticated,
"Synced" => ConnectionState::Synced,
"Failed" => ConnectionState::Failed,
"Killed" => ConnectionState::Killed,
_ => ConnectionState::Disconnected,
}
}
}
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
/// Defines the various authorization modes a contact can be in
@ -60,14 +77,18 @@ pub struct CwtchEvent {
pub struct Contact {
/// onion address / id of the contact
pub onion: String,
/// unique identifier of the contact/conversation to be used in API access
pub identifier: i32,
/// display name of the contact, as determined in libcwtch-go from name specified by contact
pub name: String,
#[serde_as(deserialize_as = "DefaultOnError")]
// cwtch loads profile/contacts from storage and leaves status blank, it's filled in "soon" by events...
/// contact connection status
pub status: ConnectionState,
/// contact authorization state as set by profile
pub authorization: ContactAuthorization,
/// has the contact been manually accpted
pub accepted: bool,
/// has the contact been manually blocked
pub blocked: bool,
/// is this contact a group? if so "onion" will be a group ID
pub is_group: bool,
//attr: HashMap<String, String>,
@ -109,6 +130,103 @@ pub struct Message {
pub d: String,
}
#[derive(Debug, Serialize, Deserialize)]
/// Settings as defined in libcwtch-go/utils/settings.go and should be populated by handeling the UpdateGlobalSettings event emited during cwtch.start()
#[allow(non_snake_case)]
pub struct Settings {
/// locale for the UI to use
pub Locale: String,
/// theme of the UI
pub Theme: String,
/// Theme mode of the ui, light or dark
pub ThemeMode: String,
/// previous pid of the run, managed by libcwtch-go
pub PreviousPid: i64,
/// controls if subsequent experiments are enabled at all
pub ExperimentsEnabled: bool,
/// map of experiment names and their enabled status
pub Experiments: HashMap<String, bool>,
/// Should the app block unknown contacts
pub BlockUnknownConnections: bool,
// Notification policy of a UI app
//pub NotificationPolicy: String, //Todo: NotificationPolicy struct
// Notification content to show for a UI app
//pub NotificationContent: String,
/// Should the UI hide contact IDs
pub StreamerMode: bool,
/// Unused?
pub StateRootPane: i32,
/// is this the first run
pub FirstTime: bool,
/// UI column mode
pub UIColumnModePortrait: String,
/// UI column mode
pub UIColumnModeLandscape: String,
/// Path to download files to
pub DownloadPath: String,
// Turn on advanced tor config in the UI
//pub AllowAdvancedTorConfig: bool,
// Custom torrc value
//pub CustomTorrc: String,
// Use the value of CustomTorrc with tor
//pub UseCustomTorrc: bool,
// Unused? delete
//pub UseExternalTor: bool,
// Tor socks port, if not default
//pub CustomSocksPort: i32,
// Tor control port if not default
//pub CustomControlPort: i32,
// Use tor cache for faster start
//pub UseTorCache: bool,
// Tor config dir
//pub TorCacheDir: String,
}
/// Enum of experiment types that can be managed in Settings
pub enum Experiments {
/// experiment enabeling in app management and running of Cwtch servers
ServersExperiment,
/// experiment enabeling use of Cwtch groups
GroupExperiment,
/// experiment enabeling filesharing
FileSharingExperiment,
/// experiment enabeling auto downloading of image files to Settings::DownloadPath
ImagePreviewsExperiment,
}
impl Experiments {
/// returns the experiment settings key
pub fn to_key_string(self) -> String {
match self {
Experiments::ServersExperiment => "servers-experiment".to_string(),
Experiments::GroupExperiment => "tapir-groups-experiment".to_string(),
Experiments::FileSharingExperiment => "filesharing".to_string(),
Experiments::ImagePreviewsExperiment => "filesharing-images".to_string(),
}
}
}
impl Settings {
/// Given a CwtchLib, handles sending an event to it with updated settings represented by this struct for saving
pub fn save(&self, cwtch: &dyn CwtchLib) -> Result<(), String> {
let settings_json = match serde_json::to_string(&self) {
Ok(s) => s,
Err(e) => return Err(e.to_string()),
};
let save_settings_event: CwtchEvent = CwtchEvent {
event_type: "UpdateGlobalSettings".to_string(),
event_ID: "0".to_string(),
data: HashMap::from([("Data".to_string(), settings_json)]),
};
let event_json = match serde_json::to_string(&save_settings_event) {
Ok(s) => s,
Err(e) => return Err(e.to_string()),
};
cwtch.send_app_event(&event_json);
return Ok(());
}
}
impl Profile {
/// Create a new profile populated from supplied data
/// contacts_json as supplied by libcwtch-go, a map of contacts