Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion crates/devnull-api/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1073,12 +1073,28 @@ impl UserPreview {

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Activity {
#[serde(default)]
#[serde(default = "year_scale", deserialize_with = "deserialize_scale")]
pub scale: String,
#[serde(default)]
pub buckets: Vec<ActivityBucket>,
}

fn year_scale() -> String {
"year".to_owned()
}

/// Draw a scale we don't know (or a missing / non-string one) as a year.
fn deserialize_scale<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let scale = serde_json::Value::deserialize(deserializer)?;
Ok(match scale.as_str() {
Some(scale @ ("day" | "week" | "month" | "year")) => scale.to_owned(),
_ => year_scale(),
})
}

#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ActivityBucket {
pub date: String,
Expand Down
33 changes: 33 additions & 0 deletions crates/devnull-api/tests/signing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,39 @@ fn profile_reads_activity_achievements_and_follow_state() {
assert_eq!(profile.pasteis_received, Some(3));
}

#[test]
fn profile_reads_an_unexpected_activity_scale_as_a_year() {
// An empty entry leaves `scale` out entirely.
for scale in [
r#""scale": "quarter","#,
r#""scale": ["day"],"#,
r#""scale": null,"#,
r#""scale": 3,"#,
"",
] {
let json = format!(
r#"{{
"data": {{
"id": "u1",
"username": "ada",
"activity": {{
{scale}
"buckets": [
{{"date": "2026-10-18", "count": 0}},
{{"date": "2026-10-19", "count": 3}}
]
}}
}}
}}"#
);
let profile =
devnull_api::Profile::from_value(serde_json::from_str(&json).unwrap()).unwrap();
assert_eq!(profile.activity.scale, "year", "{scale:?}");
assert_eq!(profile.activity.window(), "in the last year");
assert_eq!(profile.activity.cells().len(), 2, "{scale:?}");
}
}

fn armor(signature: &str) -> String {
let mut out = String::from("-----BEGIN SSH SIGNATURE-----\n");
for chunk in signature.as_bytes().chunks(70) {
Expand Down
Loading