Skip to:

Opened 7 years ago

Closed 7 years ago

Last modified 7 years ago

#5018 closed defect (bug) (fixed)

Update user meta field 'total_group_count' when user joins group

Reported by: mukkundthanki Owned by: boonebgorges
Milestone: 1.8 Priority: normal
Severity: normal Version: 1.7
Component: Groups Keywords:


In buddypress if user leaves group we update user meta 'total_group_count' field but it missed when user joins group.

And somehow my user count 'total_group_count' went in minus number. so, it is advisable to check

if ( groups_is_user_member( $user_id, $group_id ) )

before update count,

we have done it when user joins group.

Is it appropriate to prevent minus count ?? I have just advised.

Attachments (1)

5018.01.patch (2.7 KB) - added by r-a-y 7 years ago.

Download all attachments as: .zip

Change History (7)

#1 @boonebgorges
7 years ago

  • Milestone changed from Awaiting Review to 1.8

Very odd that we're not upgrading a user's total_group_count when they join. (We do when they accept an invitation or when the request is approved.) I guess it's never come up, because we don't really appear to use this value anywhere in BP, at least not where I can see.

So, yes, it appears that we should be doing so.

Also, everywhere where we change total_group_count, we do it by incrementing or decrementing the current count. This can cause problems like the one you describe. Instead, I think it makes more sense to actually tabulate the group count at that moment, by querying the group member table. It definitely takes more resources to do this, but joining/leaving groups is a pretty rare occurrence.

Moving to 1.8 for examination. If you have a patch for either of these two problems, please feel free to post it.

#2 @mukkundthanki
7 years ago

ya, we have this function 'groups_total_groups_for_user' which count groups for user we can make use of it to update it. and I think while we decrease this count simple check for greater than zero would work...

bp_update_user_meta( $user_id, 'total_group_count', (int) groups_total_groups_for_user( $user_id ) + 1 );

and for leave group

if( (int) groups_total_groups_for_user( $user_id ) > 0 )
        bp_update_user_meta( $user_id, 'total_group_count', (int) (int) groups_total_groups_for_user( $user_id ) - 1 );

if user's group count is already zero then we don't decrease it. It seems little funny but I think it could be happen by mistake, because we don't check for user membership in this function so, its necessary to avoid minus count.

I have not tested it though, but it seems working.

7 years ago

#3 @boonebgorges
7 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 7179:

Improvements to refreshing of total_group_count on group membership events

When group membership status is changed for a user, that user's
total_group_count meta must be updated. This changeset introduces a number of
improvements to the way this updating is done:

  • Update using an actual queried group count, rather than incrementing. This ensures that even if the count is off (due to a bug in BP or some other plugin), it will be corrected the next time a membership event is recorded.
  • Move the total_group_count refreshing further down the stack. Previously, the updating was happening at various depths: sometimes in the bp-groups-functions.php functions, sometimes in the database methods, and sometimes not at all. By moving the updates so that they directly follow the database queries, we reduce redundancy and ensure that the group count is updated in all cases

This changeset also introduces integration tests for the relevant groups_*
membership functions and total_group_count.

Fixes #5018

Props mukkundthanki, r-a-y for early patches

#4 @boonebgorges
7 years ago

In 7180:

Adds missing changes from previous changeset. See #5018

#5 @boonebgorges
7 years ago

In 7182:

Improvements to how group total_member_count is refreshed on membership events

  • Use an actual member count when refreshing, rather than incrementing
  • Centralize all of the updating in the database methods, rather than having the updates spread throughout the groups component

Also adds integration tests for total_member_count as relates to the groups_*

See #5018

#6 @boonebgorges
7 years ago

In 7183:

Better cache busting of user group counts.

See #5018

Props r-a-y

Note: See TracTickets for help on using tickets.