mirror of
https://github.com/modrinth/code.git
synced 2026-08-30 11:36:05 +00:00
remove: backend implementation
This commit is contained in:
@@ -142,9 +142,9 @@ export async function remove_custom_skin(skin: Skin): Promise<void> {
|
||||
})
|
||||
}
|
||||
|
||||
export async function reorder_custom_skins(skins: Skin[]): Promise<void> {
|
||||
await invoke('plugin:minecraft-skins|reorder_custom_skins', {
|
||||
skins,
|
||||
export async function set_custom_skin_order(textureKeys: string[]): Promise<void> {
|
||||
await invoke('plugin:minecraft-skins|set_custom_skin_order', {
|
||||
textureKeys,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -46,7 +46,7 @@ import {
|
||||
get_normalized_skin_texture,
|
||||
normalize_skin_texture,
|
||||
remove_custom_skin,
|
||||
reorder_custom_skins,
|
||||
set_custom_skin_order,
|
||||
} from '@/helpers/skins.ts'
|
||||
import { hasPride26Badge } from '@/helpers/user-campaigns.ts'
|
||||
import { handleSevereError } from '@/store/error'
|
||||
@@ -516,7 +516,7 @@ async function reorderSavedSkins(orderedSkins: Skin[]) {
|
||||
generateSkinPreviews(skins.value, capes.value)
|
||||
|
||||
try {
|
||||
await reorder_custom_skins(nextSavedSkins)
|
||||
await set_custom_skin_order(nextSavedSkins.map((skin) => skin.texture_key))
|
||||
} catch (error) {
|
||||
skins.value = previousSkins
|
||||
generateSkinPreviews(skins.value, capes.value)
|
||||
|
||||
@@ -116,7 +116,6 @@ fn main() {
|
||||
"add_and_equip_custom_skin",
|
||||
"equip_skin",
|
||||
"remove_custom_skin",
|
||||
"reorder_custom_skins",
|
||||
"save_custom_skin",
|
||||
"unequip_skin",
|
||||
"flush_pending_skin_change",
|
||||
|
||||
@@ -13,7 +13,6 @@ pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
|
||||
add_and_equip_custom_skin,
|
||||
equip_skin,
|
||||
remove_custom_skin,
|
||||
reorder_custom_skins,
|
||||
save_custom_skin,
|
||||
unequip_skin,
|
||||
flush_pending_skin_change,
|
||||
@@ -71,14 +70,6 @@ pub async fn remove_custom_skin(skin: Skin) -> Result<()> {
|
||||
Ok(minecraft_skins::remove_custom_skin(skin).await?)
|
||||
}
|
||||
|
||||
/// `invoke('plugin:minecraft-skins|reorder_custom_skins', skins)`
|
||||
///
|
||||
/// See also: [minecraft_skins::reorder_custom_skins]
|
||||
#[tauri::command]
|
||||
pub async fn reorder_custom_skins(skins: Vec<Skin>) -> Result<()> {
|
||||
Ok(minecraft_skins::reorder_custom_skins(skins).await?)
|
||||
}
|
||||
|
||||
/// `invoke('plugin:minecraft-skins|save_custom_skin', skin, texture_blob, variant, cape, replace_texture)`
|
||||
///
|
||||
/// See also: [minecraft_skins::save_custom_skin]
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
ALTER TABLE custom_minecraft_skins
|
||||
ADD COLUMN display_order INTEGER NOT NULL DEFAULT 0;
|
||||
|
||||
UPDATE custom_minecraft_skins
|
||||
SET display_order = rowid;
|
||||
@@ -445,6 +445,7 @@ pub async fn get_available_skins() -> crate::Result<Vec<Skin>> {
|
||||
});
|
||||
}
|
||||
|
||||
custom_skins.sort_by(|a, b| a.texture.as_str().cmp(b.texture.as_str()));
|
||||
available_skins.extend(custom_skins);
|
||||
|
||||
for default_skin in assets::DEFAULT_SKINS.iter() {
|
||||
@@ -800,55 +801,6 @@ pub async fn remove_custom_skin(skin: Skin) -> crate::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Reorders saved skins for the currently selected Minecraft profile.
|
||||
#[tracing::instrument(skip(skins))]
|
||||
pub async fn reorder_custom_skins(skins: Vec<Skin>) -> crate::Result<()> {
|
||||
let state = State::get().await?;
|
||||
|
||||
let selected_credentials = Credentials::get_default_credential(&state.pool)
|
||||
.await?
|
||||
.ok_or(ErrorKind::NoCredentialsError)?;
|
||||
let profile_id = selected_credentials.offline_profile.id;
|
||||
|
||||
for skin in &skins {
|
||||
if !matches!(skin.source, SkinSource::CustomExternal) {
|
||||
continue;
|
||||
}
|
||||
|
||||
let texture_blob = png_util::url_to_data_stream(&skin.texture)
|
||||
.await?
|
||||
.try_fold(Vec::new(), |mut texture, chunk| async move {
|
||||
texture.extend_from_slice(&chunk);
|
||||
Ok(texture)
|
||||
})
|
||||
.await?;
|
||||
|
||||
CustomMinecraftSkin::add(
|
||||
profile_id,
|
||||
&skin.texture_key,
|
||||
&texture_blob,
|
||||
skin.variant,
|
||||
skin.cape_id,
|
||||
&state.pool,
|
||||
)
|
||||
.await?;
|
||||
}
|
||||
|
||||
let texture_keys = skins
|
||||
.iter()
|
||||
.map(|skin| skin.texture_key.to_string())
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
CustomMinecraftSkin::reorder(
|
||||
profile_id,
|
||||
&texture_keys,
|
||||
&state.pool,
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Adds or updates a saved skin locally without applying it to Mojang.
|
||||
///
|
||||
/// This is used by the skin editor. If the edited skin is currently equipped, the caller should
|
||||
|
||||
@@ -44,23 +44,6 @@ impl CustomMinecraftSkin {
|
||||
|
||||
let mut transaction = db.begin().await?;
|
||||
|
||||
let display_order = sqlx::query_scalar!(
|
||||
"SELECT display_order FROM custom_minecraft_skins WHERE minecraft_user_uuid = ? AND texture_key = ?",
|
||||
minecraft_user_id,
|
||||
texture_key
|
||||
)
|
||||
.fetch_optional(&mut *transaction)
|
||||
.await?;
|
||||
let display_order = match display_order {
|
||||
Some(display_order) => display_order,
|
||||
None => sqlx::query_scalar!(
|
||||
"SELECT COALESCE(MAX(display_order) + 1, 0) AS 'display_order!: i64' FROM custom_minecraft_skins WHERE minecraft_user_uuid = ?",
|
||||
minecraft_user_id
|
||||
)
|
||||
.fetch_one(&mut *transaction)
|
||||
.await?,
|
||||
};
|
||||
|
||||
sqlx::query!(
|
||||
"DELETE FROM custom_minecraft_skins WHERE minecraft_user_uuid = ? AND texture_key = ?",
|
||||
minecraft_user_id,
|
||||
@@ -77,8 +60,8 @@ impl CustomMinecraftSkin {
|
||||
.await?;
|
||||
|
||||
sqlx::query!(
|
||||
"INSERT OR REPLACE INTO custom_minecraft_skins (minecraft_user_uuid, texture_key, variant, cape_id, display_order) VALUES (?, ?, ?, ?, ?)",
|
||||
minecraft_user_id, texture_key, variant, cape_id, display_order
|
||||
"INSERT OR REPLACE INTO custom_minecraft_skins (minecraft_user_uuid, texture_key, variant, cape_id) VALUES (?, ?, ?, ?)",
|
||||
minecraft_user_id, texture_key, variant, cape_id
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
@@ -123,16 +106,13 @@ impl CustomMinecraftSkin {
|
||||
) -> crate::Result<impl Stream<Item = Self>> {
|
||||
let minecraft_user_id = minecraft_user_id.as_hyphenated();
|
||||
|
||||
Ok(stream::iter(sqlx::query_as!(
|
||||
CustomMinecraftSkinRow,
|
||||
Ok(stream::iter(sqlx::query!(
|
||||
"SELECT texture_key, variant AS 'variant: MinecraftSkinVariant', cape_id AS 'cape_id: Hyphenated' \
|
||||
FROM custom_minecraft_skins \
|
||||
WHERE minecraft_user_uuid = ? \
|
||||
ORDER BY display_order ASC, rowid ASC \
|
||||
ORDER BY rowid ASC \
|
||||
LIMIT ? OFFSET ?",
|
||||
minecraft_user_id,
|
||||
count,
|
||||
offset
|
||||
minecraft_user_id, count, offset
|
||||
)
|
||||
.fetch_all(&mut *db.acquire().await?)
|
||||
.await?)
|
||||
@@ -181,32 +161,4 @@ impl CustomMinecraftSkin {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn reorder(
|
||||
minecraft_user_id: Uuid,
|
||||
texture_keys: &[String],
|
||||
db: impl sqlx::Acquire<'_, Database = sqlx::Sqlite>,
|
||||
) -> crate::Result<()> {
|
||||
let minecraft_user_id = minecraft_user_id.as_hyphenated();
|
||||
let mut transaction = db.begin().await?;
|
||||
|
||||
for (display_order, texture_key) in texture_keys.iter().enumerate() {
|
||||
let display_order = display_order as i64;
|
||||
|
||||
sqlx::query!(
|
||||
"UPDATE custom_minecraft_skins \
|
||||
SET display_order = ? \
|
||||
WHERE minecraft_user_uuid = ? AND texture_key = ?",
|
||||
display_order,
|
||||
minecraft_user_id,
|
||||
texture_key
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
.await?;
|
||||
}
|
||||
|
||||
transaction.commit().await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user