From 82a0c1024c654ffa4b8561fac15d4c3d9fc37ee0 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Tue, 26 Oct 2021 15:08:51 +0200 Subject: [PATCH] return static string when member is missing and add logging --- src/matrix/room/BaseRoom.js | 2 +- src/matrix/room/Invite.js | 6 +++--- src/matrix/room/Room.js | 2 +- src/matrix/room/members/Heroes.js | 14 ++++++++++---- 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/src/matrix/room/BaseRoom.js b/src/matrix/room/BaseRoom.js index 43b2cf51..d26d716d 100644 --- a/src/matrix/room/BaseRoom.js +++ b/src/matrix/room/BaseRoom.js @@ -208,7 +208,7 @@ export class BaseRoom extends EventEmitter { if (this._summary.data.needsHeroes) { this._heroes = new Heroes(this._roomId); 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) { throw new WrappedError(`Could not load room ${this._roomId}`, err); diff --git a/src/matrix/room/Invite.js b/src/matrix/room/Invite.js index b8190322..1bf802dd 100644 --- a/src/matrix/room/Invite.js +++ b/src/matrix/room/Invite.js @@ -139,7 +139,7 @@ export class Invite extends EventEmitter { const summaryData = this._createSummaryData(inviteState); let heroes; if (!summaryData.name && !summaryData.canonicalAlias) { - heroes = await this._createHeroes(inviteState); + heroes = await this._createHeroes(inviteState, log); } const myInvite = this._getMyInvite(inviteState); if (!myInvite) { @@ -204,7 +204,7 @@ export class Invite extends EventEmitter { 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 otherMembers = members.filter(e => e.state_key !== this._user.id); const memberChanges = otherMembers.reduce((map, e) => { @@ -220,7 +220,7 @@ export class Invite extends EventEmitter { const countSummary = new SummaryData(null, this.id); 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); - heroes.applyChanges(changes, countSummary); + heroes.applyChanges(changes, countSummary, log); return heroes; } diff --git a/src/matrix/room/Room.js b/src/matrix/room/Room.js index a0d7c693..aaf66be1 100644 --- a/src/matrix/room/Room.js +++ b/src/matrix/room/Room.js @@ -240,7 +240,7 @@ export class Room extends BaseRoom { } if (this._heroes && heroChanges) { const oldName = this.name; - this._heroes.applyChanges(heroChanges, this._summary.data); + this._heroes.applyChanges(heroChanges, this._summary.data, log); if (oldName !== this.name) { emitChange = true; } diff --git a/src/matrix/room/members/Heroes.js b/src/matrix/room/members/Heroes.js index 29732def..ce2fe587 100644 --- a/src/matrix/room/members/Heroes.js +++ b/src/matrix/room/members/Heroes.js @@ -16,7 +16,7 @@ limitations under the License. import {RoomMember} from "./RoomMember.js"; -function calculateRoomName(sortedMembers, summaryData) { +function calculateRoomName(sortedMembers, summaryData, log) { const countWithoutMe = summaryData.joinCount + summaryData.inviteCount - 1; if (sortedMembers.length >= countWithoutMe) { if (sortedMembers.length > 1) { @@ -24,7 +24,13 @@ function calculateRoomName(sortedMembers, summaryData) { const firstMembers = sortedMembers.slice(0, sortedMembers.length - 1); return firstMembers.map(m => m.name).join(", ") + " and " + lastMember.name; } 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) { return sortedMembers.map(m => m.name).join(", ") + ` and ${countWithoutMe} others`; @@ -74,7 +80,7 @@ export class Heroes { return {updatedHeroMembers: updatedHeroMembers.values(), removedUserIds}; } - applyChanges({updatedHeroMembers, removedUserIds}, summaryData) { + applyChanges({updatedHeroMembers, removedUserIds}, summaryData, log) { for (const userId of removedUserIds) { this._members.delete(userId); } @@ -82,7 +88,7 @@ export class Heroes { this._members.set(member.userId, member); } 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() {