libcwtch-rs/src/event.rs

846 lines
31 KiB
Rust
Raw Permalink Normal View History

2022-07-07 06:52:30 +00:00
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
2022-07-07 06:52:30 +00:00
use chrono::{DateTime, FixedOffset};
use chrono::prelude::*;
use std::convert::From;
2022-07-07 06:52:30 +00:00
use crate::structs::{ACL, ConnectionState, CwtchEvent, MessageWrapper, Profile, Settings};
2022-07-07 06:52:30 +00:00
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
/// Profile ID used to refer to profiles in Cwtch
pub struct ProfileIdentity(String);
impl From<String> for ProfileIdentity {
fn from(x: String) -> Self {
ProfileIdentity(x)
}
}
2022-07-15 19:23:13 +00:00
impl From<ProfileIdentity> for String {
fn from(x: ProfileIdentity) -> Self {
x.into()
}
}
impl From<&str> for ProfileIdentity {
fn from(x: &str) -> Self {
ProfileIdentity(x.to_string())
}
}
impl From<&ProfileIdentity> for String {
fn from(x: &ProfileIdentity) -> Self {
x.0.clone()
}
}
impl ProfileIdentity {
/// Get &str of ProfileIdentity String
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
/// Contact ID used to refer to contacts in Cwtch
pub struct ContactIdentity(String);
impl From<String> for ContactIdentity {
fn from(x: String) -> Self {
ContactIdentity(x)
}
}
impl ContactIdentity {
/// Get &str of ContactIdentity String
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, Hash, Eq, PartialEq)]
/// Conversation ID user to refer to a conversation with a Contact or Group in Cwtch
pub struct ConversationID(pub i32) ;
impl From<i32> for ConversationID {
fn from(x: i32) -> Self {
ConversationID(x)
}
}
2022-07-15 19:23:13 +00:00
impl From<ConversationID> for i32 {
fn from(x: ConversationID) -> Self {
x.0
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
/// Group ID used to refer to a Group in Cwtch
pub struct GroupID(String) ;
impl From<String> for GroupID {
fn from(x: String) -> Self {
GroupID(x)
}
}
impl GroupID {
/// Get &str of GroupID String
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
/// Server ID user to refer to a server in Cwtch
pub struct ServerIdentity(String);
impl From<String> for ServerIdentity {
fn from(x: String) -> Self {
ServerIdentity(x)
}
}
2022-07-15 19:23:13 +00:00
impl From<&str> for ServerIdentity {
fn from(x: &str) -> Self {
ServerIdentity(x.to_string())
}
}
2022-07-15 19:43:27 +00:00
impl From<ServerIdentity> for String {
2022-07-15 19:23:13 +00:00
fn from(x: ServerIdentity) -> Self {
2022-07-15 19:43:27 +00:00
x.into()
2022-07-15 19:23:13 +00:00
}
}
impl ServerIdentity {
/// Get &str of ServerIdentity String
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Hash, Eq, PartialEq)]
/// FileKey ID user to refer to a file share in Cwtch
pub struct FileKey(String);
impl From<String> for FileKey {
fn from(x: String) -> Self {
FileKey(x)
}
}
2022-07-07 21:30:20 +00:00
2022-07-15 19:43:27 +00:00
impl From<FileKey> for String {
fn from(x: FileKey) -> Self {
x.into()
}
}
impl From<&FileKey> for String {
fn from(x: &FileKey) -> Self {
x.into()
}
}
impl FileKey {
/// Get &str of FileKey String
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
#[derive(Debug, Clone)]
2022-07-07 21:30:20 +00:00
/// Enum for type of notification a UI/client should emit for a new message
2022-07-07 06:52:30 +00:00
pub enum MessageNotification {
2022-07-07 21:30:20 +00:00
/// means no notification
2022-07-07 06:52:30 +00:00
None,
2022-07-07 21:30:20 +00:00
/// means emit a notification that a message event happened only
2022-07-07 06:52:30 +00:00
SimpleEvent,
2022-07-07 21:30:20 +00:00
/// means emit a notification event with Conversation handle included
2022-07-07 06:52:30 +00:00
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, Clone)]
2022-07-07 21:30:20 +00:00
/// Enum for results from NetworkCheck plugin on profiles
2022-07-07 06:52:30 +00:00
pub enum NetworkCheckStatus {
2022-07-07 21:30:20 +00:00
/// There was an error, this profile cannot connect to itself
2022-07-07 06:52:30 +00:00
Error,
2022-07-07 21:30:20 +00:00
/// Success connecting to self, profile is "online"
2022-07-07 06:52:30 +00:00
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, Clone)]
2022-07-07 21:30:20 +00:00
/// Enum denoting server storage mode
2022-07-07 06:52:30 +00:00
pub enum ServerStorageType {
2022-07-07 21:30:20 +00:00
/// indicates some app supplied default password is being used on this server for storage encryption
2022-07-07 06:52:30 +00:00
DefaultPassword,
2022-07-07 21:30:20 +00:00
/// indicates a user supplied password is being used on this server for storage encryption
2022-07-07 06:52:30 +00:00
Password,
2022-07-07 21:30:20 +00:00
/// indicates no password and no encryption is being used on server storage
2022-07-07 06:52:30 +00:00
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, Clone)]
2022-07-07 21:30:20 +00:00
/// Enum used by servers to declare their intent to be running or stopped
2022-07-07 06:52:30 +00:00
pub enum ServerIntent {
2022-07-07 21:30:20 +00:00
/// a server intends to be running
2022-07-07 06:52:30 +00:00
Running,
2022-07-07 21:30:20 +00:00
/// a server intends to be stopped
2022-07-07 06:52:30 +00:00
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, Clone)]
2022-07-07 21:30:20 +00:00
/// Enum for events from Cwtch appbus
2022-07-07 06:52:30 +00:00
pub enum Event {
// App events
2022-07-07 21:30:20 +00:00
/// Emited by libcwtch once Cwtch.Start() has been completed successfully, you can start making API calls, lcg is initialized
2022-07-07 06:52:30 +00:00
CwtchStarted,
2022-07-07 21:30:20 +00:00
/// A new peer has been loaded, details of peer. Identity should be stored so further API calls can be made
2022-07-07 06:52:30 +00:00
NewPeer {
/// identity field
profile_id: ProfileIdentity,
/// optional client specified tag
2022-07-07 06:52:30 +00:00
tag: String,
/// is this a newly created profile event, or a load
2022-07-07 06:52:30 +00:00
created: bool,
/// user supplied name of the profile
2022-07-07 06:52:30 +00:00
name: String,
/// default picture path
2022-07-07 06:52:30 +00:00
default_picture: String,
/// user supplied picture path
2022-07-07 06:52:30 +00:00
picture: String,
/// is the profile online
2022-07-07 06:52:30 +00:00
online: String,
2022-07-15 21:39:32 +00:00
/// The deserialized profile with contacts and server info
profile_data: Result<Profile, String>,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// Cwtch had an error at the app level (not profile level), usually in response to an API call
2022-07-07 06:52:30 +00:00
AppError {
/// details of the app error that occured
error: String,
/// possible data about the error
data: String
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// Global settings being emited from lcg, usually in response to them being sent to be saved by client
2022-07-07 06:52:30 +00:00
UpdateGlobalSettings {
/// map of setting names to values (https://git.openprivacy.ca/cwtch.im/libcwtch-go/src/branch/trunk/utils/settings.go)
settings: Settings,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// A profile has an error, usually emited in response to an API call
2022-07-07 06:52:30 +00:00
PeerError {
/// details of the peer error that occured
2022-07-07 06:52:30 +00:00
error: String
},
2022-07-07 21:30:20 +00:00
/// A profile was successfully deleted, it is no longer usable
2022-07-07 06:52:30 +00:00
PeerDeleted {
/// identity of deleted peer
profile_id: ProfileIdentity
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// Cwtch is shutting down, stop making API calls
2022-07-07 06:52:30 +00:00
Shutdown,
2022-07-07 21:30:20 +00:00
/// indicates status of the ACN (tor), usually during boot.
2022-07-07 06:52:30 +00:00
ACNStatus {
2022-07-07 21:30:20 +00:00
/// the percent of ACN boot (-1 to 100)
2022-07-07 06:52:30 +00:00
progress: i8,
2022-07-07 21:30:20 +00:00
/// an associated status message from the ACN
2022-07-15 21:39:32 +00:00
status: String,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// Version of the ACN (currently tor)
2022-07-07 06:52:30 +00:00
ACNVersion {
/// version string from ACN app
version: String,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// Notice from libCwtch that before completing load of a profile a storage migration is occuring so Start will take longer
2022-07-07 06:52:30 +00:00
StartingStorageMiragtion,
2022-07-07 21:30:20 +00:00
/// Notice from libCwtch that the storage migration is complete, profile being loaded resuming
2022-07-07 06:52:30 +00:00
DoneStorageMigration,
// app server events
2022-07-07 21:30:20 +00:00
/// A new server has been loaded
2022-07-07 06:52:30 +00:00
NewServer {
/// identity of server
server: ServerIdentity,
/// sharable / importable server bundle
2022-07-07 06:52:30 +00:00
server_bundle: String,
/// user supplied description
2022-07-07 06:52:30 +00:00
description: String,
/// storage mode the server is using
2022-07-07 06:52:30 +00:00
storage_type: ServerStorageType,
/// does/should the server auto start on cwtch start
2022-07-07 06:52:30 +00:00
autostart: bool,
/// is the server running
2022-07-07 06:52:30 +00:00
running: bool,
},
2022-07-07 21:30:20 +00:00
/// Response to request for server intent change, indicating the server is indending the new state
2022-07-07 06:52:30 +00:00
ServerIntentUpdate {
/// identity of server
server: ServerIdentity,
/// intent of the server to be running or not
2022-07-15 21:39:32 +00:00
intent: ServerIntent,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// Notice a server was deleted (in response to an API call) and is no longer usable
2022-07-07 06:52:30 +00:00
ServerDeleted {
/// identity of server
server: ServerIdentity,
/// was deletion a success
2022-07-07 06:52:30 +00:00
success: bool,
/// optional error string in case of failure to delete
2022-07-07 06:52:30 +00:00
error: Option<String>,
},
2022-07-07 21:30:20 +00:00
/// Stats info for a server, periodically emited
2022-07-07 06:52:30 +00:00
ServerStatsUpdate {
/// identity of server
server: ServerIdentity,
/// count of total messages on the server
2022-07-07 06:52:30 +00:00
total_messages: i32,
/// count of total current connections to the server
2022-07-07 06:52:30 +00:00
connections: i32,
},
// profile events
2022-07-07 21:30:20 +00:00
/// A new message was received
2022-07-07 06:52:30 +00:00
NewMessageFromPeer {
/// identity field
profile_id: ProfileIdentity,
/// conversation id
conversation_id: ConversationID,
/// contact id
contact_id: ContactIdentity,
/// name of contact
2022-07-07 06:52:30 +00:00
nick: String,
/// time message was received
2022-07-07 06:52:30 +00:00
timestamp_received: DateTime<FixedOffset>,
/// the message
message: MessageWrapper,
/// notification instructions (based on settings)
2022-07-07 06:52:30 +00:00
notification: MessageNotification,
/// path to picture for the contact
2022-07-07 06:52:30 +00:00
picture: String,
},
2022-07-07 21:30:20 +00:00
/// A new contact has been created (imported, added, or contacted by)
2022-07-07 06:52:30 +00:00
ContactCreated {
/// identity field
profile_id: ProfileIdentity,
/// conversation id
conversation_id: ConversationID,
/// contact id
contact_id: ContactIdentity,
/// name of group
2022-07-07 06:52:30 +00:00
nick: String,
/// connection status to group server
2022-07-07 06:52:30 +00:00
status: ConnectionState,
/// number of unread messages in group
2022-07-07 06:52:30 +00:00
unread: i32,
/// path to picture for group
2022-07-07 06:52:30 +00:00
picture: String,
/// path to default picture for group
2022-07-07 06:52:30 +00:00
default_picture: String,
/// total number of messages in group
2022-07-07 06:52:30 +00:00
num_messages: i32,
/// has the user accepted the group
2022-07-07 06:52:30 +00:00
accepted: bool,
/// ACL for the group
2022-07-07 06:52:30 +00:00
access_control_list: ACL,
/// is the group blocked
2022-07-07 06:52:30 +00:00
blocked: bool,
/// is the group syncing
2022-07-07 06:52:30 +00:00
loading: bool,
/// time of last message from the group
2022-07-07 06:52:30 +00:00
last_msg_time: DateTime<FixedOffset>,
},
2022-07-07 21:30:20 +00:00
/// A peer has changed state
2022-07-07 06:52:30 +00:00
PeerStateChange {
/// identity field
profile_id: ProfileIdentity,
/// contact id
contact_id: ContactIdentity,
/// connection status to contact
2022-07-07 06:52:30 +00:00
connection_state: ConnectionState,
},
2022-07-07 21:30:20 +00:00
/// Notice from the network check plugin, a profile self check test by attempting to connecting to itself
2022-07-07 06:52:30 +00:00
NetworkStatus {
/// profile the check was performed on
profile_id: ProfileIdentity,
2022-07-15 21:39:32 +00:00
// it's technically a profile self check
/// error if there was one (can be empty)
2022-07-07 06:52:30 +00:00
error: String,
/// status of profile self connection check
2022-07-07 06:52:30 +00:00
status: NetworkCheckStatus,
},
2022-07-07 21:30:20 +00:00
/// Information from the ACN about a peer
2022-07-07 06:52:30 +00:00
ACNInfo {
/// identity field
profile_id: ProfileIdentity,
/// contact id
contact_id: ContactIdentity,
/// key of info
2022-07-07 06:52:30 +00:00
key: String,
/// data of info
2022-07-07 06:52:30 +00:00
data: String,
},
2022-07-07 21:30:20 +00:00
/// a profile attribute has been updated with a new value
2022-07-07 06:52:30 +00:00
UpdatedProfileAttribute {
/// identity field
profile_id: ProfileIdentity,
/// attribute key
2022-07-07 06:52:30 +00:00
key: String,
/// attribute new value
2022-07-07 06:52:30 +00:00
value: String,
},
2022-07-07 21:30:20 +00:00
/// emited to confirm ack of a message succeeded
2022-07-07 06:52:30 +00:00
IndexedAcknowledgement {
/// identity field
profile_id: ProfileIdentity,
/// conversation id
2022-07-07 06:52:30 +00:00
conversation_id: i32,
/// index of message acked
2022-07-07 06:52:30 +00:00
index: i32,
},
2022-07-07 21:30:20 +00:00
/// emited to signal failure to ack a message
2022-07-07 06:52:30 +00:00
IndexedFailure {
/// identity field
profile_id: ProfileIdentity,
/// conversation id
conversation_id: ConversationID,
/// index of failure of message to ack
2022-07-07 06:52:30 +00:00
index: i32,
/// contact id
contact_id: ContactIdentity,
/// error string
2022-07-15 21:39:32 +00:00
error: String,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// a peer has acked a message
2022-07-07 06:52:30 +00:00
PeerAcknowledgement {
/// identity field
profile_id: ProfileIdentity,
/// message id this is an ack to
2022-07-07 06:52:30 +00:00
event_id: String,
/// contact id
contact_id: ContactIdentity,
/// conversation id
2022-07-07 06:52:30 +00:00
conversation_id: i32,
},
2022-07-07 21:30:20 +00:00
/// New message received on a group
2022-07-07 06:52:30 +00:00
NewMessageFromGroup {
/// identity field
profile_id: ProfileIdentity,
/// conversation id
conversation_id: ConversationID,
/// time of message
2022-07-07 06:52:30 +00:00
timestamp_sent: DateTime<FixedOffset>,
/// contact id
contact_id: ContactIdentity,
/// message index
2022-07-07 06:52:30 +00:00
index: i32,
/// the message
message: MessageWrapper,
/// hash of the message
2022-07-07 06:52:30 +00:00
content_hash: String,
/// path to picture for sender
2022-07-07 06:52:30 +00:00
picture: String,
/// notification policy (based on settings)
2022-07-07 06:52:30 +00:00
notification: MessageNotification,
},
2022-07-07 21:30:20 +00:00
/// notice a group has been created
2022-07-07 06:52:30 +00:00
GroupCreated {
/// identity field
profile_id: ProfileIdentity,
/// conversation id
conversation_id: ConversationID,
/// group id
group_id: GroupID,
/// server the group is on
2022-07-07 06:52:30 +00:00
group_server: String,
/// name of group
2022-07-07 06:52:30 +00:00
group_name: String,
/// path to picture for group
2022-07-07 06:52:30 +00:00
picture: String,
/// Access Control List for group
2022-07-07 06:52:30 +00:00
access_control_list: ACL,
},
2022-07-07 21:30:20 +00:00
/// notice a new group exists
2022-07-07 06:52:30 +00:00
NewGroup {
/// identity field
profile_id: ProfileIdentity,
/// conversation id
conversation_id: ConversationID,
/// server the group is on
2022-07-07 06:52:30 +00:00
group_server: String,
/// invite string
2022-07-07 06:52:30 +00:00
group_invite: String,
/// group name
2022-07-07 06:52:30 +00:00
group_name: String,
},
2022-07-07 21:30:20 +00:00
/// a server connection state has changed
2022-07-07 06:52:30 +00:00
ServerStateChange {
/// identity field
profile_id: ProfileIdentity,
/// server the group is on
2022-07-07 06:52:30 +00:00
group_server: String,
/// state of connection to server
2022-07-15 21:39:32 +00:00
state: ConnectionState,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// A getval call to a peer has returned a response
2022-07-07 06:52:30 +00:00
NewRetValMessageFromPeer {
/// identity field
profile_id: ProfileIdentity,
/// conversation id
contact_id: ContactIdentity,
/// scope of the val
2022-07-07 06:52:30 +00:00
scope: String,
/// path of the val (zone.key)
2022-07-07 06:52:30 +00:00
path: String,
/// does the queried for value exist
2022-07-07 06:52:30 +00:00
exists: bool,
/// value
2022-07-07 06:52:30 +00:00
data: String,
/// optional filepath if there was a downloaded component
2022-07-15 21:39:32 +00:00
file_path: Option<String>,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// result of a call to share a file, the filekey and manifest to operate on it
2022-07-07 06:52:30 +00:00
ShareManifest {
/// identity field
profile_id: ProfileIdentity,
/// filekey
filekey: FileKey,
/// serialized manifest of the share
2022-07-15 21:39:32 +00:00
serialized_manifest: String,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// Information on a peer fileshare has been received
2022-07-07 06:52:30 +00:00
ManifestSizeReceived {
/// identity field
profile_id: ProfileIdentity,
/// filekey
filekey: FileKey,
/// size of manifest received for a share
2022-07-07 06:52:30 +00:00
manifest_size: i32,
/// contact id
contact_id: ContactIdentity,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// An error has occured while trying to parse a peer sharefile
2022-07-07 06:52:30 +00:00
ManifestError {
/// identity field
profile_id: ProfileIdentity,
/// contact id
contact_id: ContactIdentity,
/// filekey
filekey: FileKey,
2022-07-07 06:52:30 +00:00
},
2022-07-07 21:30:20 +00:00
/// A peer message about a shared file has been received
2022-07-07 06:52:30 +00:00
ManifestReceived {
/// identity field
profile_id: ProfileIdentity,
/// contact id
contact_id: ContactIdentity,
/// filekey
filekey: FileKey,
/// serialized manifest
2022-07-07 06:52:30 +00:00
serialized_manifest: String,
},
2022-07-07 21:30:20 +00:00
/// a received manfiest has been saved
2022-07-07 06:52:30 +00:00
ManifestSaved {
/// identity field
profile_id: ProfileIdentity,
/// contact id
contact_id: ContactIdentity,
/// filekey
filekey: FileKey,
/// serialized manifest
2022-07-07 06:52:30 +00:00
serialized_manifest: String,
/// temporary storage path for share download
2022-07-07 06:52:30 +00:00
temp_file: String,
/// contact suggested share file name
2022-07-07 06:52:30 +00:00
name_suggestion: String,
},
2022-07-07 21:30:20 +00:00
/// periodically emited status updates about an active download of a shared file
2022-07-07 06:52:30 +00:00
FileDownloadProgressUpdate {
/// identity field
profile_id: ProfileIdentity,
/// filekey
filekey: FileKey,
/// progress of download of share in chunks
2022-07-07 06:52:30 +00:00
progress: i32,
/// size of share in chunks
2022-07-07 06:52:30 +00:00
filesize_in_chunks: i32,
/// contact suggested name for file
2022-07-07 06:52:30 +00:00
name_suggestion: String,
},
// FileDownloaded, ??
2022-07-07 21:30:20 +00:00
/// 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 of unhandled event
2022-07-07 21:30:20 +00:00
name: String,
/// map of key:val attributes of unhandled event
2022-07-07 21:30:20 +00:00
data: HashMap<String, String>,
},
2022-07-07 06:52:30 +00:00
}
impl From<&CwtchEvent> for Event {
fn from(cwtch_event: &CwtchEvent) -> Self {
println!("EVENT: {:?}", cwtch_event);
2022-07-07 06:52:30 +00:00
match cwtch_event.event_type.as_str() {
"CwtchStarted" => Event::CwtchStarted,
"NewPeer" => Event::NewPeer {
profile_id: cwtch_event.data["Identity"].clone().into(),
2022-07-15 21:39:32 +00:00
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(),
profile_data: Profile::new(
cwtch_event.data["Identity"].clone().into(),
2022-07-15 21:39:32 +00:00
&cwtch_event.data["name"],
&cwtch_event.data["picture"],
&cwtch_event.data["ContactsJson"],
&cwtch_event.data["ServerList"],
)
2022-07-07 06:52:30 +00:00
},
"NewMessageFromPeer" => Event::NewMessageFromPeer {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-2).into(),
contact_id: cwtch_event.data["RemotePeer"].clone().into(),
2022-07-07 06:52:30 +00:00
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: MessageWrapper::from_json(&cwtch_event.data["Data"]),
2022-07-07 06:52:30 +00:00
notification: MessageNotification::from(cwtch_event.data["notification"].clone()),
picture: cwtch_event.data["picture"].clone(),
2022-07-07 06:52:30 +00:00
},
"PeerError" => Event::PeerError { error: cwtch_event.data["Error"].clone() },
"AppError" => Event::AppError {
error: match cwtch_event.data.contains_key("Error") {
true => cwtch_event.data["Error"].clone(),
false => "".to_string()
},
data: match cwtch_event.data.contains_key("Data") {
true => cwtch_event.data["Data"].clone(),
false => "".to_string()
}
2022-07-07 06:52:30 +00:00
},
"ContactCreated" => Event::ContactCreated {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
conversation_id: cwtch_event.data["ConversationID"].clone().parse().unwrap_or(-2).into(),
contact_id: cwtch_event.data["RemotePeer"].clone().into(),
2022-07-07 06:52:30 +00:00
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 {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
contact_id: cwtch_event.data["RemotePeer"].clone().into(),
2022-07-07 06:52:30 +00:00
connection_state: ConnectionState::from(cwtch_event.data["ConnectionState"].as_str()),
},
"UpdateGlobalSettings" => Event::UpdateGlobalSettings {
settings: serde_json::from_str(cwtch_event.data["Data"].as_str()).expect("could not parse settings from libCwtch-go"),
2022-07-07 06:52:30 +00:00
},
"PeerDeleted" => Event::PeerDeleted { profile_id: cwtch_event.data["Identity"].clone().into() },
2022-07-07 06:52:30 +00:00
"ACNStatus" => Event::ACNStatus {
progress: cwtch_event.data["Progress"].parse().unwrap_or(0),
status: cwtch_event.data["Status"].clone(),
},
"ACNVersion" => Event::ACNVersion { version: cwtch_event.data["Data"].clone()},
2022-07-07 06:52:30 +00:00
"NetworkError" => Event::NetworkStatus {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
2022-07-07 06:52:30 +00:00
error: cwtch_event.data["Error"].clone(),
status: NetworkCheckStatus::from(cwtch_event.data["Status"].clone())
},
"ACNInfo" => Event::ACNInfo {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
contact_id: cwtch_event.data["Handle"].clone().into(),
2022-07-07 06:52:30 +00:00
key: cwtch_event.data["Key"].clone(),
data: cwtch_event.data["Data"].clone(),
},
"UpdatedProfileAttribute" => Event::UpdatedProfileAttribute {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
2022-07-07 06:52:30 +00:00
key: cwtch_event.data["Key"].clone(),
value: cwtch_event.data["Data"].clone(),
},
"IndexedAcknowledgement" => Event::IndexedAcknowledgement {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-1).into(),
2022-07-07 06:52:30 +00:00
index: cwtch_event.data["Index"].parse().unwrap_or(-1),
},
"ShareManifest" => Event::ShareManifest {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone().into(),
2022-07-15 21:39:32 +00:00
serialized_manifest: cwtch_event.data["SerializedManifest"].clone(),
2022-07-07 06:52:30 +00:00
},
"NewServer" => Event::NewServer {
server: cwtch_event.data["Onion"].clone().into(),
2022-07-07 06:52:30 +00:00
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 {
server: cwtch_event.data["Identity"].clone().into(),
2022-07-07 06:52:30 +00:00
intent: ServerIntent::from(cwtch_event.data["Intent"].clone())
},
"ServerDeleted" => Event::ServerDeleted {
server: cwtch_event.data["Identity"].clone().into(),
2022-07-07 06:52:30 +00:00
success: cwtch_event.data["Status"].clone() == "success",
error: match cwtch_event.data.get("Error") {
Some(e) => Some(e.clone()),
None => None,
}
},
"ServerStatsUpdate" => Event::ServerStatsUpdate {
server: cwtch_event.data["Identity"].clone().into(),
2022-07-07 06:52:30 +00:00
total_messages: cwtch_event.data["TotalMessages"].parse().unwrap_or(0),
connections: cwtch_event.data["Connections"].parse().unwrap_or(0),
},
"ManifestSizeReceived" => Event::ManifestSizeReceived {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone().into(),
2022-07-07 06:52:30 +00:00
manifest_size: cwtch_event.data["ManifestSize"].parse().unwrap_or(0),
contact_id: cwtch_event.data["Handle"].clone().into(),
2022-07-07 06:52:30 +00:00
},
"ManifestError" => Event::ManifestError {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
contact_id: cwtch_event.data["Handle"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone().into(),
2022-07-07 06:52:30 +00:00
},
"ManifestReceived" => Event::ManifestReceived {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
contact_id: cwtch_event.data["Handle"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone().into(),
2022-07-07 06:52:30 +00:00
serialized_manifest: cwtch_event.data["SerializedManifest"].clone(),
},
"ManifestSaved" => Event::ManifestSaved {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
contact_id: cwtch_event.data["Handle"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone().into(),
2022-07-07 06:52:30 +00:00
serialized_manifest: cwtch_event.data["SerializedManifest"].clone(),
temp_file: cwtch_event.data["TempFile"].clone(),
name_suggestion: cwtch_event.data["NameSuggestion"].clone(),
},
"FileDownloadProgressUpdate" => Event::FileDownloadProgressUpdate {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
filekey: cwtch_event.data["FileKey"].clone().into(),
2022-07-07 06:52:30 +00:00
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 {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
event_id: cwtch_event.data["EventID"].clone(),
contact_id: cwtch_event.data["RemotePeer"].clone().into(),
conversation_id: cwtch_event.data.get("ConversationID").unwrap_or(&"0".to_string()).parse().unwrap_or(0)
2022-07-07 06:52:30 +00:00
},
"NewMessageFromGroup" => Event::NewMessageFromGroup {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
2022-07-07 06:52:30 +00:00
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).into(),
contact_id: cwtch_event.data["RemotePeer"].clone().into(),
message: MessageWrapper::from_json(&cwtch_event.data["Data"]),
2022-07-07 06:52:30 +00:00
notification: MessageNotification::from(cwtch_event.data["notification"].clone()),
picture: cwtch_event.data["picture"].clone(),
2022-07-07 06:52:30 +00:00
},
"GroupCreated" => Event::GroupCreated {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-2).into(),
group_id: cwtch_event.data["GroupID"].clone().into(),
2022-07-07 06:52:30 +00:00
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 {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
conversation_id: cwtch_event.data["ConversationID"].parse().unwrap_or(-2).into(),
2022-07-07 06:52:30 +00:00
group_server: cwtch_event.data["GroupServer"].clone(),
group_name: cwtch_event.data["GroupName"].clone(),
2023-10-21 17:00:17 +00:00
group_invite: cwtch_event.data["GroupInvite"].clone(),
2022-07-07 06:52:30 +00:00
},
"ServerStateChange" => Event::ServerStateChange {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
2022-07-07 06:52:30 +00:00
group_server: cwtch_event.data["GroupServer"].clone(),
state: ConnectionState::from(cwtch_event.data["ConnectionState"].as_str()),
},
"NewRetValMessageFromPeer" => Event::NewRetValMessageFromPeer {
profile_id: cwtch_event.data["ProfileOnion"].clone().into(),
contact_id: cwtch_event.data["RemotePeer"].clone().into(),
2022-07-07 06:52:30 +00:00
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(),
},
}
}
}