diff --git a/src/interactions/services/reactions.py b/src/interactions/services/reactions.py index 1282ae2f7..56986cebe 100644 --- a/src/interactions/services/reactions.py +++ b/src/interactions/services/reactions.py @@ -6,7 +6,7 @@ from user.models import User -def react_to_page(user: User, page: Page, reaction_type: str | None): +def react_to_page(user: User, page: Page, reaction_type: str | None) -> Reaction | None: if not isinstance(page, NewsPage): raise ValueError("The page must be a NewsPage.") @@ -25,13 +25,13 @@ def react_to_page(user: User, page: Page, reaction_type: str | None): return reaction -def get_reaction_count(page: Page): +def get_reaction_count(page: Page) -> int | None: if not isinstance(page, NewsPage): return None return Reaction.objects.filter(page=page).count() -def get_reaction_counts(page: Page): +def get_reaction_counts(page: Page) -> dict: if not isinstance(page, NewsPage): return {} @@ -44,3 +44,10 @@ def get_reaction_counts(page: Page): {reaction["type"]: reaction["count"] for reaction in reactions} ) return reaction_counts + + +def get_user_reaction(user: User, page: Page) -> ReactionType | None: + reaction = Reaction.objects.filter(user=user, page=page).first() + if reaction: + return reaction.type + return None diff --git a/src/interactions/test/services/test_reactions.py b/src/interactions/test/services/test_reactions.py index 3a87756fb..a10e20811 100644 --- a/src/interactions/test/services/test_reactions.py +++ b/src/interactions/test/services/test_reactions.py @@ -5,6 +5,7 @@ react_to_page, get_reaction_count, get_reaction_counts, + get_user_reaction, ) ALL_REACTION_TYPES = ReactionType.values @@ -100,3 +101,13 @@ def assert_counts(counts): @pytest.mark.django_db def test_get_reaction_counts_invalid_page(about_page): assert get_reaction_counts(about_page) == {} + + +@pytest.mark.django_db +def test_get_user_reaction(user, news_page, create_reaction): + assert get_user_reaction(user, news_page) == ReactionType.LIKE + + +@pytest.mark.django_db +def test_get_user_reaction_no_page_found(user, news_page): + assert get_user_reaction(user, news_page) is None