libcwtch-rs/src/event.rs

568 lines
22 KiB
Rust

use std::collections::HashMap;
use chrono::{DateTime, FixedOffset};
use chrono::format::Fixed::TimezoneOffset;
use chrono::prelude::*;
use chrono::offset::LocalResult;
use crate::structs::{ACL, ConnectionState, CwtchEvent};
#[derive(Debug)]
pub struct ProfileIdentity(String);
impl Into<String> for ProfileIdentity {
fn into(self) -> String {
self.0.clone()
}
}
impl From<String> for ProfileIdentity {
fn from(x: String) -> Self {
ProfileIdentity(x)
}
}
#[derive(Debug)]
pub struct ContactIdentity(String);
impl From<String> for ContactIdentity {
fn from(x: String) -> Self {
ContactIdentity(x)
}
}
#[derive(Debug)]
pub struct ServerIdentity(String);
impl From<String> for ServerIdentity {
fn from(x: String) -> Self {
ServerIdentity(x)
}
}
pub type ConversationID = i32;
pub type FileKey = String;
#[derive(Debug)]
/// Enum for type of notification a UI/client should emit for a new message
pub enum MessageNotification {
/// means no notification
None,
/// means emit a notification that a message event happened only
SimpleEvent,
/// means emit a notification event with Conversation handle included
ContactInfo,
}
impl From<String> for MessageNotification {
fn from(str: String) -> MessageNotification {
match str.as_str() {
"None" => MessageNotification::None,
"SimpleEvent" => MessageNotification::SimpleEvent,
"ContactInfo" => MessageNotification::ContactInfo,
_ => MessageNotification::None,
}
}
}
#[derive(Debug)]
/// Enum for results from NetworkCheck plugin on profiles
pub enum NetworkCheckStatus {
/// There was an error, this profile cannot connect to itself
Error,
/// Success connecting to self, profile is "online"
Success
}
impl From<String> for NetworkCheckStatus {
fn from(str: String) -> NetworkCheckStatus {
match str.as_str() {
"Error" => NetworkCheckStatus::Error,
"Success" => NetworkCheckStatus::Success,
_ => NetworkCheckStatus::Error,
}
}
}
#[derive(Debug)]
/// Enum denoting server storage mode
pub enum ServerStorageType {
/// indicates some app supplied default password is being used on this server for storage encryption
DefaultPassword,
/// indicates a user supplied password is being used on this server for storage encryption
Password,
/// indicates no password and no encryption is being used on server storage
NoPassword
}
impl From<String> for ServerStorageType {
fn from(str: String) -> ServerStorageType {
match str.as_str() {
"storage-default-password" => ServerStorageType::DefaultPassword,
"storage-password" => ServerStorageType::Password,
"storage-no-password" => ServerStorageType::NoPassword,
_ => ServerStorageType::DefaultPassword,
}
}
}
#[derive(Debug)]
/// Enum used by servers to declare their intent to be running or stopped
pub enum ServerIntent {
/// a server intends to be running
Running,
/// a server intends to be stopped
Stopped
}
impl From<String> for ServerIntent {
fn from(str: String) -> ServerIntent {
match str.as_str() {
"running" => ServerIntent::Running,
"stopped" => ServerIntent::Stopped,
_ => ServerIntent::Stopped,
}
}
}
#[derive(Debug)]
/// Enum for events from Cwtch appbus
pub enum Event {
// App events
/// Emited by libcwtch once Cwtch.Start() has been completed successfully, you can start making API calls, lcg is initialized
CwtchStarted,
/// A new peer has been loaded, details of peer. Identity should be stored so further API calls can be made
NewPeer {
identity: ProfileIdentity,
tag: String,
created: bool,
name: String,
default_picture: String,
picture: String,
online: String,
contacts_json: String,
server_json: String, //ServerList
},
/// Cwtch had an error at the app level (not profile level), usually in response to an API call
AppError {
error: String
},
/// Global settings being emited from lcg, usually in response to them being sent to be saved by client
UpdateGlobalSettings {
settings: HashMap<String, String>,
},
/// A profile has an error, usually emited in response to an API call
PeerError {
error: String
},
/// A profile was successfully deleted, it is no longer usable
PeerDeleted {
identity: String
},
/// Cwtch is shutting down, stop making API calls
Shutdown,
/// indicates status of the ACN (tor), usually during boot.
ACNStatus {
/// the percent of ACN boot (-1 to 100)
progress: i8,
/// an associated status message from the ACN
status: String
},
/// Version of the ACN (currently tor)
ACNVersion {
verstion: String,
},
/// Notice from libCwtch that before completing load of a profile a storage migration is occuring so Start will take longer
StartingStorageMiragtion,
/// Notice from libCwtch that the storage migration is complete, profile being loaded resuming
DoneStorageMigration,
// app server events
/// A new server has been loaded
NewServer {
onion: ServerIdentity,
server_bundle: String,
description: String,
storage_type: ServerStorageType,
autostart: bool,
running: bool,
},
/// Response to request for server intent change, indicating the server is indending the new state
ServerIntentUpdate {
identity: ServerIdentity,
intent: ServerIntent
},
/// Notice a server was deleted (in response to an API call) and is no longer usable
ServerDeleted {
identity: ServerIdentity,
success: bool,
error: Option<String>,
},
/// Stats info for a server, periodically emited
ServerStatsUpdate {
identity: ServerIdentity,
total_messages: i32,
connections: i32,
},
// profile events
/// A new message was received
NewMessageFromPeer {
conversation_id: i32,
handle: String,
nick: String,
timestamp_received: DateTime<FixedOffset>,
message: String,
notification: MessageNotification,
picture: String,
},
/// A new contact has been created (imported, added, or contacted by)
ContactCreated {
conversation_id: i32,
remote_peer: ContactIdentity,
nick: String,
status: ConnectionState,
unread: i32,
picture: String,
default_picture: String,
num_messages: i32,
accepted: bool,
access_control_list: ACL,
blocked: bool,
loading: bool,
last_msg_time: DateTime<FixedOffset>,
},
/// A peer has changed state
PeerStateChange {
remote_peer: ContactIdentity,
connection_state: ConnectionState,
},
/// Notice from the network check plugin, a profile self check test by attempting to connecting to itself
NetworkStatus {
address: ProfileIdentity,
error: String,
status: NetworkCheckStatus,
},
/// Information from the ACN about a peer
ACNInfo {
handle: ContactIdentity,
key: String,
data: String,
},
/// a profile attribute has been updated with a new value
UpdatedProfileAttribute {
key: String,
value: String,
},
/// emited to confirm ack of a message succeeded
IndexedAcknowledgement {
conversation_id: i32,
index: i32,
},
/// emited to signal failure to ack a message
IndexedFailure {
conversation_id: i32,
index: i32,
handle: ContactIdentity,
error: String
},
/// a peer has acked a message
PeerAcknowledgement {
event_id: String,
remote_peer: ContactIdentity,
conversation_id: i32,
},
/// New message received on a group
NewMessageFromGroup {
conversation_id: i32,
timestamp_sent: DateTime<FixedOffset>,
remote_peer: ContactIdentity,
index: i32,
message: String,
content_hash: String,
picture: String,
nick: String,
notification: MessageNotification,
},
/// notice a group has been created
GroupCreated {
conversation_id: i32,
group_id: String,
group_server: String,
group_name: String,
picture: String,
access_control_list: ACL,
},
/// notice a new group exists
NewGroup {
conversation_id: i32,
group_id: String,
group_server: String,
group_invite: String,
group_name: String,
picture: String,
access_control_list: ACL,
},
/// a server connection state has changed
ServerStateChange {
group_server: String,
state: ConnectionState
},
/// A getval call to a peer has returned a response
NewRetValMessageFromPeer {
remote_peer: ContactIdentity,
scope: String,
path: String,
exists: bool,
data: String,
file_path: Option<String>
},
/// result of a call to share a file, the filekey and manifest to operate on it
ShareManifest {
filekey: String,
serializedManifest: String,
},
/// Information on a peer fileshare has been received
ManifestSizeReceived {
filekey: String,
manifest_size: i32,
handle: ContactIdentity,
},
/// An error has occured while trying to parse a peer sharefile
ManifestError {
handle: ContactIdentity,
filekey: String,
},
/// A peer message about a shared file has been received
ManifestReceived {
handle: ContactIdentity,
filekey: String,
serialized_manifest: String,
},
/// a received manfiest has been saved
ManifestSaved {
handle: ContactIdentity,
filekey: String,
serialized_manifest: String,
temp_file: String,
name_suggestion: String,
},
/// periodically emited status updates about an active download of a shared file
FileDownloadProgressUpdate {
filekey: String,
progress: i32,
filesize_in_chunks: i32,
name_suggestion: String,
},
// FileDownloaded, ??
/// Indicates an event was sent from libCwtch that we don't handle/didn't anticipate
/// still passing it on giving the user a chance to react, but should be reported so we can handle it properly
ErrUnhandled {
name: String,
data: HashMap<String, String>,
},
}
impl From<&CwtchEvent> for Event {
fn from(cwtch_event: &CwtchEvent) -> Self {
match cwtch_event.event_type.as_str() {
"CwtchStarted" => Event::CwtchStarted,
"NewPeer" => Event::NewPeer {
identity: cwtch_event.data["Identity"].clone().into(),
tag: cwtch_event.data["tag"].clone(),
created: cwtch_event.data["Created"] == "true",
name: cwtch_event.data["name"].clone(),
default_picture: cwtch_event.data["defaultPicture"].clone(),
picture: cwtch_event.data["picture"].clone(),
online: cwtch_event.data["Online"].clone(),
contacts_json: cwtch_event.data["ContactsJson"].clone(),
server_json: cwtch_event.data["ServerList"].clone(),
},
"NewMessageFromPeer" => Event::NewMessageFromPeer {
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-2),
handle: cwtch_event.data["RemotePeer"].clone(),
nick: cwtch_event.data["Nick"].clone(),
timestamp_received: DateTime::parse_from_rfc3339(cwtch_event.data["TimestampReceived"].as_str()).unwrap_or( DateTime::from(Utc::now())),
message: cwtch_event.data["Data"].clone(),
notification: MessageNotification::from(cwtch_event.data["notification"].clone()),
picture: cwtch_event.data["Picture"].clone(),
},
"PeerError" => Event::PeerError { error: cwtch_event.data["Error"].clone() },
"AppError" => Event::AppError {
error: cwtch_event.data["Error"].clone(),
},
"ContactCreated" => Event::ContactCreated {
conversation_id: cwtch_event.data["ConversationID"].clone().parse().unwrap_or(-2),
remote_peer: cwtch_event.data["RemotePeer"].clone().into(),
unread: cwtch_event.data["unread"].clone().parse().unwrap_or(0),
picture: cwtch_event.data["picture"].clone(),
default_picture: cwtch_event.data["defaultPicture"].clone(),
num_messages: cwtch_event.data["numMessages"].clone().parse().unwrap_or(0),
nick: cwtch_event.data["nick"].clone(),
accepted: cwtch_event.data["accepted"].clone().parse().unwrap_or(false),
status: ConnectionState::from(cwtch_event.data["status"].as_str()),
access_control_list: serde_json::from_str(cwtch_event.data["accessControlList"].as_str()).unwrap_or(ACL::new()),
blocked: cwtch_event.data["blocked"].clone().parse().unwrap_or(false),
loading: cwtch_event.data["loading"].clone().parse().unwrap_or(false),
last_msg_time: DateTime::parse_from_rfc3339(cwtch_event.data["lastMsgTime"].as_str()).unwrap_or(DateTime::from(Utc::now())),
},
"PeerStateChange" => Event::PeerStateChange {
remote_peer: cwtch_event.data["RemotePeer"].clone().into(),
connection_state: ConnectionState::from(cwtch_event.data["ConnectionState"].as_str()),
},
"UpdateGlobalSettings" => Event::UpdateGlobalSettings {
settings: serde_json::from_str(cwtch_event.data["Data"].as_str()).unwrap_or(HashMap::new()),
},
"PeerDeleted" => Event::PeerDeleted { identity: cwtch_event.data["Identity"].clone() },
"ACNStatus" => Event::ACNStatus {
progress: cwtch_event.data["Progress"].parse().unwrap_or(0),
status: cwtch_event.data["Status"].clone(),
},
"ACNVersion" => Event::ACNVersion { verstion: cwtch_event.data["Data"].clone()},
"NetworkError" => Event::NetworkStatus {
address: cwtch_event.data["Onion"].clone().into(),
error: cwtch_event.data["Error"].clone(),
status: NetworkCheckStatus::from(cwtch_event.data["Status"].clone())
},
"ACNInfo" => Event::ACNInfo {
handle: cwtch_event.data["Handle"].clone().into(),
key: cwtch_event.data["Key"].clone(),
data: cwtch_event.data["Data"].clone(),
},
"UpdatedProfileAttribute" => Event::UpdatedProfileAttribute {
key: cwtch_event.data["Key"].clone(),
value: cwtch_event.data["Data"].clone(),
},
"IndexedAcknowledgement" => Event::IndexedAcknowledgement {
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-1),
index: cwtch_event.data["Index"].parse().unwrap_or(-1),
},
"IndexedAcknowledgement" => Event::IndexedFailure {
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-1),
index: cwtch_event.data["Index"].parse().unwrap_or(-1),
handle: cwtch_event.data["Handle"].clone().into(),
error: cwtch_event.data["Error"].clone(),
},
"ShareManifest" => Event::ShareManifest {
filekey: cwtch_event.data["FileKey"].clone(),
serializedManifest: cwtch_event.data["SerializedManifest"].clone(),
},
"NewServer" => Event::NewServer {
onion: cwtch_event.data["Onion"].clone().into(),
server_bundle: cwtch_event.data["ServerBundle"].clone(),
description: cwtch_event.data["Description"].clone(),
storage_type: ServerStorageType::from(cwtch_event.data["StorageType"].clone()),
autostart: cwtch_event.data["Autostart"].parse().unwrap_or(false),
running: cwtch_event.data["Running"].parse().unwrap_or(false),
},
"ServerIntentUpdate" => Event::ServerIntentUpdate {
identity: cwtch_event.data["Identity"].clone().into(),
intent: ServerIntent::from(cwtch_event.data["Intent"].clone())
},
"ServerDeleted" => Event::ServerDeleted {
identity: cwtch_event.data["Identity"].clone().into(),
success: cwtch_event.data["Status"].clone() == "success",
error: match cwtch_event.data.get("Error") {
Some(e) => Some(e.clone()),
None => None,
}
},
"ServerStatsUpdate" => Event::ServerStatsUpdate {
identity: cwtch_event.data["Identity"].clone().into(),
total_messages: cwtch_event.data["TotalMessages"].parse().unwrap_or(0),
connections: cwtch_event.data["Connections"].parse().unwrap_or(0),
},
"ManifestSizeReceived" => Event::ManifestSizeReceived {
filekey: cwtch_event.data["FileKey"].clone(),
manifest_size: cwtch_event.data["ManifestSize"].parse().unwrap_or(0),
handle: cwtch_event.data["Handle"].clone().into(),
},
"ManifestError" => Event::ManifestError {
handle: cwtch_event.data["Handle"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone(),
},
"ManifestReceived" => Event::ManifestReceived {
handle: cwtch_event.data["Handle"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone(),
serialized_manifest: cwtch_event.data["SerializedManifest"].clone(),
},
"ManifestSaved" => Event::ManifestSaved {
handle: cwtch_event.data["Handle"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone(),
serialized_manifest: cwtch_event.data["SerializedManifest"].clone(),
temp_file: cwtch_event.data["TempFile"].clone(),
name_suggestion: cwtch_event.data["NameSuggestion"].clone(),
},
"FileDownloadProgressUpdate" => Event::FileDownloadProgressUpdate {
filekey: cwtch_event.data["FileKey"].clone(),
progress: cwtch_event.data["Progress"].parse().unwrap_or(0),
filesize_in_chunks: cwtch_event.data["FileSizeInChunks"].parse().unwrap_or(0),
name_suggestion: cwtch_event.data["NameSuggestion"].clone(),
},
"PeerAcknowledgement" => Event::PeerAcknowledgement {
event_id: cwtch_event.data["EventId"].clone(),
remote_peer: cwtch_event.data["RemotePeer"].clone().into(),
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(0)
},
"NewMessageFromGroup" => Event::NewMessageFromGroup {
timestamp_sent: DateTime::parse_from_rfc3339(cwtch_event.data["TimestampSent"].as_str()).unwrap_or( DateTime::from(Utc::now())),
index: cwtch_event.data["RemotePeer"].parse().unwrap_or(-1),
content_hash: cwtch_event.data["ContentHash"].clone(),
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-2),
remote_peer: cwtch_event.data["RemotePeer"].clone().into(),
nick: cwtch_event.data["Nick"].clone(),
message: cwtch_event.data["Data"].clone(),
notification: MessageNotification::from(cwtch_event.data["notification"].clone()),
picture: cwtch_event.data["Picture"].clone(),
},
"GroupCreated" => Event::GroupCreated {
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-2),
group_id: cwtch_event.data["GroupID"].clone(),
group_server: cwtch_event.data["GroupServer"].clone(),
group_name: cwtch_event.data["GroupName"].clone(),
picture: cwtch_event.data["picture"].clone(),
access_control_list: serde_json::from_str(cwtch_event.data["accessControlList"].as_str()).unwrap_or(ACL::new()),
},
"NewGroup" => Event::NewGroup {
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-2),
group_id: cwtch_event.data["GroupID"].clone(),
group_server: cwtch_event.data["GroupServer"].clone(),
group_invite: cwtch_event.data["GroupInvite"].clone(),
group_name: cwtch_event.data["GroupName"].clone(),
picture: cwtch_event.data["picture"].clone(),
access_control_list: serde_json::from_str(cwtch_event.data["accessControlList"].as_str()).unwrap_or(ACL::new()),
},
"ServerStateChange" => Event::ServerStateChange {
group_server: cwtch_event.data["GroupServer"].clone(),
state: ConnectionState::from(cwtch_event.data["ConnectionState"].as_str()),
},
"NewRetValMessageFromPeer" => Event::NewRetValMessageFromPeer {
remote_peer: cwtch_event.data["RemotePeer"].clone().into(),
scope: cwtch_event.data["Scope"].clone(),
path: cwtch_event.data["Path"].clone(),
exists: cwtch_event.data["Exists"].parse().unwrap_or(false),
data: cwtch_event.data["Data"].clone(),
file_path: match cwtch_event.data.get("FilePath") {
Some(fp) => Some(fp.to_string()),
None => None,
},
},
_ => Event::ErrUnhandled {
name: cwtch_event.event_type.to_string(),
data: cwtch_event.data.clone(),
},
}
}
}