Skip to content

Commit

Permalink
fix: Worker list_users_returns a Vec<User> instead of Vec<Identity>
Browse files Browse the repository at this point in the history
chore: formatter going to town
  • Loading branch information
ghyatzo committed Aug 16, 2024
1 parent 9d5aae4 commit e7fa9f4
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 36 deletions.
5 changes: 4 additions & 1 deletion src/cursor/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
//! a controller implementation for cursor actions
use std::sync::Arc;

use tokio::sync::{broadcast::{self, error::TryRecvError}, mpsc, watch, Mutex};
use tokio::sync::{
broadcast::{self, error::TryRecvError},
mpsc, watch, Mutex,
};
use tonic::async_trait;

use crate::api::{controller::ControllerCallback, Controller, Cursor};
Expand Down
25 changes: 15 additions & 10 deletions src/workspace/service.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
use codemp_proto::{auth::Token, buffer::buffer_client::BufferClient, cursor::cursor_client::CursorClient, workspace::workspace_client::WorkspaceClient};
use tonic::{service::{interceptor::InterceptedService, Interceptor}, transport::{Channel, Endpoint}};

use codemp_proto::{
auth::Token, buffer::buffer_client::BufferClient, cursor::cursor_client::CursorClient,
workspace::workspace_client::WorkspaceClient,
};
use tonic::{
service::{interceptor::InterceptedService, Interceptor},
transport::{Channel, Endpoint},
};

#[derive(Clone)]
pub struct WorkspaceInterceptor {
token: tokio::sync::watch::Receiver<Token>
token: tokio::sync::watch::Receiver<Token>,
}

