Skip to content

Commit

Permalink
feat: changed BeatmapSmall to be the same as BeatmapSeachOsu typed an…
Browse files Browse the repository at this point in the history
…d renamed to BeatmapsetSmall
  • Loading branch information
112batuhan committed Dec 8, 2024
1 parent cced7aa commit a528970
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 94 deletions.
4 changes: 2 additions & 2 deletions src/database/influence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
use crate::{
error::AppError,
handlers::influence::InfluenceCreationOptions,
osu_api::{BeatmapEnum, OsuBeatmapSmall},
osu_api::{BeatmapEnum, BeatmapsetSmall},
};

use super::{numerical_thing, user::UserSmall, DatabaseClient};
Expand All @@ -18,7 +18,7 @@ pub struct Influence {
/// `OsuUserSmall` type. This array will be empty for mentions endpoint even if the
/// influence contains beatmaps
#[serde(default)]
#[schemars(with = "Vec<OsuBeatmapSmall>")]
#[schemars(with = "Vec<BeatmapsetSmall>")]
pub beatmaps: Vec<BeatmapEnum>,
}

Expand Down
4 changes: 2 additions & 2 deletions src/database/leaderboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize};

use crate::{
error::AppError,
osu_api::{BeatmapEnum, OsuBeatmapSmall},
osu_api::{BeatmapEnum, BeatmapsetSmall},
};

