From 559a437971185fd7e218424e4ea3c504e7f96b2c Mon Sep 17 00:00:00 2001 From: Sun Date: Tue, 5 Apr 2022 16:29:30 +0800 Subject: [PATCH] upgrade to new chat --- Cargo.toml | 8 +-- src/apps.rs | 4 +- src/apps/chat/layer.rs | 20 +++--- src/apps/chat/models.rs | 3 +- src/apps/chat/models/message.rs | 2 +- src/apps/chat/rpc.rs | 19 +++--- src/apps/group/layer.rs | 2 +- src/apps/group/models/message.rs | 4 +- src/apps/group/rpc.rs | 4 +- src/apps/jarvis/models.rs | 3 +- src/apps/jarvis/rpc.rs | 3 +- src/event.rs | 3 +- src/layer.rs | 5 +- src/rpc.rs | 5 +- src/server.rs | 16 +++-- types/README.md | 11 ++-- types/chat/Cargo.toml | 14 ----- types/chat/src/lib.rs | 69 --------------------- types/cloud/Cargo.toml | 2 +- types/cloud/src/lib.rs | 2 +- types/dao/Cargo.toml | 6 +- types/dao/src/lib.rs | 5 +- types/data/Cargo.toml | 4 +- types/domain/Cargo.toml | 2 +- types/domain/src/lib.rs | 2 +- types/group/Cargo.toml | 4 +- types/group/src/lib.rs | 5 +- types/primitives/Cargo.toml | 7 ++- types/primitives/src/lib.rs | 102 ++++++++++++++++++++++++++----- 29 files changed, 157 insertions(+), 179 deletions(-) delete mode 100644 types/chat/Cargo.toml delete mode 100644 types/chat/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index b48a0f6..4e065d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,7 +8,6 @@ license = "MIT/Apache-2.0" [workspace] members = [ "types/primitives", - "types/chat", "types/group", "types/dao", "types/domain", @@ -53,11 +52,10 @@ sysinfo = "0.23" serde = { version = "1", features = ["derive"] } tokio = { version = "1", features = ["full"] } web3 = { version = "0.18", default-features = false, features = ["http-tls", "signing"] } -tdn = { version = "0.7", default-features = false, features = ["std"] } -tdn_did = { version = "0.7" } -tdn_storage = { version = "0.7" } +tdn = { version = "0.8", default-features = false, features = ["std"] } +tdn_did = { version = "0.8" } +tdn_storage = { version = "0.8" } esse_primitives = { version = "0.1", path = "./types/primitives" } -chat_types = { version = "0.1", path = "./types/chat" } group_types = { version = "0.1", path = "./types/group" } cloud_types = { version = "0.1", path = "./types/cloud" } domain_types = { version = "0.1", path = "./types/domain" } diff --git a/src/apps.rs b/src/apps.rs index f4ed69a..07c169c 100644 --- a/src/apps.rs +++ b/src/apps.rs @@ -1,7 +1,7 @@ -use chat_types::CHAT_ID; use cloud_types::CLOUD_ID; use dao_types::DAO_ID; use domain_types::DOMAIN_ID; +use esse_primitives::ESSE_ID; use group_types::{GroupChatId, GROUP_CHAT_ID}; use std::collections::HashMap; use std::sync::Arc; @@ -46,7 +46,7 @@ pub(crate) async fn app_layer_handle( ) -> Result { debug!("TODO GOT LAYER MESSAGE: ====== {} -> {} ===== ", fgid, tgid); match (fgid, tgid) { - (CHAT_ID, 0) | (0, CHAT_ID) => chat::handle(msg, global).await, + (ESSE_ID, 0) | (0, ESSE_ID) => chat::handle(msg, global).await, (GROUP_CHAT_ID, 0) | (0, GROUP_CHAT_ID) => group::handle(msg, global).await, (DOMAIN_ID, 0) | (0, DOMAIN_ID) => domain::handle(msg, global).await, (CLOUD_ID, 0) | (0, CLOUD_ID) => cloud::handle(msg, global).await, diff --git a/src/apps/chat/layer.rs b/src/apps/chat/layer.rs index def0563..20f2574 100644 --- a/src/apps/chat/layer.rs +++ b/src/apps/chat/layer.rs @@ -1,4 +1,4 @@ -use chat_types::{MessageType, NetworkMessage, CHAT_ID}; +use esse_primitives::{MessageType, NetworkMessage, ESSE_ID}; use serde::{Deserialize, Serialize}; use std::sync::Arc; use tdn::types::{ @@ -63,15 +63,15 @@ pub(crate) async fn handle(msg: RecvType, global: &Arc) -> Result { @@ -81,10 +81,10 @@ pub(crate) async fn handle(msg: RecvType, global: &Arc) -> Result { @@ -298,7 +298,7 @@ impl LayerEvent { )); let data = bincode::serialize(&info).unwrap_or(vec![]); let msg = SendType::Event(0, fpid, data); - results.layers.push((CHAT_ID, msg)); + results.layers.push((ESSE_ID, msg)); } } LayerEvent::InfoRes(remote) => { @@ -336,7 +336,7 @@ impl LayerEvent { drop(db); results.rpcs.push(rpc::friend_close(fid)); if !keep { - results.layers.push((CHAT_ID, SendType::Disconnect(fpid))) + results.layers.push((ESSE_ID, SendType::Disconnect(fpid))) } // TODO close session } @@ -349,7 +349,7 @@ impl LayerEvent { pub(crate) fn chat_conn(pid: PeerId, results: &mut HandleResult) { results .layers - .push((CHAT_ID, SendType::Connect(0, Peer::peer(pid), vec![]))); + .push((ESSE_ID, SendType::Connect(0, Peer::peer(pid), vec![]))); } // UPDATE SESSION. diff --git a/src/apps/chat/models.rs b/src/apps/chat/models.rs index 1fc641f..a82839f 100644 --- a/src/apps/chat/models.rs +++ b/src/apps/chat/models.rs @@ -6,8 +6,7 @@ pub(crate) use self::friend::Friend; pub(crate) use self::message::{from_model, handle_nmsg, Message}; pub(crate) use self::request::Request; -use chat_types::{MessageType, NetworkMessage}; -use esse_primitives::{id_from_str, id_to_str}; +use esse_primitives::{id_from_str, id_to_str, MessageType, NetworkMessage}; use group_types::GroupChatId; use std::path::PathBuf; use tdn::types::primitives::{HandleResult, PeerId, Result}; diff --git a/src/apps/chat/models/message.rs b/src/apps/chat/models/message.rs index 8dbfe17..5ee8954 100644 --- a/src/apps/chat/models/message.rs +++ b/src/apps/chat/models/message.rs @@ -1,4 +1,4 @@ -use chat_types::{MessageType, NetworkMessage}; +use esse_primitives::{MessageType, NetworkMessage}; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; use tdn::types::{ diff --git a/src/apps/chat/rpc.rs b/src/apps/chat/rpc.rs index 04de7da..20c8647 100644 --- a/src/apps/chat/rpc.rs +++ b/src/apps/chat/rpc.rs @@ -1,5 +1,4 @@ -use chat_types::{MessageType, CHAT_ID}; -use esse_primitives::id_from_str; +use esse_primitives::{id_from_str, MessageType, ESSE_ID}; use std::sync::Arc; use tdn::types::{ message::SendType, @@ -183,11 +182,11 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { let data = bincode::serialize(&LayerEvent::Close)?; results .layers - .push((CHAT_ID, SendType::Event(0, friend.pid, data))); + .push((ESSE_ID, SendType::Event(0, friend.pid, data))); results .layers - .push((CHAT_ID, SendType::Disconnect(friend.pid))); + .push((ESSE_ID, SendType::Disconnect(friend.pid))); } // state.group.write().await.broadcast( @@ -223,11 +222,11 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { let data = bincode::serialize(&LayerEvent::Close)?; results .layers - .push((CHAT_ID, SendType::Event(0, friend.pid, data))); + .push((ESSE_ID, SendType::Event(0, friend.pid, data))); results .layers - .push((CHAT_ID, SendType::Disconnect(friend.pid))); + .push((ESSE_ID, SendType::Disconnect(friend.pid))); } // state.group.write().await.broadcast( @@ -286,7 +285,7 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { let req = LayerEvent::Request(name, request.remark); let data = bincode::serialize(&req).unwrap_or(vec![]); let msg = SendType::Event(0, request.pid, data); - results.layers.push((CHAT_ID, msg)); + results.layers.push((ESSE_ID, msg)); Ok(results) }, @@ -333,7 +332,7 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { let data = bincode::serialize(&LayerEvent::Agree).unwrap_or(vec![]); let msg = SendType::Event(0, friend.pid, data); - results.layers.push((CHAT_ID, msg)); + results.layers.push((ESSE_ID, msg)); Ok(results) }, @@ -356,7 +355,7 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { let data = bincode::serialize(&LayerEvent::Reject).unwrap_or(vec![]); let msg = SendType::Event(0, req.pid, data); - let mut results = HandleResult::layer(CHAT_ID, msg); + let mut results = HandleResult::layer(ESSE_ID, msg); // state.group.write().await.broadcast( // &gid, @@ -455,7 +454,7 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { let data = bincode::serialize(&event).unwrap_or(vec![]); results .layers - .push((CHAT_ID, SendType::Event(tid, fpid, data))); + .push((ESSE_ID, SendType::Event(tid, fpid, data))); // UPDATE SESSION. let s_db = session_db(&state.base, &pid, &db_key)?; diff --git a/src/apps/group/layer.rs b/src/apps/group/layer.rs index f68fb98..0e4b7d3 100644 --- a/src/apps/group/layer.rs +++ b/src/apps/group/layer.rs @@ -1,4 +1,4 @@ -use chat_types::MessageType; +use esse_primitives::MessageType; use group_types::{Event, GroupChatId, LayerConnect, LayerEvent, LayerResult, GROUP_CHAT_ID}; use std::sync::Arc; use tdn::types::{ diff --git a/src/apps/group/models/message.rs b/src/apps/group/models/message.rs index 99b49fe..65cc6dc 100644 --- a/src/apps/group/models/message.rs +++ b/src/apps/group/models/message.rs @@ -1,4 +1,4 @@ -use esse_primitives::id_from_str; +use esse_primitives::{id_from_str, MessageType, NetworkMessage}; use std::collections::HashMap; use std::path::PathBuf; use std::time::{SystemTime, UNIX_EPOCH}; @@ -8,8 +8,6 @@ use tdn::types::{ }; use tdn_storage::local::{DStorage, DsValue}; -use chat_types::{MessageType, NetworkMessage}; - use crate::apps::chat::{from_network_message, raw_to_network_message, to_network_message as tnm}; use crate::storage::group_db; diff --git a/src/apps/group/rpc.rs b/src/apps/group/rpc.rs index 3cef2a7..127bcf4 100644 --- a/src/apps/group/rpc.rs +++ b/src/apps/group/rpc.rs @@ -1,4 +1,4 @@ -use chat_types::{MessageType, CHAT_ID}; +use esse_primitives::{MessageType, ESSE_ID}; use group_types::{Event, LayerEvent, GROUP_CHAT_ID}; use std::sync::Arc; use tdn::types::{ @@ -178,7 +178,7 @@ pub(crate) fn new_rpc_handler(handler: &mut RpcHandler) { let tid = state.layer.write().await.delivery(msg.id); let data = bincode::serialize(&event).unwrap_or(vec![]); let lmsg = SendType::Event(tid, f.pid, data); - results.layers.push((CHAT_ID, lmsg)); + results.layers.push((ESSE_ID, lmsg)); // update session. crate::apps::chat::update_session(&s_db, &id, &msg, &mut results); diff --git a/src/apps/jarvis/models.rs b/src/apps/jarvis/models.rs index a02ea1b..a353c28 100644 --- a/src/apps/jarvis/models.rs +++ b/src/apps/jarvis/models.rs @@ -1,3 +1,4 @@ +use esse_primitives::MessageType; use std::time::{SystemTime, UNIX_EPOCH}; use tdn::types::{ primitives::Result, @@ -5,8 +6,6 @@ use tdn::types::{ }; use tdn_storage::local::{DStorage, DsValue}; -use chat_types::MessageType; - pub(crate) struct Message { pub id: i64, pub is_me: bool, diff --git a/src/apps/jarvis/rpc.rs b/src/apps/jarvis/rpc.rs index 58f8274..10e78f5 100644 --- a/src/apps/jarvis/rpc.rs +++ b/src/apps/jarvis/rpc.rs @@ -1,3 +1,4 @@ +use esse_primitives::MessageType; use rand::Rng; use std::sync::Arc; use tdn::types::{ @@ -9,8 +10,6 @@ use tdn_did::Language; use tdn_storage::local::DStorage; use tokio::sync::mpsc::Sender; -use chat_types::MessageType; - use crate::account::lang_from_i64; use crate::apps::chat::raw_to_network_message; use crate::global::Global; diff --git a/src/event.rs b/src/event.rs index 4595f7c..6d35090 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,3 +1,4 @@ +use esse_primitives::NetworkMessage; use serde::{Deserialize, Serialize}; use std::collections::HashMap; use std::path::PathBuf; @@ -11,8 +12,6 @@ use tdn::types::{ use tdn_storage::local::DStorage; use tokio::sync::{mpsc::Sender, RwLock}; -use chat_types::NetworkMessage; - use crate::account::{Account, User}; use crate::apps::chat::LayerEvent; use crate::consensus::Event as OldEvent; diff --git a/src/layer.rs b/src/layer.rs index 6640243..4e908cc 100644 --- a/src/layer.rs +++ b/src/layer.rs @@ -1,5 +1,4 @@ -use chat_types::CHAT_ID; -use esse_primitives::id_to_str; +use esse_primitives::{id_to_str, ESSE_ID}; use group_types::GroupChatId; use serde::{Deserialize, Serialize}; use std::collections::HashMap; @@ -263,7 +262,7 @@ impl Layer { for fpid in self.chats.keys() { let msg = SendType::Event(0, *fpid, data.clone()); - results.layers.push((CHAT_ID, msg)); + results.layers.push((ESSE_ID, msg)); } // TODO GROUPS diff --git a/src/rpc.rs b/src/rpc.rs index 299c433..8eecae2 100644 --- a/src/rpc.rs +++ b/src/rpc.rs @@ -1,5 +1,4 @@ -use chat_types::CHAT_ID; -use esse_primitives::{id_from_str, id_to_str}; +use esse_primitives::{id_from_str, id_to_str, ESSE_ID}; use group_types::{GroupChatId, LayerEvent as GroupLayerEvent, GROUP_CHAT_ID}; use std::net::SocketAddr; use std::sync::Arc; @@ -465,7 +464,7 @@ fn new_rpc_handler(global: Arc) -> RpcHandler { } let data = bincode::serialize(&ChatLayerEvent::Suspend)?; let msg = SendType::Event(0, remote_id, data); - results.layers.push((CHAT_ID, msg)); + results.layers.push((ESSE_ID, msg)); } SessionType::Group => { let remote_gid: GroupChatId = diff --git a/src/server.rs b/src/server.rs index 3361e45..8256578 100644 --- a/src/server.rs +++ b/src/server.rs @@ -89,11 +89,14 @@ pub async fn start(db_path: String) -> Result<()> { while let Some(message) = self_recv.recv().await { match message { - ReceiveMessage::Group(g_msg) => { - if let Ok(handle_result) = group_handle(g_msg, &global).await { + ReceiveMessage::Own(o_msg) => { + if let Ok(handle_result) = group_handle(o_msg, &global).await { handle(handle_result, now_rpc_uid, true, &global).await; } } + ReceiveMessage::Group(_) => { + warn!("ESSE has no Group Message!"); + } ReceiveMessage::Layer(fgid, tgid, l_msg) => { if let Ok(handle_result) = app_layer_handle(fgid, tgid, l_msg, &global).await { handle(handle_result, now_rpc_uid, true, &global).await; @@ -214,10 +217,11 @@ pub async fn start(db_path: String) -> Result<()> { #[inline] async fn handle(handle_result: HandleResult, uid: u64, is_ws: bool, global: &Arc) { let HandleResult { + mut owns, mut rpcs, - mut groups, mut layers, mut networks, + groups: _, // no-group message. } = handle_result; loop { @@ -235,10 +239,10 @@ async fn handle(handle_result: HandleResult, uid: u64, is_ws: bool, global: &Arc if let Ok(sender) = global.sender().await { loop { - if groups.len() != 0 { - let msg = groups.remove(0); + if owns.len() != 0 { + let msg = owns.remove(0); sender - .send(SendMessage::Group(msg)) + .send(SendMessage::Own(msg)) .await .expect("TDN channel closed"); } else { diff --git a/types/README.md b/types/README.md index df998c4..104cee3 100644 --- a/types/README.md +++ b/types/README.md @@ -3,12 +3,11 @@ ESSE bulit-in app open types. ### Default GROUP_ID -- 0 - Own distributed devices. -- 1 - Chat. -- 2 - Group Chat. -- 3 - DAO. -- 4 - Domain. -- 5 - Cloud. +- 0 - ESSE (IM with friends). +- 1 - Group Chat. +- 2 - DAO. +- 3 - Domain. +- 4 - Cloud. ### Open Apps - Group Chat diff --git a/types/chat/Cargo.toml b/types/chat/Cargo.toml deleted file mode 100644 index bc0ffe6..0000000 --- a/types/chat/Cargo.toml +++ /dev/null @@ -1,14 +0,0 @@ -[package] -name = "chat_types" -version = "0.1.0" -authors = ["CympleTech "] -edition = "2021" -readme = "README.md" -description = "ESSE common chat types." -repository = "https://github.com/cympletech/esse_types/chat" -keywords = ["distributed", "p2p", "chat", "ESSE"] -license = "MIT/Apache-2.0" - -[dependencies] -serde = { version = "1", features = ["derive"] } -tdn_types = { version = "0.7", default-features = false } \ No newline at end of file diff --git a/types/chat/src/lib.rs b/types/chat/src/lib.rs deleted file mode 100644 index 4e72e3e..0000000 --- a/types/chat/src/lib.rs +++ /dev/null @@ -1,69 +0,0 @@ -use serde::{Deserialize, Serialize}; -use tdn_types::{group::GroupId, primitives::PeerId}; - -/// Chat service default TDN GROUP ID. -#[rustfmt::skip] -pub const CHAT_ID: GroupId = 1; - -/// message type use in network. -#[derive(Serialize, Deserialize, Clone)] -pub enum NetworkMessage { - String(String), // content - Image(Vec), // image bytes. - File(String, Vec), // filename, file bytes. - Contact(PeerId, String, Vec), // PeerId, name, avatar bytes. - Record(Vec, u32), // record audio bytes. - Emoji, - Phone, - Video, - Invite(String), - Transfer(String), -} - -/// common message types. -#[derive(Copy, Clone, Eq, PartialEq)] -pub enum MessageType { - String, - Image, - File, - Contact, - Record, - Emoji, - Phone, - Video, - Invite, - Transfer, -} - -impl MessageType { - pub fn to_int(&self) -> i64 { - match self { - MessageType::String => 0, - MessageType::Image => 1, - MessageType::File => 2, - MessageType::Contact => 3, - MessageType::Record => 4, - MessageType::Emoji => 5, - MessageType::Phone => 6, - MessageType::Video => 7, - MessageType::Invite => 8, - MessageType::Transfer => 9, - } - } - - pub fn from_int(i: i64) -> MessageType { - match i { - 0 => MessageType::String, - 1 => MessageType::Image, - 2 => MessageType::File, - 3 => MessageType::Contact, - 4 => MessageType::Record, - 5 => MessageType::Emoji, - 6 => MessageType::Phone, - 7 => MessageType::Video, - 8 => MessageType::Invite, - 9 => MessageType::Transfer, - _ => MessageType::String, - } - } -} diff --git a/types/cloud/Cargo.toml b/types/cloud/Cargo.toml index 0831f88..e707d9b 100644 --- a/types/cloud/Cargo.toml +++ b/types/cloud/Cargo.toml @@ -11,4 +11,4 @@ license = "MIT/Apache-2.0" [dependencies] serde = { version = "1", features = ["derive"] } -tdn_types = { version = "0.7", default-features = false } +tdn_types = { version = "0.8", default-features = false } diff --git a/types/cloud/src/lib.rs b/types/cloud/src/lib.rs index c82d92b..9f607c9 100644 --- a/types/cloud/src/lib.rs +++ b/types/cloud/src/lib.rs @@ -2,7 +2,7 @@ use serde::{Deserialize, Serialize}; use tdn_types::{group::GroupId, primitives::PeerId}; /// Personal data cloud service default TDN GROUP ID. -pub const CLOUD_ID: GroupId = 5; +pub const CLOUD_ID: GroupId = 4; /// ESSE service to peer Event. #[derive(Serialize, Deserialize)] diff --git a/types/dao/Cargo.toml b/types/dao/Cargo.toml index e3ff05a..81bf43e 100644 --- a/types/dao/Cargo.toml +++ b/types/dao/Cargo.toml @@ -10,7 +10,7 @@ keywords = ["distributed", "p2p", "DAO", "ESSE"] license = "MIT/Apache-2.0" [dependencies] -chat_types = { path = "../chat", version = "0.1" } +esse_primitives = { version = "0.1", path = "../primitives" } serde = { version = "1", features = ["derive"] } -tdn_types = { version = "0.7", default-features = false } -tdn_did = { version = "0.7", default-features = false } +tdn_types = { version = "0.8", default-features = false } +tdn_did = { version = "0.8", default-features = false } diff --git a/types/dao/src/lib.rs b/types/dao/src/lib.rs index 698564a..374ac4d 100644 --- a/types/dao/src/lib.rs +++ b/types/dao/src/lib.rs @@ -1,10 +1,9 @@ +use esse_primitives::NetworkMessage; use serde::{Deserialize, Serialize}; use tdn_types::{group::GroupId, primitives::PeerId}; -use chat_types::NetworkMessage; - /// Dao app(service) default TDN GROUP ID. -pub const DAO_ID: GroupId = 3; +pub const DAO_ID: GroupId = 2; /// Dao ID type. pub type DaoId = u64; diff --git a/types/data/Cargo.toml b/types/data/Cargo.toml index 5d4fe1b..ce6c01e 100644 --- a/types/data/Cargo.toml +++ b/types/data/Cargo.toml @@ -14,5 +14,5 @@ default = ["tdn"] tdn = ["tdn_did", "tdn_types"] [dependencies] -tdn_did = { version = "0.7", default-features = false, optional = true } -tdn_types = { version = "0.7", default-features = false, optional = true } +tdn_did = { version = "0.8", default-features = false, optional = true } +tdn_types = { version = "0.8", default-features = false, optional = true } diff --git a/types/domain/Cargo.toml b/types/domain/Cargo.toml index d972d1f..d577351 100644 --- a/types/domain/Cargo.toml +++ b/types/domain/Cargo.toml @@ -11,4 +11,4 @@ license = "MIT/Apache-2.0" [dependencies] serde = { version = "1", features = ["derive"] } -tdn_types = { version = "0.7", default-features = false } +tdn_types = { version = "0.8", default-features = false } diff --git a/types/domain/src/lib.rs b/types/domain/src/lib.rs index 409a6cd..4ecbaa8 100644 --- a/types/domain/src/lib.rs +++ b/types/domain/src/lib.rs @@ -4,7 +4,7 @@ use tdn_types::{group::GroupId, primitives::PeerId}; // Same ID can has many name !. /// Group chat app(service) default TDN GROUP ID. -pub const DOMAIN_ID: GroupId = 4; +pub const DOMAIN_ID: GroupId = 3; /// ESSE domain service to peer layer Event. #[derive(Serialize, Deserialize)] diff --git a/types/group/Cargo.toml b/types/group/Cargo.toml index ad0a699..10ae49e 100644 --- a/types/group/Cargo.toml +++ b/types/group/Cargo.toml @@ -10,6 +10,6 @@ keywords = ["distributed", "p2p", "group chat", "ESSE"] license = "MIT/Apache-2.0" [dependencies] -chat_types = { path = "../chat", version = "0.1" } +esse_primitives = { version = "0.1", path = "../primitives" } serde = { version = "1", features = ["derive"] } -tdn_types = { version = "0.7", default-features = false } +tdn_types = { version = "0.8", default-features = false } diff --git a/types/group/src/lib.rs b/types/group/src/lib.rs index 050370b..86b0467 100644 --- a/types/group/src/lib.rs +++ b/types/group/src/lib.rs @@ -1,10 +1,9 @@ +use esse_primitives::NetworkMessage; use serde::{Deserialize, Serialize}; use tdn_types::{group::GroupId, primitives::PeerId}; -use chat_types::NetworkMessage; - /// Group chat app(service) default TDN GROUP ID. -pub const GROUP_CHAT_ID: GroupId = 2; +pub const GROUP_CHAT_ID: GroupId = 1; /// Group chat Group ID. pub type GroupChatId = u64; diff --git a/types/primitives/Cargo.toml b/types/primitives/Cargo.toml index 0010f38..de87c10 100644 --- a/types/primitives/Cargo.toml +++ b/types/primitives/Cargo.toml @@ -4,10 +4,11 @@ version = "0.1.0" authors = ["CympleTech "] edition = "2021" readme = "README.md" -description = "ESSE common primitives." +description = "ESSE common primitives and chat types." repository = "https://github.com/cympletech/esse_types/primitives" -keywords = ["distributed", "p2p", "did", "ESSE"] +keywords = ["distributed", "p2p", "chat", "ESSE"] license = "MIT/Apache-2.0" [dependencies] -tdn_types = { version = "0.7", default-features = false } \ No newline at end of file +serde = { version = "1", features = ["derive"] } +tdn_types = { version = "0.8", default-features = false } \ No newline at end of file diff --git a/types/primitives/src/lib.rs b/types/primitives/src/lib.rs index 39aab94..391d0f1 100644 --- a/types/primitives/src/lib.rs +++ b/types/primitives/src/lib.rs @@ -1,3 +1,89 @@ +use serde::{Deserialize, Serialize}; +use tdn_types::{ + group::GroupId, + primitives::{new_io_error, PeerId, PEER_ID_LENGTH}, +}; + +/// ESSE chat service default TDN GROUP ID. +pub const ESSE_ID: GroupId = 0; + +/// message type use in network. +#[derive(Serialize, Deserialize, Clone)] +pub enum NetworkMessage { + String(String), // content + Image(Vec), // image bytes. + File(String, Vec), // filename, file bytes. + Contact(PeerId, String, Vec), // PeerId, name, avatar bytes. + Record(Vec, u32), // record audio bytes. + Emoji, + Phone, + Video, + Invite(String), + Transfer(String), +} + +/// common message types. +#[derive(Copy, Clone, Eq, PartialEq)] +pub enum MessageType { + String, + Image, + File, + Contact, + Record, + Emoji, + Phone, + Video, + Invite, + Transfer, +} + +impl MessageType { + pub fn to_int(&self) -> i64 { + match self { + MessageType::String => 0, + MessageType::Image => 1, + MessageType::File => 2, + MessageType::Contact => 3, + MessageType::Record => 4, + MessageType::Emoji => 5, + MessageType::Phone => 6, + MessageType::Video => 7, + MessageType::Invite => 8, + MessageType::Transfer => 9, + } + } + + pub fn from_int(i: i64) -> MessageType { + match i { + 0 => MessageType::String, + 1 => MessageType::Image, + 2 => MessageType::File, + 3 => MessageType::Contact, + 4 => MessageType::Record, + 5 => MessageType::Emoji, + 6 => MessageType::Phone, + 7 => MessageType::Video, + 8 => MessageType::Invite, + 9 => MessageType::Transfer, + _ => MessageType::String, + } + } +} + +pub fn id_to_str(peer: &PeerId) -> String { + bs32::encode(&peer.0) +} + +pub fn id_from_str(s: &str) -> std::io::Result { + let data = bs32::decode(s).ok_or(new_io_error("id from string is failure."))?; + if data.len() != PEER_ID_LENGTH { + return Err(new_io_error("id from string is failure.")); + } + let mut bytes = [0u8; PEER_ID_LENGTH]; + bytes.copy_from_slice(&data); + Ok(PeerId(bytes)) +} + pub mod bs32 { use std::cmp::min; @@ -74,19 +160,3 @@ pub mod bs32 { Some(ret) } } - -use tdn_types::primitives::{new_io_error, PeerId, PEER_ID_LENGTH}; - -pub fn id_to_str(peer: &PeerId) -> String { - bs32::encode(&peer.0) -} - -pub fn id_from_str(s: &str) -> std::io::Result { - let data = bs32::decode(s).ok_or(new_io_error("id from string is failure."))?; - if data.len() != PEER_ID_LENGTH { - return Err(new_io_error("id from string is failure.")); - } - let mut bytes = [0u8; PEER_ID_LENGTH]; - bytes.copy_from_slice(&data); - Ok(PeerId(bytes)) -}