mirror of
https://github.com/uqbar-dao/nectar.git
synced 2024-12-22 08:01:47 +03:00
contacts FE
This commit is contained in:
parent
d7a94beac3
commit
c2cce9d376
26
Cargo.lock
generated
26
Cargo.lock
generated
@ -1867,7 +1867,7 @@ checksum = "f7144d30dcf0fafbce74250a3963025d8d52177934239851c917d29f1df280c2"
|
||||
name = "contacts"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"kinode_process_lib 0.9.3",
|
||||
"kinode_process_lib 0.9.4",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"wit-bindgen",
|
||||
@ -3759,6 +3759,28 @@ dependencies = [
|
||||
"wit-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kinode_process_lib"
|
||||
version = "0.9.4"
|
||||
source = "git+https://github.com/kinode-dao/process_lib?rev=088a549#088a5497257eada697e0869d6a8d7e9ef5e620f6"
|
||||
dependencies = [
|
||||
"alloy 0.1.4",
|
||||
"alloy-primitives",
|
||||
"alloy-sol-macro",
|
||||
"alloy-sol-types",
|
||||
"anyhow",
|
||||
"bincode",
|
||||
"http 1.1.0",
|
||||
"mime_guess",
|
||||
"rand 0.8.5",
|
||||
"rmp-serde",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"url",
|
||||
"wit-bindgen",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kit"
|
||||
version = "0.7.7"
|
||||
@ -5580,7 +5602,7 @@ dependencies = [
|
||||
"anyhow",
|
||||
"base64 0.22.1",
|
||||
"bincode",
|
||||
"kinode_process_lib 0.9.3",
|
||||
"kinode_process_lib 0.9.4",
|
||||
"rmp-serde",
|
||||
"serde",
|
||||
"serde_json",
|
||||
|
@ -129,6 +129,7 @@ The distro userspace packages are:
|
||||
|
||||
- `app_store:sys`
|
||||
- `chess:sys`
|
||||
- `contacts:sys`
|
||||
- `homepage:sys`
|
||||
- `kino_updates:sys`
|
||||
- `kns_indexer:sys`
|
||||
|
5
kinode/packages/contacts/Cargo.lock
generated
5
kinode/packages/contacts/Cargo.lock
generated
@ -1462,9 +1462,8 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "kinode_process_lib"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7722aef4bff0625445fafda89a02f82ce0e16c7def6024e1317ae55a632ad331"
|
||||
version = "0.9.4"
|
||||
source = "git+https://github.com/kinode-dao/process_lib?rev=088a549#088a5497257eada697e0869d6a8d7e9ef5e620f6"
|
||||
dependencies = [
|
||||
"alloy",
|
||||
"alloy-primitives",
|
||||
|
@ -26,6 +26,7 @@ interface contacts {
|
||||
add-field,
|
||||
remove-contact,
|
||||
remove-field,
|
||||
error(string),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@ edition = "2021"
|
||||
simulation-mode = []
|
||||
|
||||
[dependencies]
|
||||
kinode_process_lib = "0.9.3"
|
||||
kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "088a549" }
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
wit-bindgen = "0.24.0"
|
||||
|
@ -1 +1 @@
|
||||
data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMTM2IiBoZWlnaHQ9IjEzNiIgdmlld0JveD0iMCAwIDEzNiAxMzYiIGZpbGw9Im5vbmUiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CjxyZWN0IHdpZHRoPSIxMzYiIGhlaWdodD0iMTM2IiByeD0iNjgiIGZpbGw9IiNGRkY1RDkiIGZpbGwtb3BhY2l0eT0iMC40Ii8+CjxnIGNsaXAtcGF0aD0idXJsKCNjbGlwMF8xODIxXzkwOTMpIj4KPHBhdGggZD0iTTY3LjQ4NTUgOTQuOTg4MUM2NS44MTYxIDk0Ljk4ODEgNjQuMTQ2NyA5NC45NjQzIDYyLjQ3NzIgOTQuOTg4MUM2MS41NjAyIDk1LjAxMTkgNjEu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KSIvPgo8ZGVmcz4KPGxpbmVhckdyYWRpZW50IGlkPSJwYWludDBfbGluZWFyXzIwOTFfMTAyODIiIHgxPSI1MDAiIHkxPSIwIiB4Mj0iNTAwIiB5Mj0iMTAwMCIgZ3JhZGllbnRVbml0cz0idXNlclNwYWNlT25Vc2UiPgo8c3RvcCBzdG9wLWNvbG9yPSIjRjM1NDIyIi8+CjxzdG9wIG9mZnNldD0iMSIgc3RvcC1vcGFjaXR5PSIwIi8+CjwvbGluZWFyR3JhZGllbnQ+CjxsaW5lYXJHcmFkaWVudCBpZD0icGFpbnQxX2xpbmVhcl8yMDkxXzEwMjgyIiB4MT0iNzgyLjUiIHkxPSI3My41IiB4Mj0iMTg1LjUiIHkyPSI4OTQuNSIgZ3JhZGllbnRVbml0cz0idXNlclNwYWNlT25Vc2UiPgo8c3RvcCBzdG9wLWNvbG9yPSIjRjM1NDIyIi8+CjxzdG9wIG9mZnNldD0iMSIgc3RvcC1jb2xvcj0iI0E1MzEwQyIvPgo8L2xpbmVhckdyYWRpZW50Pgo8bGluZWFyR3JhZGllbnQgaWQ9InBhaW50Ml9saW5lYXJfMjA5MV8xMDI4MiIgeDE9Ijc1NCIgeTE9Ijg5LjUiIHgyPSIyNTYiIHkyPSIxMDE1LjUiIGdyYWRpZW50VW5pdHM9InVzZXJTcGFjZU9uVXNlIj4KPHN0b3Agc3RvcC1jb2xvcj0iI0YzNTQyMiIvPgo8c3RvcCBvZmZzZXQ9IjEiIHN0b3AtY29sb3I9IiNBNzMyMEQiLz4KPC9saW5lYXJHcmFkaWVudD4KPC9kZWZzPgo8L3N2Zz4K
|
@ -1,7 +1,7 @@
|
||||
use crate::kinode::process::contacts::{ContactsRequest, ContactsResponse};
|
||||
use kinode_process_lib::{
|
||||
await_message, call_init, get_blob, get_typed_state, homepage, http, println, set_state,
|
||||
Address, LazyLoadBlob, Message, NodeId, Response,
|
||||
await_message, call_init, eth, get_blob, get_typed_state, homepage, http, kimap, kiprintln,
|
||||
set_state, Address, LazyLoadBlob, Message, NodeId, Response,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::collections::HashMap;
|
||||
@ -17,6 +17,7 @@ struct Contacts(HashMap<NodeId, Contact>);
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
struct ContactsState {
|
||||
our: Address,
|
||||
kimap: kimap::Kimap,
|
||||
contacts: Contacts,
|
||||
}
|
||||
|
||||
@ -24,6 +25,7 @@ impl ContactsState {
|
||||
fn new(our: Address) -> Self {
|
||||
get_typed_state(|bytes| serde_json::from_slice(bytes)).unwrap_or(Self {
|
||||
our,
|
||||
kimap: kimap::Kimap::default(30),
|
||||
contacts: Contacts(HashMap::new()),
|
||||
})
|
||||
}
|
||||
@ -42,10 +44,12 @@ impl ContactsState {
|
||||
|
||||
fn add_contact(&mut self, node: NodeId) {
|
||||
self.contacts.0.insert(node, Contact(HashMap::new()));
|
||||
self.save();
|
||||
}
|
||||
|
||||
fn remove_contact(&mut self, node: NodeId) {
|
||||
self.contacts.0.remove(&node);
|
||||
self.save();
|
||||
}
|
||||
|
||||
fn add_field(&mut self, node: NodeId, field: String, value: serde_json::Value) {
|
||||
@ -55,12 +59,25 @@ impl ContactsState {
|
||||
.or_insert_with(|| Contact(HashMap::new()))
|
||||
.0
|
||||
.insert(field, value);
|
||||
self.save();
|
||||
}
|
||||
|
||||
fn remove_field(&mut self, node: NodeId, field: String) {
|
||||
if let Some(contact) = self.contacts.0.get_mut(&node) {
|
||||
contact.0.remove(&field);
|
||||
}
|
||||
self.save();
|
||||
}
|
||||
|
||||
fn ws_update(&self, http_server: &mut http::server::HttpServer) {
|
||||
http_server.ws_push_all_channels(
|
||||
"/",
|
||||
http::server::WsMessageType::Text,
|
||||
LazyLoadBlob::new(
|
||||
Some("application/json"),
|
||||
serde_json::to_vec(self.contacts()).unwrap(),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -73,6 +90,8 @@ wit_bindgen::generate!({
|
||||
|
||||
call_init!(initialize);
|
||||
fn initialize(our: Address) {
|
||||
kiprintln!("started");
|
||||
|
||||
homepage::add_to_homepage("Contacts", Some(ICON), Some("/"), None);
|
||||
|
||||
let mut state: ContactsState = ContactsState::new(our);
|
||||
@ -101,24 +120,13 @@ fn main_loop(state: &mut ContactsState, http_server: &mut http::server::HttpServ
|
||||
// ignore send errors, local-only process
|
||||
continue;
|
||||
}
|
||||
Ok(Message::Request {
|
||||
source,
|
||||
body,
|
||||
expects_response,
|
||||
..
|
||||
}) => {
|
||||
Ok(Message::Request { source, body, .. }) => {
|
||||
// ignore messages from other nodes -- technically superfluous check
|
||||
// since manifest does not acquire networking capability
|
||||
if source.node() != state.our.node {
|
||||
continue; // ignore messages from other nodes
|
||||
}
|
||||
let response_and_blob = handle_request(&source, &body, state, http_server);
|
||||
// state.ws_update(http_server);
|
||||
if expects_response.is_some() && response_and_blob.is_some() {
|
||||
let (response, blob) = response_and_blob.unwrap();
|
||||
Response::new()
|
||||
.body(serde_json::to_vec(&response).unwrap())
|
||||
.send()
|
||||
.unwrap();
|
||||
continue;
|
||||
}
|
||||
handle_request(&source, &body, state, http_server);
|
||||
}
|
||||
_ => continue, // ignore responses
|
||||
}
|
||||
@ -130,7 +138,7 @@ fn handle_request(
|
||||
body: &[u8],
|
||||
state: &mut ContactsState,
|
||||
http_server: &mut http::server::HttpServer,
|
||||
) -> Option<ContactsResponse> {
|
||||
) {
|
||||
// source node is ALWAYS ourselves since networking is disabled
|
||||
if source.process == "http_server:distro:sys" {
|
||||
// receive HTTP requests and websocket connection messages from our server
|
||||
@ -143,13 +151,15 @@ fn handle_request(
|
||||
// we don't expect websocket messages
|
||||
},
|
||||
);
|
||||
None
|
||||
} else {
|
||||
// let settings_request = serde_json::from_slice::<SettingsRequest>(body)
|
||||
// .map_err(|_| SettingsError::MalformedRequest)?;
|
||||
// handle_settings_request(state, settings_request)
|
||||
None
|
||||
let (response, blob) = handle_contacts_request(state, body);
|
||||
let mut response = Response::new().body(serde_json::to_vec(&response).unwrap());
|
||||
if let Some(blob) = blob {
|
||||
response = response.blob(blob);
|
||||
}
|
||||
response.send().unwrap();
|
||||
}
|
||||
state.ws_update(http_server);
|
||||
}
|
||||
|
||||
/// Handle HTTP requests from our own frontend.
|
||||
@ -158,21 +168,27 @@ fn handle_http_request(
|
||||
http_request: &http::server::IncomingHttpRequest,
|
||||
) -> (http::server::HttpResponse, Option<LazyLoadBlob>) {
|
||||
match http_request.method().unwrap().as_str() {
|
||||
"GET" => {
|
||||
// state.fetch().unwrap();
|
||||
(
|
||||
http::server::HttpResponse::new(http::StatusCode::OK)
|
||||
.header("Content-Type", "application/json"),
|
||||
Some(LazyLoadBlob::new(
|
||||
Some("application/json"),
|
||||
serde_json::to_vec(&state).unwrap(),
|
||||
)),
|
||||
)
|
||||
}
|
||||
"GET" => (
|
||||
http::server::HttpResponse::new(http::StatusCode::OK)
|
||||
.header("Content-Type", "application/json"),
|
||||
Some(LazyLoadBlob::new(
|
||||
Some("application/json"),
|
||||
serde_json::to_vec(state.contacts()).unwrap(),
|
||||
)),
|
||||
),
|
||||
"POST" => {
|
||||
let blob = get_blob().unwrap();
|
||||
let request = serde_json::from_slice::<ContactsRequest>(&blob.bytes).unwrap();
|
||||
let (_response, blob) = handle_contacts_request(state, request);
|
||||
let (response, blob) = handle_contacts_request(state, blob.bytes());
|
||||
if let ContactsResponse::Error(e) = response {
|
||||
return (
|
||||
http::server::HttpResponse::new(http::StatusCode::BAD_REQUEST)
|
||||
.header("Content-Type", "application/json"),
|
||||
Some(LazyLoadBlob::new(
|
||||
Some("application/json"),
|
||||
serde_json::to_vec(&e).unwrap(),
|
||||
)),
|
||||
);
|
||||
}
|
||||
(
|
||||
http::server::HttpResponse::new(http::StatusCode::OK)
|
||||
.header("Content-Type", "application/json"),
|
||||
@ -195,23 +211,83 @@ fn handle_http_request(
|
||||
|
||||
fn handle_contacts_request(
|
||||
state: &mut ContactsState,
|
||||
request: ContactsRequest,
|
||||
request_bytes: &[u8],
|
||||
) -> (ContactsResponse, Option<LazyLoadBlob>) {
|
||||
let response = match request {
|
||||
ContactsRequest::GetNames => ContactsResponse::GetNames(
|
||||
state
|
||||
.contacts()
|
||||
.0
|
||||
.keys()
|
||||
.map(|node| node.to_string())
|
||||
.collect(),
|
||||
),
|
||||
ContactsRequest::GetAllContacts => ContactsResponse::GetAllContacts,
|
||||
ContactsRequest::GetContact(node) => ContactsResponse::GetContact,
|
||||
ContactsRequest::AddContact(node) => ContactsResponse::AddContact,
|
||||
ContactsRequest::AddField((node, field, value)) => ContactsResponse::AddField,
|
||||
ContactsRequest::RemoveContact(node) => ContactsResponse::RemoveContact,
|
||||
ContactsRequest::RemoveField((node, field)) => ContactsResponse::RemoveField,
|
||||
let Ok(request) = serde_json::from_slice::<ContactsRequest>(request_bytes) else {
|
||||
return (
|
||||
ContactsResponse::Error("Malformed request".to_string()),
|
||||
None,
|
||||
);
|
||||
};
|
||||
(response, None)
|
||||
match request {
|
||||
ContactsRequest::GetNames => (
|
||||
ContactsResponse::GetNames(
|
||||
state
|
||||
.contacts()
|
||||
.0
|
||||
.keys()
|
||||
.map(|node| node.to_string())
|
||||
.collect(),
|
||||
),
|
||||
None,
|
||||
),
|
||||
ContactsRequest::GetAllContacts => (
|
||||
ContactsResponse::GetAllContacts,
|
||||
Some(LazyLoadBlob::new(
|
||||
Some("application/json"),
|
||||
serde_json::to_vec(state.contacts()).unwrap(),
|
||||
)),
|
||||
),
|
||||
ContactsRequest::GetContact(node) => (
|
||||
ContactsResponse::GetContact,
|
||||
Some(LazyLoadBlob::new(
|
||||
Some("application/json"),
|
||||
serde_json::to_vec(&state.get_contact(node)).unwrap(),
|
||||
)),
|
||||
),
|
||||
ContactsRequest::AddContact(node) => {
|
||||
if let Some((response, blob)) = invalid_node(state, &node) {
|
||||
return (response, blob);
|
||||
}
|
||||
state.add_contact(node);
|
||||
(ContactsResponse::AddContact, None)
|
||||
}
|
||||
ContactsRequest::AddField((node, field, value)) => {
|
||||
if let Some((response, blob)) = invalid_node(state, &node) {
|
||||
return (response, blob);
|
||||
}
|
||||
let Ok(value) = serde_json::from_str::<serde_json::Value>(&value) else {
|
||||
return (ContactsResponse::Error("Malformed value".to_string()), None);
|
||||
};
|
||||
state.add_field(node, field, value);
|
||||
(ContactsResponse::AddField, None)
|
||||
}
|
||||
ContactsRequest::RemoveContact(node) => {
|
||||
state.remove_contact(node);
|
||||
(ContactsResponse::RemoveContact, None)
|
||||
}
|
||||
ContactsRequest::RemoveField((node, field)) => {
|
||||
state.remove_field(node, field);
|
||||
(ContactsResponse::RemoveField, None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid_node(
|
||||
state: &ContactsState,
|
||||
node: &str,
|
||||
) -> Option<(ContactsResponse, Option<LazyLoadBlob>)> {
|
||||
if state
|
||||
.kimap
|
||||
.get(&node)
|
||||
.map(|(tba, _, _)| tba != eth::Address::ZERO)
|
||||
.unwrap_or(false)
|
||||
{
|
||||
None
|
||||
} else {
|
||||
Some((
|
||||
ContactsResponse::Error("Node name invalid or does not exist".to_string()),
|
||||
None,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
@ -5,11 +5,13 @@
|
||||
"on_exit": "Restart",
|
||||
"request_networking": false,
|
||||
"request_capabilities": [
|
||||
"eth:distro:sys",
|
||||
"homepage:homepage:sys",
|
||||
"http_server:distro:sys",
|
||||
"vfs:distro:sys"
|
||||
],
|
||||
"grant_capabilities": [
|
||||
"eth:distro:sys",
|
||||
"http_server:distro:sys",
|
||||
"vfs:distro:sys"
|
||||
],
|
||||
|
@ -47,6 +47,18 @@
|
||||
<body>
|
||||
<h1>contacts</h1>
|
||||
<main>
|
||||
<article id="edit">
|
||||
<h2>Contacts</h2>
|
||||
<form id="add-contact">
|
||||
<input type="text" name="node">
|
||||
<button type="submit">add contact</button>
|
||||
</form>
|
||||
</article>
|
||||
|
||||
<article id="contacts-article">
|
||||
<ul id="contacts"></ul>
|
||||
</article>
|
||||
|
||||
<script src="/contacts:contacts:sys/script.js"></script>
|
||||
</main>
|
||||
</body>
|
||||
|
@ -1,16 +1,16 @@
|
||||
const APP_PATH = '/contacts:contacts:sys/';
|
||||
const APP_PATH = '/contacts:contacts:sys/ask';
|
||||
|
||||
// Fetch initial data and populate the UI
|
||||
function init() {
|
||||
fetch(APP_PATH + 'get')
|
||||
fetch(APP_PATH)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
populate(data);
|
||||
});
|
||||
}
|
||||
|
||||
function api_call(path, body) {
|
||||
fetch(APP_PATH + path, {
|
||||
function api_call(body) {
|
||||
fetch(APP_PATH, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
@ -21,17 +21,58 @@ function api_call(path, body) {
|
||||
|
||||
function populate(data) {
|
||||
console.log(data);
|
||||
populate_contacts(data);
|
||||
}
|
||||
|
||||
function populate_contacts(contacts) {
|
||||
const ul = document.getElementById('contacts');
|
||||
ul.innerHTML = '';
|
||||
Object.entries(contacts).forEach(([node, contact]) => {
|
||||
const li = document.createElement('li');
|
||||
li.innerHTML = `${JSON.stringify(node, undefined, 2)}`;
|
||||
ul.appendChild(li);
|
||||
});
|
||||
}
|
||||
|
||||
document.getElementById('add-contact').addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
const data = new FormData(e.target);
|
||||
const node = data.get('node');
|
||||
const body = {
|
||||
"AddContact": node
|
||||
};
|
||||
fetch(APP_PATH, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(body),
|
||||
}).then(response => {
|
||||
if (response.status === 200) {
|
||||
return null;
|
||||
} else {
|
||||
return response.json();
|
||||
}
|
||||
}).then(data => {
|
||||
if (data === null) {
|
||||
e.target.reset();
|
||||
return;
|
||||
} else {
|
||||
alert(JSON.stringify(data));
|
||||
}
|
||||
}).catch(error => {
|
||||
console.error('Error:', error);
|
||||
});
|
||||
})
|
||||
|
||||
// Call init to start the application
|
||||
init();
|
||||
|
||||
// Setup WebSocket connection
|
||||
// const wsProtocol = location.protocol === 'https:' ? 'wss://' : 'ws://';
|
||||
// const ws = new WebSocket(wsProtocol + location.host + "/settings:settings:sys/");
|
||||
// ws.onmessage = event => {
|
||||
// const data = JSON.parse(event.data);
|
||||
// console.log(data);
|
||||
// populate(data);
|
||||
// };
|
||||
const wsProtocol = location.protocol === 'https:' ? 'wss://' : 'ws://';
|
||||
const ws = new WebSocket(wsProtocol + location.host + "/contacts:contacts:sys/");
|
||||
ws.onmessage = event => {
|
||||
const data = JSON.parse(event.data);
|
||||
populate(data);
|
||||
};
|
||||
|
||||
|
5
kinode/packages/settings/Cargo.lock
generated
5
kinode/packages/settings/Cargo.lock
generated
@ -1452,9 +1452,8 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "kinode_process_lib"
|
||||
version = "0.9.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "76c5b69ac1fc0cb457c7714ceb8c0a5bdbee4ee00b837f9f16ea711e902bdfe8"
|
||||
version = "0.9.4"
|
||||
source = "git+https://github.com/kinode-dao/process_lib?rev=088a549#088a5497257eada697e0869d6a8d7e9ef5e620f6"
|
||||
dependencies = [
|
||||
"alloy",
|
||||
"alloy-primitives",
|
||||
|
@ -10,7 +10,7 @@ simulation-mode = []
|
||||
anyhow = "1.0"
|
||||
base64 = "0.22.0"
|
||||
bincode = "1.3.3"
|
||||
kinode_process_lib = "0.9.1"
|
||||
kinode_process_lib = { git = "https://github.com/kinode-dao/process_lib", rev = "088a549" }
|
||||
rmp-serde = "1.2.0"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
|
Loading…
Reference in New Issue
Block a user