use super::{user::UserSmall, DatabaseClient};
Expand All @@ -19,7 +19,7 @@ pub struct LeaderboardUser {
#[derive(Debug, Serialize, Deserialize, Clone, JsonSchema, PartialEq)]
/// `LeaderboardBeatmap` type
pub struct LeaderboardBeatmap {
#[schemars(with = "OsuBeatmapSmall")]
#[schemars(with = "BeatmapsetSmall")]
pub beatmap: BeatmapEnum,
/// Amount of times that this map has been added to the mentions
pub count: u32,
Expand Down
4 changes: 2 additions & 2 deletions src/database/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use surrealdb::sql::Thing;

use crate::{
error::AppError,
osu_api::{BeatmapEnum, Group, OsuBeatmapSmall, UserOsu},
osu_api::{BeatmapEnum, BeatmapsetSmall, Group, UserOsu},
retry::Retryable,
};

Expand All @@ -31,7 +31,7 @@ pub struct User {
pub loved_beatmapset_count: u32,
pub graveyard_beatmapset_count: u32,
pub pending_beatmapset_count: u32,
#[schemars(with = "Vec<OsuBeatmapSmall>")]
#[schemars(with = "Vec<BeatmapsetSmall>")]
pub beatmaps: Vec<BeatmapEnum>,
/// This will have a number if the data is coming from database.
/// If the data comes from osu! API, then this will be null
Expand Down
10 changes: 5 additions & 5 deletions src/documentation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{database::user::UserSmall, osu_api::OsuBeatmapSmall};
use crate::{database::user::UserSmall, osu_api::BeatmapsetSmall};

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
pub struct FlattenedActivityType {
pub event_type: EventType,
pub influence: Option<UserSmallActivity>,
pub beatmap: Option<OsuBeatmapSmallActivity>,
pub beatmap: Option<BeatmapsetSmallActivity>,
/// Changed influence description. for `EDIT_INFLUENCE_DESC` activity type.
pub description: Option<String>,
/// Changed influence type. for `EDIT_INFLUENCE_TYPE` activity type.
Expand All @@ -29,14 +29,14 @@ pub struct UserSmallActivity {
inner: UserSmall,
}

/// Added or removed beatmap. `OsuBeatmapSmall` type. For `ADD_USER_BEATMAP`, `REMOVE_USER_BEATMAP`,
/// Added or removed beatmap. `BeatmapsetSmall` type. For `ADD_USER_BEATMAP`, `REMOVE_USER_BEATMAP`,
/// `ADD_INFLUENCE_BEATMAP`, `REMOVE_INFLUENCE_BEATMAP` activity types.
///
/// This is a placeholder type for documentation only. It's the same as `OsuBeatmapSmall`
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
pub struct OsuBeatmapSmallActivity {
pub struct BeatmapsetSmallActivity {
#[serde(flatten)]
inner: OsuBeatmapSmall,
inner: BeatmapsetSmall,
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
Expand Down
63 changes: 5 additions & 58 deletions src/handlers/osu_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,18 @@ use axum::{
};
use cached::proc_macro::cached;
use itertools::Itertools;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
custom_cache::CustomCache,
database::user::UserSmall,
error::AppError,
jwt::AuthData,
osu_api::{
cached_requester::cached_osu_user_request, BaseBeatmapset, BeatmapOsu, OsuBeatmapSmall,
OsuMultipleUser,
},
osu_api::{cached_requester::cached_osu_user_request, BeatmapsetSmall},
AppState,
};

use super::{PathBeatmapId, PathQuery};

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
/// `SearchBeatmapset` type. For more compact beatmap search results
pub struct SearchBeatmapset {
pub id: u32,
pub beatmaps: Vec<BeatmapOsu>,
pub title: String,
pub artist: String,
pub cover: String,
pub user_name: String,
pub user_avatar_url: String,
pub user_id: u32,
}

impl SearchBeatmapset {
/// This function combines [`BaseBeatmapset`] and [`OsuMultipleUser`].
///
/// If user is not returned from the query, we fallback to beatmapset user.
/// This usually happens if the original mapper is banned. If the beatmapset submitter is also
/// banned, we don't have to worry about the avatar_url as osu automatically falls back to
/// guest picture.
pub fn from_base_beapmapset_and_user(
api_set: BaseBeatmapset,
user_multiple: Option<OsuMultipleUser>,
) -> Self {
let user_name: String;
let user_avatar_url: String;

if let Some(user_multiple) = user_multiple {
user_name = user_multiple.username;
user_avatar_url = user_multiple.avatar_url;
} else {
user_name = api_set.creator;
user_avatar_url = format!("https://a.ppy.sh/{}?", api_set.user_id);
}

SearchBeatmapset {
id: api_set.id,
beatmaps: api_set.beatmaps,
title: api_set.title,
artist: api_set.artist,
cover: api_set.covers.cover,
user_id: api_set.user_id,
user_name,
user_avatar_url,
}
}
}

#[cached(
ty = "CustomCache<String, Json<Vec<UserSmall>>>",
create = "{CustomCache::new(600)}",
Expand Down Expand Up @@ -119,7 +66,7 @@ pub async fn osu_user_search(
}

#[cached(
ty = "CustomCache<String, Json<Vec<SearchBeatmapset>>>",
ty = "CustomCache<String, Json<Vec<BeatmapsetSmall>>>",
create = "{CustomCache::new(300)}",
convert = r#"{request.uri().to_string()}"#,
result = true
Expand All @@ -128,7 +75,7 @@ pub async fn osu_beatmap_search(
Extension(auth_data): Extension<AuthData>,
State(state): State<Arc<AppState>>,
request: Request,
) -> Result<Json<Vec<SearchBeatmapset>>, AppError> {
) -> Result<Json<Vec<BeatmapsetSmall>>, AppError> {
let uri = request.uri().to_string();
let query = uri
.strip_prefix("/search/map?")
Expand All @@ -155,7 +102,7 @@ pub async fn osu_beatmap_search(
.into_iter()
.map(|beatmapset| {
let user = user_map.get(&beatmapset.user_id).cloned();
SearchBeatmapset::from_base_beapmapset_and_user(beatmapset, user)
BeatmapsetSmall::from_base_beapmapset_and_user(beatmapset, user)
})
.collect();

Expand All @@ -166,7 +113,7 @@ pub async fn osu_singular_beatmap_serch(
Path(beatmap_path): Path<PathBeatmapId>,
Extension(auth_data): Extension<AuthData>,
State(state): State<Arc<AppState>>,
) -> Result<Json<OsuBeatmapSmall>, AppError> {
) -> Result<Json<BeatmapsetSmall>, AppError> {
let beatmap_map = state
.cached_combined_requester
.clone()
Expand Down
6 changes: 3 additions & 3 deletions src/osu_api/cached_requester.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use serde_json::Value;
use crate::{custom_cache::CustomCache, error::AppError};

use super::{
request::Requester, GetID, OsuBeatmapSmall, OsuMultipleBeatmap, OsuMultipleUser, UserOsu,
request::Requester, BeatmapsetSmall, GetID, OsuMultipleBeatmap, OsuMultipleUser, UserOsu,
};

pub struct CachedRequester<T: DeserializeOwned + GetID + Clone + Send + 'static> {
Expand Down Expand Up @@ -97,7 +97,7 @@ impl CombinedRequester {
&self,
ids: &[u32],
access_token: &str,
) -> Result<HashMap<u32, OsuBeatmapSmall>, AppError> {
) -> Result<HashMap<u32, BeatmapsetSmall>, AppError> {
let beatmap_map = self
.beatmap_requester
.clone()
Expand All @@ -117,7 +117,7 @@ impl CombinedRequester {
.into_iter()
.map(|(beatmap_id, beatmap)| {
let user = user_map.get(&beatmap.user_id).cloned();
let new_beatmap = OsuBeatmapSmall::from_osu_beatmap_and_user_data(beatmap, user);
let new_beatmap = BeatmapsetSmall::from_osu_beatmap_and_user_data(beatmap, user);
(beatmap_id, new_beatmap)
})
.collect();
Expand Down
78 changes: 56 additions & 22 deletions src/osu_api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@ pub struct OsuSearchUserResponse {
pub user: OsuSearchUserData,
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema)]
/// `BeatmapOsu` type. Used in `SearchBeatmapset` type
#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, PartialEq)]
/// `BeatmapOsu` type. Used in `BeatmapsetSmall` type
pub struct BeatmapOsu {
pub difficulty_rating: f64,
pub difficulty_rating: f32,
pub id: u32,
pub mode: String,
pub beatmapset_id: u32,
pub version: String,
}

Expand Down Expand Up @@ -205,23 +204,22 @@ pub struct OsuMultipleBeatmapsetResponse {
}

#[derive(Serialize, Deserialize, Debug, Clone, JsonSchema, PartialEq)]
/// `OsuBeatmapSmall` type. Mainly for beatmap cards
pub struct OsuBeatmapSmall {
/// `BeatmapsetSmall` type. Mainly for beatmap cards.
pub struct BeatmapsetSmall {
pub id: u32,
pub difficulty_rating: f32,
pub mode: String,
pub beatmapset_id: u32,
pub version: String,
pub user_id: u32,
pub user_name: String,
pub user_avatar_url: String,
pub beatmaps: Vec<BeatmapOsu>,
pub title: String,
pub artist: String,
pub cover: String,
pub user_name: String,
pub user_avatar_url: String,
pub user_id: u32,
}

impl OsuBeatmapSmall {
impl BeatmapsetSmall {
/// This function combines [`OsuMultipleBeatmap`] and [`OsuMultipleUser`].
/// [`OsuMultipleBeatmap`] is returned from multiple beatmap queries and then we also save this
/// data in the memory cache.
///
/// If user is not returned from the query, we fallback to beatmapset user.
/// This usually happens if the original mapper is banned. If the beatmapset submitter is also
Expand All @@ -230,7 +228,7 @@ impl OsuBeatmapSmall {
pub fn from_osu_beatmap_and_user_data(
osu_multiple: OsuMultipleBeatmap,
user_multiple: Option<OsuMultipleUser>,
) -> OsuBeatmapSmall {
) -> BeatmapsetSmall {
let user_name: String;
let user_avatar_url: String;

Expand All @@ -242,12 +240,14 @@ impl OsuBeatmapSmall {
user_avatar_url = format!("https://a.ppy.sh/{}?", osu_multiple.beatmapset.user_id);
}

OsuBeatmapSmall {
id: osu_multiple.id,
difficulty_rating: osu_multiple.difficulty_rating,
mode: osu_multiple.mode,
beatmapset_id: osu_multiple.beatmapset_id,
version: osu_multiple.version,
BeatmapsetSmall {
id: osu_multiple.beatmapset_id,
beatmaps: vec![BeatmapOsu {
difficulty_rating: osu_multiple.difficulty_rating,
id: osu_multiple.id,
mode: osu_multiple.mode,
version: osu_multiple.version,
}],
user_id: osu_multiple.user_id,
user_name,
user_avatar_url,
Expand All @@ -256,14 +256,48 @@ impl OsuBeatmapSmall {
cover: osu_multiple.beatmapset.covers.cover,
}
}

/// This function combines [`BaseBeatmapset`] and [`OsuMultipleUser`].
/// [`BaseBeatmapset`] is returned from beatmap search endpoint.
///
/// If user is not returned from the query, we fallback to beatmapset user.
/// This usually happens if the original mapper is banned. If the beatmapset submitter is also
/// banned, we don't have to worry about the avatar_url as osu automatically falls back to
/// guest picture.
pub fn from_base_beapmapset_and_user(
api_set: BaseBeatmapset,
user_multiple: Option<OsuMultipleUser>,
) -> Self {
let user_name: String;
let user_avatar_url: String;

if let Some(user_multiple) = user_multiple {
user_name = user_multiple.username;
user_avatar_url = user_multiple.avatar_url;
} else {
user_name = api_set.creator;
user_avatar_url = format!("https://a.ppy.sh/{}?", api_set.user_id);
}

BeatmapsetSmall {
id: api_set.id,
beatmaps: api_set.beatmaps,
title: api_set.title,
artist: api_set.artist,
cover: api_set.covers.cover,
user_id: api_set.user_id,
user_name,
user_avatar_url,
}
}
}

/// Despite having two variants for beatmaps, the API will always return the full beatmap
/// objects instead of integer id's.
#[derive(Debug, Serialize, Deserialize, JsonSchema, Clone, PartialEq)]
#[serde(untagged)]
pub enum BeatmapEnum {
All(OsuBeatmapSmall),
All(BeatmapsetSmall),
Id(u32),
}

Expand Down

0 comments on commit a528970

Please sign in to comment.