Update users and invite count after an invite gets redeemed

This commit is contained in:
Antonio Scandurra 2022-05-20 12:02:31 +02:00
parent eedb8ba59f
commit 6f2c3f1e37
4 changed files with 34 additions and 9 deletions

View file

@ -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<AppState>) -> Router<Body> {
pub fn routes(rpc_server: &Arc<crate::rpc::Server>, state: Arc<AppState>) -> Router<Body> {
Router::new()
.route("/users", get(get_users).post(create_user))
.route(
@ -31,6 +32,7 @@ pub fn routes(state: Arc<AppState>) -> Router<Body> {
.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<CreateUserParams>,
Extension(app): Extension<Arc<AppState>>,
Extension(rpc_server): Extension<Arc<crate::rpc::Server>>,
) -> Result<Json<User>> {
let user_id = if let Some(invite_code) = params.invite_code {
app.db
let invitee_id = app
.db
.redeem_invite_code(&invite_code, &params.github_login)
.await?
.await?;
rpc_server
.invite_code_redeemed(&invite_code, invitee_id)
.await
.trace_err();
invitee_id
} else {
app.db
.create_user(&params.github_login, params.admin)

View file

@ -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::<Body>::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::<SocketAddr>())

View file

@ -412,6 +412,23 @@ impl Server {
Ok(())
}
pub async fn invite_code_redeemed(self: &Arc<Self>, 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<Server>,
_: TypedEnvelope<proto::Ping>,
@ -1530,13 +1547,12 @@ impl Header for ProtocolVersion {
}
}
pub fn routes(app_state: Arc<AppState>) -> Router<Body> {
let server = Server::new(app_state.clone(), None);
pub fn routes(server: Arc<Server>) -> Router<Body> {
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)),
)

View file

@ -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,