Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache the joined member count for a room state #619

Merged
merged 2 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
16 changes: 16 additions & 0 deletions src/models/room-state.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,24 @@ function RoomState(roomId) {
this._displayNameToUserIds = {};
this._userIdsToDisplayNames = {};
this._tokenToInvite = {}; // 3pid invite state_key to m.room.member invite
this._joinedMemberCount = null; // cache of the number of joined members
}
utils.inherits(RoomState, EventEmitter);

/**
* Returns the number of joined members in this room
* This method caches the result.
* @return {integer} The number of members in this room whose membership is 'join'
*/
RoomState.prototype.getJoinedMemberCount = function() {
if (this._joinedMemberCount === null) {
this._joinedMemberCount = this.getMembers().filter((m) => {
return m.membership === 'join';
}).length;
}
return this._joinedMemberCount;
};

/**
* Get all RoomMembers in this room.
* @return {Array<RoomMember>} A list of RoomMembers.
Expand Down Expand Up @@ -218,6 +233,7 @@ RoomState.prototype.setStateEvents = function(stateEvents) {
delete self._sentinels[userId];

self.members[userId] = member;
self._joinedMemberCount = null;
self.emit("RoomState.members", event, self, member);
} else if (event.getType() === "m.room.power_levels") {
const members = utils.values(self.members);
Expand Down
4 changes: 1 addition & 3 deletions src/pushprocessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ function PushProcessor(client) {
return false;
}

const memberCount = Object.keys(room.currentState.members).filter(function(m) {
return room.currentState.members[m].membership == 'join';
}).length;
const memberCount = room.currentState.getJoinedMemberCount();

const m = cond.is.match(/^([=<>]*)([0-9]*)$/);
if (!m) {
Expand Down