@@ -146,6 +146,8 @@ def get_report_by_id(self, report_id: str) -> Optional[Dict[str, Any]]:
146146 report ['_id' ] = str (report ['_id' ])
147147 if 'reported_message_id' in report and report ['reported_message_id' ]:
148148 report ['reported_message_id' ] = str (report ['reported_message_id' ])
149+ if 'reported_content_id' in report and report ['reported_content_id' ]:
150+ report ['reported_content_id' ] = str (report ['reported_content_id' ])
149151 report ['reported_by' ] = str (report ['reported_by' ])
150152 if report ['reviewed_by' ]:
151153 report ['reviewed_by' ] = str (report ['reviewed_by' ])
@@ -160,12 +162,22 @@ def get_pending_reports(self, topic_id: Optional[str] = None, limit: int = 50, o
160162 query = {'status' : 'pending' }
161163
162164 if topic_id :
163- # Filter by reports from specific topic
165+ # Filter by topic_id field OR messages in that topic (legacy support)
166+ topic_conditions = [{'topic_id' : ObjectId (topic_id )}]
167+
168+ # Legacy: find messages in topic
164169 from .message import Message
165170 message_model = Message (self .db )
166171 topic_message_ids = [ObjectId (msg ['_id' ]) for msg in
167172 self .db .messages .find ({'topic_id' : ObjectId (topic_id )}, {'_id' : 1 })]
168- query ['reported_message_id' ] = {'$in' : topic_message_ids }
173+
174+ if topic_message_ids :
175+ topic_conditions .append ({'reported_message_id' : {'$in' : topic_message_ids }})
176+
177+ if len (topic_conditions ) > 1 :
178+ query ['$or' ] = topic_conditions
179+ else :
180+ query .update (topic_conditions [0 ])
169181
170182 reports = list (self .collection .find (query )
171183 .sort ([('created_at' , - 1 )])
@@ -174,7 +186,10 @@ def get_pending_reports(self, topic_id: Optional[str] = None, limit: int = 50, o
174186
175187 for report in reports :
176188 report ['_id' ] = str (report ['_id' ])
177- report ['reported_message_id' ] = str (report ['reported_message_id' ])
189+ if 'reported_message_id' in report and report ['reported_message_id' ]:
190+ report ['reported_message_id' ] = str (report ['reported_message_id' ])
191+ if 'reported_content_id' in report and report ['reported_content_id' ]:
192+ report ['reported_content_id' ] = str (report ['reported_content_id' ])
178193 report ['reported_by' ] = str (report ['reported_by' ])
179194
180195 # Enrich with details
@@ -204,12 +219,14 @@ def _enrich_report_with_details(self, report: Dict[str, Any]) -> None:
204219 if reported_message :
205220 report ['reported_content' ] = reported_message
206221 elif report .get ('content_type' ) == 'post' :
222+ # Fetch reported post details
207223 from .post import Post
208224 post_model = Post (self .db )
209225 reported_post = post_model .get_post_by_id (content_id )
210226 if reported_post :
211227 report ['reported_content' ] = reported_post
212228 elif report .get ('content_type' ) == 'comment' :
229+ # Fetch reported comment details
213230 from .comment import Comment
214231 comment_model = Comment (self .db )
215232 reported_comment = comment_model .get_comment_by_id (content_id )
@@ -382,19 +399,30 @@ def get_user_reports(self, user_id: str, as_reporter: bool = True) -> List[Dict[
382399 if as_reporter :
383400 query = {'reported_by' : ObjectId (user_id )}
384401 else :
385- # User who was reported (get messages they sent that were reported)
402+ # User who was reported:
403+ # 1. Directly reported via reported_user_id
404+ # 2. Their messages were reported (legacy/fallback)
386405 from .message import Message
387406 message_model = Message (self .db )
388407 user_message_ids = [ObjectId (msg ['_id' ]) for msg in
389408 self .db .messages .find ({'user_id' : ObjectId (user_id )}, {'_id' : 1 })]
390- query = {'reported_message_id' : {'$in' : user_message_ids }}
409+
410+ query = {
411+ '$or' : [
412+ {'reported_user_id' : ObjectId (user_id )},
413+ {'reported_message_id' : {'$in' : user_message_ids }}
414+ ]
415+ }
391416
392417 reports = list (self .collection .find (query )
393418 .sort ([('created_at' , - 1 )]))
394419
395420 for report in reports :
396421 report ['_id' ] = str (report ['_id' ])
397- report ['reported_message_id' ] = str (report ['reported_message_id' ])
422+ if 'reported_message_id' in report and report ['reported_message_id' ]:
423+ report ['reported_message_id' ] = str (report ['reported_message_id' ])
424+ if 'reported_content_id' in report and report ['reported_content_id' ]:
425+ report ['reported_content_id' ] = str (report ['reported_content_id' ])
398426 report ['reported_by' ] = str (report ['reported_by' ])
399427 if report ['reviewed_by' ]:
400428 report ['reviewed_by' ] = str (report ['reviewed_by' ])
@@ -452,19 +480,33 @@ def get_recent_reports(self, limit: int = 10, topic_id: Optional[str] = None) ->
452480 """Get recent reports for dashboard."""
453481 query = {}
454482 if topic_id :
483+ # Filter by topic_id field OR messages in that topic (legacy support)
484+ topic_conditions = [{'topic_id' : ObjectId (topic_id )}]
485+
486+ # Legacy: find messages in topic
455487 from .message import Message
456488 message_model = Message (self .db )
457489 topic_message_ids = [ObjectId (msg ['_id' ]) for msg in
458490 self .db .messages .find ({'topic_id' : ObjectId (topic_id )}, {'_id' : 1 })]
459- query ['reported_message_id' ] = {'$in' : topic_message_ids }
491+
492+ if topic_message_ids :
493+ topic_conditions .append ({'reported_message_id' : {'$in' : topic_message_ids }})
494+
495+ if len (topic_conditions ) > 1 :
496+ query ['$or' ] = topic_conditions
497+ else :
498+ query .update (topic_conditions [0 ])
460499
461500 reports = list (self .collection .find (query )
462501 .sort ([('created_at' , - 1 )])
463502 .limit (limit ))
464503
465504 for report in reports :
466505 report ['_id' ] = str (report ['_id' ])
467- report ['reported_message_id' ] = str (report ['reported_message_id' ])
506+ if 'reported_message_id' in report and report ['reported_message_id' ]:
507+ report ['reported_message_id' ] = str (report ['reported_message_id' ])
508+ if 'reported_content_id' in report and report ['reported_content_id' ]:
509+ report ['reported_content_id' ] = str (report ['reported_content_id' ])
468510 report ['reported_by' ] = str (report ['reported_by' ])
469511 if report ['reviewed_by' ]:
470512 report ['reviewed_by' ] = str (report ['reviewed_by' ])
0 commit comments