make userlist interacting with frontend

This commit is contained in:
Horhik 2021-04-21 23:07:28 +03:00
parent 8160d9915d
commit 0572d90dc8
3 changed files with 37 additions and 19 deletions

View file

@ -1,5 +1,4 @@
use super::response::User; use super::response::User;
use super::response::UserList;
use super::response::{AppStatus, ResponseType}; use super::response::{AppStatus, ResponseType};
use crate::api::request::Request; use crate::api::request::Request;
use crate::chat::init_config; use crate::chat::init_config;
@ -44,10 +43,15 @@ pub fn start_app(server_sender: SP) -> Result<()> {
Ok(res) => { Ok(res) => {
let conf = std::fs::read_to_string(&config_path).unwrap(); let conf = std::fs::read_to_string(&config_path).unwrap();
log::debug!("Responsing to start_app: {}", &conf); log::debug!("Responsing to start_app: {}", &conf);
let toml: crate::chat::Config = toml::from_str(&conf[..]).unwrap(); let toml: crate::chat::Config = toml::from_str(&conf[..]).unwrap();
server_sender server_sender
.send(PackedMessage::ToClient( .send(PackedMessage::ToClient(
serde_json::to_string(&toml).unwrap(), serde_json::to_string(&crate::api::response::ResponseType::InitialConfig {
id: toml.id.clone(),
public_key: toml.public_key.clone(),
private_key: toml.private_key.clone(),
})
.unwrap(),
)) ))
.unwrap(); .unwrap();
log::debug!("Responsing to start_app"); log::debug!("Responsing to start_app");
@ -68,7 +72,7 @@ pub fn load_users(conn: &Connection, server_sender: SP) -> Result<()> {
.into_iter() .into_iter()
.map(|x| x.to_jsonable()) .map(|x| x.to_jsonable())
.collect(); .collect();
let users: String = serde_json::to_string(&UserList { let users: String = serde_json::to_string(&crate::api::response::ResponseType::UserList {
users: jsoned_users, users: jsoned_users,
}) })
.unwrap(); .unwrap();
@ -137,22 +141,35 @@ pub fn add_user(
conn: &Connection, conn: &Connection,
server_sender: SP, server_sender: SP,
) -> Result<()> { ) -> Result<()> {
let new_id = Uuid::new_v4();
let user = db::types::User { let user = db::types::User {
id: db::types::Id(Uuid::new_v4()), id: db::types::Id(new_id.clone()),
name: name, name: name.clone(),
sign_key: sign_key, sign_key: sign_key.clone(),
insert_key: SSK::parse(&insert_key[..]).unwrap(), insert_key: SSK::parse(&insert_key[..]).unwrap(),
messages_count: 0, messages_count: 0,
}; };
let user_jsoned = crate::api::response::User{
id: new_id.clone().to_string(),
name: name.clone(),
sign_key: sign_key.clone(),
insert_key: insert_key,
messages_count: 0
};
db::users::add_user(user, &conn).unwrap(); db::users::add_user(user, &conn).unwrap();
// Sending "Ok" response to client // Sending "Ok" response to client
//
load_users(conn, server_sender).unwrap();
// TODO senging only one user to client{
/*
server_sender server_sender
.send(PackedMessage::ToClient( .send(PackedMessage::ToClient(
json!(AppStatus { json!(ResponseType::UserAdded(user_jsoned))
res_type: ResponseType::UserAdded
})
.to_string(), .to_string(),
)) ))
.unwrap(); .unwrap();
*/
Ok(()) Ok(())
} }

View file

@ -3,23 +3,27 @@ use crate::db::types::User as SqliteUser;
use serde_derive::{Deserialize, Serialize}; use serde_derive::{Deserialize, Serialize};
use tungstenite::http::Response; use tungstenite::http::Response;
pub type InsertKey = String; pub type InsertKey = String;
#[derive(Serialize, Deserialize, Debug, PartialEq)] #[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
#[serde(tag = "type")] #[serde(tag = "type")]
pub enum ResponseType { pub enum ResponseType {
Error, Error,
NewMessage, NewMessage,
UserList,
NewUser, NewUser,
FetchedMessages, FetchedMessages,
InstanceCreated, InstanceCreated,
InstanceAccepted, InstanceAccepted,
UserAdded, #[serde(rename_all = "camelCase")]
UserAdded(User),
#[serde(rename_all = "camelCase")]
InitialConfig { InitialConfig {
id: crate::db::types::Id, id: crate::db::types::Id,
public_key: fcpv2::types::SSK, public_key: fcpv2::types::SSK,
private_key: fcpv2::types::SSK, private_key: fcpv2::types::SSK,
}, },
UserList {
users: Vec<User>,
}
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub enum ErrorType { pub enum ErrorType {
@ -39,7 +43,8 @@ pub struct AppStatus {
pub res_type: ResponseType, pub res_type: ResponseType,
} }
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct User { pub struct User {
pub id: String, pub id: String,
pub name: String, pub name: String,
@ -48,7 +53,3 @@ pub struct User {
pub messages_count: u32, pub messages_count: u32,
} }
#[derive(Serialize, Deserialize)]
pub struct UserList {
pub users: Vec<User>,
}

View file

@ -19,7 +19,7 @@ pub type InsertKey = SSK;
#[derive(Debug, Deserialize, Serialize, PartialEq, Clone)] #[derive(Debug, Deserialize, Serialize, PartialEq, Clone)]
pub struct Id(pub uuid::Uuid); pub struct Id(pub uuid::Uuid);
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize, Clone)]
pub struct User { pub struct User {
pub id: Id, pub id: Id,
pub name: String, pub name: String,