From 31227b86414d228a9f73e86fda4cb050fae917c5 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Wed, 27 Mar 2019 12:13:25 +0100
Subject: [PATCH] Fix #772: Prevent skipping on file import if album_mbid is
 different

---
 api/funkwhale_api/music/tasks.py | 12 ++++++++----
 api/tests/music/test_tasks.py    | 23 +++++++++++++++++++++++
 changes/changelog.d/772.bugfix   |  1 +
 3 files changed, 32 insertions(+), 4 deletions(-)
 create mode 100644 changes/changelog.d/772.bugfix

diff --git a/api/funkwhale_api/music/tasks.py b/api/funkwhale_api/music/tasks.py
index 946d7a411f..e9c73273e4 100644
--- a/api/funkwhale_api/music/tasks.py
+++ b/api/funkwhale_api/music/tasks.py
@@ -431,9 +431,10 @@ def _get_track(data):
     artist_mbid = data.get("musicbrainz_artistid", None)
     artist_fid = data.get("artist_fid", None)
     artist_name = data["artist"]
-    query = Q(name__iexact=artist_name)
     if artist_mbid:
-        query |= Q(mbid=artist_mbid)
+        query = Q(mbid=artist_mbid)
+    else:
+        query = Q(name__iexact=artist_name)
     if artist_fid:
         query |= Q(fid=artist_fid)
     defaults = {
@@ -476,9 +477,12 @@ def _get_track(data):
     # get / create album
     album_title = data["album"]
     album_fid = data.get("album_fid", None)
-    query = Q(title__iexact=album_title, artist=album_artist)
+
     if album_mbid:
-        query |= Q(mbid=album_mbid)
+        query = Q(mbid=album_mbid)
+    else:
+        query = Q(title__iexact=album_title, artist=album_artist)
+
     if album_fid:
         query |= Q(fid=album_fid)
     defaults = {
diff --git a/api/tests/music/test_tasks.py b/api/tests/music/test_tasks.py
index ac62c31b86..76d820cca7 100644
--- a/api/tests/music/test_tasks.py
+++ b/api/tests/music/test_tasks.py
@@ -133,6 +133,29 @@ def test_can_create_track_from_file_metadata_fid_existing_album_artist(
     assert track.artist == artist
 
 
+def test_can_create_track_from_file_metadata_distinct_release_mbid(factories):
+    """Cf https://dev.funkwhale.audio/funkwhale/funkwhale/issues/772"""
+    artist = factories["music.Artist"]()
+    album = factories["music.Album"](artist=artist)
+    track = factories["music.Track"](album=album, artist=artist)
+    metadata = {
+        "artist": artist.name,
+        "album": album.title,
+        "title": track.title,
+        "track_number": 4,
+        "fid": "https://hello",
+        "musicbrainz_artistid": artist.mbid,
+        "musicbrainz_albumid": str(uuid.uuid4()),
+    }
+
+    new_track = tasks.get_track_from_import_metadata(metadata)
+
+    # the returned track should be different from the existing one, and mapped
+    # to a new album, because the albumid is different
+    assert new_track.album != album
+    assert new_track != track
+
+
 def test_can_create_track_from_file_metadata_federation(factories, mocker, r_mock):
     metadata = {
         "artist": "Artist",
diff --git a/changes/changelog.d/772.bugfix b/changes/changelog.d/772.bugfix
new file mode 100644
index 0000000000..5c0ddbeae0
--- /dev/null
+++ b/changes/changelog.d/772.bugfix
@@ -0,0 +1 @@
+Prevent skipping on file import if album_mbid is different (#772)
-- 
GitLab