register process optional value handlin in the sql database

This commit is contained in:
Alex Emmet 2026-01-13 22:57:48 +01:00
commit d51b2f73a2
4 changed files with 205 additions and 143 deletions

View file

@ -10,6 +10,7 @@ pub enum DataTypes {
error_type, error_type,
accepted_ids, accepted_ids,
uuid, uuid,
register_id,
settings, settings,
settings_name, settings_name,
chat_partner_id, chat_partner_id,
@ -92,6 +93,7 @@ impl DataTypes {
match normalized.as_str() { match normalized.as_str() {
"errortype" => DataTypes::error_type, "errortype" => DataTypes::error_type,
"chatpartnerid" => DataTypes::chat_partner_id, "chatpartnerid" => DataTypes::chat_partner_id,
"registerid" => DataTypes::register_id,
"uuid" => DataTypes::uuid, "uuid" => DataTypes::uuid,
"settings" => DataTypes::settings, "settings" => DataTypes::settings,
"settingsname" => DataTypes::settings_name, "settingsname" => DataTypes::settings_name,
@ -174,6 +176,7 @@ impl DataTypes {
#[allow(non_camel_case_types, dead_code)] #[allow(non_camel_case_types, dead_code)]
pub enum CommunicationType { pub enum CommunicationType {
error, error,
error_internal,
error_invalid_data, error_invalid_data,
error_invalid_user_id, error_invalid_user_id,
error_invalid_omikron_id, error_invalid_omikron_id,
@ -211,6 +214,8 @@ pub enum CommunicationType {
register_response, register_response,
identification, identification,
identification_response, identification_response,
register_iota,
register_iota_success,
ping, ping,
pong, pong,
add_chat, add_chat,
@ -273,6 +278,7 @@ impl CommunicationType {
"function" => CommunicationType::function, "function" => CommunicationType::function,
"update" => CommunicationType::update, "update" => CommunicationType::update,
"createuser" => CommunicationType::create_user, "createuser" => CommunicationType::create_user,
"errorinternal" => CommunicationType::error_internal,
"errorinvaliddata" => CommunicationType::error_invalid_data, "errorinvaliddata" => CommunicationType::error_invalid_data,
"errorinvaliduserid" => CommunicationType::error_invalid_user_id, "errorinvaliduserid" => CommunicationType::error_invalid_user_id,
"errorinvalidomikronid" => CommunicationType::error_invalid_omikron_id, "errorinvalidomikronid" => CommunicationType::error_invalid_omikron_id,
@ -310,6 +316,8 @@ impl CommunicationType {
"registerresponse" => CommunicationType::register_response, "registerresponse" => CommunicationType::register_response,
"identification" => CommunicationType::identification, "identification" => CommunicationType::identification,
"identificationresponse" => CommunicationType::identification_response, "identificationresponse" => CommunicationType::identification_response,
"registeriota" => CommunicationType::register_iota,
"registeriotasuccess" => CommunicationType::register_iota_success,
"ping" => CommunicationType::ping, "ping" => CommunicationType::ping,
"pong" => CommunicationType::pong, "pong" => CommunicationType::pong,
"addchat" => CommunicationType::add_chat, "addchat" => CommunicationType::add_chat,

View file

@ -9,6 +9,7 @@ use crate::{
util::crypto_helper::public_key_to_base64, util::crypto_helper::public_key_to_base64,
}; };
use axum::http::HeaderValue; use axum::http::HeaderValue;
use base64::Engine as _;
use http_body_util::Full; use http_body_util::Full;
use hyper::body::Bytes; use hyper::body::Bytes;
use hyper::{HeaderMap, Response as HttpResponse, StatusCode}; use hyper::{HeaderMap, Response as HttpResponse, StatusCode};
@ -112,29 +113,29 @@ pub async fn handle(
} }
// get/id/<username> // get/id/<username>
"id" => { "id" => {
let username = path_parts[2]; if path_parts.len() != 4 {
if username.is_empty() { bad_request()
not_found()
} else { } else {
if let Ok(( let username = path_parts[3];
id, if username.is_empty() {
iota_id, not_found()
username, } else {
display, if let Ok((
status, id,
about, iota_id,
avatar, username,
sub_level, _,
sub_end, _,
public_key, _,
_, _,
_, sub_level,
)) = sql::get_by_username(username).await sub_end,
{ public_key,
( _,
StatusCode::OK, _,
"application/json", )) = sql::get_by_username(username).await
CommunicationValue::new(CommunicationType::success) {
let cv = CommunicationValue::new(CommunicationType::success)
.add_data_str(DataTypes::username, username) .add_data_str(DataTypes::username, username)
.add_data_str(DataTypes::public_key, public_key) .add_data_str(DataTypes::public_key, public_key)
.add_data( .add_data(
@ -145,10 +146,6 @@ pub async fn handle(
DataTypes::iota_id, DataTypes::iota_id,
JsonValue::Number(Number::from(iota_id)), JsonValue::Number(Number::from(iota_id)),
) )
.add_data_str(DataTypes::display, display)
.add_data_str(DataTypes::status, status)
.add_data_str(DataTypes::about, about)
.add_data_str(DataTypes::avatar, avatar)
.add_data( .add_data(
DataTypes::sub_level, DataTypes::sub_level,
JsonValue::Number(Number::from(sub_level)), JsonValue::Number(Number::from(sub_level)),
@ -156,18 +153,17 @@ pub async fn handle(
.add_data( .add_data(
DataTypes::sub_end, DataTypes::sub_end,
JsonValue::Number(Number::from(sub_end)), JsonValue::Number(Number::from(sub_end)),
) );
.to_json() (StatusCode::OK, "application/json", cv.to_json().to_string())
.to_string(), } else {
) (
} else { StatusCode::OK,
( "application/json",
StatusCode::OK, CommunicationValue::new(CommunicationType::error_not_found)
"application/json", .to_json()
CommunicationValue::new(CommunicationType::error_not_found) .to_string(),
.to_json() )
.to_string(), }
)
} }
} }
} }
@ -177,30 +173,30 @@ pub async fn handle(
public_key_to_base64(&get_public_key()), public_key_to_base64(&get_public_key()),
), ),
"user" => { "user" => {
let id = path_parts[2]; if path_parts.len() != 4 {
let id: i64 = id.parse().unwrap_or(0);
if id == 0 {
bad_request() bad_request()
} else { } else {
if let Ok(( let id = path_parts[3];
id, let id: i64 = id.parse().unwrap_or(0);
iota_id, if id == 0 {
username, bad_request()
display, } else {
status, if let Ok((
about, id,
avatar, iota_id,
sub_level, username,
sub_end, display,
public_key, status,
_, about,
_, avatar,
)) = sql::get_by_user_id(id).await sub_level,
{ sub_end,
( public_key,
StatusCode::OK, _,
"application/json", _,
CommunicationValue::new(CommunicationType::success) )) = sql::get_by_user_id(id).await
{
let mut cv = CommunicationValue::new(CommunicationType::success)
.add_data_str(DataTypes::username, username) .add_data_str(DataTypes::username, username)
.add_data_str(DataTypes::public_key, public_key) .add_data_str(DataTypes::public_key, public_key)
.add_data( .add_data(
@ -211,10 +207,6 @@ pub async fn handle(
DataTypes::iota_id, DataTypes::iota_id,
JsonValue::Number(Number::from(iota_id)), JsonValue::Number(Number::from(iota_id)),
) )
.add_data_str(DataTypes::display, display)
.add_data_str(DataTypes::status, status)
.add_data_str(DataTypes::about, about)
.add_data_str(DataTypes::avatar, avatar)
.add_data( .add_data(
DataTypes::sub_level, DataTypes::sub_level,
JsonValue::Number(Number::from(sub_level)), JsonValue::Number(Number::from(sub_level)),
@ -222,18 +214,32 @@ pub async fn handle(
.add_data( .add_data(
DataTypes::sub_end, DataTypes::sub_end,
JsonValue::Number(Number::from(sub_end)), JsonValue::Number(Number::from(sub_end)),
) );
.to_json() if let Some(display) = display {
.to_string(), cv = cv.add_data_str(DataTypes::display, display);
) }
} else { if let Some(status) = status {
( cv = cv.add_data_str(DataTypes::status, status);
StatusCode::OK, }
"application/json", if let Some(about) = about {
CommunicationValue::new(CommunicationType::error_not_found) cv = cv.add_data_str(DataTypes::about, about);
.to_json() }
.to_string(), if let Some(avatar) = avatar {
) cv = cv.add_data_str(
DataTypes::avatar,
base64::engine::general_purpose::STANDARD.encode(avatar),
);
}
(StatusCode::OK, "application/json", cv.to_json().to_string())
} else {
(
StatusCode::OK,
"application/json",
CommunicationValue::new(CommunicationType::error_not_found)
.to_json()
.to_string(),
)
}
} }
} }
} }

View file

@ -186,7 +186,8 @@ impl OmikronConnection {
self.send_message( self.send_message(
&CommunicationValue::new(CommunicationType::identification_response) &CommunicationValue::new(CommunicationType::identification_response)
.with_id(cv.get_id()), .with_id(cv.get_id())
.add_data(DataTypes::accepted, JsonValue::Boolean(true)),
) )
.await; .await;
} else { } else {
@ -311,10 +312,6 @@ impl OmikronConnection {
DataTypes::iota_id, DataTypes::iota_id,
JsonValue::Number(Number::from(iota_id)), JsonValue::Number(Number::from(iota_id)),
) )
.add_data_str(DataTypes::display, display)
.add_data_str(DataTypes::status, status)
.add_data_str(DataTypes::about, about)
.add_data_str(DataTypes::avatar, avatar)
.add_data( .add_data(
DataTypes::sub_level, DataTypes::sub_level,
JsonValue::Number(Number::from(sub_level)), JsonValue::Number(Number::from(sub_level)),
@ -324,6 +321,20 @@ impl OmikronConnection {
JsonValue::Number(Number::from(sub_end)), JsonValue::Number(Number::from(sub_end)),
); );
if let Some(display) = display {
response = response.add_data_str(DataTypes::display, display);
}
if let Some(status) = status {
response = response.add_data_str(DataTypes::status, status);
}
if let Some(about) = about {
response = response.add_data_str(DataTypes::about, about);
}
if let Some(avatar) = avatar {
response =
response.add_data_str(DataTypes::avatar, STANDARD.encode(avatar));
}
let user_status = user_online_tracker::get_user_status(id).await; let user_status = user_online_tracker::get_user_status(id).await;
let iota_connections = let iota_connections =
user_online_tracker::get_iota_omikron_connections(iota_id) user_online_tracker::get_iota_omikron_connections(iota_id)
@ -386,10 +397,6 @@ impl OmikronConnection {
DataTypes::iota_id, DataTypes::iota_id,
JsonValue::Number(Number::from(iota_id)), JsonValue::Number(Number::from(iota_id)),
) )
.add_data_str(DataTypes::display, display)
.add_data_str(DataTypes::status, status)
.add_data_str(DataTypes::about, about)
.add_data_str(DataTypes::avatar, avatar)
.add_data( .add_data(
DataTypes::sub_level, DataTypes::sub_level,
JsonValue::Number(Number::from(sub_level)), JsonValue::Number(Number::from(sub_level)),
@ -399,6 +406,20 @@ impl OmikronConnection {
JsonValue::Number(Number::from(sub_end)), JsonValue::Number(Number::from(sub_end)),
); );
if let Some(display) = display {
response = response.add_data_str(DataTypes::display, display);
}
if let Some(status) = status {
response = response.add_data_str(DataTypes::status, status);
}
if let Some(about) = about {
response = response.add_data_str(DataTypes::about, about);
}
if let Some(avatar) = avatar {
response =
response.add_data_str(DataTypes::avatar, STANDARD.encode(avatar));
}
let user_status = user_online_tracker::get_user_status(id).await; let user_status = user_online_tracker::get_user_status(id).await;
let iota_connections = let iota_connections =
user_online_tracker::get_iota_omikron_connections(iota_id) user_online_tracker::get_iota_omikron_connections(iota_id)
@ -570,23 +591,44 @@ impl OmikronConnection {
return; return;
} }
if cv.is_type(CommunicationType::complete_register_iota) { if cv.is_type(CommunicationType::complete_register_iota) {
if let (Some(iota_id), Some(public_key)) = ( let iota_id_opt = cv.get_data(DataTypes::iota_id).and_then(|v| v.as_i64());
cv.get_data(DataTypes::iota_id).and_then(|v| v.as_i64()),
cv.get_data(DataTypes::public_key).and_then(|v| v.as_str()), if let Some(public_key) = cv.get_data(DataTypes::public_key).and_then(|v| v.as_str()) {
) { if let Some(iota_id) = iota_id_opt {
match sql::register_complete_iota(iota_id, public_key.to_string()).await { // Existing logic to update iota
Ok(_) => { match sql::register_complete_iota(iota_id, public_key.to_string()).await {
let response = CommunicationValue::new(CommunicationType::success) Ok(_) => {
.with_id(cv.get_id()); let response = CommunicationValue::new(CommunicationType::success)
self.send_message(&response).await; .with_id(cv.get_id());
self.send_message(&response).await;
}
Err(e) => {
self.send_message(
&CommunicationValue::new(CommunicationType::error)
.with_id(cv.get_id())
.add_data_str(DataTypes::error_type, e.to_string()),
)
.await;
}
} }
Err(e) => { } else {
self.send_message( // New logic to create iota and return id
&CommunicationValue::new(CommunicationType::error) match sql::create_new_iota(public_key.to_string()).await {
.with_id(cv.get_id()) Ok(new_iota_id) => {
.add_data_str(DataTypes::error_type, e.to_string()), let response =
) CommunicationValue::new(CommunicationType::complete_register_iota)
.await; .with_id(cv.get_id())
.add_data(DataTypes::iota_id, JsonValue::from(new_iota_id));
self.send_message(&response).await;
}
Err(e) => {
self.send_message(
&CommunicationValue::new(CommunicationType::error)
.with_id(cv.get_id())
.add_data_str(DataTypes::error_type, e.to_string()),
)
.await;
}
} }
} }
} else { } else {

View file

@ -150,10 +150,10 @@ pub async fn get_by_username(
i64, i64,
i64, i64,
String, String,
String, Option<String>,
String, Option<String>,
String, Option<String>,
String, Option<Vec<u8>>,
i32, i32,
i64, i64,
String, String,
@ -177,10 +177,10 @@ pub async fn get_by_username(
let id: i64 = row.get("id"); let id: i64 = row.get("id");
let iota_id: i64 = row.get("iota_id"); let iota_id: i64 = row.get("iota_id");
let username: String = row.get("username"); let username: String = row.get("username");
let display: Vec<u8> = row.get("display"); let display: Option<Vec<u8>> = row.get("display");
let status: Vec<u8> = row.get("status"); let status: Option<Vec<u8>> = row.get("status");
let about: Vec<u8> = row.get("about"); let about: Option<Vec<u8>> = row.get("about");
let avatar: Vec<u8> = row.get("avatar"); let avatar: Option<Vec<u8>> = row.get("avatar");
let sub_level: i32 = row.get("sub_level"); let sub_level: i32 = row.get("sub_level");
let sub_end: i64 = row.get("sub_end"); let sub_end: i64 = row.get("sub_end");
let public_key: String = row.get("public_key"); let public_key: String = row.get("public_key");
@ -191,10 +191,10 @@ pub async fn get_by_username(
id, id,
iota_id, iota_id,
username, username,
String::from_utf8_lossy(&display).to_string(), display.map(|d| String::from_utf8_lossy(&d).to_string()),
String::from_utf8_lossy(&status).to_string(), status.map(|s| String::from_utf8_lossy(&s).to_string()),
String::from_utf8_lossy(&about).to_string(), about.map(|a| String::from_utf8_lossy(&a).to_string()),
String::from_utf8_lossy(&avatar).to_string(), avatar,
sub_level, sub_level,
sub_end, sub_end,
public_key, public_key,
@ -213,10 +213,10 @@ pub async fn get_by_user_id(
i64, i64,
i64, i64,
String, String,
String, Option<String>,
String, Option<String>,
String, Option<String>,
String, Option<Vec<u8>>,
i32, i32,
i64, i64,
String, String,
@ -240,10 +240,10 @@ pub async fn get_by_user_id(
let id: i64 = row.get("id"); let id: i64 = row.get("id");
let iota_id: i64 = row.get("iota_id"); let iota_id: i64 = row.get("iota_id");
let username: String = row.get("username"); let username: String = row.get("username");
let display: Vec<u8> = row.get("display"); let display: Option<Vec<u8>> = row.get("display");
let status: Vec<u8> = row.get("status"); let status: Option<Vec<u8>> = row.get("status");
let about: Vec<u8> = row.get("about"); let about: Option<Vec<u8>> = row.get("about");
let avatar: Vec<u8> = row.get("avatar"); let avatar: Option<Vec<u8>> = row.get("avatar");
let sub_level: i32 = row.get("sub_level"); let sub_level: i32 = row.get("sub_level");
let sub_end: i64 = row.get("sub_end"); let sub_end: i64 = row.get("sub_end");
let public_key: String = row.get("public_key"); let public_key: String = row.get("public_key");
@ -254,10 +254,10 @@ pub async fn get_by_user_id(
id, id,
iota_id, iota_id,
username, username,
String::from_utf8_lossy(&display).to_string(), display.map(|d| String::from_utf8_lossy(&d).to_string()),
String::from_utf8_lossy(&status).to_string(), status.map(|s| String::from_utf8_lossy(&s).to_string()),
String::from_utf8_lossy(&about).to_string(), about.map(|a| String::from_utf8_lossy(&a).to_string()),
String::from_utf8_lossy(&avatar).to_string(), avatar,
sub_level, sub_level,
sub_end, sub_end,
public_key, public_key,
@ -276,10 +276,10 @@ pub async fn get_users_by_iota_id(
i64, i64,
i64, i64,
String, String,
String, Option<String>,
String, Option<String>,
String, Option<String>,
String, Option<Vec<u8>>,
i32, i32,
i64, i64,
String, String,
@ -303,10 +303,10 @@ pub async fn get_users_by_iota_id(
let id: i64 = row.get("id"); let id: i64 = row.get("id");
let iota_id: i64 = row.get("iota_id"); let iota_id: i64 = row.get("iota_id");
let username: String = row.get("username"); let username: String = row.get("username");
let display: Vec<u8> = row.get("display"); let display: Option<Vec<u8>> = row.get("display");
let status: Vec<u8> = row.get("status"); let status: Option<Vec<u8>> = row.get("status");
let about: Vec<u8> = row.get("about"); let about: Option<Vec<u8>> = row.get("about");
let avatar: Vec<u8> = row.get("avatar"); let avatar: Option<Vec<u8>> = row.get("avatar");
let sub_level: i32 = row.get("sub_level"); let sub_level: i32 = row.get("sub_level");
let sub_end: i64 = row.get("sub_end"); let sub_end: i64 = row.get("sub_end");
let public_key: String = row.get("public_key"); let public_key: String = row.get("public_key");
@ -317,10 +317,10 @@ pub async fn get_users_by_iota_id(
id, id,
iota_id, iota_id,
username, username,
String::from_utf8_lossy(&display).to_string(), display.map(|d| String::from_utf8_lossy(&d).to_string()),
String::from_utf8_lossy(&status).to_string(), status.map(|s| String::from_utf8_lossy(&s).to_string()),
String::from_utf8_lossy(&about).to_string(), about.map(|a| String::from_utf8_lossy(&a).to_string()),
String::from_utf8_lossy(&avatar).to_string(), avatar,
sub_level, sub_level,
sub_end, sub_end,
public_key, public_key,
@ -477,9 +477,9 @@ pub async fn print_users() -> Result<(), Box<dyn std::error::Error>> {
let id: i64 = row.get("id"); let id: i64 = row.get("id");
let iota_id: i64 = row.get("iota_id"); let iota_id: i64 = row.get("iota_id");
let username: String = row.get("username"); let username: String = row.get("username");
let display: Vec<u8> = row.get("display"); let display: Option<Vec<u8>> = row.get("display");
let status: Vec<u8> = row.get("status"); let status: Option<Vec<u8>> = row.get("status");
let about: Vec<u8> = row.get("about"); let about: Option<Vec<u8>> = row.get("about");
let sub_level: i32 = row.get("sub_level"); let sub_level: i32 = row.get("sub_level");
let sub_end: i64 = row.get("sub_end"); let sub_end: i64 = row.get("sub_end");
@ -489,9 +489,9 @@ pub async fn print_users() -> Result<(), Box<dyn std::error::Error>> {
id, id,
iota_id, iota_id,
username, username,
String::from_utf8_lossy(&display), display.map_or("".to_string(), |d| String::from_utf8_lossy(&d).to_string()),
String::from_utf8_lossy(&status), status.map_or("".to_string(), |s| String::from_utf8_lossy(&s).to_string()),
String::from_utf8_lossy(&about), about.map_or("".to_string(), |a| String::from_utf8_lossy(&a).to_string()),
sub_level, sub_level,
sub_end sub_end
) )
@ -504,6 +504,12 @@ pub async fn print_users() -> Result<(), Box<dyn std::error::Error>> {
// ========================================================================================== // ==========================================================================================
// IOTA // IOTA
// ========================================================================================== // ==========================================================================================
pub async fn create_new_iota(public_key: String) -> Result<i64, sqlx::Error> {
let new_id = get_register_id().await as i64;
register_complete_iota(new_id, public_key).await?;
Ok(new_id)
}
pub async fn register_complete_iota(id: i64, public_key: String) -> Result<(), sqlx::Error> { pub async fn register_complete_iota(id: i64, public_key: String) -> Result<(), sqlx::Error> {
let db_lock = SQL_DB.read().await; let db_lock = SQL_DB.read().await;
let pool = db_lock.as_ref().expect("Database pool is not initialized"); let pool = db_lock.as_ref().expect("Database pool is not initialized");