Skip to content

Commit

Permalink
WIP. Changed the way how memberCount is incremented or decremented. F…
Browse files Browse the repository at this point in the history
…ixes #12275 (#12308)

* Changed the way how memberCount is incremented or decremented.

* Updated the logic for incrementing/decrementing memberCount for parties and guilds.

* Fixed lint errors

* Added relevant comment. Changed the way how memberCount is updated to replace the usage of custom query.

* Fixed lint errors.

* Reverted changes owing to failing tests.

* Added relevant comments. Removed duplicate and unwanted code. Added await for async function call.

* Minor change due to lint error.

* Reverted changes for removing the party member when the menber leaves.
  • Loading branch information
JalanshMunshi authored Jul 3, 2020
1 parent af1d13d commit 453d60b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 20 deletions.
18 changes: 16 additions & 2 deletions website/server/controllers/api-v3/groups.js
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,14 @@ api.joinGroup = {
group.leader = user._id; // If new user is only member -> set as leader
}

group.memberCount += 1;
if (group.type === 'party') {
// For parties we count the number of members from the database to get the correct value.
// See #12275 on why this is necessary and only done for parties.
const currentMembers = await group.getMemberCount();
group.memberCount = currentMembers + 1;
} else {
group.memberCount += 1;
}

let promises = [group.save(), user.save()];

Expand Down Expand Up @@ -948,7 +955,14 @@ api.removeGroupMember = {
}

if (isInGroup) {
group.memberCount -= 1;
// For parties we count the number of members from the database to get the correct value.
// See #12275 on why this is necessary and only done for parties.
if (group.type === 'party') {
const currentMembers = await group.getMemberCount();
group.memberCount = currentMembers - 1;
} else {
group.memberCount -= 1;
}

if (group.quest && group.quest.leader === member._id) {
group.quest.key = undefined;
Expand Down
19 changes: 1 addition & 18 deletions website/server/models/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -1415,23 +1415,12 @@ schema.methods.leave = async function leaveGroup (user, keep = 'keep-all', keepC
} else {
members = await User.find({ 'party._id': group._id }).select('_id').exec();
}

_.remove(members, { _id: user._id });

if (members.length === 0) {
promises.push(group.remove());
return Promise.all(promises);
}
// otherwise If the leader is leaving
// (or if the leader previously left, and this wasn't accounted for)
} else if (group.leader === user._id) {
const query = group.type === 'party' ? { 'party._id': group._id } : { guilds: group._id };
query._id = { $ne: user._id };
const seniorMember = await User.findOne(query).select('_id').exec();

// could be missing in case of public guild (that can have 0 members)
// with 1 member who is leaving
if (seniorMember) update.$set = { leader: seniorMember._id };
}
// otherwise If the leader is leaving
// (or if the leader previously left, and this wasn't accounted for)
Expand Down Expand Up @@ -1720,13 +1709,7 @@ schema.methods.hasCancelled = function hasCancelled () {

schema.methods.updateGroupPlan = async function updateGroupPlan (removingMember) {
// Recheck the group plan count
let members;
if (this.type === 'guild') {
members = await User.find({ guilds: this._id }).select('_id').exec();
} else {
members = await User.find({ 'party._id': this._id }).select('_id').exec();
}
this.memberCount = members.length;
this.memberCount = await this.getMemberCount();

if (this.purchased.plan.paymentMethod === stripePayments.constants.PAYMENT_METHOD) {
await stripePayments.chargeForAdditionalGroupMember(this);
Expand Down

0 comments on commit 453d60b

Please sign in to comment.