refactored api_posts() in app.py and added some debugging to trace issue 28
This commit is contained in:
72
app.py
72
app.py
@@ -432,55 +432,67 @@ def api_posts():
|
|||||||
cutoff_date = datetime.utcnow() - timedelta(days=time_filter_days)
|
cutoff_date = datetime.utcnow() - timedelta(days=time_filter_days)
|
||||||
time_cutoff = cutoff_date.timestamp()
|
time_cutoff = cutoff_date.timestamp()
|
||||||
|
|
||||||
# Collect raw posts for filtering
|
# ====================================================================
|
||||||
raw_posts = []
|
# START OF REFACTORED SECTION
|
||||||
for post_uuid, post_data in cached_posts.items():
|
# ====================================================================
|
||||||
# Apply time filter first if enabled
|
|
||||||
|
def _post_should_be_included(post_data):
|
||||||
|
"""Check if a post passes all pre-filterset criteria."""
|
||||||
|
# Apply time filter
|
||||||
if time_filter_enabled and time_cutoff:
|
if time_filter_enabled and time_cutoff:
|
||||||
post_timestamp = post_data.get('timestamp', 0)
|
if post_data.get('timestamp', 0) < time_cutoff:
|
||||||
if post_timestamp < time_cutoff:
|
return False
|
||||||
continue
|
|
||||||
# Apply community filter (before filterset)
|
# Apply community filter
|
||||||
if community and post_data.get('source', '').lower() != community.lower():
|
if community and post_data.get('source', '').lower() != community.lower():
|
||||||
continue
|
return False
|
||||||
|
|
||||||
# Apply platform filter (before filterset)
|
# Apply platform filter
|
||||||
if platform and post_data.get('platform', '').lower() != platform.lower():
|
if platform and post_data.get('platform', '').lower() != platform.lower():
|
||||||
continue
|
return False
|
||||||
|
|
||||||
# Apply user's community preferences (before filterset)
|
# Apply user's community preferences
|
||||||
if user_communities:
|
if user_communities:
|
||||||
post_source = post_data.get('source', '').lower()
|
post_source = post_data.get('source', '').lower()
|
||||||
post_platform = post_data.get('platform', '').lower()
|
post_platform = post_data.get('platform', '').lower()
|
||||||
|
if not any(
|
||||||
|
post_source == c or post_platform == c or c in post_source
|
||||||
|
for c in user_communities
|
||||||
|
):
|
||||||
|
# ====================================================================
|
||||||
|
# MODIFICATION: Add logging here
|
||||||
|
# ====================================================================
|
||||||
|
logger.error(
|
||||||
|
f"Post filtered out for user {current_user.id if current_user.is_authenticated else 'anonymous'}: "
|
||||||
|
f"Community mismatch. Platform='{post_platform}', Source='{post_source}', "
|
||||||
|
f"User Communities={user_communities}"
|
||||||
|
)
|
||||||
|
# ====================================================================
|
||||||
|
return False
|
||||||
|
|
||||||
# Check if this post matches any of the user's selected communities
|
# Apply search filter
|
||||||
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)
|
|
||||||
if search_query:
|
if search_query:
|
||||||
title = post_data.get('title', '').lower()
|
title = post_data.get('title', '').lower()
|
||||||
content = post_data.get('content', '').lower()
|
content = post_data.get('content', '').lower()
|
||||||
author = post_data.get('author', '').lower()
|
author = post_data.get('author', '').lower()
|
||||||
source = post_data.get('source', '').lower()
|
source = post_data.get('source', '').lower()
|
||||||
|
|
||||||
if not (search_query in title or
|
if not (search_query in title or
|
||||||
search_query in content or
|
search_query in content or
|
||||||
search_query in author or
|
search_query in author or
|
||||||
search_query in source):
|
search_query in source):
|
||||||
continue
|
return False
|
||||||
|
|
||||||
raw_posts.append(post_data)
|
return True
|
||||||
|
|
||||||
|
# Collect raw posts using a clean, declarative list comprehension
|
||||||
|
raw_posts = [
|
||||||
|
post_data for post_data in cached_posts.values()
|
||||||
|
if _post_should_be_included(post_data)
|
||||||
|
]
|
||||||
|
|
||||||
|
# ====================================================================
|
||||||
|
# END OF REFACTORED SECTION
|
||||||
|
# ====================================================================
|
||||||
|
|
||||||
# Apply filterset using FilterEngine
|
# Apply filterset using FilterEngine
|
||||||
filtered_posts = filter_engine.apply_filterset(raw_posts, filterset_name, use_cache=True)
|
filtered_posts = filter_engine.apply_filterset(raw_posts, filterset_name, use_cache=True)
|
||||||
|
|||||||
Reference in New Issue
Block a user