Skip to content

Commit 9c77cea

Browse files
committed
feat: most API procedures now want UUIDs
this is an incomplete update: there are some protocol design flaws which make it very complex or impossible to properly migrate to uuids. so this commit makes it compile, but doesn't make it "work"
1 parent 7c04214 commit 9c77cea

3 files changed

Lines changed: 55 additions & 66 deletions

File tree

src/client.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use codemp_proto::{
2020
auth::{LoginRequest, auth_client::AuthClient},
2121
common::{Empty, Identifier, Token},
2222
session::{
23-
InviteRequest, OwnedWorkspaceRequest, WorkspaceRequest, session_client::SessionClient,
23+
InviteRequest, OwnedWorkspaceRequest, session_client::SessionClient,
2424
},
2525
};
2626

@@ -110,28 +110,26 @@ impl Client {
110110
}
111111

112112
/// Delete an existing workspace if possible.
113-
pub async fn delete_workspace(&self, name: impl AsRef<str>) -> RemoteResult<()> {
113+
pub async fn delete_workspace(&self, id: uuid::Uuid) -> RemoteResult<()> {
114114
self.0
115115
.session
116116
.clone()
117-
.delete_workspace(OwnedWorkspaceRequest {
118-
name: name.as_ref().to_string(),
119-
})
117+
.delete_workspace(Identifier::from(id))
120118
.await?;
121119
Ok(())
122120
}
123121

124122
/// Invite user with given username to the given workspace, if possible.
125123
pub async fn invite_to_workspace(
126124
&self,
127-
workspace_name: impl AsRef<str>,
125+
workspace_id: uuid::Uuid,
128126
user_name: impl AsRef<str>,
129127
) -> RemoteResult<()> {
130128
self.0
131129
.session
132130
.clone()
133131
.invite_to_workspace(InviteRequest {
134-
workspace: workspace_name.as_ref().to_string(),
132+
workspace: Identifier::from(workspace_id),
135133
user: user_name.as_ref().to_string(),
136134
})
137135
.await?;
@@ -149,7 +147,7 @@ impl Client {
149147
.into_inner()
150148
.owned
151149
.into_iter()
152-
.map(|x| crate::api::WorkspaceInfo::from(x))
150+
.map(crate::api::WorkspaceInfo::from)
153151
.collect())
154152
}
155153

@@ -164,7 +162,7 @@ impl Client {
164162
.into_inner()
165163
.invited
166164
.into_iter()
167-
.map(|x| crate::api::WorkspaceInfo::from(x))
165+
.map(crate::api::WorkspaceInfo::from)
168166
.collect())
169167
}
170168

