Send received messages to DB

This commit is contained in:
horhik 2021-04-29 10:00:20 +03:00
parent 78c84d1e17
commit b78eae0f7e
2 changed files with 20 additions and 2 deletions

View file

@ -68,7 +68,7 @@ pub struct User {
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct FreenetMessage { pub struct FreenetMessage {
pub id: uuid::Uuid, pub id: uuid::Uuid,

View file

@ -1,7 +1,9 @@
use super::serv_handler::to_server_sender; use super::serv_handler::to_server_sender;
use crate::api::types::Message as FrontMessage; use crate::api::types::Message as FrontMessage;
use crate::chat::types::{PackedMessage, RP, SP}; use crate::chat::types::{PackedMessage, RP, SP};
use crate::db::users::increase_messages_count; use crate::db::users::{increase_messages_count, get_user_by_id};
use crate::db::messages::add_message;
use crate::db::types::{Message as DBMessage, Id};
use async_std::task; use async_std::task;
use fcpv2::types::traits::FcpParser; use fcpv2::types::traits::FcpParser;
use r2d2::Pool; use r2d2::Pool;
@ -126,6 +128,7 @@ async fn server_responce_getter(
>(&message[..]); >(&message[..]);
match parsed_message { match parsed_message {
Ok(json) => { Ok(json) => {
let json_clone = json.clone();
let front_message = FrontMessage { let front_message = FrontMessage {
date: json.date, date: json.date,
from_me: false, from_me: false,
@ -138,8 +141,23 @@ async fn server_responce_getter(
)) ))
.unwrap(); .unwrap();
log::debug!("Send gotted message to frontend..."); log::debug!("Send gotted message to frontend...");
log::debug!("Adding message to DB...");
let user = get_user_by_id(Id(json_clone.clone().id), &db).unwrap();
add_message(DBMessage{
id: user.messages_count,
date: json_clone.date,
user_id: Id(json_clone.id),
message: json_clone.message,
from_me: false,
}, &db).unwrap();
log::debug!("Increasing messages count..."); log::debug!("Increasing messages count...");
increase_messages_count(crate::db::types::Id(json.id), &db).unwrap(); increase_messages_count(crate::db::types::Id(json.id), &db).unwrap();
} }
Err(_) => { Err(_) => {
log::error!("Failed to parse gotted message"); log::error!("Failed to parse gotted message");