Skip to content
This repository was archived by the owner on Dec 21, 2024. It is now read-only.

Commit 0bc96ce

Browse files
authored
Merge pull request #19 from rivet-gg/max/identity-service
Add identity service
2 parents ebfaa17 + 7774e62 commit 0bc96ce

File tree

17 files changed

+5716
-689
lines changed

17 files changed

+5716
-689
lines changed

cli/src/commands/avatar.rs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
use anyhow::{Context, Result};
2+
use clap::Parser;
3+
use tabled::Tabled;
4+
5+
use crate::util::{fmt, term, upload};
6+
7+
#[derive(Parser)]
8+
pub enum SubCommand {
9+
List,
10+
}
11+
12+
impl SubCommand {
13+
pub async fn execute(&self, ctx: &cli_core::Ctx) -> Result<()> {
14+
match self {
15+
SubCommand::List => {
16+
let custom_avatars_res = ctx
17+
.client()
18+
.list_game_custom_avatars()
19+
.game_id(&ctx.game_id)
20+
.send()
21+
.await
22+
.context("client.list_game_custom_avatars")?;
23+
24+
#[derive(Tabled)]
25+
struct CustomAvatar {
26+
#[tabled(rename = "Url")]
27+
url: String,
28+
#[tabled(rename = "Created")]
29+
created: String,
30+
#[tabled(rename = "Size")]
31+
size: String,
32+
}
33+
34+
let custom_avatars = custom_avatars_res
35+
.custom_avatars()
36+
.context("custom_avatars_res.custom_avatars")?
37+
.iter()
38+
.map(|custom_avatar| {
39+
Ok(CustomAvatar {
40+
url: custom_avatar
41+
.url()
42+
.context("custom_avatar.url")?
43+
.to_string(),
44+
created: fmt::date(
45+
custom_avatar
46+
.create_ts()
47+
.context("custom_avatar.create_ts")?,
48+
),
49+
size: upload::format_file_size(
50+
custom_avatar
51+
.content_length()
52+
.context("custom_avatar.content_length")? as u64,
53+
)?,
54+
})
55+
})
56+
.collect::<Result<Vec<_>>>()?;
57+
58+
term::table(&custom_avatars);
59+
60+
Ok(())
61+
}
62+
}
63+
}
64+
}

cli/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pub mod avatar;
12
pub mod build;
23
pub mod dev;
34
pub mod game;

cli/src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ enum SubCommand {
4949
#[clap(subcommand)]
5050
command: site::SubCommand,
5151
},
52+
Avatar {
53+
#[clap(subcommand)]
54+
command: avatar::SubCommand,
55+
},
5256
}
5357

5458
#[tokio::main]
@@ -82,6 +86,7 @@ async fn main() -> Result<()> {
8286
game_id = ctx.game_id
8387
);
8488
}
89+
SubCommand::Avatar { command } => command.execute(&ctx).await?,
8590
SubCommand::Dev { command } => command.execute(&term, &ctx).await?,
8691
SubCommand::Game { command } => command.execute(&ctx).await?,
8792
SubCommand::Namespace { command } => command.execute(&ctx).await?,

cli/tests/static/basic/rivet.version.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,12 @@
2020

2121
[kv]
2222

23+
[identity]
24+
custom_display_names = ["a", "b"]
25+
26+
custom_avatars = [
27+
"7bbdaa3a-4620-4342-a407-55dfeecdef16",
28+
"e4e075b0-f5ae-4b02-b4bd-edf1737cd35d",
29+
]
30+
31+
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
use serde::Deserialize;
2+
3+
use crate::error::Error;
4+
5+
#[derive(Debug, Deserialize)]
6+
#[serde(deny_unknown_fields)]
7+
pub struct Identity {
8+
pub custom_display_names: Vec<CustomDisplayName>,
9+
pub custom_avatars: Vec<CustomAvatar>,
10+
}
11+
12+
#[derive(Debug, Deserialize)]
13+
#[serde(untagged)]
14+
pub enum CustomDisplayName {
15+
Full { display_name: String },
16+
DisplayName(String),
17+
}
18+
19+
impl CustomDisplayName {
20+
fn display_name(&self) -> &str {
21+
match self {
22+
Self::Full { display_name } => &display_name,
23+
Self::DisplayName(display_name) => &display_name,
24+
}
25+
}
26+
}
27+
28+
#[derive(Debug, Deserialize)]
29+
#[serde(untagged)]
30+
pub enum CustomAvatar {
31+
Full { upload_id: String },
32+
UploadId(String),
33+
}
34+
35+
impl CustomAvatar {
36+
fn upload_id(&self) -> &str {
37+
match self {
38+
Self::Full { upload_id } => &upload_id,
39+
Self::UploadId(upload_id) => &upload_id,
40+
}
41+
}
42+
}
43+
44+
impl Identity {
45+
pub fn build_model(
46+
&self,
47+
_game: &rivet_cloud::model::GameFull,
48+
) -> Result<rivet_cloud::model::IdentityVersionConfig, Error> {
49+
use rivet_cloud::model::*;
50+
51+
let custom_display_names = self
52+
.custom_display_names
53+
.iter()
54+
.map(|custom_display_name| {
55+
CustomDisplayName::builder()
56+
.display_name(custom_display_name.display_name())
57+
.build()
58+
})
59+
.collect::<Vec<_>>();
60+
let custom_avatars = self
61+
.custom_avatars
62+
.iter()
63+
.map(|custom_avatar| {
64+
CustomAvatar::builder()
65+
.upload_id(custom_avatar.upload_id())
66+
.build()
67+
})
68+
.collect::<Vec<_>>();
69+
70+
Ok(IdentityVersionConfig::builder()
71+
.set_custom_display_names(Some(custom_display_names))
72+
.set_custom_avatars(Some(custom_avatars))
73+
.build())
74+
}
75+
}

core/src/config/version/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use serde::Deserialize;
33
use crate::error::Error;
44

55
pub mod cdn;
6+
pub mod identity;
67
pub mod kv;
78
pub mod mm;
89

@@ -15,6 +16,8 @@ pub struct Version {
1516
pub matchmaker: Option<mm::Matchmaker>,
1617
#[serde(default)]
1718
pub kv: Option<kv::Kv>,
19+
#[serde(default)]
20+
pub identity: Option<identity::Identity>,
1821
}
1922

2023
impl Version {
@@ -33,6 +36,12 @@ impl Version {
3336
.transpose()?,
3437
)
3538
.set_kv(self.kv.as_ref().map(|x| x.build_model(game)).transpose()?)
39+
.set_identity(
40+
self.identity
41+
.as_ref()
42+
.map(|x| x.build_model(game))
43+
.transpose()?,
44+
)
3645
.build())
3746
}
3847
}

0 commit comments

Comments
 (0)