feat: added command to backfill skins
This commit is contained in:
parent
28b32c8135
commit
dd8c945a26
1 changed files with 48 additions and 2 deletions
|
@ -1,12 +1,58 @@
|
|||
use std::time::Duration;
|
||||
use std::{time::Duration, collections::HashSet};
|
||||
|
||||
use axum::{extract::State, Json};
|
||||
use reqwest::StatusCode;
|
||||
use sea_orm::{EntityTrait, QueryFilter, ColumnTrait, Set, ActiveValue::NotSet};
|
||||
use sea_orm::{EntityTrait, QueryFilter, ColumnTrait, Set, ActiveValue::NotSet, DatabaseConnection, DbErr};
|
||||
use tracing::info;
|
||||
|
||||
use crate::{AppState, proto::{self, Response}, entities};
|
||||
|
||||
// TODO shouldn't be here probably
|
||||
pub async fn fill_missing_skins(db: &DatabaseConnection) -> Result<(), DbErr> {
|
||||
let mut users_with_skins = HashSet::new();
|
||||
|
||||
for skin in entities::property::Entity::find().all(db).await? {
|
||||
users_with_skins.insert(skin.user_id);
|
||||
}
|
||||
|
||||
for user in entities::user::Entity::find().all(db).await? {
|
||||
if users_with_skins.contains(&user.id) {
|
||||
continue;
|
||||
}
|
||||
|
||||
info!("Updating skin for user {} ({})", user.name, user.uuid);
|
||||
let url = format!("https://sessionserver.mojang.com/session/minecraft/profile/{}?unsigned=false", user.uuid.simple());
|
||||
let res = reqwest::Client::new().get(url.clone()) //TODO: needs trimmed uuid, is it trimmed by default?
|
||||
.json(&()).send()
|
||||
.await.map_err(|_| DbErr::Custom(format!("could not fetch profile for user {}", user.uuid)))?
|
||||
.text().await.expect("invalid body on response");
|
||||
|
||||
let doc = serde_json::from_str::<proto::SessionUser>(&res)
|
||||
.map_err(|_| DbErr::Json("invalid texture response".into()))?;
|
||||
|
||||
let mut skin = proto::Property::default_skin();
|
||||
|
||||
for s in doc.properties.expect("missing properties field") {
|
||||
if s.name == "textures" {
|
||||
skin = s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
entities::property::Entity::insert(
|
||||
entities::property::ActiveModel {
|
||||
id: NotSet,
|
||||
name: Set("textures".into()),
|
||||
user_id: Set(user.id),
|
||||
value: Set(skin.value),
|
||||
signature: Set(skin.signature),
|
||||
}
|
||||
).exec(db).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
//TODO: replace format! with axum's own
|
||||
pub async fn register_unmigrated(State(state): State<AppState>, Json(payload): Json<proto::RegisterRequest>) -> Response<proto::RegisterResponse> {
|
||||
info!(target: "REGISTER", "[UNMIGRATED] called with {:?}", payload);
|
||||
|
|
Loading…
Reference in a new issue