import { MatrixClient } from "../MatrixClient" ; import { Permalinks } from "./Permalinks" ; import { extractRequestError , LogService } from ".." ; category export class MentionPill { private constructor ( private entityPermalink , private displayName ) { } /**
     * The HTML component of the mention.
     */ public get html ( ) : string { return `<a href="${ this . entityPermalink }">${ this . displayName }</a>` ; } /**
     * The plain text component of the mention.
     */ public get text ( ) : string { return this . displayName ; } param { string } userId param { String } inRoomId param { MatrixClient } client returns { Promise < MentionPill > } public static async forUser ( userId , inRoomId = null , client = null ) { const permalink = Permalinks . forUser ( userId ) ; let displayName = userId ; try { if ( client ) { let profile = null ; if ( inRoomId ) { profile = await client . getRoomStateEvent ( inRoomId , "m.room.member" , userId ) ; } if ( ! profile ) { profile = await client . getUserProfile ( userId ) ; } if ( profile [ 'displayname' ] ) { displayName = profile [ 'displayname' ] ; } } } catch ( e ) { LogService . warn ( "MentionPill" , "Error getting profile" , extractRequestError ( e ) ) ; } return new MentionPill ( permalink , displayName ) ; } param { string } roomIdOrAlias param { MatrixClient } client returns { Promise < MentionPill > } public static async forRoom ( roomIdOrAlias , client = null ) { let permalink = Permalinks . forRoom ( roomIdOrAlias ) ; let displayProp = roomIdOrAlias ; try { if ( client ) { const roomId = await client . resolveRoom ( roomIdOrAlias ) ; const canonicalAlias = await client . getRoomStateEvent ( roomId , "m.room.canonical_alias" , "" ) ; if ( canonicalAlias ?. alias ) { displayProp = canonicalAlias . alias ; permalink = Permalinks . forRoom ( displayProp ) ; } } } catch ( e ) { LogService . warn ( "MentionPill" , "Error getting room information" , extractRequestError ( e ) ) ; } return new MentionPill ( permalink , displayProp ) ; } param { string } userId param { string } displayName returns { MentionPill } public static withDisplayName ( userId , displayName ) { return new MentionPill ( Permalinks . forUser ( userId ) , displayName || userId ) ; } }