Skip to content
Draft
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
55 changes: 33 additions & 22 deletions frappe_comment_xt/helpers/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,42 @@ def filter_comments_by_visibility(comments, user):
Filter comments based on the visibility settings
Only show comments that the user is allowed to see
"""
if user == "Administrator":
return comments

filtered_comments = []

if user != "Administrator":
for comment in comments:
if comment.custom_visibility == "Visible to only you":
if comment.owner == user:
filtered_comments.append(comment)

elif comment.custom_visibility == "Visible to mentioned":
member = frappe.db.get_all(
"User Group Member",
filters={
"user": user,
"parent": comment.name,
"parenttype": "Comment",
},
)

if comment.owner == user or (len(member) > 0):
filtered_comments.append(comment)

else:
# Collect comment names that need mentioned-user check
mentioned_visibility_comments = [c.name for c in comments if c.custom_visibility == "Visible to mentioned"]

# Batch fetch all User Group Member records for this user
user_mentioned_in = set()
if mentioned_visibility_comments:
members = frappe.db.get_all(
"User Group Member",
filters={
"user": user,
"parent": ["in", mentioned_visibility_comments],
"parenttype": "Comment",
},
fields=["parent"],
)
user_mentioned_in = {m.parent for m in members}

# Now filter without additional queries
for comment in comments:
if comment.custom_visibility == "Visible to only you":
if comment.owner == user:
filtered_comments.append(comment)

elif comment.custom_visibility == "Visible to mentioned":
# O(1) lookup instead of query
if comment.owner == user or comment.name in user_mentioned_in:
filtered_comments.append(comment)
else:
filtered_comments = comments

else:
filtered_comments.append(comment)

return filtered_comments


Expand Down
Loading