GLM-5.3 Flash · 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 Opus 5.5.

Gold bugs found
3 of 13
Comments published
4
Comments matching a bug
3
Comments not matching
1
Cost of this PR
$0.090

Published comments (4)

highbugapp/controllers/admin/groups_controller.rb:65-81

add_members raises RecordNotUnique when re-adding an existing member — Resubmitting a username that is already a member hits the unique index on `group_users` and raises an unhandled `ActiveRecord::RecordNotUnique`, a likely case since the admin UI never clears its input (admin-group.js.es6). Skip users already in the group before calling `Group#add`, or reuse the diff-based `usernames=` setter.

mediumbugapp/assets/javascripts/admin/templates/group_member.hbs:1

`automatic` resolves against member, remove link shows for automatic groups — Clicking the remove link sends a DELETE the server rejects with 422 `can_not_modify_automatic`, and `Group#removeMember` swallows the failure with only `.then`. Bind the group's `automatic` flag into the item view instead of the bare `automatic` lookup.

mediumbugapp/controllers/groups_controller.rb:22-23

Unbounded limit/offset params cause 500 errors and empty results in members — `?limit=-1` or `?offset=-5` make PostgreSQL reject the query with a 500, and `limit=0` returns an empty member list — previously only automatic groups accepted these params, so this now hits every group. Clamp `limit` and floor `offset` at 0.

mediumbugapp/controllers/admin/groups_controller.rb:84-96

remove_member bypasses GroupUser destroy, leaving user_count stale — The raw `group.users.delete` bypasses `GroupUser#destroy`, so the counter cache never decrements and the stored `user_count` shown by `BasicGroupSerializer` and admin pagination stays too high for custom groups. Use `Group#remove`, which destroys the `GroupUser` rows and lets the counter cache update.

Gold bugs found (3)

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 (10)

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.
mediumCreating a group ignores the alias_level the admin selected, saving only name and visibility, so new groups keep the default alias level.
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.
mediumGroupsController#members now returns {members, meta} with pagination, but the public groups controller spec still expects a bare array and stays pending, so the new response contract is untested.
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.
mediumGroup#removeMember and #addMembers have no rejection handler and the admin controller discards their promises, so failed member additions/removals are silently swallowed with no error shown to the admin.
mediumAfter removeMember succeeds, findMembers reloads with the old offset, clamped only by the stale user_count; removing the only member on the last page (e.g. 51 members, offset 50) requests offset equal to the new total and the admin sees an empty, out-of-range page.