return static string when member is missing and add logging

This commit is contained in:
Bruno Windels 2021-10-26 15:08:51 +02:00
parent e3c5def536
commit 82a0c1024c
4 changed files with 15 additions and 9 deletions

View file

@ -208,7 +208,7 @@ export class BaseRoom extends EventEmitter {
if (this._summary.data.needsHeroes) { if (this._summary.data.needsHeroes) {
this._heroes = new Heroes(this._roomId); this._heroes = new Heroes(this._roomId);
const changes = await this._heroes.calculateChanges(this._summary.data.heroes, [], txn); const changes = await this._heroes.calculateChanges(this._summary.data.heroes, [], txn);
this._heroes.applyChanges(changes, this._summary.data); this._heroes.applyChanges(changes, this._summary.data, log);
} }
} catch (err) { } catch (err) {
throw new WrappedError(`Could not load room ${this._roomId}`, err); throw new WrappedError(`Could not load room ${this._roomId}`, err);

View file

@ -139,7 +139,7 @@ export class Invite extends EventEmitter {
const summaryData = this._createSummaryData(inviteState); const summaryData = this._createSummaryData(inviteState);
let heroes; let heroes;
if (!summaryData.name && !summaryData.canonicalAlias) { if (!summaryData.name && !summaryData.canonicalAlias) {
heroes = await this._createHeroes(inviteState); heroes = await this._createHeroes(inviteState, log);
} }
const myInvite = this._getMyInvite(inviteState); const myInvite = this._getMyInvite(inviteState);
if (!myInvite) { if (!myInvite) {
@ -204,7 +204,7 @@ export class Invite extends EventEmitter {
return inviteState.reduce(processStateEvent, new SummaryData(null, this.id)); return inviteState.reduce(processStateEvent, new SummaryData(null, this.id));
} }
async _createHeroes(inviteState) { async _createHeroes(inviteState, log) {
const members = inviteState.filter(e => e.type === MEMBER_EVENT_TYPE); const members = inviteState.filter(e => e.type === MEMBER_EVENT_TYPE);
const otherMembers = members.filter(e => e.state_key !== this._user.id); const otherMembers = members.filter(e => e.state_key !== this._user.id);
const memberChanges = otherMembers.reduce((map, e) => { const memberChanges = otherMembers.reduce((map, e) => {
@ -220,7 +220,7 @@ export class Invite extends EventEmitter {
const countSummary = new SummaryData(null, this.id); const countSummary = new SummaryData(null, this.id);
countSummary.joinCount = members.reduce((sum, e) => sum + (e.content?.membership === "join" ? 1 : 0), 0); countSummary.joinCount = members.reduce((sum, e) => sum + (e.content?.membership === "join" ? 1 : 0), 0);
countSummary.inviteCount = members.reduce((sum, e) => sum + (e.content?.membership === "invite" ? 1 : 0), 0); countSummary.inviteCount = members.reduce((sum, e) => sum + (e.content?.membership === "invite" ? 1 : 0), 0);
heroes.applyChanges(changes, countSummary); heroes.applyChanges(changes, countSummary, log);
return heroes; return heroes;
} }

View file

@ -240,7 +240,7 @@ export class Room extends BaseRoom {
} }
if (this._heroes && heroChanges) { if (this._heroes && heroChanges) {
const oldName = this.name; const oldName = this.name;
this._heroes.applyChanges(heroChanges, this._summary.data); this._heroes.applyChanges(heroChanges, this._summary.data, log);
if (oldName !== this.name) { if (oldName !== this.name) {
emitChange = true; emitChange = true;
} }

View file

@ -16,7 +16,7 @@ limitations under the License.
import {RoomMember} from "./RoomMember.js"; import {RoomMember} from "./RoomMember.js";
function calculateRoomName(sortedMembers, summaryData) { function calculateRoomName(sortedMembers, summaryData, log) {
const countWithoutMe = summaryData.joinCount + summaryData.inviteCount - 1; const countWithoutMe = summaryData.joinCount + summaryData.inviteCount - 1;
if (sortedMembers.length >= countWithoutMe) { if (sortedMembers.length >= countWithoutMe) {
if (sortedMembers.length > 1) { if (sortedMembers.length > 1) {
@ -24,7 +24,13 @@ function calculateRoomName(sortedMembers, summaryData) {
const firstMembers = sortedMembers.slice(0, sortedMembers.length - 1); const firstMembers = sortedMembers.slice(0, sortedMembers.length - 1);
return firstMembers.map(m => m.name).join(", ") + " and " + lastMember.name; return firstMembers.map(m => m.name).join(", ") + " and " + lastMember.name;
} else { } else {
return sortedMembers[0].name; const otherMember = sortedMembers[0];
if (otherMember) {
return otherMember.name;
} else {
log.log({l: "could get get other member name", length: sortedMembers.length, otherMember: !!otherMember, otherMemberMembership: otherMember?.membership});
return "Unknown DM Name";
}
} }
} else if (sortedMembers.length < countWithoutMe) { } else if (sortedMembers.length < countWithoutMe) {
return sortedMembers.map(m => m.name).join(", ") + ` and ${countWithoutMe} others`; return sortedMembers.map(m => m.name).join(", ") + ` and ${countWithoutMe} others`;
@ -74,7 +80,7 @@ export class Heroes {
return {updatedHeroMembers: updatedHeroMembers.values(), removedUserIds}; return {updatedHeroMembers: updatedHeroMembers.values(), removedUserIds};
} }
applyChanges({updatedHeroMembers, removedUserIds}, summaryData) { applyChanges({updatedHeroMembers, removedUserIds}, summaryData, log) {
for (const userId of removedUserIds) { for (const userId of removedUserIds) {
this._members.delete(userId); this._members.delete(userId);
} }
@ -82,7 +88,7 @@ export class Heroes {
this._members.set(member.userId, member); this._members.set(member.userId, member);
} }
const sortedMembers = Array.from(this._members.values()).sort((a, b) => a.name.localeCompare(b.name)); const sortedMembers = Array.from(this._members.values()).sort((a, b) => a.name.localeCompare(b.name));
this._roomName = calculateRoomName(sortedMembers, summaryData); this._roomName = calculateRoomName(sortedMembers, summaryData, log);
} }
get roomName() { get roomName() {