Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(services/user): migrate user-team-list proto op #1650

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
615 changes: 309 additions & 306 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/api/actor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition.workspace = true
[dependencies]
api-helper.workspace = true
chirp-client.workspace = true
chirp-workflow.workspace = true
rivet-operation.workspace = true
chrono = "0.4"
http = "0.2"
Expand Down Expand Up @@ -53,9 +54,8 @@ token-create.workspace = true
token-revoke.workspace = true
upload-complete.workspace = true
upload-get.workspace = true
user-get.workspace = true
user.workspace = true
user-identity-get.workspace = true
user-team-list.workspace = true

[dev-dependencies]
rivet-connection.workspace = true
Expand Down
21 changes: 13 additions & 8 deletions packages/api/actor/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,15 +157,21 @@ impl Auth {
} else if let Ok(user_ent) = claims.as_user() {
// Get the user
let (user_res, game_res, team_list_res) = tokio::try_join!(
op!([ctx] user_get {
user_ids: vec![user_ent.user_id.into()],
}),
chirp_workflow::compat::op(
&ctx,
::user::ops::get::Input {
user_ids: vec![user_ent.user_id],
},
),
op!([ctx] game_get {
game_ids: vec![game_id.into()],
}),
op!([ctx] user_team_list {
user_ids: vec![user_ent.user_id.into()],
}),
chirp_workflow::compat::op(
&ctx,
user::ops::team_list::Input {
user_ids: vec![user_ent.user_id],
},
),
)?;
let Some(user) = user_res.users.first() else {
bail_with!(TOKEN_REVOKED)
Expand All @@ -186,8 +192,7 @@ impl Auth {
let is_part_of_team = user_teams
.teams
.iter()
.filter_map(|x| x.team_id)
.any(|x| x.as_uuid() == dev_team_id);
.any(|x| x.team_id == dev_team_id);
ensure_with!(is_part_of_team, GROUP_NOT_MEMBER);

// Get team
Expand Down
5 changes: 0 additions & 5 deletions packages/api/auth/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ tokio = { version = "1.40" }
tracing = "0.1"
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt", "json", "ansi"] }
url = "2.2.2"
user-get.workspace = true
user-identity-create.workspace = true
user-resolve-email.workspace = true
user-token-create.workspace = true
user.workspace = true
uuid = { version = "1", features = ["v4"] }

Expand All @@ -50,7 +46,6 @@ rivet-auth.workspace = true
rivet-connection.workspace = true

faker-user.workspace = true
user-token-create.workspace = true
debug-email-res.workspace = true
user-identity-get.workspace = true

9 changes: 6 additions & 3 deletions packages/api/auth/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,12 @@ impl Auth {
let claims = self.claims()?;
let user_ent = claims.as_user()?;

let user_res = op!([ctx] user_get {
user_ids: vec![user_ent.user_id.into()],
})
let user_res = chirp_workflow::compat::op(
&ctx,
::user::ops::get::Input {
user_ids: vec![user_ent.user_id],
},
)
.await?;
let Some(user) = user_res.users.first() else {
bail_with!(TOKEN_REVOKED)
Expand Down
18 changes: 9 additions & 9 deletions packages/api/auth/src/route/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub async fn complete(
return Ok(models::AuthIdentityCompleteEmailVerificationResponse { status });
}

let email_res = op!([ctx] user_resolve_email {
let email_res = (*ctx).op(::user::ops::resolve_email::Input {
emails: vec![res.email.clone()],
})
.await?;
Expand All @@ -106,13 +106,13 @@ pub async fn complete(
if let Some(new_user) = email_res.users.first() {
tracing::info!(email = %new_user.email, "resolved email");

let new_user_id = unwrap_ref!(new_user.user_id).as_uuid();
let new_user_id = new_user.user_id;

tracing::info!(old_user_id = %user_ent.user_id, %new_user_id, "identity found, switching user");

let token_res = op!([ctx] user_token_create {
user_id: Some(new_user_id.into()),
client: Some(ctx.client_info()),
let token_res = (*ctx).op(::user::ops::token_create::Input {
user_id: new_user_id,
client: ctx.client_info(),
})
.await?;

Expand All @@ -130,15 +130,15 @@ pub async fn complete(
else {
tracing::info!(user_id = %user_ent.user_id, "creating new identity for guest");

op!([ctx] user_identity_create {
user_id: Some(Into::into(user_ent.user_id)),
identity: Some(backend::user_identity::Identity {
(*ctx).op(::user::ops::identity::create::Input {
user_id: user_ent.user_id,
identity: backend::user_identity::Identity {
kind: Some(backend::user_identity::identity::Kind::Email(
backend::user_identity::identity::Email {
email: res.email.clone(),
}
))
})
}
})
.await?;

Expand Down
17 changes: 10 additions & 7 deletions packages/api/auth/src/route/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
utils::{delete_refresh_token_header, refresh_token_header},
};

// Also see user-token-create/src/main.rs
// Also see user/src/ops/token_create.rs
pub const TOKEN_TTL: i64 = util::duration::minutes(15);
pub const REFRESH_TOKEN_TTL: i64 = util::duration::days(90);

Expand Down Expand Up @@ -137,8 +137,8 @@ pub async fn identity(

// Verify user is not deleted
if has_refresh_token {
let user_res = op!([ctx] user_get {
user_ids: vec![user_ent.user_id.into()],
let user_res = (*ctx).op(::user::ops::get::Input {
user_ids: vec![user_ent.user_id],
})
.await?;
let user = unwrap!(user_res.users.first());
Expand Down Expand Up @@ -205,10 +205,13 @@ async fn fallback_user(
};

// Generate token
let token_res = op!([ctx] user_token_create {
user_id: Some(user_id.into()),
client: Some(client_info),
})
let token_res = chirp_workflow::compat::op(
ctx,
::user::ops::token_create::Input {
user_id: user_id,
client: client_info,
},
)
.await?;

Ok((token_res.token.clone(), token_res.refresh_token))
Expand Down
5 changes: 2 additions & 3 deletions packages/api/cloud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ async-trait = "0.1"
base64 = "0.13"
bytes = "1.0"
chirp-client.workspace = true
chirp-workflow.workspace = true
chrono = "0.4"
futures-util = "0.3"
http = "0.2"
Expand Down Expand Up @@ -107,9 +108,7 @@ upload-complete.workspace = true
upload-file-list.workspace = true
upload-get.workspace = true
upload-prepare.workspace = true
user-get.workspace = true
user-identity-get.workspace = true
user-team-list.workspace = true
user.workspace = true
rivet-config.workspace = true
rivet-env.workspace = true

Expand Down
9 changes: 6 additions & 3 deletions packages/api/cloud/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ use crate::auth::Auth;
/// Validates that a given user ID is registered.
pub async fn user_registered(ctx: &OperationContext<()>, user_id: Uuid) -> GlobalResult<()> {
// If the user has at least one identity they are considered registered
let identity = op!([ctx] user_identity_get {
user_ids: vec![user_id.into()]
})
let identity = chirp_workflow::compat::op(
&ctx,
::user::ops::identity::get::Input {
user_ids: vec![user_id]
},
)
.await?;

let identities = &unwrap_ref!(identity.users.first()).identities;
Expand Down
42 changes: 25 additions & 17 deletions packages/api/cloud/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ impl Auth {
let claims = self.claims()?;
let user_ent = claims.as_user()?;

let user_res = op!([ctx] user_get {
user_ids: vec![user_ent.user_id.into()],
})
let user_res = chirp_workflow::compat::op(
&ctx,
::user::ops::get::Input {
user_ids: vec![user_ent.user_id],
},
)
.await?;
let Some(user) = user_res.users.first() else {
bail_with!(TOKEN_REVOKED)
Expand Down Expand Up @@ -94,16 +97,19 @@ impl Auth {
let (user, user_ent) = self.user(ctx).await?;
assert::user_registered(ctx, user_ent.user_id).await?;

let team_list_res = op!([ctx] user_team_list {
user_ids: vec![user_ent.user_id.into()],
})
let team_list_res = chirp_workflow::compat::op(
&ctx,
user::ops::team_list::Input {
user_ids: vec![user_ent.user_id.into()],
},
)
.await?;

let user_teams = unwrap!(team_list_res.users.first());
let user_team_ids = user_teams
.teams
.iter()
.map(|t| Ok(unwrap_ref!(t.team_id).as_uuid()))
.map(|t| Ok(t.team_id))
.collect::<GlobalResult<HashSet<_>>>()?;
let has_teams = team_ids
.iter()
Expand Down Expand Up @@ -271,24 +277,26 @@ impl Auth {
let (_, user_ent) = self.user(ctx).await?;

// Fetch teams associated with user
let teams_res = op!([ctx] user_team_list {
user_ids: vec![user_ent.user_id.into()],
})
let teams_res = chirp_workflow::compat::op(
&ctx,
::user::ops::team_list::Input {
user_ids: vec![user_ent.user_id.into()],
},
)
.await?;
let user = unwrap!(teams_res.users.first());
let team_ids_proto = user
let team_ids = user
.teams
.iter()
.filter_map(|t| t.team_id)
.collect::<Vec<common::Uuid>>();
let team_ids = team_ids_proto
.iter()
.map(common::Uuid::as_uuid)
.map(|t| t.team_id)
.collect::<Vec<_>>();

// Fetch games associated with teams
let games_res = op!([ctx] game_list_for_team {
team_ids: team_ids_proto,
team_ids: team_ids
.iter()
.map(|id| (*id).into())
.collect::<Vec<_>>()
})
.await?;

Expand Down
4 changes: 2 additions & 2 deletions packages/api/games/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition.workspace = true
[dependencies]
api-helper.workspace = true
chirp-client.workspace = true
chirp-workflow.workspace = true
rivet-operation.workspace = true
chrono = "0.4"
http = "0.2"
Expand Down Expand Up @@ -45,9 +46,8 @@ token-revoke.workspace = true
token-create.workspace = true
upload-complete.workspace = true
upload-get.workspace = true
user-get.workspace = true
user.workspace = true
user-identity-get.workspace = true
user-team-list.workspace = true
rivet-config.workspace = true
rivet-env.workspace = true

Expand Down
21 changes: 13 additions & 8 deletions packages/api/games/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,21 @@ impl Auth {
} else if let Ok(user_ent) = claims.as_user() {
// Get the user
let (user_res, game_res, team_list_res) = tokio::try_join!(
op!([ctx] user_get {
user_ids: vec![user_ent.user_id.into()],
}),
chirp_workflow::compat::op(
&ctx,
::user::ops::get::Input {
user_ids: vec![user_ent.user_id],
},
),
op!([ctx] game_get {
game_ids: vec![game_id.into()],
}),
op!([ctx] user_team_list {
user_ids: vec![user_ent.user_id.into()],
}),
chirp_workflow::compat::op(
&ctx,
::user::ops::team_list::Input {
user_ids: vec![user_ent.user_id.into()],
},
)
)?;
let Some(user) = user_res.users.first() else {
bail_with!(TOKEN_REVOKED)
Expand All @@ -126,8 +132,7 @@ impl Auth {
let is_part_of_team = user_teams
.teams
.iter()
.filter_map(|x| x.team_id)
.any(|x| x.as_uuid() == dev_team_id);
.any(|x| x.team_id == dev_team_id);
ensure_with!(is_part_of_team, GROUP_NOT_MEMBER);

// Get team
Expand Down
6 changes: 2 additions & 4 deletions packages/api/group/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ rivet-convert.workspace = true
api-helper.workspace = true
async-trait = "0.1"
chirp-client.workspace = true
chirp-workflow.workspace = true
rivet-operation.workspace = true
chrono = "0.4"
futures-util = "0.3"
Expand Down Expand Up @@ -50,9 +51,7 @@ team-user-ban-get.workspace = true
team-user-ban-list.workspace = true
token-revoke.workspace = true
upload-prepare.workspace = true
user-get.workspace = true
user-identity-get.workspace = true
user-team-list.workspace = true
user.workspace = true
rivet-config.workspace = true
rivet-env.workspace = true

Expand All @@ -62,4 +61,3 @@ rivet-group.workspace = true
rand = "0.8"

faker-user.workspace = true
user-token-create.workspace = true
4 changes: 2 additions & 2 deletions packages/api/group/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use crate::auth::Auth;
/// Validates that a given user ID is registered.
pub async fn user_registered(ctx: &Ctx<Auth>, user_id: Uuid) -> GlobalResult<()> {
// If the user has at least one identity they are considered registered
let identity = op!([ctx] user_identity_get {
user_ids: vec![user_id.into()]
let identity = (*ctx).op(::user::ops::identity::get::Input {
user_ids: vec![user_id]
})
.await?;

Expand Down
Loading
Loading