From 6f2c3f1e377b795645eafaf7db453e95cf722e7c Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Fri, 20 May 2022 12:02:31 +0200 Subject: [PATCH] Update users and invite count after an invite gets redeemed --- crates/collab/src/api.rs | 15 +++++++++++--- crates/collab/src/main.rs | 5 +++-- crates/collab/src/rpc.rs | 22 ++++++++++++++++++--- crates/contacts_panel/src/contacts_panel.rs | 1 - 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/crates/collab/src/api.rs b/crates/collab/src/api.rs index 7dbd28e5e9..4f6c85f7ff 100644 --- a/crates/collab/src/api.rs +++ b/crates/collab/src/api.rs @@ -1,6 +1,7 @@ use crate::{ auth, db::{User, UserId}, + rpc::ResultExt, AppState, Error, Result, }; use anyhow::anyhow; @@ -18,7 +19,7 @@ use std::sync::Arc; use tower::ServiceBuilder; use tracing::instrument; -pub fn routes(state: Arc) -> Router { +pub fn routes(rpc_server: &Arc, state: Arc) -> Router { Router::new() .route("/users", get(get_users).post(create_user)) .route( @@ -31,6 +32,7 @@ pub fn routes(state: Arc) -> Router { .layer( ServiceBuilder::new() .layer(Extension(state)) + .layer(Extension(rpc_server.clone())) .layer(middleware::from_fn(validate_api_token)), ) // TODO: Compression on API routes? @@ -82,11 +84,18 @@ struct CreateUserParams { async fn create_user( Json(params): Json, Extension(app): Extension>, + Extension(rpc_server): Extension>, ) -> Result> { let user_id = if let Some(invite_code) = params.invite_code { - app.db + let invitee_id = app + .db .redeem_invite_code(&invite_code, ¶ms.github_login) - .await? + .await?; + rpc_server + .invite_code_redeemed(&invite_code, invitee_id) + .await + .trace_err(); + invitee_id } else { app.db .create_user(¶ms.github_login, params.admin) diff --git a/crates/collab/src/main.rs b/crates/collab/src/main.rs index 1713da9092..617f1f273f 100644 --- a/crates/collab/src/main.rs +++ b/crates/collab/src/main.rs @@ -60,10 +60,11 @@ async fn main() -> Result<()> { let listener = TcpListener::bind(&format!("0.0.0.0:{}", config.http_port)) .expect("failed to bind TCP listener"); + let rpc_server = rpc::Server::new(state.clone(), None); let app = Router::::new() - .merge(api::routes(state.clone())) - .merge(rpc::routes(state)); + .merge(api::routes(&rpc_server, state.clone())) + .merge(rpc::routes(rpc_server)); axum::Server::from_tcp(listener)? .serve(app.into_make_service_with_connect_info::()) diff --git a/crates/collab/src/rpc.rs b/crates/collab/src/rpc.rs index f8ed1dc86b..3163325471 100644 --- a/crates/collab/src/rpc.rs +++ b/crates/collab/src/rpc.rs @@ -412,6 +412,23 @@ impl Server { Ok(()) } + pub async fn invite_code_redeemed(self: &Arc, code: &str, invitee_id: UserId) -> Result<()> { + let user = self.app_state.db.get_user_for_invite_code(code).await?; + let store = self.store().await; + let invitee_contact = store.contact_for_user(invitee_id, true); + for connection_id in store.connection_ids_for_user(user.id) { + self.peer.send(connection_id, proto::UpdateContacts { + contacts: vec![invitee_contact.clone()], + ..Default::default() + })?; + self.peer.send(connection_id, proto::UpdateInviteInfo { + url: format!("{}{}", self.app_state.invite_link_prefix, code), + count: user.invite_count as u32, + })?; + } + Ok(()) + } + async fn ping( self: Arc, _: TypedEnvelope, @@ -1530,13 +1547,12 @@ impl Header for ProtocolVersion { } } -pub fn routes(app_state: Arc) -> Router { - let server = Server::new(app_state.clone(), None); +pub fn routes(server: Arc) -> Router { Router::new() .route("/rpc", get(handle_websocket_request)) .layer( ServiceBuilder::new() - .layer(Extension(app_state)) + .layer(Extension(server.app_state.clone())) .layer(middleware::from_fn(auth::validate_header)) .layer(Extension(server)), ) diff --git a/crates/contacts_panel/src/contacts_panel.rs b/crates/contacts_panel/src/contacts_panel.rs index 4121e545d7..95b4cf20e6 100644 --- a/crates/contacts_panel/src/contacts_panel.rs +++ b/crates/contacts_panel/src/contacts_panel.rs @@ -8,7 +8,6 @@ use contact_notification::ContactNotification; use editor::{Cancel, Editor}; use fuzzy::{match_strings, StringMatchCandidate}; use gpui::{ - color::Color, elements::*, geometry::{rect::RectF, vector::vec2f}, impl_actions, impl_internal_actions,