Fix remaining compilation errors

This commit is contained in:
Nathan Sobo 2021-09-20 19:44:30 -06:00
parent 5dfd4be174
commit aa671f1041
2 changed files with 33 additions and 23 deletions

View file

@ -5,12 +5,11 @@ use super::{
db::{ChannelId, MessageId, UserId}, db::{ChannelId, MessageId, UserId},
AppState, AppState,
}; };
use crate::errors::TideResultExt;
use anyhow::anyhow; use anyhow::anyhow;
use async_std::{sync::RwLock, task}; use async_std::{sync::RwLock, task};
use async_tungstenite::{tungstenite::protocol::Role, WebSocketStream}; use async_tungstenite::{tungstenite::protocol::Role, WebSocketStream};
use futures::{future::BoxFuture, FutureExt}; use futures::{future::BoxFuture, FutureExt};
use postage::{broadcast, mpsc, prelude::Sink as _, prelude::Stream as _}; use postage::{mpsc, prelude::Sink as _, prelude::Stream as _};
use sha1::{Digest as _, Sha1}; use sha1::{Digest as _, Sha1};
use std::{ use std::{
any::TypeId, any::TypeId,
@ -20,7 +19,7 @@ use std::{
sync::Arc, sync::Arc,
time::Instant, time::Instant,
}; };
use store::{ReplicaId, Store, Worktree}; use store::{Store, Worktree};
use surf::StatusCode; use surf::StatusCode;
use tide::log; use tide::log;
use tide::{ use tide::{
@ -71,6 +70,7 @@ impl Server {
.add_handler(Server::share_worktree) .add_handler(Server::share_worktree)
.add_handler(Server::unshare_worktree) .add_handler(Server::unshare_worktree)
.add_handler(Server::join_worktree) .add_handler(Server::join_worktree)
.add_handler(Server::leave_worktree)
.add_handler(Server::update_worktree) .add_handler(Server::update_worktree)
.add_handler(Server::open_buffer) .add_handler(Server::open_buffer)
.add_handler(Server::close_buffer) .add_handler(Server::close_buffer)
@ -199,7 +199,7 @@ impl Server {
} }
self.update_collaborators_for_users(removed_connection.collaborator_ids.iter()) self.update_collaborators_for_users(removed_connection.collaborator_ids.iter())
.await; .await?;
Ok(()) Ok(())
} }
@ -420,22 +420,24 @@ impl Server {
} }
async fn leave_worktree( async fn leave_worktree(
self: &Arc<Server>, self: Arc<Server>,
worktree_id: u64, request: TypedEnvelope<proto::LeaveWorktree>,
sender_conn_id: ConnectionId,
) -> tide::Result<()> { ) -> tide::Result<()> {
let sender_id = request.sender_id;
let worktree_id = request.payload.worktree_id;
if let Some((connection_ids, collaborator_ids)) = self if let Some((connection_ids, collaborator_ids)) = self
.store .store
.write() .write()
.await .await
.leave_worktree(sender_conn_id, worktree_id) .leave_worktree(sender_id, worktree_id)
{ {
broadcast(sender_conn_id, connection_ids, |conn_id| { broadcast(sender_id, connection_ids, |conn_id| {
self.peer.send( self.peer.send(
conn_id, conn_id,
proto::RemovePeer { proto::RemovePeer {
worktree_id, worktree_id,
peer_id: sender_conn_id.0, peer_id: sender_id.0,
}, },
) )
}) })
@ -1550,19 +1552,23 @@ mod tests {
.await; .await;
assert_eq!( assert_eq!(
server.state().await.channels[&channel_id] server
.state()
.await
.channel(channel_id)
.unwrap()
.connection_ids .connection_ids
.len(), .len(),
2 2
); );
cx_b.update(|_| drop(channel_b)); cx_b.update(|_| drop(channel_b));
server server
.condition(|state| state.channels[&channel_id].connection_ids.len() == 1) .condition(|state| state.channel(channel_id).unwrap().connection_ids.len() == 1)
.await; .await;
cx_a.update(|_| drop(channel_a)); cx_a.update(|_| drop(channel_a));
server server
.condition(|state| !state.channels.contains_key(&channel_id)) .condition(|state| state.channel(channel_id).is_none())
.await; .await;
} }

View file

@ -1,4 +1,4 @@
use crate::db::{ChannelId, MessageId, UserId}; use crate::db::{ChannelId, UserId};
use crate::errors::TideResultExt; use crate::errors::TideResultExt;
use anyhow::anyhow; use anyhow::anyhow;
use std::collections::{hash_map, HashMap, HashSet}; use std::collections::{hash_map, HashMap, HashSet};
@ -27,15 +27,15 @@ pub struct Worktree {
pub share: Option<WorktreeShare>, pub share: Option<WorktreeShare>,
} }
struct WorktreeShare { pub struct WorktreeShare {
pub guest_connection_ids: HashMap<ConnectionId, ReplicaId>, pub guest_connection_ids: HashMap<ConnectionId, ReplicaId>,
pub active_replica_ids: HashSet<ReplicaId>, pub active_replica_ids: HashSet<ReplicaId>,
pub entries: HashMap<u64, proto::Entry>, pub entries: HashMap<u64, proto::Entry>,
} }
#[derive(Default)] #[derive(Default)]
struct Channel { pub struct Channel {
connection_ids: HashSet<ConnectionId>, pub connection_ids: HashSet<ConnectionId>,
} }
pub type ReplicaId = u16; pub type ReplicaId = u16;
@ -73,7 +73,7 @@ impl Store {
return Err(anyhow!("no such connection"))?; return Err(anyhow!("no such connection"))?;
}; };
for channel_id in connection.channels { for channel_id in &connection.channels {
if let Some(channel) = self.channels.get_mut(&channel_id) { if let Some(channel) = self.channels.get_mut(&channel_id) {
channel.connection_ids.remove(&connection_id); channel.connection_ids.remove(&connection_id);
} }
@ -89,12 +89,12 @@ impl Store {
} }
let mut result = RemovedConnectionState::default(); let mut result = RemovedConnectionState::default();
for worktree_id in connection.worktrees { for worktree_id in connection.worktrees.clone() {
if let Ok(worktree) = self.remove_worktree(worktree_id, connection_id) { if let Ok(worktree) = self.remove_worktree(worktree_id, connection_id) {
result.hosted_worktrees.insert(worktree_id, worktree);
result result
.collaborator_ids .collaborator_ids
.extend(worktree.collaborator_user_ids.iter().copied()); .extend(worktree.collaborator_user_ids.iter().copied());
result.hosted_worktrees.insert(worktree_id, worktree);
} else { } else {
if let Some(worktree) = self.worktrees.get(&worktree_id) { if let Some(worktree) = self.worktrees.get(&worktree_id) {
result result
@ -110,6 +110,10 @@ impl Store {
Ok(result) Ok(result)
} }
pub fn channel(&self, id: ChannelId) -> Option<&Channel> {
self.channels.get(&id)
}
pub fn join_channel(&mut self, connection_id: ConnectionId, channel_id: ChannelId) { pub fn join_channel(&mut self, connection_id: ConnectionId, channel_id: ChannelId) {
if let Some(connection) = self.connections.get_mut(&connection_id) { if let Some(connection) = self.connections.get_mut(&connection_id) {
connection.channels.insert(channel_id); connection.channels.insert(channel_id);
@ -230,7 +234,7 @@ impl Store {
connection.worktrees.remove(&worktree_id); connection.worktrees.remove(&worktree_id);
} }
if let Some(share) = worktree.share { if let Some(share) = &worktree.share {
for connection_id in share.guest_connection_ids.keys() { for connection_id in share.guest_connection_ids.keys() {
if let Some(connection) = self.connections.get_mut(connection_id) { if let Some(connection) = self.connections.get_mut(connection_id) {
connection.worktrees.remove(&worktree_id); connection.worktrees.remove(&worktree_id);
@ -238,7 +242,7 @@ impl Store {
} }
} }
for collaborator_user_id in worktree.collaborator_user_ids { for collaborator_user_id in &worktree.collaborator_user_ids {
if let Some(visible_worktrees) = self if let Some(visible_worktrees) = self
.visible_worktrees_by_user_id .visible_worktrees_by_user_id
.get_mut(&collaborator_user_id) .get_mut(&collaborator_user_id)
@ -289,7 +293,7 @@ impl Store {
let connection_ids = worktree.connection_ids(); let connection_ids = worktree.connection_ids();
if let Some(share) = worktree.share.take() { if let Some(_) = worktree.share.take() {
for connection_id in &connection_ids { for connection_id in &connection_ids {
if let Some(connection) = self.connections.get_mut(connection_id) { if let Some(connection) = self.connections.get_mut(connection_id) {
connection.worktrees.remove(&worktree_id); connection.worktrees.remove(&worktree_id);