sdk-ts/docs/name.md

872 B

function getName(room: Room): string {
  const nameEvent = room.getState("m.room.name", "")?.content;
  if (nameEvent) return nameEvent;
  
  const purposes = room.getState("m.room.purpose", "")?.content?.purposes;
  if (Array.isArray(purposes) && purposes.includes("m.direct")) {
    const members = room.getAllState("m.room.member")
      .filter(i => i.content.membership === "join")
      .map(i => i.content.displayname || i.stateKey);
    switch (members.length) {
      case 1: return members[0];
      case 2: return `${members[0]} and ${members[1]}`;
      case 3: return `${members[0]}, ${members[1]}, and ${members[2]}`;
      case 4: return `${members[0]}, ${members[1]}, ${members[2]}, and ${members[3]}`;
      default: return `${members[0]}, ${members[1]}, ${members[2]}, and ${members.length - 3} others`;
    }
  }
  
  return "Unnamed room";
}