From 2c08336e33bef7ff6f89fad08ae1cb4271a22b62 Mon Sep 17 00:00:00 2001 From: Colin Powell Date: Mon, 12 Feb 2024 11:59:15 -0500 Subject: [PATCH] [locations] Don't scrobble all the locations! --- vrobbler/apps/scrobbles/models.py | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/vrobbler/apps/scrobbles/models.py b/vrobbler/apps/scrobbles/models.py index 1ae4b91..ef850f7 100644 --- a/vrobbler/apps/scrobbles/models.py +++ b/vrobbler/apps/scrobbles/models.py @@ -802,6 +802,7 @@ class Scrobble(TimeStampedModel): def user_has_moved_locations( cls, location: GeoLocation, user_id: int ) -> bool: + moved_location = False scrobble = ( cls.objects.filter( media_type=cls.MediaType.GEO_LOCATION, user_id=user_id @@ -809,18 +810,30 @@ class Scrobble(TimeStampedModel): .order_by("-timestamp") .first() ) - moved_location = True - if scrobble and scrobble.media_obj != location: + if not scrobble: logger.info( - f"[scrobbling] New location {location} and last location {scrobble.media_obj} are different" + f"[scrobbling] No existing location scrobbles, {location} should be created" ) - past_scrobbles = Scrobble.objects.filter( - media_type="GeoLocation", - user_id=user_id, - ).order_by("-timestamp")[1:POINTS_FOR_MOVEMENT_HISTORY] - past_points = [s.media_obj for s in past_scrobbles] + moved_location = True - moved_location = location.has_moved(past_points) + else: + if scrobble.media_obj == location: + logger.info( + f"[scrobbling] New location {location} and last location {scrobble.media_obj} are the same" + ) + moved_location = False + + if scrobble.media_obj != location: + logger.info( + f"[scrobbling] New location {location} and last location {scrobble.media_obj} are different" + ) + past_scrobbles = Scrobble.objects.filter( + media_type="GeoLocation", + user_id=user_id, + ).order_by("-timestamp")[1:POINTS_FOR_MOVEMENT_HISTORY] + past_points = [s.media_obj for s in past_scrobbles] + + moved_location = location.has_moved(past_points) return moved_location def update(self, scrobble_data: dict) -> "Scrobble":