mirror of
https://github.com/modrinth/code.git
synced 2026-08-31 03:55:59 +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> {
|
export async function set_custom_skin_order(textureKeys: string[]): Promise<void> {
|
||||||
await invoke('plugin:minecraft-skins|reorder_custom_skins', {
|
await invoke('plugin:minecraft-skins|set_custom_skin_order', {
|
||||||
skins,
|
textureKeys,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ import {
|
|||||||
get_normalized_skin_texture,
|
get_normalized_skin_texture,
|
||||||
normalize_skin_texture,
|
normalize_skin_texture,
|
||||||
remove_custom_skin,
|
remove_custom_skin,
|
||||||
reorder_custom_skins,
|
set_custom_skin_order,
|
||||||
} from '@/helpers/skins.ts'
|
} from '@/helpers/skins.ts'
|
||||||
import { hasPride26Badge } from '@/helpers/user-campaigns.ts'
|
import { hasPride26Badge } from '@/helpers/user-campaigns.ts'
|
||||||
import { handleSevereError } from '@/store/error'
|
import { handleSevereError } from '@/store/error'
|
||||||
@@ -516,7 +516,7 @@ async function reorderSavedSkins(orderedSkins: Skin[]) {
|
|||||||
generateSkinPreviews(skins.value, capes.value)
|
generateSkinPreviews(skins.value, capes.value)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await reorder_custom_skins(nextSavedSkins)
|
await set_custom_skin_order(nextSavedSkins.map((skin) => skin.texture_key))
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
skins.value = previousSkins
|
skins.value = previousSkins
|
||||||
generateSkinPreviews(skins.value, capes.value)
|
generateSkinPreviews(skins.value, capes.value)
|
||||||
|
|||||||
@@ -116,7 +116,6 @@ fn main() {
|
|||||||
"add_and_equip_custom_skin",
|
"add_and_equip_custom_skin",
|
||||||
"equip_skin",
|
"equip_skin",
|
||||||
"remove_custom_skin",
|
"remove_custom_skin",
|
||||||
"reorder_custom_skins",
|
|
||||||
"save_custom_skin",
|
"save_custom_skin",
|
||||||
"unequip_skin",
|
"unequip_skin",
|
||||||
"flush_pending_skin_change",
|
"flush_pending_skin_change",
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ pub fn init<R: tauri::Runtime>() -> tauri::plugin::TauriPlugin<R> {
|
|||||||
add_and_equip_custom_skin,
|
add_and_equip_custom_skin,
|
||||||
equip_skin,
|
equip_skin,
|
||||||
remove_custom_skin,
|
remove_custom_skin,
|
||||||
reorder_custom_skins,
|
|
||||||
save_custom_skin,
|
save_custom_skin,
|
||||||
unequip_skin,
|
unequip_skin,
|
||||||
flush_pending_skin_change,
|
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?)
|
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)`
|
/// `invoke('plugin:minecraft-skins|save_custom_skin', skin, texture_blob, variant, cape, replace_texture)`
|
||||||
///
|
///
|
||||||
/// See also: [minecraft_skins::save_custom_skin]
|
/// 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);
|
available_skins.extend(custom_skins);
|
||||||
|
|
||||||
for default_skin in assets::DEFAULT_SKINS.iter() {
|
for default_skin in assets::DEFAULT_SKINS.iter() {
|
||||||
@@ -800,55 +801,6 @@ pub async fn remove_custom_skin(skin: Skin) -> crate::Result<()> {
|
|||||||
Ok(())
|
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.
|
/// 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
|
/// 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 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!(
|
sqlx::query!(
|
||||||
"DELETE FROM custom_minecraft_skins WHERE minecraft_user_uuid = ? AND texture_key = ?",
|
"DELETE FROM custom_minecraft_skins WHERE minecraft_user_uuid = ? AND texture_key = ?",
|
||||||
minecraft_user_id,
|
minecraft_user_id,
|
||||||
@@ -77,8 +60,8 @@ impl CustomMinecraftSkin {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
sqlx::query!(
|
sqlx::query!(
|
||||||
"INSERT OR REPLACE INTO custom_minecraft_skins (minecraft_user_uuid, texture_key, variant, cape_id, display_order) VALUES (?, ?, ?, ?, ?)",
|
"INSERT OR REPLACE INTO custom_minecraft_skins (minecraft_user_uuid, texture_key, variant, cape_id) VALUES (?, ?, ?, ?)",
|
||||||
minecraft_user_id, texture_key, variant, cape_id, display_order
|
minecraft_user_id, texture_key, variant, cape_id
|
||||||
)
|
)
|
||||||
.execute(&mut *transaction)
|
.execute(&mut *transaction)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -123,16 +106,13 @@ impl CustomMinecraftSkin {
|
|||||||
) -> crate::Result<impl Stream<Item = Self>> {
|
) -> crate::Result<impl Stream<Item = Self>> {
|
||||||
let minecraft_user_id = minecraft_user_id.as_hyphenated();
|
let minecraft_user_id = minecraft_user_id.as_hyphenated();
|
||||||
|
|
||||||
Ok(stream::iter(sqlx::query_as!(
|
Ok(stream::iter(sqlx::query!(
|
||||||
CustomMinecraftSkinRow,
|
|
||||||
"SELECT texture_key, variant AS 'variant: MinecraftSkinVariant', cape_id AS 'cape_id: Hyphenated' \
|
"SELECT texture_key, variant AS 'variant: MinecraftSkinVariant', cape_id AS 'cape_id: Hyphenated' \
|
||||||
FROM custom_minecraft_skins \
|
FROM custom_minecraft_skins \
|
||||||
WHERE minecraft_user_uuid = ? \
|
WHERE minecraft_user_uuid = ? \
|
||||||
ORDER BY display_order ASC, rowid ASC \
|
ORDER BY rowid ASC \
|
||||||
LIMIT ? OFFSET ?",
|
LIMIT ? OFFSET ?",
|
||||||
minecraft_user_id,
|
minecraft_user_id, count, offset
|
||||||
count,
|
|
||||||
offset
|
|
||||||
)
|
)
|
||||||
.fetch_all(&mut *db.acquire().await?)
|
.fetch_all(&mut *db.acquire().await?)
|
||||||
.await?)
|
.await?)
|
||||||
@@ -181,32 +161,4 @@ impl CustomMinecraftSkin {
|
|||||||
|
|
||||||
Ok(())
|
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