-
-
- {avatars}
-
-
- {summary}{joinAndLeft ? joinAndLeft + ' ' + noun + ' joined and left' : ''}
-
- {toggleButton}
-
+ if (fewEvents) {
+ return (
+
+ {expandedEvents}
);
}
+ // Map user IDs to the first and last member events in eventsToRender for each user
+ let userEvents = {
+ // $userId : {first : e0, last : e1}
+ };
+
+ eventsToRender.forEach((e) => {
+ const userId = e.getSender();
+ // Initialise a user's events
+ if (!userEvents[userId]) {
+ userEvents[userId] = {first: null, last: null};
+ }
+ if (!userEvents[userId].first) {
+ userEvents[userId].first = e;
+ } else {
+ userEvents[userId].last = e;
+ }
+ });
+
+ // Populate the join/leave event arrays with events that represent what happened
+ // overall to a user's membership. If no events are added to either array for a
+ // particular user, they will be considered a user that "joined and left".
+ let joinEvents = [];
+ let leaveEvents = [];
+ let joinedAndLeft = 0;
+ let senders = Object.keys(userEvents);
+ senders.forEach(
+ (userId) => {
+ let firstEvent = userEvents[userId].first;
+ let lastEvent = userEvents[userId].last;
+ // Only one membership event was recorded for this userId
+ if (!lastEvent) {
+ lastEvent = firstEvent;
+ }
+
+ // Membership BEFORE eventsToRender
+ let previousMembership = firstEvent.getPrevContent().membership || "leave";
+
+ // If the last membership event differs from previousMembership, use that.
+ if (previousMembership !== lastEvent.getContent().membership) {
+ if (lastEvent.event.content.membership === 'join') {
+ joinEvents.push(lastEvent);
+ } else if (lastEvent.event.content.membership === 'leave') {
+ leaveEvents.push(lastEvent);
+ }
+ } else {
+ // Increment the number of users whose membership change was nil overall
+ joinedAndLeft++;
+ }
+ }
+ );
+
+ let avatars = this._renderAvatars(joinEvents.concat(leaveEvents));
+ let summary = this._renderSummary(joinEvents, leaveEvents);
+ let toggleButton = (
+
+ {expanded ? 'collapse' : 'expand'}
+
+ );
+ let plural = (joinEvents.length + leaveEvents.length > 0) ? 'others' : 'users';
+ let noun = (joinedAndLeft === 1 ? 'user' : plural);
+
+ let summaryContainer = (
+
+
+
+ {avatars}
+
+
+ {summary}{joinedAndLeft ? joinedAndLeft + ' ' + noun + ' joined and left' : ''}
+
+ {toggleButton}
+
+
+ );
+
return (
{summaryContainer}