From 0c2be3e576951c794650fb30c788ebf9daf52824 Mon Sep 17 00:00:00 2001 From: Petitminion Date: Mon, 17 Mar 2025 21:46:23 +0100 Subject: [PATCH] make sure upload get moove to built-in libs --- .../0061_migrate_libraries_to_playlist.py | 44 +++++++++++++++++++ .../playlist-library-federation/index.md | 7 ++- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/api/funkwhale_api/music/migrations/0061_migrate_libraries_to_playlist.py b/api/funkwhale_api/music/migrations/0061_migrate_libraries_to_playlist.py index c6bde5ef9..d3443eea8 100644 --- a/api/funkwhale_api/music/migrations/0061_migrate_libraries_to_playlist.py +++ b/api/funkwhale_api/music/migrations/0061_migrate_libraries_to_playlist.py @@ -38,6 +38,9 @@ def migrate_libraries_to_playlist(apps, schema_editor): Actor = apps.get_model("federation", "Actor") Channel = apps.get_model("audio", "Channel") + to_instance_libs = [] + to_public_libs = [] + to_me_libs = [] for library in Library.objects.all(): if ( not federation_utils.is_local(library.actor.fid) @@ -86,6 +89,15 @@ def migrate_libraries_to_playlist(apps, schema_editor): with transaction.atomic(): insert_tracks_to_playlist(apps, playlist, uploads) + if library.privacy_level == "me": + to_me_libs.append(library) + if library.privacy_level == "instance": + to_instance_libs.append(library) + if library.privacy_level == "everyone": + to_public_libs.append(library) + + library.privacy_level = "me" + library.save() except Exception as e: print(f"An error occurred during library.playlist creation : {e}") continue @@ -113,6 +125,35 @@ def migrate_libraries_to_playlist(apps, schema_editor): library.uploads.all().update(library=build_in_lib) library.delete + if privacy_level == "everyone": + for lib in to_public_libs: + lib.uploads.all().update(library=build_in_lib) + if privacy_level == "instance": + for lib in to_instance_libs: + lib.uploads.all().update(library=build_in_lib) + if privacy_level == "me": + for lib in to_me_libs: + lib.uploads.all().update(library=build_in_lib) + + +def check_succefull_migration(apps, schema_editor): + Actor = apps.get_model("federation", "Actor") + Playlist = apps.get_model("playlists", "Playlist") + + for actor in Actor.objects.all(): + not_build_in_libs = len(actor.playlists.all()) + len( + actor.libraries.filter(channel__isnull=False) + ) + if len(actor.libraries.all()) - 2 != not_build_in_libs: + raise Exception( + "Incoherent library database state, check for errors in log and share them to the funkwhale team. Migration was abordted to prevent data loss" + ) + for playlist in Playlist.objects.all(): + if playlist.library.privacy_level != "me": + raise Exception( + "Incoherent playlist database state, check for errors in log and share them to the funkwhale team. Migration was abordted to prevent data loss" + ) + class Migration(migrations.Migration): dependencies = [ @@ -132,4 +173,7 @@ class Migration(migrations.Migration): migrations.RunPython( migrate_libraries_to_playlist, reverse_code=migrations.RunPython.noop ), + migrations.RunPython( + check_succefull_migration, reverse_code=migrations.RunPython.noop + ), ] diff --git a/docs/specs/playlist-library-federation/index.md b/docs/specs/playlist-library-federation/index.md index 1959f2b85..07b8ce011 100644 --- a/docs/specs/playlist-library-federation/index.md +++ b/docs/specs/playlist-library-federation/index.md @@ -46,13 +46,11 @@ There is no other reason to share the playlit.library to remote. - [x] library and playlist scan delay are long (24h), force on ap update - [x] make sure only owned upload are added to the playlist.library - [x] update the "drop library" migrations to use the playlist.library instead of user follow -- [ ] make sure user get the new libraries created after library drop +- [x] make sure user get the new libraries created after library drop - [x] update the federation api : when we receive a fetch for a library the upload serializer need to know which lib (playlist lib or user lib) - [x] Support library.playlist_uploads in library scan -> add playlist_uploads in items in library federation viewset - [x] investigate library scan bug : don't delete old content of the lib (local cache?): we need to empty the playlist before the scan(not ideal but less work) -- [ ] if the user change the upload to another built-in lib, make sure the upload is not delete (we would loose the playlist_library relation) but only updated. -- [ ] enforce actor.libraries to only have three entries. -- [ ] enforce upload.playlist_libraries to always be private +- [x] check actor has only have three built-in libs and upload.playlist_libraries is private after migration ### Follow up @@ -62,3 +60,4 @@ There is no other reason to share the playlit.library to remote. - [ ] Playlist discovery : add the playlist to my playlist collection = follow request to playlist - [ ] Playlist Track activity (to avoid having to refetch the whole playlist) - [ ] Document : The user that want to federate need to activate remote activities in it's user settings. Even if the library is public the playlist activities will not be sended to remote -> We need to implement a followers activity setting (#2362) +- [ ] allow users to change the upload to another built-in lib, make sure the upload is not delete (we would loose the playlist_library relation) but only updated