add send_message function
This commit is contained in:
parent
52185a7ae0
commit
c822025d12
|
@ -1,14 +1,22 @@
|
||||||
use super::{request::*, response::{AppStatus, ResponseType}};
|
|
||||||
use super::response::User;
|
use super::response::User;
|
||||||
use super::response::UserList;
|
use super::response::UserList;
|
||||||
|
use super::{
|
||||||
|
request::*,
|
||||||
|
response::{AppStatus, ResponseType},
|
||||||
|
};
|
||||||
use crate::chat::types::PackedMessage;
|
use crate::chat::types::PackedMessage;
|
||||||
use crate::chat::types::SP;
|
use crate::chat::types::SP;
|
||||||
use crate::db::{self, messages, types, users};
|
use crate::db::{self, messages, types, users};
|
||||||
use async_std::io::Result;
|
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 rusqlite::Connection;
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
pub fn start_app(request: StartAppReq, server_sender: &SP) -> Result<()> {
|
pub fn start_app(request: StartAppReq, server_sender: &SP) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
//sending *JSON*, what everything is OK
|
//sending *JSON*, what everything is OK
|
||||||
|
@ -32,7 +40,27 @@ pub fn load_users(request: LoadUsersReq, conn: &Connection, server_sender: &SP)
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
pub fn send_message(request: SendMessageReq, conn: &Connection, server_sender: &SP) -> Result<()> {
|
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
|
//sending FCP request
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,9 +69,20 @@ pub fn load_messages(
|
||||||
conn: &Connection,
|
conn: &Connection,
|
||||||
server_sender: &SP,
|
server_sender: &SP,
|
||||||
) -> Result<()> {
|
) -> 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*
|
//sending *JSON*
|
||||||
}
|
}
|
||||||
|
// Adding user to DB
|
||||||
pub fn add_user(request: AddUserReq, conn: &Connection, server_sender: &SP) -> Result<()> {
|
pub fn add_user(request: AddUserReq, conn: &Connection, server_sender: &SP) -> Result<()> {
|
||||||
let user = db::types::User {
|
let user = db::types::User {
|
||||||
id: db::types::Id(Uuid::new_v4()),
|
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,
|
messages_count: 0,
|
||||||
};
|
};
|
||||||
db::users::add_user(user, &conn).unwrap();
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
use std::sync::atomic::AtomicU32;
|
||||||
use fcpv2::types::SSK;
|
use fcpv2::types::SSK;
|
||||||
use serde_derive::{Deserialize, Serialize};
|
use serde_derive::{Deserialize, Serialize};
|
||||||
use serde_json::Result;
|
use serde_json::Result;
|
||||||
|
|
||||||
|
type Id = crate::db::types::Id;
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
||||||
pub struct CreateInstance;
|
pub struct CreateInstance;
|
||||||
|
|
||||||
|
@ -47,16 +50,16 @@ pub struct LoadUsersReq {
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct SendMessageReq {
|
pub struct SendMessageReq {
|
||||||
pub req_type: SendMessage,
|
pub req_type: SendMessage,
|
||||||
pub user_id: u32,
|
pub user_id: Id,
|
||||||
pub message: String,
|
pub message: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct LoadMessagesReq {
|
pub struct LoadMessagesReq {
|
||||||
pub req_type: LoadMessages,
|
pub req_type: LoadMessages,
|
||||||
pub user_id: u32,
|
pub user_id: Id,
|
||||||
pub count: u8,
|
pub count: u32,
|
||||||
pub start_index: u8,
|
pub start_index: u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
|
|
|
@ -18,7 +18,9 @@ pub enum ResponseType{
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub enum ErrorType{
|
pub enum ErrorType{
|
||||||
WrongKey,
|
WrongKey,
|
||||||
FailedToAddUser
|
FailedToAddUser,
|
||||||
|
WrongUserId
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
|
|
|
@ -35,6 +35,7 @@ pub async fn request_selector(json: String, server_sender: &SP, conn: &Connectio
|
||||||
match handlers::add_user(res, conn, server_sender) {
|
match handlers::add_user(res, conn, server_sender) {
|
||||||
Ok(_) => {},
|
Ok(_) => {},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
|
// Sending error to user, because failed to add user
|
||||||
let _ = server_sender
|
let _ = server_sender
|
||||||
.send(PackedMessage::ToClient(
|
.send(PackedMessage::ToClient(
|
||||||
json!(AppError {
|
json!(AppError {
|
||||||
|
|
|
@ -10,6 +10,7 @@ fn ret_mes(row: &rusqlite::Row<'_>) -> Result<Message> {
|
||||||
message: row.get(3)?,
|
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 selected = conn.prepare("SELECT * FROM messages WHERE id = ?1 AND user_id = ?2")?;
|
||||||
|
@ -33,7 +34,7 @@ pub fn select_all_user_message(id: u32, conn: &Connection) -> Result<Vec<Message
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn select_n_last_messages(
|
pub fn select_n_last_messages(
|
||||||
user_id: u32,
|
user_id: Id,
|
||||||
start: u32,
|
start: u32,
|
||||||
count: u32,
|
count: u32,
|
||||||
conn: &Connection,
|
conn: &Connection,
|
||||||
|
|
|
@ -3,7 +3,7 @@ use fcpv2::types::{SSK, traits::FcpParser};
|
||||||
|
|
||||||
use rusqlite::{params, Connection, Result, NO_PARAMS};
|
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 selected = conn.prepare("SELECT * FROM users WHERE id = ?")?;
|
||||||
let mut user_iter = selected.query_map(params![id], |row| {
|
let mut user_iter = selected.query_map(params![id], |row| {
|
||||||
Ok(User {
|
Ok(User {
|
||||||
|
@ -15,7 +15,7 @@ pub fn get_user_by_id(id: u32, conn: &Connection) -> Result<User> {
|
||||||
})
|
})
|
||||||
})?;
|
})?;
|
||||||
let user = user_iter.next().unwrap();
|
let user = user_iter.next().unwrap();
|
||||||
log::info!("User {:} founded", id);
|
log::info!("User {:} founded", id.0.to_string());
|
||||||
user
|
user
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue