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
14 changes: 5 additions & 9 deletions engine/app/models/coplan/comment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,25 @@ def author
private

def first_comment_in_thread?
!comment_thread.comments.where("id < ?", id).exists?
# IDs are random UUIDs, not insertion-ordered, so we can't compare them
# with `id < ?`. after_create_commit guarantees the row is persisted, so
# a total count of 1 reliably means this comment opened the thread.
comment_thread.comments.count == 1
end

def notify_plan_author
CoPlan::NotificationJob.perform_later("comment_created", { comment_thread_id: comment_thread_id })
end

def track_comment_created
# NOTE: We deliberately don't reuse `first_comment_in_thread?` here —
# that helper compares UUIDs with `id < ?`, which is not insertion-
# ordered. After-create-commit guarantees the row is persisted, so a
# total count of 1 is the reliable signal for "this comment opened
# the thread."
is_first = comment_thread.comments.count == 1

CoPlan::Analytics.track(
"comment_created",
user: author,
plan_id: comment_thread.plan_id,
comment_thread_id: comment_thread_id,
comment_id: id,
author_type: author_type,
is_first_in_thread: is_first,
is_first_in_thread: first_comment_in_thread?,
body_length: body_markdown.to_s.length
)
end
Expand Down
15 changes: 15 additions & 0 deletions spec/models/comment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,20 @@
create(:comment, comment_thread: thread_record)
}.not_to have_enqueued_job(CoPlan::NotificationJob)
end

# Regression for COPLAN-30: `first_comment_in_thread?` used to compare
# UUIDs with `id < ?`, which is not insertion-ordered. A reply whose UUID
# sorts before the opener's was wrongly treated as the thread opener,
# firing a duplicate "new comment thread" notification for the plan author.
it "does not enqueue NotificationJob for a reply whose UUID sorts before the opener" do
high_id = "ffffffff-ffff-ffff-ffff-ffffffffffff"
low_id = "00000000-0000-0000-0000-000000000001"

create(:comment, comment_thread: thread_record, id: high_id)

expect {
create(:comment, comment_thread: thread_record, id: low_id)
}.not_to have_enqueued_job(CoPlan::NotificationJob)
end
end
end
Loading