From 3aa41232db161ec8c87a926e161859725661d1ba Mon Sep 17 00:00:00 2001 From: Simon Date: Sun, 2 Feb 2025 16:30:43 +0700 Subject: [PATCH] fix progress inconsistency+ --- backend/common/src/watched.py | 6 +++++- backend/common/views.py | 2 +- backend/video/views.py | 9 ++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/backend/common/src/watched.py b/backend/common/src/watched.py index ad5a671a..d3d3b376 100644 --- a/backend/common/src/watched.py +++ b/backend/common/src/watched.py @@ -6,15 +6,17 @@ functionality: from datetime import datetime from common.src.es_connect import ElasticWrap +from common.src.ta_redis import RedisArchivist from common.src.urlparser import Parser class WatchState: """handle watched checkbox for videos and channels""" - def __init__(self, youtube_id, is_watched): + def __init__(self, youtube_id: str, is_watched: bool, user_id: int): self.youtube_id = youtube_id self.is_watched = is_watched + self.user_id = user_id self.stamp = int(datetime.now().timestamp()) self.pipeline = f"_ingest/pipeline/watch_{youtube_id}" @@ -50,6 +52,8 @@ class WatchState: } } response, status_code = ElasticWrap(path).post(data=data) + key = f"{self.user_id}:progress:{self.youtube_id}" + RedisArchivist().del_message(key) if status_code != 200: print(response) raise ValueError("failed to mark video as watched") diff --git a/backend/common/views.py b/backend/common/views.py index 20c12804..b7b477c9 100644 --- a/backend/common/views.py +++ b/backend/common/views.py @@ -75,7 +75,7 @@ class WatchedView(ApiBaseView): message = {"message": "missing id or is_watched"} return Response(message, status=400) - WatchState(youtube_id, is_watched).change() + WatchState(youtube_id, is_watched, request.user.id).change() return Response({"message": "success"}, status=200) diff --git a/backend/video/views.py b/backend/video/views.py index d601282d..0b3d93bb 100644 --- a/backend/video/views.py +++ b/backend/video/views.py @@ -126,10 +126,9 @@ class VideoProgressView(ApiBaseView): current_progress = self.response["data"]["player"] current_progress.update({"position": position, "youtube_id": video_id}) - watched = self._check_watched_state(video_id, current_progress) + watched = self._check_watched(request, video_id, current_progress) if watched: - redis_con.del_message(key) - expire = 360 + expire = 60 else: expire = False @@ -138,7 +137,7 @@ class VideoProgressView(ApiBaseView): return Response(current_progress) - def _check_watched_state(self, video_id, current_progress) -> bool: + def _check_watched(self, request, video_id, current_progress) -> bool: """check watched state""" if current_progress["watched"]: return True @@ -147,7 +146,7 @@ class VideoProgressView(ApiBaseView): current_progress["duration"], current_progress["position"] ) if watched: - WatchState(video_id, watched).change() + WatchState(video_id, watched, request.user.id).change() return watched