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

View file

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