impl Interceptor for WorkspaceInterceptor {
fn call(&mut self, mut request: tonic::Request<()>) -> Result<tonic::Request<()>, tonic::Status> {
fn call(
&mut self,
mut request: tonic::Request<()>,
) -> Result<tonic::Request<()>, tonic::Status> {
if let Ok(token) = self.token.borrow().token.parse() {
request.metadata_mut().insert("auth", token);
}

Ok(request)
}
}
Expand All @@ -29,9 +37,7 @@ pub struct Services {

impl Services {
pub async fn try_new(dest: &str, token: Token) -> crate::Result<Self> {
let channel = Endpoint::from_shared(dest.to_string())?
.connect()
.await?;
let channel = Endpoint::from_shared(dest.to_string())?.connect().await?;
let (token_tx, token_rx) = tokio::sync::watch::channel(token);
let inter = WorkspaceInterceptor { token: token_rx };
Ok(Self {
Expand Down Expand Up @@ -61,5 +67,4 @@ impl Services {
pub fn cur(&self) -> CursorClient<AuthedService> {
self.cursor.clone()
}

}
68 changes: 43 additions & 25 deletions src/workspace/worker.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
use crate::{
api::{controller::ControllerWorker, Controller, User},
api::{controller::ControllerWorker, Controller, Event, User},
buffer::{self, worker::BufferWorker},
cursor::{self, worker::CursorWorker},
workspace::service::Services,
};

use codemp_proto::{
common::Empty,
auth::Token,
common::Identity,
common::Empty,
files::BufferNode,
workspace::{
workspace_event::{
Expand Down Expand Up @@ -54,14 +53,12 @@ impl Workspace {
token: Token,
) -> crate::Result<Self> {
let services = Services::try_new(dest, token).await?;
let ws_stream = services.ws()
.attach(Empty{})
.await?
.into_inner();
let ws_stream = services.ws().attach(Empty {}).await?.into_inner();

let (tx, rx) = mpsc::channel(256);
let (ev_tx, ev_rx) = mpsc::unbounded_channel();
let cur_stream = services.cur()
let cur_stream = services
.cur()
.attach(tokio_stream::wrappers::ReceiverStream::new(rx))
.await?
.into_inner();
Expand Down Expand Up @@ -92,7 +89,11 @@ impl Workspace {
Ok(ws)
}

pub(crate) fn run_actor(&self, mut stream: Streaming<WorkspaceEvent>, tx: mpsc::UnboundedSender<crate::api::Event>) {
pub(crate) fn run_actor(
&self,
mut stream: Streaming<WorkspaceEvent>,
tx: mpsc::UnboundedSender<crate::api::Event>,
) {
// TODO for buffer and cursor controller we invoke the tokio::spawn outside, but here inside..?
let inner = self.0.clone();
let name = self.id();
Expand All @@ -109,7 +110,9 @@ impl Workspace {
match ev {
// user
WorkspaceEventInner::Join(UserJoin { user }) => {
inner.users.insert(user.clone().into(), User { id: user.into() });
inner
.users
.insert(user.clone().into(), User { id: user.into() });
}
WorkspaceEventInner::Leave(UserLeave { user }) => {
inner.users.remove(&user.into());
Expand All @@ -132,7 +135,7 @@ impl Workspace {
if tx.send(update).is_err() {
tracing::warn!("no active controller to receive workspace event");
}
},
}
}
}
});
Expand Down Expand Up @@ -175,10 +178,7 @@ impl Workspace {
tonic::metadata::MetadataValue::try_from(credentials.id.id)
.expect("could not represent path as byte sequence"),
);
let stream = self.0.services.buf()
.attach(req)
.await?
.into_inner();
let stream = self.0.services.buf().attach(req).await?.into_inner();

let worker = BufferWorker::new(self.0.user_id, path);
let controller = worker.controller();
Expand Down Expand Up @@ -206,17 +206,24 @@ impl Workspace {
pub fn detach(&self, path: &str) -> DetachResult {
match self.0.buffers.remove(path) {
None => DetachResult::NotAttached,
Some((_name, controller)) => if controller.stop() {
DetachResult::Detaching
} else {
DetachResult::AlreadyDetached
Some((_name, controller)) => {
if controller.stop() {
DetachResult::Detaching
} else {
DetachResult::AlreadyDetached
}
}
}
}

/// await next workspace [crate::api::Event] and return it
pub async fn event(&self) -> crate::Result<crate::api::Event> {
self.0.events.lock().await.recv().await
pub async fn event(&self) -> crate::Result<Event> {
self.0
.events
.lock()
.await
.recv()
.await
.ok_or(crate::Error::Channel { send: false })
}

Expand Down Expand Up @@ -261,15 +268,18 @@ impl Workspace {
/// get a list of the users attached to a specific buffer
///
/// TODO: discuss implementation details
pub async fn list_buffer_users(&self, path: &str) -> crate::Result<Vec<Identity>> {
pub async fn list_buffer_users(&self, path: &str) -> crate::Result<Vec<User>> {
let mut workspace_client = self.0.services.ws();
let buffer_users = workspace_client
.list_buffer_users(tonic::Request::new(BufferNode {
path: path.to_string(),
}))
.await?
.into_inner()
.users;
.users
.into_iter()
.map(|id| id.into())
.collect();

Ok(buffer_users)
}
Expand Down Expand Up @@ -313,7 +323,11 @@ impl Workspace {
/// get a list of all the currently attached to buffers
// #[cfg_attr(feature = "js", napi)] // https://github.com/napi-rs/napi-rs/issues/1120
pub fn buffer_list(&self) -> Vec<String> {
self.0.buffers.iter().map(|elem| elem.key().clone()).collect()
self.0
.buffers
.iter()
.map(|elem| elem.key().clone())
.collect()
}

/// get the currently cached "filetree"
Expand All @@ -327,7 +341,11 @@ impl Drop for WorkspaceInner {
fn drop(&mut self) {
for entry in self.buffers.iter() {
if !entry.value().stop() {
tracing::warn!("could not stop buffer worker {} for workspace {}", entry.value().name(), self.id);
tracing::warn!(
"could not stop buffer worker {} for workspace {}",
entry.value().name(),
self.id
);
}
}
if !self.cursor.stop() {
Expand Down

0 comments on commit e7fa9f4

Please sign in to comment.