import LogWrapper from "./LogWrapper" ; import { Appservice } from "matrix-bot-sdk" ; import axios from "axios" ; const log = new LogWrapper ( "IntentUtils" ) ; export async function getIntentForUser ( user , as ) { const intent = as . getIntentForSuffix ( user . login ) ; const displayName = `${ user . login }` ; let profile ; await intent . ensureRegistered ( ) ; try { profile = await intent . underlyingClient . getUserProfile ( intent . userId ) ; } catch ( ex ) { profile = { } ; } if ( profile . displayname !== displayName ) { log . debug ( `Updating ${ intent . userId }'s displayname` ) ; log . info ( `${ intent . userId }'s profile is out of date` ) ; await intent . underlyingClient . setDisplayName ( displayName ) ; } if ( ! profile . avatar_url && user . avatarUrl ) { log . debug ( `Updating ${ intent . userId }'s avatar` ) ; const buffer = await axios . get ( user . avatarUrl , { responseType : "arraybuffer" , } ) ; log . info ( `Uploading ${ user . avatarUrl }` ) ; const contentType = buffer . headers [ "content-type" ] ; const mxc = await intent . underlyingClient . uploadContent ( Buffer . from ( buffer . data as ArrayBuffer ) , contentType ) ; await intent . underlyingClient . setAvatarUrl ( mxc ) ; } return intent ; }