Skip to content

Commit ccd4a5e

Browse files
committed
feat: add graph UI metadata management endpoints and structures
1 parent e06d69e commit ccd4a5e

File tree

6 files changed

+557
-0
lines changed

6 files changed

+557
-0
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
use std::collections::HashMap;
8+
use std::{fs, path::PathBuf};
9+
10+
use anyhow::Result;
11+
use serde::{Deserialize, Serialize};
12+
use uuid::Uuid;
13+
14+
use crate::constants::METADATA_FILE;
15+
16+
use super::get_default_home_dir;
17+
18+
#[derive(Debug, Serialize, Deserialize, Clone)]
19+
pub struct NodeGeometry {
20+
pub app: Option<String>,
21+
pub extension: String,
22+
23+
pub x: i64,
24+
pub y: i64,
25+
}
26+
27+
#[derive(Debug, Serialize, Deserialize, Clone)]
28+
pub struct GraphGeometry {
29+
pub nodes_geometry: Vec<NodeGeometry>,
30+
}
31+
32+
#[derive(Debug, Serialize, Deserialize, Default, Clone)]
33+
pub struct GraphUiMetadata {
34+
pub graphs_geometry: HashMap<Uuid, GraphGeometry>,
35+
}
36+
37+
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
38+
pub struct TmanMetadata {
39+
pub graph_ui: GraphUiMetadata,
40+
}
41+
42+
fn get_default_metadata_path() -> PathBuf {
43+
let mut config_path = get_default_home_dir();
44+
config_path.push(METADATA_FILE);
45+
config_path
46+
}
47+
48+
// Read the internal configuration from the specified path.
49+
pub fn read_metadata() -> Result<Option<TmanMetadata>> {
50+
let config_path = get_default_metadata_path();
51+
if !config_path.exists() {
52+
return Ok(None);
53+
}
54+
55+
let config_data = fs::read_to_string(config_path.clone())?;
56+
let config: TmanMetadata = serde_json::from_str(&config_data)?;
57+
58+
Ok(Some(config))
59+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
use std::sync::Arc;
8+
9+
use actix_web::{web, HttpResponse, Responder};
10+
use serde::{Deserialize, Serialize};
11+
use uuid::Uuid;
12+
13+
use crate::config::metadata::GraphGeometry;
14+
use crate::designer::response::{ApiResponse, Status};
15+
use crate::designer::DesignerState;
16+
17+
#[derive(Debug, Serialize, Deserialize)]
18+
pub struct GetGraphUiRequestPayload {
19+
pub graph_id: Uuid,
20+
}
21+
22+
#[derive(Debug, Serialize, Deserialize)]
23+
pub struct GetGraphUiResponseData {
24+
#[serde(skip_serializing_if = "Option::is_none")]
25+
pub graph_geometry: Option<GraphGeometry>,
26+
}
27+
28+
pub async fn get_graph_ui_endpoint(
29+
request_payload: web::Json<GetGraphUiRequestPayload>,
30+
state: web::Data<Arc<DesignerState>>,
31+
) -> Result<impl Responder, actix_web::Error> {
32+
let graph_id = request_payload.graph_id;
33+
34+
// Look for the graph geometry with the given graph_id.
35+
let graph_geometry = state
36+
.tman_metadata
37+
.read()
38+
.await
39+
.graph_ui
40+
.graphs_geometry
41+
.get(&graph_id)
42+
.cloned();
43+
44+
let response_data = GetGraphUiResponseData { graph_geometry };
45+
46+
let response = ApiResponse {
47+
status: Status::Ok,
48+
data: response_data,
49+
meta: None,
50+
};
51+
52+
Ok(HttpResponse::Ok().json(response))
53+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
pub mod get;
8+
pub mod set;
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
use std::sync::Arc;
8+
9+
use actix_web::{web, HttpResponse, Responder};
10+
use serde::{Deserialize, Serialize};
11+
use uuid::Uuid;
12+
13+
use crate::config::metadata::GraphGeometry;
14+
use crate::designer::response::{ApiResponse, Status};
15+
use crate::designer::DesignerState;
16+
17+
#[derive(Debug, Serialize, Deserialize)]
18+
pub struct SetGraphUiRequestPayload {
19+
pub graph_id: Uuid,
20+
pub graph_geometry: GraphGeometry,
21+
}
22+
23+
#[derive(Debug, Serialize, Deserialize)]
24+
pub struct SetGraphUiResponseData {
25+
pub success: bool,
26+
}
27+
28+
pub async fn set_graph_ui_endpoint(
29+
request_payload: web::Json<SetGraphUiRequestPayload>,
30+
state: web::Data<Arc<DesignerState>>,
31+
) -> Result<impl Responder, actix_web::Error> {
32+
// Extract the payload data.
33+
let payload = request_payload.into_inner();
34+
let graph_id = payload.graph_id;
35+
let graph_geometry = payload.graph_geometry;
36+
37+
state
38+
.tman_metadata
39+
.write()
40+
.await
41+
.graph_ui
42+
.graphs_geometry
43+
.insert(graph_id, graph_geometry);
44+
45+
let response_data = SetGraphUiResponseData { success: true };
46+
let response = ApiResponse {
47+
status: Status::Ok,
48+
data: response_data,
49+
meta: None,
50+
};
51+
52+
Ok(HttpResponse::Ok().json(response))
53+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//
2+
// Copyright © 2025 Agora
3+
// This file is part of TEN Framework, an open source project.
4+
// Licensed under the Apache License, Version 2.0, with certain conditions.
5+
// Refer to the "LICENSE" file in the root directory for more information.
6+
//
7+
pub mod graph_ui;

0 commit comments

Comments
 (0)