send gotted messags from freenet to frontend

This commit is contained in:
Horhik 2021-04-23 13:17:30 +03:00
parent 09f26108c8
commit 7c28f99546
11 changed files with 1798 additions and 27 deletions

43
Cargo.lock generated
View file

@ -639,6 +639,7 @@ dependencies = [
"futures-util",
"log",
"ntru",
"r2d2_sqlite",
"regex",
"rusqlite",
"serde",
@ -744,9 +745,9 @@ checksum = "f2f96b10ec2560088a8e76961b00d47107b3a625fecb76dedb29ee7ccbf98235"
[[package]]
name = "libsqlite3-sys"
version = "0.20.1"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64d31059f22935e6c31830db5249ba2b7ecd54fd73a9909286f0a67aa55c2fbd"
checksum = "19cb1effde5f834799ac5e5ef0e40d45027cd74f271b1de786ba8abb30e2164d"
dependencies = [
"pkg-config",
"vcpkg",
@ -1077,6 +1078,27 @@ dependencies = [
"proc-macro2",
]
[[package]]
name = "r2d2"
version = "0.8.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "545c5bc2b880973c9c10e4067418407a0ccaa3091781d1671d46eb35107cb26f"
dependencies = [
"log",
"parking_lot",
"scheduled-thread-pool",
]
[[package]]
name = "r2d2_sqlite"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d24607049214c5e42d3df53ac1d8a23c34cc6a5eefe3122acb2c72174719959"
dependencies = [
"r2d2",
"rusqlite",
]
[[package]]
name = "rand"
version = "0.7.3"
@ -1207,9 +1229,9 @@ dependencies = [
[[package]]
name = "rusqlite"
version = "0.24.2"
version = "0.25.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d5f38ee71cbab2c827ec0ac24e76f82eca723cee92c509a65f67dee393c25112"
checksum = "fbc783b7ddae608338003bac1fa00b6786a75a9675fbd8e87243ecfdea3f6ed2"
dependencies = [
"bitflags",
"chrono",
@ -1263,6 +1285,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "scheduled-thread-pool"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f74fd1204073fa02d5d5d68bec8021be4c38690b61264b2fdb48083d0e7d7"
dependencies = [
"parking_lot",
]
[[package]]
name = "scopeguard"
version = "1.1.0"
@ -1377,9 +1408,9 @@ checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8"
[[package]]
name = "smallvec"
version = "1.4.2"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbee7696b84bbf3d89a1c2eccff0850e3047ed46bfcd2e92c29a2d074d57e252"
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
[[package]]
name = "socket2"

View file

@ -8,7 +8,7 @@ edition = "2018"
[dependencies]
ntru = "0.5.6"
rusqlite = {version = "0.24.2", features = ["chrono"]}
rusqlite = {version = "0.25.1", features = ["chrono"]}
toml = "0.5.8"
tungstenite = "0.12.0"
tokio = { version = "0.3.3", features = ["full"] }
@ -25,6 +25,7 @@ simple_logger = "1.11.0"
chrono = {version = "0.4.19", features = ["serde"]}
uuid = {version = "0.8.2", features = ["serde" , "v4"]}
regex = "1.4.5"
r2d2_sqlite = "0.18.0"
[dependencies.fcpv2]
path = "../FCPv2"

1705
log Normal file

File diff suppressed because one or more lines are too long

View file

@ -98,7 +98,6 @@ pub fn send_message(
let identifier = &user_data.id.0.to_string()[..];
let message_id: u32 = user_data.my_messages_count;
let id = Id(uuid::Uuid::parse_str(identifier).expect("failed to parse user ID"));
let _ = db::users::increase_my_messages_count(id.clone(), conn);
let db_message = db::types::Message {
id: message_id,
date: chrono::offset::Local::now(),
@ -113,8 +112,7 @@ pub fn send_message(
server_sender
.send(PackedMessage::ToFreenet(fcp_req))
.unwrap();
log::debug!("Increasing messages count");
let _ = db::users::increase_my_messages_count(id.clone(), &conn);
let _ = db::users::increase_my_messages_count(id.clone(), conn);
Ok(())
} else {
// create error types

17
src/api/identifier.rs Normal file
View file

@ -0,0 +1,17 @@
use crate::db::types::Id;
use regex::Regex;
use uuid::{self, Uuid};
pub fn parse_message_identifier(identifier: &str) -> (Id, u32) {
let reg = Regex::new(
r"new-message-(.*)/(.*)",
)
.unwrap();
let res = reg.captures(identifier).unwrap();
let uuid = Id(Uuid::parse_str(&res[1]).unwrap());
let id = u32::from_str_radix(&res[2], 10).unwrap();
return (uuid, id)
}

View file

@ -2,3 +2,4 @@ pub mod request;
pub mod response;
pub mod handlers;
pub mod selector;
pub mod identifier;

View file

@ -1,4 +1,5 @@
use crate::chat::types::PackedMessage;
use fcpv2::{types::traits::FcpParser, node::fcp_response::AllData};
use crate::db;
use async_std::{
io,
@ -101,14 +102,27 @@ async fn connection_for_receiving(
}
PackedMessage::FromFreenet(response) => {
let r = response.clone();
let res_type = r.lines().next();
log::debug!("Got:\n {}", &response);
async_std::task::block_on(
sender
// TODO freenet_response_handler
.send(Message::Text(r.to_string())),
)
.expect("Couldn't send messge");
// .await
match res_type {
Some("AllData") => {
let data = AllData::parse(&r).unwrap();
log::debug!("GOT mESSAGE {}\n FROM FREENET: {}",data.identifier, data.data );
server_sender.send(PackedMessage::ToClient(data.data));
//TOOD parse identifier
let (uuid, id) = crate::api::identifier::parse_message_identifier(&data.identifier);
log::debug!("parsed identifier: {:?} {:?}", uuid, id);
/*async_std::task::block_on(
sender
// TODO freenet_response_handler
.send(Message::Text(r.to_string())),
)
.expect("Couldn't send messge");
*/
}
None => {}
_ => {}
}
// .expect("Couldn't send messge");
}
_ => {}
@ -117,7 +131,7 @@ async fn connection_for_receiving(
}
Ok(())
}
// sending ToClient messages to frontend
async fn connection_for_sending(
mut receiver: SplitStream<WebSocketStream<TcpStream>>,
server_sender: SP,

View file

@ -25,6 +25,7 @@ pub async fn to_server_sender(
log::info!("Message sended to client thread");
}
PackedMessage::ToFreenet(req) => {
let res_type = req.lines().next().unwrap();
log::debug!("SENDED {}", req);
sender.write(req.as_bytes()).await?;
log::info!("Message sended to freenet");

View file

@ -28,15 +28,17 @@ pub async fn request_repeater(ss: SP) -> io::Result<()> {
let id = user.id.0.to_string();
let index = user.messages_count + 1;
let key = USK {
ssk: parsed.private_key.clone(),
path: format!("{}/{}", &id, &index),
};
log::debug!("sending {:?}", &key.convert());
match ss.send(PackedMessage::ToFreenet(
ClientGet::new_default(
KEY::USK(
USK {
ssk: parsed.private_key.clone(),
path: format!("{}/{}", &id, &index),
}
key
),
&format!("rec;{};{}", &id, &index)[..], // TODO create Identifier type
&format!("new-message-{}/{}", &id, &index)[..], // TODO create Identifier type
ReturnType::Direct,
)
.convert(),

View file

@ -41,7 +41,7 @@ pub fn select_n_last_messages(
conn: &Connection,
) -> Result<Vec<Message>> {
let mut selected = conn.prepare(
"SELECT * FROM messages CROSS JOIN my_messages WHERE messages.user_id = ?1 or my_messages.user_id = ?1 ORDER BY date DESC LIMIT ?2",
"SELECT * FROM messages WHERE user_id = ?1 ORDER BY date DESC LIMIT ?2",
)?;
let message_iter = selected.query_map(params![user_id, count], |row| ret_mes(row))?;
let mut messages: Vec<Message> = Vec::new();
@ -62,8 +62,8 @@ pub fn add_message(message: Message, conn: &Connection) -> Result<()> {
date,
message,
from_me
) VALUES (?1, ?2, ?3, ?4)",
params![message.id, message.user_id, message.date, message.message],
) VALUES (?1, ?2, ?3, ?4, ?5)",
params![message.id, message.user_id, message.date, message.message, false],
) {
Ok(_) => log::info!("message {:} added succsessfully!", message.id),
Err(e) => {
@ -76,7 +76,7 @@ pub fn add_message(message: Message, conn: &Connection) -> Result<()> {
pub fn add_my_message(message: Message, conn: &Connection) -> Result<()> {
match conn.execute(
"INSERT INTO my_messages (
"INSERT INTO messages (
id,
user_id,
date,

View file

@ -49,6 +49,7 @@ fn main() -> io::Result<()> {
SimpleLogger::new().init().unwrap();
let db = db::start_db().unwrap();
//let manager = r2d2_foodb::FooConnectionManager::new("localhost:1234");
let (to_server_sender, server_receiver): (Sender<PackedMessage>, Receiver<PackedMessage>) =
mpsc::channel();