Skip to content

fix: resolve read/unread state persistence issue#48

Merged
Alam-2U merged 1 commit intorelease-ulmofrom
COSMO2-904
May 7, 2026
Merged

fix: resolve read/unread state persistence issue#48
Alam-2U merged 1 commit intorelease-ulmofrom
COSMO2-904

Conversation

@Alam-2U
Copy link
Copy Markdown
Collaborator

@Alam-2U Alam-2U commented May 7, 2026

Description

This PR fixes an issue where discussion forum threads marked as read were incorrectly showing as unread after a page reload or tab switch.

Cause

Thread read state was being overwritten by requests that did not contain valid user context, causing already-read threads to appear unread.

Fix

Updated the discussion thread read-state handling to ignore requests without valid user context and preserve the correct read/unread status.

Ticket

COSMO2-904

Related PR

edx/frontend-app-discussions#30

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes incorrect persistence of thread read/unread state by preventing read-state calculation/return when the request lacks valid user context (avoids overwriting a previously-correct UI state on reload/tab switch).

Changes:

  • Thread serializer now returns None for read / unread_comments_count when user_id is missing and recalculates read state via backend when present.
  • MySQL backend get_read_states now treats user_id=None as “no user context” and returns an empty result.
  • Bumped package version to 0.5.9.

Reviewed changes

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

File Description
forum/serializers/thread.py Adjusts read/unread serialization to avoid using stale/invalid context and (currently) recomputes via backend.
forum/backends/mysql/api.py Treats missing user context (None) as non-computable for read states.
forum/init.py Version bump reflecting the fix.
Comments suppressed due to low confidence (2)

forum/serializers/thread.py:132

  • get_read() now calls backend.get_read_states() per thread, which can create an N+1 query pattern on list endpoints (and get_unread_comments_count() does a second call for the same thread). To avoid a significant performance regression, consider using the precomputed obj['read']/obj['unread_comments_count'] when user_id is present, and/or caching/bulk-fetching read states once for the serializer instead of querying inside each field method.
            course_id = obj["course_id"]
            thread_key = obj["_id"]
            is_read, _ = self.backend.get_read_states(
                [obj["_id"]], user_id, course_id
            ).get(thread_key, (False, obj["comment_count"]))

forum/serializers/thread.py:156

  • get_unread_comments_count() repeats the same get_read_states([obj['_id']], ...) lookup done in get_read(), doubling backend/DB work per thread. Consider reusing a single computed read-state result (e.g., memoize per thread in the serializer instance or attach it to obj) so read/unread count are derived from one backend call.
            course_id = obj["course_id"]
            thread_key = obj["_id"]
            _, unread_count = self.backend.get_read_states(
                [obj["_id"]], user_id, course_id
            ).get(thread_key, (False, obj["comment_count"]))

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

Comment thread forum/backends/mysql/api.py
Comment thread forum/serializers/thread.py
@Alam-2U Alam-2U merged commit b28fbc4 into release-ulmo May 7, 2026
17 checks passed
@Alam-2U Alam-2U deleted the COSMO2-904 branch May 7, 2026 10:20
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.

3 participants