diff --git a/spec/unit/room.spec.js b/spec/unit/room.spec.js index 5e2f9f98473..a533b616b37 100644 --- a/spec/unit/room.spec.js +++ b/spec/unit/room.spec.js @@ -696,7 +696,7 @@ describe("Room", function() { }); room.recalculate(); - expect(room.name).toEqual(`${userB} and 2 others`); + expect(room.name).toEqual(`${userB}, ${userC} and ${userD}`); }); it("missing hero member state reverts to mxid", function() { @@ -722,7 +722,8 @@ describe("Room", function() { }); it("uses counts from summary", function() { - const name = "Mr B"; + const firstName = "Bean"; + const name = `${firstName} Mr`; addMember(userB, "join", {name}); room.setSummary({ "m.heroes": [userB], @@ -730,19 +731,21 @@ describe("Room", function() { "m.invited_member_count": 50, }); room.recalculate(); - expect(room.name).toEqual(`${name} and 98 others`); + expect(room.name).toEqual(`${firstName} and 98 others`); }); it("relies on heroes in case of absent counts", function() { - const nameB = "Mr Bean"; - const nameC = "Mel C"; + const firstNameB = "Bean"; + const nameB = `${firstNameB} Mr`; + const firstNameC = "Mel"; + const nameC = `${firstNameC} C`; addMember(userB, "join", {name: nameB}); addMember(userC, "join", {name: nameC}); room.setSummary({ "m.heroes": [userB, userC], }); room.recalculate(); - expect(room.name).toEqual(`${nameB} and ${nameC}`); + expect(room.name).toEqual(`${firstNameB} and ${firstNameC}`); }); it("uses only heroes", function() { diff --git a/src/models/room.js b/src/models/room.js index 466f20fd6b0..f6e2adc2fef 100644 --- a/src/models/room.js +++ b/src/models/room.js @@ -1866,8 +1866,7 @@ function calculateRoomName(room, userId, ignoreRoomNameEvent) { const joinedMemberCount = room.currentState.getJoinedMemberCount(); const invitedMemberCount = room.currentState.getInvitedMemberCount(); - // -1 because these numbers include the syncing user - const inviteJoinCount = joinedMemberCount + invitedMemberCount - 1; + const inviteJoinCount = joinedMemberCount + invitedMemberCount; // get members that are NOT ourselves and are actually in the room. let otherNames = null; @@ -1890,7 +1889,7 @@ function calculateRoomName(room, userId, ignoreRoomNameEvent) { otherNames = otherMembers.map((m) => m.name); } - if (inviteJoinCount) { + if (inviteJoinCount > 1) { return memberNamesToRoomName(otherNames, inviteJoinCount); } @@ -1926,6 +1925,16 @@ function calculateRoomName(room, userId, ignoreRoomNameEvent) { } } +/** + * Simplifies a full name to a single word. + * + * @param {string} name The full name of a room member + * @return {string} A single word of the name + */ +function shortName(name) { + return name.split(" ")[0]; +} + function memberNamesToRoomName(names, count = (names.length + 1)) { const countWithoutMe = count - 1; if (!names.length) { @@ -1933,13 +1942,24 @@ function memberNamesToRoomName(names, count = (names.length + 1)) { } else if (names.length === 1 && countWithoutMe <= 1) { return names[0]; } else if (names.length === 2 && countWithoutMe <= 2) { - return `${names[0]} and ${names[1]}`; - } else { - const plural = countWithoutMe > 1; + return `${shortName(names[0])} and ${shortName(names[1])}`; + } else if (names.length === 3 && countWithoutMe <= 3) { + return `${shortName(names[0])}, ${shortName(names[1])} and ${shortName(names[2])}`; + } else if (names.length >= 2) { + const restCount = countWithoutMe - 2; + const plural = restCount > 1; + if (plural) { + return `${shortName(names[0])}, ${shortName(names[1])} and ${restCount} others`; + } else { + return `${shortName(names[0])}, ${shortName(names[1])} and 1 other`; + } + } else { // Implicit name.length === 1 + const restCount = countWithoutMe - 1; + const plural = restCount > 1; if (plural) { - return `${names[0]} and ${countWithoutMe} others`; + return `${shortName(names[0])} and ${restCount} others`; } else { - return `${names[0]} and 1 other`; + return `${shortName(names[0])} and 1 other`; } } }