This commit is contained in:
Alex Emmet 2025-11-22 13:11:12 +00:00
commit ec0a6311f1
4 changed files with 48 additions and 11 deletions

View file

@ -93,6 +93,22 @@ impl Community {
json
}
pub async fn frontend(&self) -> JsonValue {
let mut json = JsonValue::new_object();
json["name"] = self.name.clone().into();
json["owner_id"] = self.owner_id.to_string().into();
json["members"] = self
.members
.clone()
.iter()
.map(|f| f.to_string())
.collect::<Vec<String>>()
.into();
json["public_key"] = self.public_key.as_bytes().to_vec().into();
json["connections"] = self.connections.read().await.clone().len().into();
json
}
pub fn add_member(&mut self, member_id: Uuid) {
self.members.push(member_id);
}

View file

@ -262,18 +262,24 @@ impl OmikronConnection {
chat_files::add_message(
cv.get_data(DataTypes::send_time)
.unwrap()
.unwrap_or(&JsonValue::new_object())
.as_i64()
.unwrap_or(0) as u128,
.unwrap_or(
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_millis()
as i64,
) as u128,
false,
*receiver_id,
*sender_id,
cv.get_data(DataTypes::content).unwrap().as_str().unwrap(),
);
let response =
let user_forward =
CommunicationValue::new(CommunicationType::message_live)
.with_id(cv.get_id())
.with_receiver(cv.get_receiver().unwrap())
.with_receiver(*receiver_id)
.add_data(
DataTypes::send_time,
cv.get_data(DataTypes::send_time).unwrap().clone(),
@ -288,7 +294,7 @@ impl OmikronConnection {
);
Self::send_message_static(
&writer.clone(),
response.to_json().to_string(),
user_forward.to_json().to_string(),
)
.await;
return;

View file

@ -53,7 +53,7 @@ pub async fn handle(
.await
{
let cv = CommunicationValue::new(CommunicationType::create_user)
.add_data(DataTypes::user, user.to_json());
.add_data(DataTypes::user, user.frontend());
cv.to_json().to_string()
} else {
"{\"type\":\"error\"}".to_string()
@ -70,7 +70,7 @@ pub async fn handle(
let users = user_manager::get_users();
let mut json = JsonValue::new_array();
for user in users {
let _ = json.push(user.to_json());
let _ = json.push(user.frontend());
}
json.to_string()
}
@ -103,7 +103,7 @@ pub async fn handle(
let communities = community_manager::get_communities().await;
let mut json = JsonValue::new_array();
for community in communities {
let _ = json.push(community.to_json().await);
let _ = json.push(community.frontend().await);
}
json.to_string()
}
@ -124,13 +124,17 @@ pub async fn handle(
"set" => {
if let Some(key) = headers.get("key") {
if let Some(value) = headers.get("value") {
CONFIG
let _ = CONFIG
.lock()
.await
.config
.insert(key.to_str().unwrap(), value);
.insert(key.to_str().unwrap(), value.to_str().unwrap());
"{\"type\":\"success\"}".to_string()
} else {
"{\"type\":\"error\"}".to_string()
}
} else {
"{\"type\":\"error\"}".to_string()
}
}
"get" => CONFIG.lock().await.config.to_string(),

View file

@ -50,7 +50,18 @@ impl UserProfile {
}
obj
}
pub fn frontend(&self) -> JsonValue {
let mut obj = object! {
"uuid" => self.user_id.to_string(),
"username" => self.username.clone(),
"public_key" => self.public_key.clone(),
"private_key_hash" => self.private_key_hash.clone(),
};
if let Some(d) = &self.display_name {
obj["display_name"] = d.clone().into();
}
obj
}
pub async fn from_json(j: &JsonValue) -> Option<Self> {
let uuid = Uuid::parse_str(j["uuid"].as_str()?).ok()?;
let username = j["username"].as_str()?.to_string();