From baf876b230e09e4aa2008a92cdb2753b204adaed Mon Sep 17 00:00:00 2001 From: Petitminion Date: Mon, 10 Feb 2025 23:42:36 +0100 Subject: [PATCH] filter content and various updates --- api/config/settings/common.py | 4 ++-- api/funkwhale_api/federation/routes.py | 3 --- api/funkwhale_api/moderation/filters.py | 12 ++++++++++++ .../music/management/commands/wikidata_far_right.py | 2 +- api/funkwhale_api/music/models.py | 1 + api/funkwhale_api/music/tasks.py | 6 +++--- docs/specs/far-right-filter/index.md | 13 ++++--------- 7 files changed, 23 insertions(+), 18 deletions(-) diff --git a/api/config/settings/common.py b/api/config/settings/common.py index 7afaa4b35..71727eec6 100644 --- a/api/config/settings/common.py +++ b/api/config/settings/common.py @@ -983,8 +983,8 @@ CELERY_BEAT_SCHEDULE = { "schedule": crontab(day_of_month="2", minute="30", hour="3"), "options": {"expires": 60 * 60 * 24}, }, - "music.wikidata_far_righ_artists": { - "task": "music.wikidata_far_righ_artists", + "music.wikidata_far_right_artists": { + "task": "music.wikidata_far_right_artists", "schedule": crontab(day_of_month="3", minute="30", hour="3"), "options": {"expires": 60 * 60 * 24}, }, diff --git a/api/funkwhale_api/federation/routes.py b/api/funkwhale_api/federation/routes.py index 441688fad..e6a9aa50a 100644 --- a/api/funkwhale_api/federation/routes.py +++ b/api/funkwhale_api/federation/routes.py @@ -679,9 +679,6 @@ def inbox_delete_favorite(payload, context): favorite.delete() -# to do : test listening routes and broadcast - - @outbox.register({"type": "Listen", "object.type": "Track"}) def outbox_create_listening(context): track = context["track"] diff --git a/api/funkwhale_api/moderation/filters.py b/api/funkwhale_api/moderation/filters.py index ce94186e2..24db8bffa 100644 --- a/api/funkwhale_api/moderation/filters.py +++ b/api/funkwhale_api/moderation/filters.py @@ -1,6 +1,9 @@ +from django.core.cache import cache from django.db.models import Q from django_filters import rest_framework as filters +from funkwhale_api.music.models import Artist + USER_FILTER_CONFIG = { "ARTIST": {"target_artist": ["pk"]}, "CHANNEL": {"target_artist": ["artist__pk"]}, @@ -28,9 +31,18 @@ USER_FILTER_CONFIG = { def get_filtered_content_query(config, user): final_query = None + cache_key = "far_right_artists" + far_right_artists_ids = cache.get(cache_key) + if far_right_artists_ids is None: + far_right_artists_ids = list( + Artist.objects.filter(far_right_isnull=False).values_list("pk", flat=True) + ) + cache.set(cache_key, far_right_artists_ids, timeout=3600) + for filter_field, model_fields in config.items(): query = None ids = user.content_filters.values_list(filter_field, flat=True) + ids.append(far_right_artists_ids) for model_field in model_fields: q = Q(**{f"{model_field}__in": ids}) if query: diff --git a/api/funkwhale_api/music/management/commands/wikidata_far_right.py b/api/funkwhale_api/music/management/commands/wikidata_far_right.py index a735cf3cb..7a047ae22 100644 --- a/api/funkwhale_api/music/management/commands/wikidata_far_right.py +++ b/api/funkwhale_api/music/management/commands/wikidata_far_right.py @@ -5,7 +5,7 @@ from funkwhale_api.music import wikidata class Command(BaseCommand): help = """Get the list of far right artists from wikidata and save it into db. - Display missing MBID and missing english labels. + Display missing MBID and missing labels. Be aware this is querying wikidata you could be rate limited. """ diff --git a/api/funkwhale_api/music/models.py b/api/funkwhale_api/music/models.py index a4c3b9561..53a33bdf9 100644 --- a/api/funkwhale_api/music/models.py +++ b/api/funkwhale_api/music/models.py @@ -247,6 +247,7 @@ class Artist(APIModelMixin): ) modification_date = models.DateTimeField(default=timezone.now, db_index=True) api = musicbrainz.api.artists + # a wikidata object reference if the artist is known to be far right far_right = models.CharField(max_length=100, null=True, blank=True) objects = ArtistQuerySet.as_manager() diff --git a/api/funkwhale_api/music/tasks.py b/api/funkwhale_api/music/tasks.py index 7f0082822..7e5111678 100644 --- a/api/funkwhale_api/music/tasks.py +++ b/api/funkwhale_api/music/tasks.py @@ -819,7 +819,7 @@ def get_or_create_artist_from_ac(ac_data, attributed_to, from_activity_id): if artist.far_right: raise FarRightError( code="Far right artist detected", - detail=f"The artist name has been matched with this wikidata entity \ + detail=f"The artist has been matched with this wikidata entity \ {artist.far_right}. This artist will not be saved. No pasaran. \ You can checkout our coc at https://www.funkwhale.audio/code-of-conduct/", ) @@ -1269,6 +1269,6 @@ def fs_import( command.handle(**options) -@celery.app.task(name="music.wikidata_far_righ_artists") -def wikidata_far_righ_artists(): +@celery.app.task(name="music.wikidata_far_right_artists") +def wikidata_far_right_artists(): wikidata.get_far_right_artists() diff --git a/docs/specs/far-right-filter/index.md b/docs/specs/far-right-filter/index.md index 5f205ac32..7bfd33237 100644 --- a/docs/specs/far-right-filter/index.md +++ b/docs/specs/far-right-filter/index.md @@ -21,9 +21,10 @@ To find a common consensus/definition of far right ideology we will use wikidata - [x] a celery tasks that add far_right artists to the db, or update existing artist with the far_right attribute if the name matches. Launched every month. - [x] a cli tool to display the list of right wing artists that display the name, the mbid and the wikidata ref. Prompt a warning if mbid is missing so admins can add a mbid `docker compose run --rm api funkwhale-manage wikidata_far_right` - [x] A new artist attribute `far_right` displaying the wikidata id -- [ ] Display an explicit api error response that explain why the artist in banned (link the funkwhale code of conduct and the artist wikidata id): +- [x] Display an explicit api error response that explain why the artist in banned (link the funkwhale code of conduct and the artist wikidata id): - [x] on the import process : display an explicit error during import - - [ ] on the federation artist serializers + - ~~[ ] on the federation artist serializers~~ useless since we have the list locally +- [x] a filter to avoid displaying the artists and related tracks. Done in `get_filtered_content_query` workflow : querying wikidata -> create or update artist entries with the new `far_right` attribute -> filter out the artist based on the attribute and display logging info explaining why @@ -60,10 +61,4 @@ SELECT DISTINCT ?item ?itemLabel WHERE { #### Wikidata requirements - To get the musibrainz id of the artist we use : https://www.wikidata.org/wiki/P434 -- To get the artist name we use the english label - -#### Import - -get_or_create_artists_credits_from_musicbrainz - -### Frontend behavior +- To get the artist name we use the wikidata label