Skip to content

Commit

Permalink
INTR-509: Get user's reaction for a given page (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
smanga24 authored Jan 22, 2025
1 parent 78fb0f8 commit cfc9b4b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/interactions/services/reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand All @@ -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 {}

Expand All @@ -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
11 changes: 11 additions & 0 deletions src/interactions/test/services/test_reactions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
react_to_page,
get_reaction_count,
get_reaction_counts,
get_user_reaction,
)

ALL_REACTION_TYPES = ReactionType.values
Expand Down Expand Up @@ -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

0 comments on commit cfc9b4b

Please sign in to comment.