validate hifi instance reorder against pre-existing instances
This commit is contained in:
parent
ef3790d146
commit
7f94597706
3 changed files with 19 additions and 5 deletions
|
|
@ -11649,9 +11649,22 @@ class MusicDatabase:
|
||||||
return cursor.rowcount > 0
|
return cursor.rowcount > 0
|
||||||
|
|
||||||
def reorder_hifi_instances(self, urls: List[str]) -> bool:
|
def reorder_hifi_instances(self, urls: List[str]) -> bool:
|
||||||
"""Update priorities based on the given URL order."""
|
"""Update priorities based on the given URL order.
|
||||||
|
Returns False if any URL does not exist in the database.
|
||||||
|
"""
|
||||||
|
if not urls:
|
||||||
|
return True
|
||||||
conn = self._get_connection()
|
conn = self._get_connection()
|
||||||
cursor = conn.cursor()
|
cursor = conn.cursor()
|
||||||
|
placeholders = ",".join("?" for _ in urls)
|
||||||
|
cursor.execute(
|
||||||
|
f"SELECT url FROM hifi_instances WHERE url IN ({placeholders})",
|
||||||
|
urls
|
||||||
|
)
|
||||||
|
existing = {row["url"] for row in cursor.fetchall()}
|
||||||
|
missing = [u for u in urls if u not in existing]
|
||||||
|
if missing:
|
||||||
|
return False
|
||||||
for i, url in enumerate(urls):
|
for i, url in enumerate(urls):
|
||||||
cursor.execute("UPDATE hifi_instances SET priority = ? WHERE url = ?", (i, url))
|
cursor.execute("UPDATE hifi_instances SET priority = ? WHERE url = ?", (i, url))
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
|
||||||
|
|
@ -274,10 +274,10 @@ def test_reorder_hifi_instances_returns_true_on_empty_list(db):
|
||||||
assert db.reorder_hifi_instances([]) is True
|
assert db.reorder_hifi_instances([]) is True
|
||||||
|
|
||||||
|
|
||||||
def test_reorder_hifi_instances_returns_true_even_with_unknown_urls(db):
|
def test_reorder_hifi_instances_returns_false_with_unknown_urls(db):
|
||||||
"""UPDATE that matches 0 rows is not an error."""
|
"""Reorder should fail when any URL doesn't exist."""
|
||||||
_seed(db, instances=[("http://a.com", 0, 1)])
|
_seed(db, instances=[("http://a.com", 0, 1)])
|
||||||
assert db.reorder_hifi_instances(["http://a.com", "http://phantom.com"]) is True
|
assert db.reorder_hifi_instances(["http://a.com", "http://phantom.com"]) is False
|
||||||
|
|
||||||
|
|
||||||
# ── seed_hifi_instances ───────────────────────────────────────────────────
|
# ── seed_hifi_instances ───────────────────────────────────────────────────
|
||||||
|
|
|
||||||
|
|
@ -24782,7 +24782,8 @@ def hifi_reorder_instances():
|
||||||
return jsonify({'success': False, 'error': 'URL list is required'}), 400
|
return jsonify({'success': False, 'error': 'URL list is required'}), 400
|
||||||
from database.music_database import get_database
|
from database.music_database import get_database
|
||||||
db = get_database()
|
db = get_database()
|
||||||
db.reorder_hifi_instances(urls)
|
if not db.reorder_hifi_instances(urls):
|
||||||
|
return jsonify({'success': False, 'error': 'One or more URLs not found'}), 400
|
||||||
# Reload the client
|
# Reload the client
|
||||||
if soulseek_client and hasattr(soulseek_client, 'hifi') and soulseek_client.hifi:
|
if soulseek_client and hasattr(soulseek_client, 'hifi') and soulseek_client.hifi:
|
||||||
soulseek_client.hifi.reload_instances()
|
soulseek_client.hifi.reload_instances()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue