src/models/room.js
function calculateRoomName(room, userId, ignoreRoomNameEvent) {
    if (!ignoreRoomNameEvent) {
        // check for an alias, if any. for now, assume first alias is the
        // official one.
        const mRoomName = room.currentState.getStateEvents("m.room.name", "");
        if (mRoomName && mRoomName.getContent() && mRoomName.getContent().name) {
            return mRoomName.getContent().name;
        }
    }

    let alias = room.getCanonicalAlias();

    if (!alias) {
        const aliases = room.getAliases();

        if (aliases.length) {
            alias = aliases[0];
        }
    }
    if (alias) {
        return alias;
    }

    const joinedMemberCount = room.currentState.getJoinedMemberCount();
    const invitedMemberCount = room.currentState.getInvitedMemberCount();
    // -1 because these numbers include the syncing user
    const inviteJoinCount = joinedMemberCount + invitedMemberCount - 1;

    // get members that are NOT ourselves and are actually in the room.
    let otherNames = null;
    if (room._summaryHeroes) {
        // if we have a summary, the member state events
        // should be in the room state
        otherNames = room._summaryHeroes.map((userId) => {
            const member = room.getMember(userId);
            return member ? member.name : userId;
        });
    } else {
        let otherMembers = room.currentState.getMembers().filter((m) => {
            return m.userId !== userId &&
                (m.membership === "invite" || m.membership === "join");
        });
        // make sure members have stable order
        otherMembers.sort((a, b) => a.userId.localeCompare(b.userId));
        // only 5 first members, immitate _summaryHeroes
        otherMembers = otherMembers.slice(0, 5);
        otherNames = otherMembers.map((m) => m.name);
    }

    if (inviteJoinCount) {
        return memberNamesToRoomName(otherNames, inviteJoinCount);
    }

    const myMembership = room.getMyMembership();
    // if I have created a room and invited people throuh
    // 3rd party invites
    if (myMembership == 'join') {
        const thirdPartyInvites =
            room.currentState.getStateEvents("m.room.third_party_invite");

        if (thirdPartyInvites && thirdPartyInvites.length) {
            const thirdPartyNames = thirdPartyInvites.map((i) => {
                return i.getContent().display_name;
            });

            return `Inviting ${memberNamesToRoomName(thirdPartyNames)}`;
        }
    }
    // let's try to figure out who was here before
    let leftNames = otherNames;
    // if we didn't have heroes, try finding them in the room state
    if(!leftNames.length) {
        leftNames = room.currentState.getMembers().filter((m) => {
            return m.userId !== userId &&
                m.membership !== "invite" &&
                m.membership !== "join";
        }).map((m) => m.name);
    }
    if(leftNames.length) {
        return `Empty room (was ${memberNamesToRoomName(leftNames)})`;
    } else {
        return "Empty room";
    }
}
function calculateRoomName(room, userId, ignoreRoomNameEvent) {
    if (!ignoreRoomNameEvent) {
        // check for an alias, if any. for now, assume first alias is the
        // official one.
        const mRoomName = room.currentState.getStateEvents("m.room.name", "");
        if (mRoomName && mRoomName.getContent() && mRoomName.getContent().name) {
            return mRoomName.getContent().name;
        }
    }

    let alias = room.getCanonicalAlias();

    if (!alias) {
        const aliases = room.getAliases();

        if (aliases.length) {
            alias = aliases[0];
        }
    }
    if (alias) {
        return alias;
    }

    const joinedMemberCount = room.currentState.getJoinedMemberCount();
    const invitedMemberCount = room.currentState.getInvitedMemberCount();
    // -1 because these numbers include the syncing user
    const inviteJoinCount = joinedMemberCount + invitedMemberCount - 1;

    // get members that are NOT ourselves and are actually in the room.
    let otherNames = null;
    if (room._summaryHeroes) {
        // if we have a summary, the member state events
        // should be in the room state
        otherNames = room._summaryHeroes.map((userId) => {
            const member = room.getMember(userId);
            return member ? member.name : userId;
        });
    } else {
        let otherMembers = room.currentState.getMembers().filter((m) => {
            return m.userId !== userId &&
                (m.membership === "invite" || m.membership === "join");
        });
        // make sure members have stable order
        otherMembers.sort((a, b) => a.userId.localeCompare(b.userId));
        // only 5 first members, immitate _summaryHeroes
        otherMembers = otherMembers.slice(0, 5);
        otherNames = otherMembers.map((m) => m.name);
    }

    if (inviteJoinCount) {
        return memberNamesToRoomName(otherNames, inviteJoinCount);
    }

    const myMembership = room.getMyMembership();
    // if I have created a room and invited people throuh
    // 3rd party invites
    if (myMembership == 'join') {
        const thirdPartyInvites =
            room.currentState.getStateEvents("m.room.third_party_invite");

        if (thirdPartyInvites && thirdPartyInvites.length) {
            const thirdPartyNames = thirdPartyInvites.map((i) => {
                return i.getContent().display_name;
            });

            return `Inviting ${memberNamesToRoomName(thirdPartyNames)}`;
        }
    }
    // let's try to figure out who was here before
    let leftNames = otherNames;
    // if we didn't have heroes, try finding them in the room state
    if(!leftNames.length) {
        leftNames = room.currentState.getMembers().filter((m) => {
            return m.userId !== userId &&
                m.membership !== "invite" &&
                m.membership !== "join";
        }).map((m) => m.name);
    }
    if(leftNames.length) {
        return `Empty room (was ${memberNamesToRoomName(leftNames)})`;
    } else {
        return "Empty room";
    }
}
function calculateRoomName ( room , userId , ignoreRoomNameEvent ) { if ( ! ignoreRoomNameEvent ) { // check for an alias, if any. for now, assume first alias is the // official one. const mRoomName = room . currentState . getStateEvents ( "m.room.name" , "" ) ; if ( mRoomName && mRoomName . getContent ( ) && mRoomName . getContent ( ) . name ) { return mRoomName . getContent ( ) . name ; } } let alias = room . getCanonicalAlias ( ) ; if ( ! alias ) { const aliases = room . getAliases ( ) ; if ( aliases . length ) { alias = aliases [ 0 ] ; } } if ( alias ) { return alias ; } const joinedMemberCount = room . currentState . getJoinedMemberCount ( ) ; const invitedMemberCount = room . currentState . getInvitedMemberCount ( ) ; // -1 because these numbers include the syncing user const inviteJoinCount = joinedMemberCount + invitedMemberCount - 1 ; // get members that are NOT ourselves and are actually in the room. let otherNames = null ; if ( room . _summaryHeroes ) { // if we have a summary, the member state events // should be in the room state otherNames = room . _summaryHeroes . map ( ( userId ) => { const member = room . getMember ( userId ) ; return member ? member . name : userId ; } ) ; } else { let otherMembers = room . currentState . getMembers ( ) . filter ( ( m ) => { return m . userId !== userId && ( m . membership === "invite" || m . membership === "join" ) ; } ) ; // make sure members have stable order otherMembers . sort ( ( a , b ) => a . userId . localeCompare ( b . userId ) ) ; // only 5 first members, immitate _summaryHeroes otherMembers = otherMembers . slice ( 0 , 5 ) ; otherNames = otherMembers . map ( ( m ) => m . name ) ; } if ( inviteJoinCount ) { return memberNamesToRoomName ( otherNames , inviteJoinCount ) ; } const myMembership = room . getMyMembership ( ) ; // if I have created a room and invited people throuh // 3rd party invites if ( myMembership == 'join' ) { const thirdPartyInvites = room . currentState . getStateEvents ( "m.room.third_party_invite" ) ; if ( thirdPartyInvites && thirdPartyInvites . length ) { const thirdPartyNames = thirdPartyInvites . map ( ( i ) => { return i . getContent ( ) . display_name ; } ) ; return ` ${ memberNamesToRoomName ( thirdPartyNames ) } ` ; } } // let's try to figure out who was here before let leftNames = otherNames ; // if we didn't have heroes, try finding them in the room state if ( ! leftNames . length ) { leftNames = room . currentState . getMembers ( ) . filter ( ( m ) => { return m . userId !== userId && m . membership !== "invite" && m . membership !== "join" ; } ) . map ( ( m ) => m . name ) ; } if ( leftNames . length ) { return ` ${ memberNamesToRoomName ( leftNames ) } ` ; } else { return "Empty room" ; } }
This is an internal method. Calculates the name of the room from the current
room state.
@param {Room} room The matrix room.
@param {string} userId The client's user ID. Used to filter room members
correctly.
@param {bool} ignoreRoomNameEvent Return the implicit room name that we'd see if there
was no m.room.name event.
@return {string} The calculated room name.
This is an internal method . Calculates the name of the room from the current room state .