Skip to content

Fix mark next episode query to use JOIN instead of IN clause - #30

Merged
willtho89 merged 3 commits into
mainfrom
copilot/fix-mark-episode-as-watched
Jan 12, 2026
Merged

willtho89 merged 3 commits into
mainfrom
copilot/fix-mark-episode-as-watched

Conversation

Copilot AI commented Jan 12, 2026 •

Copy link
Copy Markdown
Contributor

The "Mark next episode as watched" button was incorrectly marking the first episode instead of the next unwatched episode (e.g., marking episode 1 when episodes 1-5 were watched, instead of episode 6).

Root Cause

The query fetching watched episodes used .in_() with a list comprehension instead of an explicit JOIN, inconsistent with the working pattern in _get_show_progress_bulk().

Changes

  • Query refactor in mark_watchlist_item_watched: Changed from .in_() filter to explicit JOIN matching _get_show_progress_bulk() pattern
  • Added filters: Now properly filters by show_media_item_id, air_date, and season_number to ensure only relevant episodes are considered

Before

select(WatchedItem.episode_item_id).where(
    WatchedItem.user_id == current_user.id,
    WatchedItem.media_item_id is None,
    WatchedItem.episode_item_id.in_([e.id for e in released_episodes]),
)

After

select(WatchedItem.episode_item_id)
.join(EpisodeItem, WatchedItem.episode_item_id == EpisodeItem.id)
.where(
    WatchedItem.user_id == current_user.id,
    WatchedItem.media_item_id.is_(None),
    EpisodeItem.show_media_item_id == media_item.id,
    EpisodeItem.air_date.is_not(None),
    EpisodeItem.air_date <= now_date,
    EpisodeItem.season_number > 0,
)
Original prompt

This section details on the original issue you should resolve

<issue_title>[bug] mark episode as watched adds fist episode</issue_title>
<issue_description>In Watchlist clicking on "Mark next episode as watched" leads to adding the first episode of a season as watched instead of the correct one. The progress calculation is correct: showing 5/10. but it does not mark episode 6 as watched instead this case</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot wasn't able to review any files in this pull request.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI and others added 2 commits January 12, 2026 19:50
Co-authored-by: willtho89 <4933503+willtho89@users.noreply.github.com>
Co-authored-by: willtho89 <4933503+willtho89@users.noreply.github.com>
@willtho89
willtho89 requested a review from Copilot January 12, 2026 19:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (1)

backend/tests/test_mark_next_episode_fix.py:1

  • This test file only contains documentation without actually testing the fix. Consider adding integration tests that verify the correct episode is marked as watched when multiple episodes exist, ensuring episode 6 is marked when 5 episodes are already watched rather than episode 1.
"""Test for the fix to mark next episode as watched query.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI changed the title [WIP] Fix bug marking next episode as watched Fix mark next episode query to use JOIN instead of IN clause Jan 12, 2026
Copilot AI requested a review from willtho89 January 12, 2026 19:54
@willtho89

willtho89 commented Jan 12, 2026 •

Copy link
Copy Markdown
Owner

This will likely not work with seasons, will it? Eh watched last episode of a season (S01e09), the next episode should be episode one of the next season (S02e01)
@copilot

@willtho89

Copy link
Copy Markdown
Owner

@copilot Check my comment

Copilot AI commented Jan 12, 2026 •

Copy link
Copy Markdown
Contributor Author

This will likely not work with seasons, will it? Eh watched last episode of a season (S01e09), the next episode should be episode one of the next season (S02e01)
@copilot

The implementation already handles seasons correctly. The query orders episodes by season_number, episode_number (line 748), so when iterating through released episodes (lines 774-777), it proceeds in sequence: S01E01, S01E02, ..., S01E09, S02E01, etc.

If you've watched S01E09 and S02E01 is released, the code will correctly identify S02E01 as the next unwatched episode. The loop finds the first episode in order that's not in the watched set, which naturally handles season transitions.

@copilot Check my comment

Checked and replied to your previous comment. The implementation correctly handles season transitions.

Copilot AI requested a review from willtho89 January 12, 2026 20:01
@willtho89
willtho89 marked this pull request as ready for review January 12, 2026 20:01
@willtho89
willtho89 merged commit 723c8e4 into main Jan 12, 2026
6 checks passed
@willtho89
willtho89 deleted the copilot/fix-mark-episode-as-watched branch January 13, 2026 09:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] mark episode as watched adds fist episode

3 participants