@@ -173,9 +171,7 @@ impl Client {
173171
pub async fn attach_workspace(&self, workspace: uuid::Uuid) -> ConnectionResult<Workspace> {
174172
let mut session_client = self.0.session.clone();
175173
let token = session_client
176-
.get_workspace_token(WorkspaceRequest {
177-
id: Identifier::from(workspace),
178-
})
174+
.get_workspace_token(Identifier::from(workspace))
179175
.await?
180176
.into_inner();
181177

@@ -200,7 +196,7 @@ impl Client {
200196
tokio::time::sleep(std::time::Duration::from_secs(240)).await;
201197
if weak.upgrade().is_none() { break };
202198
let new_credentials = session_client.get_workspace_token(
203-
tonic::Request::new(WorkspaceRequest { id: Identifier::from(workspace) })
199+
tonic::Request::new(Identifier::from(workspace))
204200
)
205201
.await?
206202
.into_inner();

src/tests/fixtures.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,47 +77,47 @@ impl ScopedFixture<crate::Client> for ClientFixture {
7777
pub struct WorkspaceFixture {
7878
user: String,
7979
invitee: Option<String>,
80-
workspace: String,
80+
workspace: uuid::Uuid,
8181
}
8282

8383
impl WorkspaceFixture {
84-
pub fn of(user: &str, invitee: &str, workspace: &str) -> Self {
84+
pub fn of(user: &str, invitee: &str, workspace: uuid::Uuid) -> Self {
8585
Self {
8686
user: user.to_string(),
8787
invitee: Some(invitee.to_string()),
88-
workspace: workspace.to_string(),
88+
workspace,
8989
}
9090
}
9191

92-
pub fn one(user: &str, ws: &str) -> Self {
92+
pub fn one(user: &str) -> Self {
9393
Self {
9494
user: user.to_string(),
9595
invitee: None,
96-
workspace: format!("{ws}-{}", uuid::Uuid::new_v4()),
96+
workspace: uuid::Uuid::new_v4(),
9797
}
9898
}
9999

100-
pub fn two(user: &str, invite: &str, ws: &str) -> Self {
100+
pub fn two(user: &str, invite: &str) -> Self {
101101
Self {
102102
user: user.to_string(),
103103
invitee: Some(invite.to_string()),
104-
workspace: format!("{ws}-{}", uuid::Uuid::new_v4()),
104+
workspace: uuid::Uuid::new_v4(),
105105
}
106106
}
107107
}
108108

109109
impl ScopedFixture<(crate::Client, crate::Workspace)> for WorkspaceFixture {
110110
async fn setup(&mut self) -> Result<(crate::Client, crate::Workspace), Box<dyn Error>> {
111111
let client = ClientFixture::of(&self.user).setup().await?;
112-
let ws_info = client.create_workspace(&self.workspace).await?;
112+
let ws_info = client.create_workspace(self.workspace.to_string()).await?;
113113
let workspace = client.attach_workspace(ws_info.id).await?;
114114
Ok((client, workspace))
115115
}
116116

117117
async fn cleanup(&mut self, resource: Option<(crate::Client, crate::Workspace)>) {
118118
if let Some((client, workspace)) = resource {
119119
client.leave_workspace(workspace.id());
120-
if let Err(e) = client.delete_workspace(&self.workspace).await {
120+
if let Err(e) = client.delete_workspace(self.workspace).await {
121121
eprintln!("could not delete workspace: {e}");
122122
}
123123
}
@@ -152,9 +152,9 @@ impl
152152
)
153153
.setup()
154154
.await?;
155-
let ws_info = client.create_workspace(&self.workspace).await?;
155+
let ws_info = client.create_workspace(self.workspace.to_string()).await?;
156156
client
157-
.invite_to_workspace(&self.workspace, invitee_client.current_user().name.clone())
157+
.invite_to_workspace(self.workspace, invitee_client.current_user().name.clone())
158158
.await?;
159159
let workspace = client.attach_workspace(ws_info.id).await?;
160160
let invitee_workspace = invitee_client.attach_workspace(ws_info.id).await?;
@@ -172,7 +172,7 @@ impl
172172
) {
173173
if let Some((client, ws, _, _)) = resource {
174174
client.leave_workspace(ws.id());
175-
if let Err(e) = client.delete_workspace(&self.workspace).await {
175+
if let Err(e) = client.delete_workspace(self.workspace).await {
176176
eprintln!("could not delete workspace: {e}");
177177
}
178178
}
@@ -182,35 +182,35 @@ impl
182182
pub struct BufferFixture {
183183
user: String,
184184
invitee: Option<String>,
185-
workspace: String,
186-
buffer: String,
185+
workspace: uuid::Uuid,
186+
buffer: uuid::Uuid,
187187
}
188188

189189
impl BufferFixture {
190-
pub fn of(user: &str, invitee: &str, workspace: &str, buffer: &str) -> Self {
190+
pub fn of(user: &str, invitee: &str, workspace: uuid::Uuid, buffer: uuid::Uuid) -> Self {
191191
Self {
192192
user: user.to_string(),
193193
invitee: Some(invitee.to_string()),
194-
workspace: workspace.to_string(),
195-
buffer: buffer.to_string(),
194+
workspace,
195+
buffer,
196196
}
197197
}
198198

199-
pub fn one(user: &str, ws: &str, buf: &str) -> Self {
199+
pub fn one(user: &str, buf: uuid::Uuid) -> Self {
200200
Self {
201201
user: user.to_string(),
202202
invitee: None,
203-
workspace: format!("{ws}-{}", uuid::Uuid::new_v4()),
204-
buffer: buf.to_string(),
203+
workspace: uuid::Uuid::new_v4(),
204+
buffer: buf,
205205
}
206206
}
207207

208-
pub fn two(user: &str, invite: &str, ws: &str, buf: &str) -> Self {
208+
pub fn two(user: &str, invite: &str, buf: uuid::Uuid) -> Self {
209209
Self {
210210
user: user.to_string(),
211211
invitee: Some(invite.to_string()),
212-
workspace: format!("{ws}-{}", uuid::Uuid::new_v4()),
213-
buffer: buf.to_string(),
212+
workspace: uuid::Uuid::new_v4(),
213+
buffer: buf,
214214
}
215215
}
216216
}
@@ -247,17 +247,17 @@ impl
247247
)
248248
.setup()
249249
.await?;
250-
let ws_info = client.create_workspace(&self.workspace).await?;
250+
let ws_info = client.create_workspace(self.workspace.to_string()).await?;
251251
client
252-
.invite_to_workspace(&self.workspace, invitee_client.current_user().name.clone())
252+
.invite_to_workspace(self.workspace, invitee_client.current_user().name.clone())
253253
.await?;
254254

255255
let workspace = client.attach_workspace(ws_info.id).await?;
256-
workspace.create_buffer(&self.buffer, false).await?;
257-
let buffer = workspace.attach_buffer(self.buffer.clone()).await?;
256+
workspace.create_buffer(&self.buffer.to_string(), false).await?;
257+
let buffer = workspace.attach_buffer(self.buffer, &self.buffer.to_string()).await?;
258258

259259
let invitee_workspace = invitee_client.attach_workspace(ws_info.id).await?;
260-
let invitee_buffer = invitee_workspace.attach_buffer(self.buffer.clone()).await?;
260+
let invitee_buffer = invitee_workspace.attach_buffer(self.buffer, &self.buffer.to_string()).await?;
261261

262262
Ok((
263263
client,
@@ -283,7 +283,7 @@ impl
283283
if let Some((client, ws, _, _, _, _)) = resource {
284284
// buffer deletion is implied in workspace deletion
285285
client.leave_workspace(ws.id());
286-
if let Err(e) = client.delete_workspace(&self.workspace).await {
286+
if let Err(e) = client.delete_workspace(self.workspace).await {
287287
eprintln!("could not delete workspace: {e}");
288288
}
289289
}

src/workspace.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ use crate::{
1515
};
1616

1717
use codemp_proto::{
18-
common::Empty,
18+
common::{Empty, Identifier},
1919
files::{BufferNode, BufferRequest},
2020
workspace::{
21-
workspace_event::{
21+
WorkspaceEvent, workspace_event::{
2222
Event as WorkspaceEventInner, FileCreate, FileDelete, FileRename, UserJoinBuffer, UserJoinWorkspace, UserLeaveBuffer, UserLeaveWorkspace
23-
}, WorkspaceEvent
23+
}
2424
},
2525
};
2626

@@ -48,7 +48,7 @@ use napi_derive::napi;
4848
pub struct Workspace(pub(crate) Arc<WorkspaceInner>);
4949

5050
#[derive(Debug)]
51-
struct WorkspaceInner {
51+
pub(crate) struct WorkspaceInner {
5252
id: Uuid,
5353
current_user: Arc<User>,
5454
cursor: cursor::Controller,
@@ -140,7 +140,7 @@ impl Workspace {
140140
});
141141

142142
ws.fetch_users().await?;
143-
ws.fetch_buffers("".to_string()).await?;
143+
ws.fetch_buffers().await?;
144144

145145
for buffer_ref in ws.0.buffers.iter() {
146146
ws.fetch_buffer_users(buffer_ref.key().clone()).await?;
@@ -182,35 +182,30 @@ impl Workspace {
182182

183183
/// Attach to a buffer and return a handle to it.
184184
#[tracing::instrument(skip(self))]
185-
pub async fn attach_buffer(&self, path: String) -> ConnectionResult<buffer::Controller> {
185+
pub async fn attach_buffer(&self, id: uuid::Uuid, path: &str) -> ConnectionResult<buffer::Controller> {
186186
let mut workspace_client = self.0.services.ws();
187187
let mut buffer_client = self.0.services.buf();
188-
let request = tonic::Request::new(BufferRequest {
189-
path: path.clone(),
190-
});
191-
let credentials = workspace_client.get_buffer_token(request).await?.into_inner();
188+
let credentials = workspace_client.get_buffer_token(Identifier::from(id)).await?.into_inner();
192189

193190
let (tx, rx) = mpsc::channel(256);
194191
let mut req = tonic::Request::new(tokio_stream::wrappers::ReceiverStream::new(rx));
195192
req.metadata_mut().insert("buffer", crate::ext::token_to_metadata(credentials)?);
196193
let stream = buffer_client.attach(req).await?.into_inner();
197194

198195
let controller =
199-
buffer::Controller::spawn(self.0.current_user.id, &path, tx, stream, self.0.id);
196+
buffer::Controller::spawn(self.0.current_user.id, path, tx, stream, self.0.id);
200197

201-
self.0.buffers.insert(path.clone(), controller.clone());
198+
self.0.buffers.insert(path.to_string(), controller.clone());
202199

200+
let path = path.to_string();
203201
let weak = Arc::downgrade(&controller.0);
204202
tokio::spawn(async move {
205-
let _p = path.clone();
206203
let fut = async move {
207204
loop {
208205
// TODO either configurable token refresh time or calculate depending on token lifetime
209206
tokio::time::sleep(std::time::Duration::from_secs(20)).await;
210207
if weak.upgrade().is_none() { break };
211-
let new_credentials = workspace_client.get_buffer_token(
212-
tonic::Request::new(BufferRequest { path: path.clone() })
213-
)
208+
let new_credentials = workspace_client.get_buffer_token(Identifier::from(id))
214209
.await?
215210
.into_inner();
216211
let mut request = tonic::Request::new(Empty {});
@@ -221,7 +216,7 @@ impl Workspace {
221216
};
222217

223218
if let Err(e) = fut.await {
224-
tracing::error!("error in keepalive task for buffer {_p}: {e}");
219+
tracing::error!("error in keepalive task for buffer {path}: {e}");
225220
}
226221
});
227222

@@ -248,10 +243,10 @@ impl Workspace {
248243
}
249244

250245
/// Re-fetch the list of available buffers in the workspace.
251-
pub async fn fetch_buffers(&self, path: String) -> RemoteResult<()> {
246+
pub async fn fetch_buffers(&self) -> RemoteResult<()> {
252247
let mut workspace_client = self.0.services.ws();
253248
let resp = workspace_client
254-
.fetch_buffers(tonic::Request::new(BufferRequest { path }))
249+
.fetch_buffers(Empty {})
255250
.await?
256251
.into_inner();
257252

@@ -303,14 +298,12 @@ impl Workspace {
303298
}
304299

305300
/// Delete a buffer.
306-
pub async fn delete_buffer(&self, path: &str) -> RemoteResult<()> {
301+
pub async fn delete_buffer(&self, id: uuid::Uuid, path: &str) -> RemoteResult<()> {
307302
self.detach_buffer(path); // just in case
308303

309304
let mut workspace_client = self.0.services.ws();
310305
workspace_client
311-
.delete_buffer(tonic::Request::new(BufferRequest {
312-
path: path.to_string(),
313-
}))
306+
.delete_buffer(Identifier::from(id))
314307
.await?;
315308

316309
self.0.filetree.remove(path);
@@ -369,7 +362,7 @@ impl Workspace {
369362
}
370363

371364
/// Get the filetree as it is currently cached.
372-
/// A filter may be applied, and it may be strict (equality check) or not (starts_with check).
365+
/// A filter may be applied, and it works as a "starts_with" check.
373366
// #[cfg_attr(feature = "js", napi)] // https://github.com/napi-rs/napi-rs/issues/1120
374367
pub fn search_buffers(&self, filter: Option<&str>) -> Vec<String> {
375368
let mut tree = self

0 commit comments

Comments
 (0)