2024-09-04 20:03:34 +02:00
|
|
|
//! ### Client
|
|
|
|
//! Main `codemp` client, containing and managing all underlying services.
|
2023-08-20 00:46:55 +02:00
|
|
|
|
2024-08-08 03:59:28 +02:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
2024-02-07 01:12:05 +01:00
|
|
|
use dashmap::DashMap;
|
2024-08-22 00:57:24 +02:00
|
|
|
use tonic::{service::interceptor::InterceptedService, transport::{Channel, Endpoint}};
|
2024-01-25 02:13:45 +01:00
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
use crate::{api::User, errors::{ConnectionResult, RemoteResult}, ext::InternallyMutable, network, workspace::Workspace};
|
2024-08-22 00:57:24 +02:00
|
|
|
use codemp_proto::{
|
|
|
|
auth::{auth_client::AuthClient, LoginRequest},
|
|
|
|
common::{Empty, Token}, session::{session_client::SessionClient, InviteRequest, WorkspaceRequest},
|
|
|
|
};
|
2024-08-08 23:58:45 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "python")]
|
|
|
|
use pyo3::prelude::*;
|
2024-08-08 02:30:34 +02:00
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// A `codemp` client handle.
|
2023-08-20 00:46:55 +02:00
|
|
|
///
|
2024-09-04 20:03:34 +02:00
|
|
|
/// It generates a new UUID and stores user credentials upon connecting.
|
|
|
|
///
|
|
|
|
/// A new [Client] can be obtained with [Client::connect].
|
2024-08-08 03:59:28 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2024-08-08 19:54:47 +02:00
|
|
|
#[cfg_attr(feature = "js", napi_derive::napi)]
|
2024-08-08 23:58:45 +02:00
|
|
|
#[cfg_attr(feature = "python", pyclass)]
|
2024-08-08 03:59:28 +02:00
|
|
|
pub struct Client(Arc<ClientInner>);
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct ClientInner {
|
2024-08-22 00:57:24 +02:00
|
|
|
user: User,
|
2024-08-08 02:30:34 +02:00
|
|
|
host: String,
|
2024-08-08 00:25:22 +02:00
|
|
|
workspaces: DashMap<String, Workspace>,
|
2024-08-22 00:57:24 +02:00
|
|
|
auth: AuthClient<Channel>,
|
2024-09-04 20:03:34 +02:00
|
|
|
session: SessionClient<InterceptedService<Channel, network::SessionInterceptor>>,
|
2024-08-22 00:57:24 +02:00
|
|
|
claims: InternallyMutable<Token>,
|
2024-01-25 02:13:45 +01:00
|
|
|
}
|
|
|
|
|
2023-08-17 02:58:55 +02:00
|
|
|
impl Client {
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Connect to the server, authenticate and instantiate a new [Client].
|
2024-08-22 00:57:24 +02:00
|
|
|
pub async fn connect(
|
2024-08-08 02:30:34 +02:00
|
|
|
host: impl AsRef<str>,
|
|
|
|
username: impl AsRef<str>,
|
2024-08-08 23:58:45 +02:00
|
|
|
password: impl AsRef<str>,
|
2024-09-01 02:46:03 +02:00
|
|
|
) -> ConnectionResult<Self> {
|
2024-08-08 02:30:34 +02:00
|
|
|
let host = if host.as_ref().starts_with("http") {
|
|
|
|
host.as_ref().to_string()
|
|
|
|
} else {
|
|
|
|
format!("https://{}", host.as_ref())
|
|
|
|
};
|
2024-01-25 02:13:45 +01:00
|
|
|
|
2024-08-22 00:57:24 +02:00
|
|
|
let channel = Endpoint::from_shared(host.clone())?.connect().await?;
|
|
|
|
let mut auth = AuthClient::new(channel.clone());
|
|
|
|
|
|
|
|
let resp = auth.login(LoginRequest {
|
|
|
|
username: username.as_ref().to_string(),
|
|
|
|
password: password.as_ref().to_string(),
|
|
|
|
})
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
|
|
|
|
|
|
|
let claims = InternallyMutable::new(resp.token);
|
|
|
|
|
|
|
|
let session = SessionClient::with_interceptor(
|
2024-09-04 20:03:34 +02:00
|
|
|
channel, network::SessionInterceptor(claims.channel())
|
2024-08-22 00:57:24 +02:00
|
|
|
);
|
2023-08-19 04:02:21 +02:00
|
|
|
|
2024-08-08 03:59:28 +02:00
|
|
|
Ok(Client(Arc::new(ClientInner {
|
2024-08-08 02:30:34 +02:00
|
|
|
host,
|
2024-08-22 00:57:24 +02:00
|
|
|
user: resp.user.into(),
|
2024-08-08 00:25:22 +02:00
|
|
|
workspaces: DashMap::default(),
|
2024-08-22 00:57:24 +02:00
|
|
|
claims,
|
|
|
|
auth, session,
|
2024-08-08 03:59:28 +02:00
|
|
|
})))
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Refresh session token.
|
2024-09-04 17:20:59 +02:00
|
|
|
pub async fn refresh(&self) -> tonic::Result<()> {
|
2024-08-22 00:57:24 +02:00
|
|
|
let new_token = self.0.auth.clone().refresh(self.0.claims.get())
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
|
|
|
self.0.claims.set(new_token);
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Attempt to create a new workspace with given name.
|
2024-09-04 17:20:59 +02:00
|
|
|
pub async fn create_workspace(&self, name: impl AsRef<str>) -> RemoteResult<()> {
|
2024-08-22 00:57:24 +02:00
|
|
|
self.0.session
|
|
|
|
.clone()
|
|
|
|
.create_workspace(WorkspaceRequest { workspace: name.as_ref().to_string() })
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Delete an existing workspace if possible.
|
2024-09-04 17:20:59 +02:00
|
|
|
pub async fn delete_workspace(&self, name: impl AsRef<str>) -> RemoteResult<()> {
|
2024-08-22 00:57:24 +02:00
|
|
|
self.0.session
|
|
|
|
.clone()
|
|
|
|
.delete_workspace(WorkspaceRequest { workspace: name.as_ref().to_string() })
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Invite user with given username to the given workspace, if possible.
|
2024-09-04 17:20:59 +02:00
|
|
|
pub async fn invite_to_workspace(&self, workspace_name: impl AsRef<str>, user_name: impl AsRef<str>) -> RemoteResult<()> {
|
2024-08-22 00:57:24 +02:00
|
|
|
self.0.session
|
|
|
|
.clone()
|
|
|
|
.invite_to_workspace(InviteRequest {
|
|
|
|
workspace: workspace_name.as_ref().to_string(),
|
|
|
|
user: user_name.as_ref().to_string(),
|
|
|
|
})
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// List all available workspaces, also filtering between those owned and those invited to.
|
2024-09-04 17:20:59 +02:00
|
|
|
pub async fn list_workspaces(&self, owned: bool, invited: bool) -> RemoteResult<Vec<String>> {
|
2024-08-22 00:57:24 +02:00
|
|
|
let mut workspaces = self.0.session
|
|
|
|
.clone()
|
|
|
|
.list_workspaces(Empty {})
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
|
|
|
|
|
|
|
let mut out = Vec::new();
|
|
|
|
|
|
|
|
if owned { out.append(&mut workspaces.owned) }
|
|
|
|
if invited { out.append(&mut workspaces.invited) }
|
|
|
|
|
|
|
|
Ok(out)
|
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Join and return a [Workspace].
|
2024-09-01 02:46:03 +02:00
|
|
|
pub async fn join_workspace(&self, workspace: impl AsRef<str>) -> ConnectionResult<Workspace> {
|
2024-08-22 00:57:24 +02:00
|
|
|
let token = self.0.session
|
|
|
|
.clone()
|
|
|
|
.access_workspace(WorkspaceRequest { workspace: workspace.as_ref().to_string() })
|
|
|
|
.await?
|
|
|
|
.into_inner();
|
2023-08-16 23:09:47 +02:00
|
|
|
|
2024-08-08 02:30:34 +02:00
|
|
|
let ws = Workspace::try_new(
|
|
|
|
workspace.as_ref().to_string(),
|
2024-08-22 00:57:24 +02:00
|
|
|
self.0.user.clone(),
|
2024-08-08 03:59:28 +02:00
|
|
|
&self.0.host,
|
2024-08-22 00:57:24 +02:00
|
|
|
token,
|
|
|
|
self.0.claims.channel(),
|
2024-08-08 23:58:45 +02:00
|
|
|
)
|
|
|
|
.await?;
|
2024-01-25 16:31:38 +01:00
|
|
|
|
2024-08-08 23:58:45 +02:00
|
|
|
self.0
|
|
|
|
.workspaces
|
|
|
|
.insert(workspace.as_ref().to_string(), ws.clone());
|
2024-02-07 01:12:05 +01:00
|
|
|
|
|
|
|
Ok(ws)
|
2023-08-16 23:09:47 +02:00
|
|
|
}
|
2024-01-25 03:25:45 +01:00
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Leave the [Workspace] with the given name.
|
2024-08-08 00:28:35 +02:00
|
|
|
pub fn leave_workspace(&self, id: &str) -> bool {
|
2024-08-08 03:59:28 +02:00
|
|
|
self.0.workspaces.remove(id).is_some()
|
2024-08-08 00:28:35 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Gets a [Workspace] handle by name.
|
2024-08-05 19:13:48 +02:00
|
|
|
pub fn get_workspace(&self, id: &str) -> Option<Workspace> {
|
2024-08-08 03:59:28 +02:00
|
|
|
self.0.workspaces.get(id).map(|x| x.clone())
|
2024-02-07 03:47:37 +01:00
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Get the names of all active [Workspace]s.
|
2024-08-08 04:14:13 +02:00
|
|
|
pub fn active_workspaces(&self) -> Vec<String> {
|
2024-08-08 23:58:45 +02:00
|
|
|
self.0
|
|
|
|
.workspaces
|
|
|
|
.iter()
|
|
|
|
.map(|x| x.key().to_string())
|
|
|
|
.collect()
|
2024-08-08 04:14:13 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 20:03:34 +02:00
|
|
|
/// Get the currently logged in user.
|
2024-08-22 00:57:24 +02:00
|
|
|
pub fn user(&self) -> &User {
|
|
|
|
&self.0.user
|
|
|
|
}
|
|
|
|
}
|