add send_message function

This commit is contained in:
Horhik 2021-04-01 15:44:13 +03:00
parent 52185a7ae0
commit c822025d12
6 changed files with 68 additions and 14 deletions

View file

@ -1,14 +1,22 @@
use super::{request::*, response::{AppStatus, ResponseType}};
use super::response::User;
use super::response::UserList;
use super::{
request::*,
response::{AppStatus, ResponseType},
};
use crate::chat::types::PackedMessage;
use crate::chat::types::SP;
use crate::db::{self, messages, types, users};
use async_std::io::Result;
use fcpv2::types::{traits::FcpParser, SSK};
use fcpv2::client::fcp_types::ClientPut;
use fcpv2::types::{
traits::{FcpParser, FcpRequest},
SSK,
};
use rusqlite::Connection;
use serde_json::json;
use uuid::Uuid;
pub fn start_app(request: StartAppReq, server_sender: &SP) -> Result<()> {
Ok(())
//sending *JSON*, what everything is OK
@ -32,7 +40,27 @@ pub fn load_users(request: LoadUsersReq, conn: &Connection, server_sender: &SP)
Ok(())
}
pub fn send_message(request: SendMessageReq, conn: &Connection, server_sender: &SP) -> Result<()> {
unimplemented!()
if let Ok(user_data) = db::users::get_user_by_id(request.user_id, conn) {
let key = user_data.insert_key;
let identifier = &user_data.id.0.to_string()[..];
let fcp_req: String =
ClientPut::new_default_direct(key, identifier, &request.message[..]).convert();
server_sender
.send(PackedMessage::ToFreenet(fcp_req))
.unwrap();
Ok(())
} else {
// create error types
server_sender
.send(PackedMessage::ToClient(
json!(super::response::AppError {
res_type: super::response::ErrorType::WrongUserId
})
.to_string(),
))
.unwrap();
Ok(())
}
//sending FCP request
}
@ -41,9 +69,20 @@ pub fn load_messages(
conn: &Connection,
server_sender: &SP,
) -> Result<()> {
unimplemented!()
let messages = db::messages::select_n_last_messages(
request.user_id,
request.start_index,
request.count,
conn,
)
.unwrap();
let jsoned = json!(messages);
let _ = server_sender.send(PackedMessage::ToClient(jsoned.to_string()));
Ok(())
//sending *JSON*
}
// Adding user to DB
pub fn add_user(request: AddUserReq, conn: &Connection, server_sender: &SP) -> Result<()> {
let user = db::types::User {
id: db::types::Id(Uuid::new_v4()),
@ -53,6 +92,14 @@ pub fn add_user(request: AddUserReq, conn: &Connection, server_sender: &SP) -> R
messages_count: 0,
};
db::users::add_user(user, &conn).unwrap();
server_sender.send(PackedMessage::ToClient(json!(AppStatus{res_type: ResponseType::UserAdded}).to_string())).unwrap();
// Sending "Ok" response to client
server_sender
.send(PackedMessage::ToClient(
json!(AppStatus {
res_type: ResponseType::UserAdded
})
.to_string(),
))
.unwrap();
Ok(())
}

View file

@ -1,7 +1,10 @@
use std::sync::atomic::AtomicU32;
use fcpv2::types::SSK;
use serde_derive::{Deserialize, Serialize};
use serde_json::Result;
type Id = crate::db::types::Id;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct CreateInstance;
@ -47,16 +50,16 @@ pub struct LoadUsersReq {
#[derive(Deserialize, Debug)]
pub struct SendMessageReq {
pub req_type: SendMessage,
pub user_id: u32,
pub user_id: Id,
pub message: String,
}
#[derive(Deserialize, Debug)]
pub struct LoadMessagesReq {
pub req_type: LoadMessages,
pub user_id: u32,
pub count: u8,
pub start_index: u8,
pub user_id: Id,
pub count: u32,
pub start_index: u32,
}
#[derive(Deserialize, Debug)]

View file

@ -18,7 +18,9 @@ pub enum ResponseType{
#[derive(Serialize, Deserialize)]
pub enum ErrorType{
WrongKey,
FailedToAddUser
FailedToAddUser,
WrongUserId
}
#[derive(Serialize, Deserialize)]

View file

@ -35,6 +35,7 @@ pub async fn request_selector(json: String, server_sender: &SP, conn: &Connectio
match handlers::add_user(res, conn, server_sender) {
Ok(_) => {},
Err(e) => {
// Sending error to user, because failed to add user
let _ = server_sender
.send(PackedMessage::ToClient(
json!(AppError {

View file

@ -10,8 +10,9 @@ fn ret_mes(row: &rusqlite::Row<'_>) -> Result<Message> {
message: row.get(3)?,
})
}
type Id = crate::db::types::Id;
pub fn select_message_by_id(user_id: u32, id: u32, conn: &Connection) -> Result<Message> {
pub fn select_message_by_id(user_id: u32, id:u32, conn: &Connection) -> Result<Message> {
let mut selected = conn.prepare("SELECT * FROM messages WHERE id = ?1 AND user_id = ?2")?;
let mut message_iter = selected.query_map(params![id, user_id], |row| ret_mes(row))?;
let message = message_iter.next().unwrap();
@ -33,7 +34,7 @@ pub fn select_all_user_message(id: u32, conn: &Connection) -> Result<Vec<Message
}
pub fn select_n_last_messages(
user_id: u32,
user_id: Id,
start: u32,
count: u32,
conn: &Connection,

View file

@ -3,7 +3,7 @@ use fcpv2::types::{SSK, traits::FcpParser};
use rusqlite::{params, Connection, Result, NO_PARAMS};
pub fn get_user_by_id(id: u32, conn: &Connection) -> Result<User> {
pub fn get_user_by_id(id: crate::db::types::Id, conn: &Connection) -> Result<User> {
let mut selected = conn.prepare("SELECT * FROM users WHERE id = ?")?;
let mut user_iter = selected.query_map(params![id], |row| {
Ok(User {
@ -15,7 +15,7 @@ pub fn get_user_by_id(id: u32, conn: &Connection) -> Result<User> {
})
})?;
let user = user_iter.next().unwrap();
log::info!("User {:} founded", id);
log::info!("User {:} founded", id.0.to_string());
user
}