Fix community selection filtering in API posts endpoint
The issue was that user community preferences stored in settings weren't being applied when fetching posts. Added logic to filter posts based on user's selected communities from their settings. Fixes #24 ~claude 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
24
app.py
24
app.py
@@ -386,14 +386,17 @@ def api_posts():
|
|||||||
platform = request.args.get('platform', '')
|
platform = request.args.get('platform', '')
|
||||||
search_query = request.args.get('q', '').lower().strip()
|
search_query = request.args.get('q', '').lower().strip()
|
||||||
|
|
||||||
# Get user's filterset preference (or default to no_filter)
|
# Get user's filterset preference and community selections
|
||||||
filterset_name = 'no_filter'
|
filterset_name = 'no_filter'
|
||||||
|
user_communities = []
|
||||||
if current_user.is_authenticated:
|
if current_user.is_authenticated:
|
||||||
try:
|
try:
|
||||||
user_settings = json.loads(current_user.settings) if current_user.settings else {}
|
user_settings = json.loads(current_user.settings) if current_user.settings else {}
|
||||||
filterset_name = user_settings.get('filter_set', 'no_filter')
|
filterset_name = user_settings.get('filter_set', 'no_filter')
|
||||||
|
user_communities = user_settings.get('communities', [])
|
||||||
except:
|
except:
|
||||||
filterset_name = 'no_filter'
|
filterset_name = 'no_filter'
|
||||||
|
user_communities = []
|
||||||
|
|
||||||
# Use cached data for better performance
|
# Use cached data for better performance
|
||||||
cached_posts, cached_comments = _load_posts_cache()
|
cached_posts, cached_comments = _load_posts_cache()
|
||||||
@@ -409,6 +412,25 @@ def api_posts():
|
|||||||
if platform and post_data.get('platform', '').lower() != platform.lower():
|
if platform and post_data.get('platform', '').lower() != platform.lower():
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# Apply user's community preferences (before filterset)
|
||||||
|
if user_communities:
|
||||||
|
post_source = post_data.get('source', '').lower()
|
||||||
|
post_platform = post_data.get('platform', '').lower()
|
||||||
|
|
||||||
|
# Check if this post matches any of the user's selected communities
|
||||||
|
matches_community = False
|
||||||
|
for selected_community in user_communities:
|
||||||
|
selected_community = selected_community.lower()
|
||||||
|
# Match by exact source name or platform name
|
||||||
|
if (post_source == selected_community or
|
||||||
|
post_platform == selected_community or
|
||||||
|
selected_community in post_source):
|
||||||
|
matches_community = True
|
||||||
|
break
|
||||||
|
|
||||||
|
if not matches_community:
|
||||||
|
continue
|
||||||
|
|
||||||
# Apply search filter (before filterset)
|
# Apply search filter (before filterset)
|
||||||
if search_query:
|
if search_query:
|
||||||
title = post_data.get('title', '').lower()
|
title = post_data.get('title', '').lower()
|
||||||
|
|||||||
Reference in New Issue
Block a user