2024-03-10 12:42:56 +01:00
|
|
|
use napi_derive::napi;
|
2024-08-14 18:00:12 +02:00
|
|
|
use crate::{Client, Workspace};
|
2024-03-10 12:42:56 +01:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
/// connect to codemp servers and return a client session
|
2024-08-14 18:00:12 +02:00
|
|
|
pub async fn connect(addr: Option<String>, username: String, password: String) -> napi::Result<crate::Client>{
|
2024-08-08 19:54:47 +02:00
|
|
|
let client = crate::Client::new(addr.as_deref().unwrap_or("http://codemp.alemi.dev:50053"), username, password)
|
2024-08-07 23:06:33 +02:00
|
|
|
.await?;
|
2024-03-10 12:42:56 +01:00
|
|
|
|
2024-08-08 19:54:47 +02:00
|
|
|
Ok(client)
|
2024-03-10 12:42:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
2024-08-14 18:00:12 +02:00
|
|
|
impl Client {
|
2024-08-08 19:54:47 +02:00
|
|
|
#[napi(js_name = "join_workspace")]
|
2024-03-10 12:42:56 +01:00
|
|
|
/// join workspace with given id (will start its cursor controller)
|
2024-08-14 18:00:12 +02:00
|
|
|
pub async fn js_join_workspace(&self, workspace: String) -> napi::Result<Workspace> {
|
2024-08-08 19:54:47 +02:00
|
|
|
Ok(self.join_workspace(workspace).await?)
|
2024-03-10 12:42:56 +01:00
|
|
|
}
|
|
|
|
|
2024-08-08 19:54:47 +02:00
|
|
|
#[napi(js_name = "get_workspace")]
|
2024-03-10 12:42:56 +01:00
|
|
|
/// get workspace with given id, if it exists
|
2024-08-14 18:00:12 +02:00
|
|
|
pub fn js_get_workspace(&self, workspace: String) -> Option<Workspace> {
|
2024-08-08 19:54:47 +02:00
|
|
|
self.get_workspace(&workspace)
|
2024-03-10 12:42:56 +01:00
|
|
|
}
|
|
|
|
|
2024-08-08 19:54:47 +02:00
|
|
|
#[napi(js_name = "user_id")]
|
2024-03-10 12:42:56 +01:00
|
|
|
/// return current sessions's user id
|
2024-08-08 19:54:47 +02:00
|
|
|
pub fn js_user_id(&self) -> String {
|
|
|
|
self.user_id().to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi(js_name = "active_workspaces")]
|
2024-08-14 18:00:12 +02:00
|
|
|
/// get list of all active workspaces
|
2024-08-08 19:54:47 +02:00
|
|
|
pub fn js_active_workspaces(&self) -> Vec<String> {
|
|
|
|
self.active_workspaces()
|
2024-03-10 12:42:56 +01:00
|
|
|
}
|
|
|
|
}
|