Skip to content

Commit 68dc8b0

Browse files
authored
Merge pull request #32 from 8JP8/add-report-support-post-comment-15231206498711219601
Add report support for Posts and Comments and fix report creation bugs
2 parents 0e04f73 + df25173 commit 68dc8b0

4 files changed

Lines changed: 79 additions & 11 deletions

File tree

backend/models/comment.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,14 @@ def report_comment(self, comment_id: str, reporter_id: str, reason: str) -> bool
555555

556556
if post and post.get('topic_id'):
557557
report_model = Report(self.db)
558-
report_model.create_report(comment_id, reporter_id, reason, post['topic_id'], 'comment')
558+
report_model.create_report(
559+
reporter_id=reporter_id,
560+
reason=reason,
561+
content_type='comment',
562+
content_id=comment_id,
563+
topic_id=str(post['topic_id']),
564+
reported_user_id=comment.get('user_id')
565+
)
559566

560567
return result.modified_count > 0
561568

backend/models/message.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,19 @@ def report_message(self, message_id: str, reporter_id: str, reason: str) -> bool
394394
# Create report entry in reports collection
395395
from .report import Report
396396
report_model = Report(self.db)
397-
report_model.create_report(message_id, reporter_id, reason, message['topic_id'])
397+
398+
topic_id = message.get('topic_id')
399+
if topic_id:
400+
topic_id = str(topic_id)
401+
402+
report_model.create_report(
403+
reporter_id=reporter_id,
404+
reason=reason,
405+
content_type='message',
406+
content_id=message_id,
407+
topic_id=topic_id,
408+
reported_user_id=message.get('user_id')
409+
)
398410

399411
return result.modified_count > 0
400412

backend/models/post.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,14 @@ def report_post(self, post_id: str, reporter_id: str, reason: str) -> bool:
750750
report_model = Report(self.db)
751751
topic_id = post.get('topic_id')
752752
if topic_id:
753-
report_model.create_report(post_id, reporter_id, reason, topic_id, 'post')
753+
report_model.create_report(
754+
reporter_id=reporter_id,
755+
reason=reason,
756+
content_type='post',
757+
content_id=post_id,
758+
topic_id=str(topic_id),
759+
reported_user_id=post.get('user_id')
760+
)
754761

755762
return result.modified_count > 0
756763

backend/models/report.py

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)