Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion backend/models/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,14 @@ def report_comment(self, comment_id: str, reporter_id: str, reason: str) -> bool

if post and post.get('topic_id'):
report_model = Report(self.db)
report_model.create_report(comment_id, reporter_id, reason, post['topic_id'], 'comment')
report_model.create_report(
reporter_id=reporter_id,
reason=reason,
content_type='comment',
content_id=comment_id,
topic_id=str(post['topic_id']),
reported_user_id=comment.get('user_id')
)

return result.modified_count > 0

Expand Down
14 changes: 13 additions & 1 deletion backend/models/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,19 @@ def report_message(self, message_id: str, reporter_id: str, reason: str) -> bool
# Create report entry in reports collection
from .report import Report
report_model = Report(self.db)
report_model.create_report(message_id, reporter_id, reason, message['topic_id'])

topic_id = message.get('topic_id')
if topic_id:
topic_id = str(topic_id)

report_model.create_report(
reporter_id=reporter_id,
reason=reason,
content_type='message',
content_id=message_id,
topic_id=topic_id,
reported_user_id=message.get('user_id')
)

return result.modified_count > 0

Expand Down
9 changes: 8 additions & 1 deletion backend/models/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,14 @@ def report_post(self, post_id: str, reporter_id: str, reason: str) -> bool:
report_model = Report(self.db)
topic_id = post.get('topic_id')
if topic_id:
report_model.create_report(post_id, reporter_id, reason, topic_id, 'post')
report_model.create_report(
reporter_id=reporter_id,
reason=reason,
content_type='post',
content_id=post_id,
topic_id=str(topic_id),
reported_user_id=post.get('user_id')
)

return result.modified_count > 0

Expand Down
58 changes: 50 additions & 8 deletions backend/models/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ def get_report_by_id(self, report_id: str) -> Optional[Dict[str, Any]]:
report['_id'] = str(report['_id'])
if 'reported_message_id' in report and report['reported_message_id']:
report['reported_message_id'] = str(report['reported_message_id'])
if 'reported_content_id' in report and report['reported_content_id']:
report['reported_content_id'] = str(report['reported_content_id'])
report['reported_by'] = str(report['reported_by'])
if report['reviewed_by']:
report['reviewed_by'] = str(report['reviewed_by'])
Expand All @@ -160,12 +162,22 @@ def get_pending_reports(self, topic_id: Optional[str] = None, limit: int = 50, o
query = {'status': 'pending'}

if topic_id:
# Filter by reports from specific topic
# Filter by topic_id field OR messages in that topic (legacy support)
topic_conditions = [{'topic_id': ObjectId(topic_id)}]

# Legacy: find messages in topic
from .message import Message
message_model = Message(self.db)
topic_message_ids = [ObjectId(msg['_id']) for msg in
self.db.messages.find({'topic_id': ObjectId(topic_id)}, {'_id': 1})]
query['reported_message_id'] = {'$in': topic_message_ids}

if topic_message_ids:
topic_conditions.append({'reported_message_id': {'$in': topic_message_ids}})

if len(topic_conditions) > 1:
query['$or'] = topic_conditions
else:
query.update(topic_conditions[0])

reports = list(self.collection.find(query)
.sort([('created_at', -1)])
Expand All @@ -174,7 +186,10 @@ def get_pending_reports(self, topic_id: Optional[str] = None, limit: int = 50, o

for report in reports:
report['_id'] = str(report['_id'])
report['reported_message_id'] = str(report['reported_message_id'])
if 'reported_message_id' in report and report['reported_message_id']:
report['reported_message_id'] = str(report['reported_message_id'])
if 'reported_content_id' in report and report['reported_content_id']:
report['reported_content_id'] = str(report['reported_content_id'])
report['reported_by'] = str(report['reported_by'])

# Enrich with details
Expand Down Expand Up @@ -204,12 +219,14 @@ def _enrich_report_with_details(self, report: Dict[str, Any]) -> None:
if reported_message:
report['reported_content'] = reported_message
elif report.get('content_type') == 'post':
# Fetch reported post details
from .post import Post
post_model = Post(self.db)
reported_post = post_model.get_post_by_id(content_id)
if reported_post:
report['reported_content'] = reported_post
elif report.get('content_type') == 'comment':
# Fetch reported comment details
from .comment import Comment
comment_model = Comment(self.db)
reported_comment = comment_model.get_comment_by_id(content_id)
Expand Down Expand Up @@ -382,19 +399,30 @@ def get_user_reports(self, user_id: str, as_reporter: bool = True) -> List[Dict[
if as_reporter:
query = {'reported_by': ObjectId(user_id)}
else:
# User who was reported (get messages they sent that were reported)
# User who was reported:
# 1. Directly reported via reported_user_id
# 2. Their messages were reported (legacy/fallback)
from .message import Message
message_model = Message(self.db)
user_message_ids = [ObjectId(msg['_id']) for msg in
self.db.messages.find({'user_id': ObjectId(user_id)}, {'_id': 1})]
query = {'reported_message_id': {'$in': user_message_ids}}

query = {
'$or': [
{'reported_user_id': ObjectId(user_id)},
{'reported_message_id': {'$in': user_message_ids}}
]
}

reports = list(self.collection.find(query)
.sort([('created_at', -1)]))

for report in reports:
report['_id'] = str(report['_id'])
report['reported_message_id'] = str(report['reported_message_id'])
if 'reported_message_id' in report and report['reported_message_id']:
report['reported_message_id'] = str(report['reported_message_id'])
if 'reported_content_id' in report and report['reported_content_id']:
report['reported_content_id'] = str(report['reported_content_id'])
report['reported_by'] = str(report['reported_by'])
if report['reviewed_by']:
report['reviewed_by'] = str(report['reviewed_by'])
Expand Down Expand Up @@ -452,19 +480,33 @@ def get_recent_reports(self, limit: int = 10, topic_id: Optional[str] = None) ->
"""Get recent reports for dashboard."""
query = {}
if topic_id:
# Filter by topic_id field OR messages in that topic (legacy support)
topic_conditions = [{'topic_id': ObjectId(topic_id)}]

# Legacy: find messages in topic
from .message import Message
message_model = Message(self.db)
topic_message_ids = [ObjectId(msg['_id']) for msg in
self.db.messages.find({'topic_id': ObjectId(topic_id)}, {'_id': 1})]
query['reported_message_id'] = {'$in': topic_message_ids}

if topic_message_ids:
topic_conditions.append({'reported_message_id': {'$in': topic_message_ids}})

if len(topic_conditions) > 1:
query['$or'] = topic_conditions
else:
query.update(topic_conditions[0])

reports = list(self.collection.find(query)
.sort([('created_at', -1)])
.limit(limit))

for report in reports:
report['_id'] = str(report['_id'])
report['reported_message_id'] = str(report['reported_message_id'])
if 'reported_message_id' in report and report['reported_message_id']:
report['reported_message_id'] = str(report['reported_message_id'])
if 'reported_content_id' in report and report['reported_content_id']:
report['reported_content_id'] = str(report['reported_content_id'])
report['reported_by'] = str(report['reported_by'])
if report['reviewed_by']:
report['reviewed_by'] = str(report['reviewed_by'])
Expand Down