Kimi K3.1 Pro · Balanced

fix-proper-handling-of-group-memberships-discourse-cursor

Discourse · Ruby · L · 300–799 lines · 6 files

The normal review, with one generalist agent. Judge: Claude Haiku 5.5.

Gold bugs found
7 of 10
Comments published
9
Comments matching a bug
7
Comments not matching
2
Cost of this PR
$1.367

Published comments (9)

mediumbugapp/assets/javascripts/admin/controllers/admin-group.js.es6:11-14

totalPages adds a phantom page for exact multiples of the limit — Exact multiples of the limit expose an empty extra page; 100 members at 50 per page produce a third page with no members. In `totalPages`, replace `Math.floor(user_count / limit) + 1` with `Math.ceil(user_count / limit)` for nonzero counts.

mediumbugapp/controllers/admin/groups_controller.rb:71-74

Adding an existing group member can violate the unique membership index — Adding an existing member can hit the unique `(group_id, user_id)` index and raise `ActiveRecord::RecordNotUnique` instead of succeeding idempotently. Call `group.add` only when `group.users.include?(user)` is false, or use an idempotent membership insertion.

mediumbugapp/assets/javascripts/discourse/models/group.js:23

Member reload can request an out-of-range offset after deletion — Using stale `user_count` to clamp `offset` can request the reduced total after a deletion and render an empty page. Reconcile `offset` with `result.meta.total`, then retry the last valid page if the refreshed response has no members.

mediumbugapp/assets/javascripts/discourse/models/group.js:30-36

Stale findMembers responses can overwrite newer member pages — An earlier, slower `findMembers` response can overwrite a newer page after rapid navigation, replacing its members and metadata with stale data. Ignore stale responses, or serialize or cancel requests so only the latest page reaches `setProperties`.

mediumbugapp/controllers/groups_controller.rb:22-26

Public group pages only expose the first 50 members — Members after the first 50 become unreachable because the public group-members template has no pagination controls or follow-up `offset` requests. Add public pagination, or keep this view unpaginated by leaving `limit` unset unless it is explicitly requested.

mediumperformanceapp/controllers/groups_controller.rb:22

Unbounded limit parameter allows arbitrarily large member queries — An unbounded public `limit` lets one request load and serialize an arbitrarily large membership list, causing excessive database and response work. Clamp `params[:limit]` to a reasonable maximum, such as 100, before applying `limit` to the query.

mediumperformanceapp/controllers/admin/groups_controller.rb:71-74

Batch member addition performs O(N) lookups and membership writes — Large additions incur O(N) database round trips because every username triggers a separate `User.find_by_username` lookup and persisted `group.add` write. Bulk-fetch the requested users and insert their memberships in a batch.

mediumbugapp/controllers/admin/groups_controller.rb:90

Member removal bypasses counter-cache callbacks, leaving user_count inflated — `groups.user_count` remains inflated after removal because `group.users.delete(user_id)` uses `delete_all`, bypassing `GroupUser` counter-cache callbacks, and `group.save` does not recalculate it. Destroy the matching `GroupUser` records instead, or explicitly reset the counter.

mediumbugapp/controllers/admin/groups_controller.rb:90-92

Member removal leaves users with a stale primary group — Removed users can retain a `primary_group_id` for a group they no longer belong to because this path skips the cleanup previously handled by `Group#destroy_deletions`. Clear `primary_group_id` for the removed user whenever it points to this group.

Gold bugs found (7)

mediumThe admin group pager computes total pages as floor(user_count / limit) + 1, so when the member count is an exact multiple of the limit it shows an extra page and Next navigates to an empty member list.
mediumPaging actions fire overlapping member requests and each response unconditionally overwrites offset and members, so a stale response arriving last can revert the list to an earlier page.
highThe public members endpoint now always caps results at 50 per page, but the public group members page has no pagination, so groups with more than 50 members are silently truncated for users.
mediumRemoving a member deletes the membership row directly and skips clearing the user's primary_group_id, so a removed user can keep this group as their primary group.
mediumAdmin remove_member deletes memberships with group.users.delete, which skips GroupUser callbacks, so the cached groups.user_count is never decremented and stays inflated.
mediumAdmin add_members pushes every username into group.users without skipping existing members, so adding someone already in the group hits the unique group_users index, returns a 500 and leaves earlier users partially added.
mediumThe public group members endpoint takes `limit` from params with no upper bound or positivity check, letting a visitor request huge pages or trigger a 500 with a negative limit.

Gold bugs missed (3)

mediumCreating a group ignores the alias_level the admin selected, saving only name and visibility, so new groups keep the default alias level.
mediumGroup member pagination is off by one when user_count is a multiple of limit: Next moves to offset == user_count and shows an empty page.
mediumAdmin remove_member calls group.users.delete with a raw integer id, which raises RecordNotFound (404/500) when the user is not or no longer a member, e.g. on concurrent removal.