2022-04-25 00:02:14 +00:00
|
|
|
use crate::{
|
2022-04-25 23:51:13 +00:00
|
|
|
auth,
|
2022-09-16 22:37:19 +00:00
|
|
|
db::{Invite, NewUserParams, ProjectId, Signup, User, UserId, WaitlistSummary},
|
2022-05-25 23:42:25 +00:00
|
|
|
rpc::{self, ResultExt},
|
2022-04-25 23:51:13 +00:00
|
|
|
AppState, Error, Result,
|
2022-04-25 00:02:14 +00:00
|
|
|
};
|
|
|
|
use anyhow::anyhow;
|
2022-04-24 17:08:25 +00:00
|
|
|
use axum::{
|
|
|
|
body::Body,
|
2022-04-25 23:51:13 +00:00
|
|
|
extract::{Path, Query},
|
2022-04-26 02:05:09 +00:00
|
|
|
http::{self, Request, StatusCode},
|
|
|
|
middleware::{self, Next},
|
|
|
|
response::IntoResponse,
|
|
|
|
routing::{get, post, put},
|
|
|
|
Extension, Json, Router,
|
2022-04-24 17:08:25 +00:00
|
|
|
};
|
2022-05-26 07:34:39 +00:00
|
|
|
use axum_extra::response::ErasedJson;
|
2022-04-25 23:51:13 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-06-21 16:06:31 +00:00
|
|
|
use serde_json::json;
|
2022-07-15 21:58:08 +00:00
|
|
|
use std::{sync::Arc, time::Duration};
|
2022-06-21 02:39:48 +00:00
|
|
|
use time::OffsetDateTime;
|
2022-04-26 02:05:09 +00:00
|
|
|
use tower::ServiceBuilder;
|
2022-05-02 13:17:43 +00:00
|
|
|
use tracing::instrument;
|
2022-04-24 16:45:20 +00:00
|
|
|
|
2022-05-25 23:42:25 +00:00
|
|
|
pub fn routes(rpc_server: &Arc<rpc::Server>, state: Arc<AppState>) -> Router<Body> {
|
2022-04-26 02:05:09 +00:00
|
|
|
Router::new()
|
|
|
|
.route("/users", get(get_users).post(create_user))
|
2022-09-20 22:40:56 +00:00
|
|
|
.route("/users/:id", put(update_user).delete(destroy_user))
|
2022-04-26 17:15:41 +00:00
|
|
|
.route("/users/:id/access_tokens", post(create_access_token))
|
2022-06-24 15:21:58 +00:00
|
|
|
.route("/users_with_no_invites", get(get_users_with_no_invites))
|
2022-05-19 18:35:07 +00:00
|
|
|
.route("/invite_codes/:code", get(get_user_for_invite_code))
|
2022-05-02 15:36:56 +00:00
|
|
|
.route("/panic", post(trace_panic))
|
2022-05-25 23:42:25 +00:00
|
|
|
.route("/rpc_server_snapshot", get(get_rpc_server_snapshot))
|
2022-06-21 02:39:48 +00:00
|
|
|
.route(
|
2022-06-28 22:37:56 +00:00
|
|
|
"/user_activity/summary",
|
|
|
|
get(get_top_users_activity_summary),
|
2022-06-21 02:39:48 +00:00
|
|
|
)
|
2022-06-28 22:40:48 +00:00
|
|
|
.route(
|
|
|
|
"/user_activity/timeline/:user_id",
|
|
|
|
get(get_user_activity_timeline),
|
|
|
|
)
|
2022-07-15 21:58:08 +00:00
|
|
|
.route("/user_activity/counts", get(get_active_user_counts))
|
2022-06-21 16:06:31 +00:00
|
|
|
.route("/project_metadata", get(get_project_metadata))
|
2022-09-14 01:24:40 +00:00
|
|
|
.route("/signups", post(create_signup))
|
2022-09-16 22:37:19 +00:00
|
|
|
.route("/signups_summary", get(get_waitlist_summary))
|
2022-09-16 19:25:42 +00:00
|
|
|
.route("/user_invites", post(create_invite_from_code))
|
|
|
|
.route("/unsent_invites", get(get_unsent_invites))
|
|
|
|
.route("/sent_invites", post(record_sent_invites))
|
2022-04-26 02:05:09 +00:00
|
|
|
.layer(
|
|
|
|
ServiceBuilder::new()
|
|
|
|
.layer(Extension(state))
|
2022-05-20 10:02:31 +00:00
|
|
|
.layer(Extension(rpc_server.clone()))
|
2022-04-26 02:05:09 +00:00
|
|
|
.layer(middleware::from_fn(validate_api_token)),
|
|
|
|
)
|
2022-04-24 16:45:20 +00:00
|
|
|
}
|
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
pub async fn validate_api_token<B>(req: Request<B>, next: Next<B>) -> impl IntoResponse {
|
2022-04-26 02:10:14 +00:00
|
|
|
let token = req
|
2022-04-26 02:05:09 +00:00
|
|
|
.headers()
|
|
|
|
.get(http::header::AUTHORIZATION)
|
|
|
|
.and_then(|header| header.to_str().ok())
|
|
|
|
.ok_or_else(|| {
|
|
|
|
Error::Http(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"missing authorization header".to_string(),
|
|
|
|
)
|
2022-04-26 02:10:14 +00:00
|
|
|
})?
|
|
|
|
.strip_prefix("token ")
|
|
|
|
.ok_or_else(|| {
|
|
|
|
Error::Http(
|
|
|
|
StatusCode::BAD_REQUEST,
|
|
|
|
"invalid authorization header".to_string(),
|
|
|
|
)
|
2022-04-26 02:05:09 +00:00
|
|
|
})?;
|
|
|
|
|
2022-04-26 02:10:14 +00:00
|
|
|
let state = req.extensions().get::<Arc<AppState>>().unwrap();
|
|
|
|
|
|
|
|
if token != state.api_token {
|
|
|
|
Err(Error::Http(
|
|
|
|
StatusCode::UNAUTHORIZED,
|
|
|
|
"invalid authorization token".to_string(),
|
|
|
|
))?
|
|
|
|
}
|
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
Ok::<_, Error>(next.run(req).await)
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:30:01 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
|
|
|
struct GetUsersQueryParams {
|
2022-09-20 22:40:56 +00:00
|
|
|
github_user_id: Option<i32>,
|
|
|
|
github_login: Option<String>,
|
2022-06-13 15:30:01 +00:00
|
|
|
query: Option<String>,
|
|
|
|
page: Option<u32>,
|
|
|
|
limit: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_users(
|
|
|
|
Query(params): Query<GetUsersQueryParams>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<Json<Vec<User>>> {
|
2022-09-20 22:40:56 +00:00
|
|
|
if let Some(github_login) = ¶ms.github_login {
|
|
|
|
let user = app
|
|
|
|
.db
|
|
|
|
.get_user_by_github_account(github_login, params.github_user_id)
|
|
|
|
.await?;
|
|
|
|
return Ok(Json(Vec::from_iter(user)));
|
|
|
|
}
|
|
|
|
|
2022-06-13 15:30:01 +00:00
|
|
|
let limit = params.limit.unwrap_or(100);
|
|
|
|
let users = if let Some(query) = params.query {
|
|
|
|
app.db.fuzzy_search_users(&query, limit).await?
|
|
|
|
} else {
|
|
|
|
app.db
|
|
|
|
.get_all_users(params.page.unwrap_or(0), limit)
|
|
|
|
.await?
|
|
|
|
};
|
2022-04-25 00:02:14 +00:00
|
|
|
Ok(Json(users))
|
2022-04-24 17:08:25 +00:00
|
|
|
}
|
|
|
|
|
2022-05-21 02:24:02 +00:00
|
|
|
#[derive(Deserialize, Debug)]
|
2022-04-25 23:51:13 +00:00
|
|
|
struct CreateUserParams {
|
2022-09-20 16:44:56 +00:00
|
|
|
github_user_id: i32,
|
2022-04-25 00:02:14 +00:00
|
|
|
github_login: String,
|
2022-09-16 19:25:42 +00:00
|
|
|
email_address: String,
|
|
|
|
email_confirmation_code: Option<String>,
|
2022-09-20 16:44:56 +00:00
|
|
|
#[serde(default)]
|
2022-09-16 19:25:42 +00:00
|
|
|
invite_count: i32,
|
2022-04-25 00:02:14 +00:00
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
async fn create_user(
|
|
|
|
Json(params): Json<CreateUserParams>,
|
2022-04-26 02:05:09 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
2022-05-25 23:42:25 +00:00
|
|
|
Extension(rpc_server): Extension<Arc<rpc::Server>>,
|
2022-04-25 23:51:13 +00:00
|
|
|
) -> Result<Json<User>> {
|
2022-09-20 16:44:56 +00:00
|
|
|
let user = NewUserParams {
|
|
|
|
github_login: params.github_login,
|
|
|
|
github_user_id: params.github_user_id,
|
|
|
|
invite_count: params.invite_count,
|
|
|
|
};
|
2022-09-16 19:25:42 +00:00
|
|
|
let (user_id, inviter_id) =
|
|
|
|
// Creating a user via the normal signup process
|
|
|
|
if let Some(email_confirmation_code) = params.email_confirmation_code {
|
|
|
|
app.db
|
|
|
|
.create_user_from_invite(
|
|
|
|
&Invite {
|
|
|
|
email_address: params.email_address,
|
|
|
|
email_confirmation_code,
|
|
|
|
},
|
2022-09-20 16:44:56 +00:00
|
|
|
user,
|
2022-09-16 19:25:42 +00:00
|
|
|
)
|
|
|
|
.await?
|
|
|
|
}
|
|
|
|
// Creating a user as an admin
|
|
|
|
else {
|
|
|
|
(
|
|
|
|
app.db
|
2022-09-20 16:44:56 +00:00
|
|
|
.create_user(¶ms.email_address, false, user)
|
2022-09-16 19:25:42 +00:00
|
|
|
.await?,
|
|
|
|
None,
|
2022-05-21 02:24:02 +00:00
|
|
|
)
|
2022-09-16 19:25:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(inviter_id) = inviter_id {
|
2022-05-20 10:02:31 +00:00
|
|
|
rpc_server
|
2022-09-16 19:25:42 +00:00
|
|
|
.invite_code_redeemed(inviter_id, user_id)
|
2022-05-20 10:02:31 +00:00
|
|
|
.await
|
|
|
|
.trace_err();
|
2022-09-16 19:25:42 +00:00
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-05-18 22:14:58 +00:00
|
|
|
let user = app
|
|
|
|
.db
|
|
|
|
.get_user_by_id(user_id)
|
|
|
|
.await?
|
|
|
|
.ok_or_else(|| anyhow!("couldn't find the user we just created"))?;
|
|
|
|
|
2022-04-25 00:02:14 +00:00
|
|
|
Ok(Json(user))
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 00:02:14 +00:00
|
|
|
#[derive(Deserialize)]
|
2022-04-25 23:51:13 +00:00
|
|
|
struct UpdateUserParams {
|
2022-05-18 16:23:08 +00:00
|
|
|
admin: Option<bool>,
|
|
|
|
invite_count: Option<u32>,
|
2022-04-25 00:02:14 +00:00
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 00:02:14 +00:00
|
|
|
async fn update_user(
|
|
|
|
Path(user_id): Path<i32>,
|
2022-04-25 23:51:13 +00:00
|
|
|
Json(params): Json<UpdateUserParams>,
|
2022-04-26 02:05:09 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
2022-06-06 18:14:01 +00:00
|
|
|
Extension(rpc_server): Extension<Arc<rpc::Server>>,
|
2022-04-25 23:51:13 +00:00
|
|
|
) -> Result<()> {
|
2022-06-06 18:14:01 +00:00
|
|
|
let user_id = UserId(user_id);
|
|
|
|
|
2022-05-18 16:23:08 +00:00
|
|
|
if let Some(admin) = params.admin {
|
2022-06-06 18:14:01 +00:00
|
|
|
app.db.set_user_is_admin(user_id, admin).await?;
|
2022-05-18 16:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(invite_count) = params.invite_count {
|
2022-09-16 19:25:42 +00:00
|
|
|
app.db
|
|
|
|
.set_invite_count_for_user(user_id, invite_count)
|
|
|
|
.await?;
|
2022-06-06 18:14:01 +00:00
|
|
|
rpc_server.invite_count_updated(user_id).await.trace_err();
|
2022-05-18 16:23:08 +00:00
|
|
|
}
|
|
|
|
|
2022-04-25 00:02:14 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-26 02:05:09 +00:00
|
|
|
async fn destroy_user(
|
|
|
|
Path(user_id): Path<i32>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<()> {
|
2022-04-25 23:51:13 +00:00
|
|
|
app.db.destroy_user(UserId(user_id)).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-06-24 15:57:52 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
2022-06-24 15:21:58 +00:00
|
|
|
struct GetUsersWithNoInvites {
|
|
|
|
invited_by_another_user: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_users_with_no_invites(
|
|
|
|
Query(params): Query<GetUsersWithNoInvites>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<Json<Vec<User>>> {
|
|
|
|
Ok(Json(
|
|
|
|
app.db
|
|
|
|
.get_users_with_no_invites(params.invited_by_another_user)
|
|
|
|
.await?,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
|
2022-05-02 13:17:43 +00:00
|
|
|
#[derive(Debug, Deserialize)]
|
2022-05-02 15:36:56 +00:00
|
|
|
struct Panic {
|
2022-05-02 13:17:43 +00:00
|
|
|
version: String,
|
|
|
|
text: String,
|
|
|
|
}
|
|
|
|
|
2022-05-02 15:36:56 +00:00
|
|
|
#[instrument(skip(panic))]
|
|
|
|
async fn trace_panic(panic: Json<Panic>) -> Result<()> {
|
|
|
|
tracing::error!(version = %panic.version, text = %panic.text, "panic report");
|
2022-05-02 13:17:43 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-05-26 07:34:39 +00:00
|
|
|
async fn get_rpc_server_snapshot(
|
2022-05-25 23:42:25 +00:00
|
|
|
Extension(rpc_server): Extension<Arc<rpc::Server>>,
|
2022-05-26 07:34:39 +00:00
|
|
|
) -> Result<ErasedJson> {
|
|
|
|
Ok(ErasedJson::pretty(rpc_server.snapshot().await))
|
2022-05-25 23:42:25 +00:00
|
|
|
}
|
|
|
|
|
2022-06-21 02:39:48 +00:00
|
|
|
#[derive(Deserialize)]
|
2022-06-28 22:37:56 +00:00
|
|
|
struct TimePeriodParams {
|
2022-06-21 12:03:10 +00:00
|
|
|
#[serde(with = "time::serde::iso8601")]
|
|
|
|
start: OffsetDateTime,
|
|
|
|
#[serde(with = "time::serde::iso8601")]
|
|
|
|
end: OffsetDateTime,
|
2022-06-21 02:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-06-28 22:37:56 +00:00
|
|
|
async fn get_top_users_activity_summary(
|
|
|
|
Query(params): Query<TimePeriodParams>,
|
2022-06-21 02:39:48 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<ErasedJson> {
|
2022-06-21 12:03:10 +00:00
|
|
|
let summary = app
|
|
|
|
.db
|
2022-06-28 22:37:56 +00:00
|
|
|
.get_top_users_activity_summary(params.start..params.end, 100)
|
2022-06-21 12:03:10 +00:00
|
|
|
.await?;
|
2022-06-21 02:39:48 +00:00
|
|
|
Ok(ErasedJson::pretty(summary))
|
|
|
|
}
|
|
|
|
|
2022-06-28 22:40:48 +00:00
|
|
|
async fn get_user_activity_timeline(
|
|
|
|
Path(user_id): Path<i32>,
|
|
|
|
Query(params): Query<TimePeriodParams>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<ErasedJson> {
|
|
|
|
let summary = app
|
|
|
|
.db
|
|
|
|
.get_user_activity_timeline(params.start..params.end, UserId(user_id))
|
|
|
|
.await?;
|
|
|
|
Ok(ErasedJson::pretty(summary))
|
|
|
|
}
|
|
|
|
|
2022-07-15 23:30:13 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct ActiveUserCountParams {
|
|
|
|
#[serde(flatten)]
|
|
|
|
period: TimePeriodParams,
|
|
|
|
durations_in_minutes: String,
|
2022-07-18 08:16:24 +00:00
|
|
|
#[serde(default)]
|
|
|
|
only_collaborative: bool,
|
2022-07-15 23:30:13 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 21:58:08 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct ActiveUserSet {
|
|
|
|
active_time_in_minutes: u64,
|
|
|
|
user_count: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_active_user_counts(
|
2022-07-15 23:30:13 +00:00
|
|
|
Query(params): Query<ActiveUserCountParams>,
|
2022-07-15 21:58:08 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<ErasedJson> {
|
2022-07-15 23:30:13 +00:00
|
|
|
let durations_in_minutes = params.durations_in_minutes.split(',');
|
|
|
|
let mut user_sets = Vec::new();
|
2022-07-15 21:58:08 +00:00
|
|
|
for duration in durations_in_minutes {
|
2022-07-15 23:30:13 +00:00
|
|
|
let duration = duration
|
|
|
|
.parse()
|
|
|
|
.map_err(|_| anyhow!("invalid duration: {duration}"))?;
|
2022-07-15 21:58:08 +00:00
|
|
|
user_sets.push(ActiveUserSet {
|
|
|
|
active_time_in_minutes: duration,
|
|
|
|
user_count: app
|
|
|
|
.db
|
2022-07-15 23:30:13 +00:00
|
|
|
.get_active_user_count(
|
|
|
|
params.period.start..params.period.end,
|
|
|
|
Duration::from_secs(duration * 60),
|
2022-07-18 08:16:24 +00:00
|
|
|
params.only_collaborative,
|
2022-07-15 23:30:13 +00:00
|
|
|
)
|
2022-07-15 21:58:08 +00:00
|
|
|
.await?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Ok(ErasedJson::pretty(user_sets))
|
|
|
|
}
|
|
|
|
|
2022-06-21 16:06:31 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct GetProjectMetadataParams {
|
|
|
|
project_id: u64,
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn get_project_metadata(
|
|
|
|
Query(params): Query<GetProjectMetadataParams>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<ErasedJson> {
|
|
|
|
let extensions = app
|
|
|
|
.db
|
|
|
|
.get_project_extensions(ProjectId::from_proto(params.project_id))
|
|
|
|
.await?;
|
|
|
|
Ok(ErasedJson::pretty(json!({ "extensions": extensions })))
|
|
|
|
}
|
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct CreateAccessTokenQueryParams {
|
|
|
|
public_key: String,
|
|
|
|
impersonate: Option<String>,
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
#[derive(Serialize)]
|
|
|
|
struct CreateAccessTokenResponse {
|
|
|
|
user_id: UserId,
|
|
|
|
encrypted_access_token: String,
|
|
|
|
}
|
2022-04-22 19:33:19 +00:00
|
|
|
|
2022-04-25 23:51:13 +00:00
|
|
|
async fn create_access_token(
|
2022-09-20 22:40:56 +00:00
|
|
|
Path(user_id): Path<UserId>,
|
2022-04-25 23:51:13 +00:00
|
|
|
Query(params): Query<CreateAccessTokenQueryParams>,
|
2022-04-26 02:05:09 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
2022-04-25 23:51:13 +00:00
|
|
|
) -> Result<Json<CreateAccessTokenResponse>> {
|
|
|
|
let user = app
|
|
|
|
.db
|
2022-09-20 22:40:56 +00:00
|
|
|
.get_user_by_id(user_id)
|
2022-04-25 23:51:13 +00:00
|
|
|
.await?
|
|
|
|
.ok_or_else(|| anyhow!("user not found"))?;
|
|
|
|
|
|
|
|
let mut user_id = user.id;
|
|
|
|
if let Some(impersonate) = params.impersonate {
|
|
|
|
if user.admin {
|
2022-09-20 22:40:56 +00:00
|
|
|
if let Some(impersonated_user) = app
|
|
|
|
.db
|
|
|
|
.get_user_by_github_account(&impersonate, None)
|
|
|
|
.await?
|
|
|
|
{
|
2022-04-25 23:51:13 +00:00
|
|
|
user_id = impersonated_user.id;
|
|
|
|
} else {
|
|
|
|
return Err(Error::Http(
|
|
|
|
StatusCode::UNPROCESSABLE_ENTITY,
|
|
|
|
format!("user {impersonate} does not exist"),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Err(Error::Http(
|
|
|
|
StatusCode::UNAUTHORIZED,
|
2022-08-10 21:39:24 +00:00
|
|
|
"you do not have permission to impersonate other users".to_string(),
|
2022-04-25 23:51:13 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let access_token = auth::create_access_token(app.db.as_ref(), user_id).await?;
|
|
|
|
let encrypted_access_token =
|
|
|
|
auth::encrypt_access_token(&access_token, params.public_key.clone())?;
|
|
|
|
|
|
|
|
Ok(Json(CreateAccessTokenResponse {
|
|
|
|
user_id,
|
|
|
|
encrypted_access_token,
|
|
|
|
}))
|
|
|
|
}
|
2022-05-19 18:35:07 +00:00
|
|
|
|
|
|
|
async fn get_user_for_invite_code(
|
|
|
|
Path(code): Path<String>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<Json<User>> {
|
|
|
|
Ok(Json(app.db.get_user_for_invite_code(&code).await?))
|
|
|
|
}
|
2022-09-14 01:24:40 +00:00
|
|
|
|
|
|
|
async fn create_signup(
|
|
|
|
Json(params): Json<Signup>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<()> {
|
|
|
|
app.db.create_signup(params).await?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2022-09-16 22:37:19 +00:00
|
|
|
async fn get_waitlist_summary(
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<Json<WaitlistSummary>> {
|
|
|
|
Ok(Json(app.db.get_waitlist_summary().await?))
|
|
|
|
}
|
|
|
|
|
2022-09-16 19:25:42 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct CreateInviteFromCodeParams {
|
|
|
|
invite_code: String,
|
|
|
|
email_address: String,
|
2022-09-14 01:24:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-16 19:25:42 +00:00
|
|
|
async fn create_invite_from_code(
|
|
|
|
Json(params): Json<CreateInviteFromCodeParams>,
|
2022-09-14 01:24:40 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
2022-09-16 19:25:42 +00:00
|
|
|
) -> Result<Json<Invite>> {
|
|
|
|
Ok(Json(
|
|
|
|
app.db
|
|
|
|
.create_invite_from_code(¶ms.invite_code, ¶ms.email_address)
|
|
|
|
.await?,
|
|
|
|
))
|
2022-09-14 01:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
2022-09-16 19:25:42 +00:00
|
|
|
pub struct GetUnsentInvitesParams {
|
2022-09-14 01:24:40 +00:00
|
|
|
pub count: usize,
|
|
|
|
}
|
|
|
|
|
2022-09-16 19:25:42 +00:00
|
|
|
async fn get_unsent_invites(
|
|
|
|
Query(params): Query<GetUnsentInvitesParams>,
|
2022-09-14 01:24:40 +00:00
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
2022-09-16 19:25:42 +00:00
|
|
|
) -> Result<Json<Vec<Invite>>> {
|
|
|
|
Ok(Json(app.db.get_unsent_invites(params.count).await?))
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn record_sent_invites(
|
|
|
|
Json(params): Json<Vec<Invite>>,
|
|
|
|
Extension(app): Extension<Arc<AppState>>,
|
|
|
|
) -> Result<()> {
|
|
|
|
app.db.record_sent_invites(¶ms).await?;
|
|
|
|
Ok(())
|
2022-09-14 01:24:40 +00:00
|
|
|
}
|