@@ -148,6 +148,8 @@ Request:
148148 can configure/lay out the widget in different ways. All widgets must have a type.
149149 - `name` (String) is an optional human-readable string about the widget.
150150 - `data` (Object) is some optional data about the widget, and can contain arbitrary key/value pairs.
151+ - `avatar_url` (String|Blob) is some optional avatar of the widget. Can contain either be an existing mxc:
152+ URL or the mxc: URL of a Blob that is automatically uploaded to the room.
151153Response:
152154{
153155 success: true
@@ -313,12 +315,13 @@ function inviteUser(event: MessageEvent<any>, roomId: string, userId: string): v
313315 } ) ;
314316}
315317
316- function setWidget ( event : MessageEvent < any > , roomId : string ) : void {
318+ async function setWidget ( event : MessageEvent < any > , roomId : string ) : void {
317319 const widgetId = event . data . widget_id ;
318320 let widgetType = event . data . type ;
319321 const widgetUrl = event . data . url ;
320322 const widgetName = event . data . name ; // optional
321323 const widgetData = event . data . data ; // optional
324+ let widgetAvatarUrl = event . data . avatar_url ; // optional
322325 const userWidget = event . data . userWidget ;
323326
324327 // both adding/removing widgets need these checks
@@ -337,6 +340,15 @@ function setWidget(event: MessageEvent<any>, roomId: string): void {
337340 sendError ( event , _t ( "Unable to create widget." ) , new Error ( "Optional field 'data' must be an Object." ) ) ;
338341 return ;
339342 }
343+ if ( widgetAvatarUrl !== undefined && typeof widgetAvatarUrl !== 'string'
344+ && ! ( widgetAvatarUrl instanceof Blob ) ) {
345+ sendError (
346+ event ,
347+ _t ( "Unable to create widget." ) ,
348+ new Error ( "Optional field 'avatar_url' must be a string or Blob." ) ,
349+ ) ;
350+ return ;
351+ }
340352 if ( typeof widgetType !== 'string' ) {
341353 sendError ( event , _t ( "Unable to create widget." ) , new Error ( "Field 'type' must be a string." ) ) ;
342354 return ;
@@ -347,30 +359,48 @@ function setWidget(event: MessageEvent<any>, roomId: string): void {
347359 }
348360 }
349361
362+ // A blob was transmitted as a widget avatar url, so we have to upload it to
363+ // the room first
364+ if ( widgetAvatarUrl && widgetAvatarUrl instanceof Blob ) {
365+ try {
366+ const client = MatrixClientPeg . get ( ) ;
367+ widgetAvatarUrl = await client . uploadContent ( widgetAvatarUrl , {
368+ type : widgetAvatarUrl . type ,
369+ } ) ;
370+ } catch ( err ) {
371+ sendError ( event , _t ( 'Unable to create widget.' ) , err ) ;
372+ }
373+ }
374+
350375 // convert the widget type to a known widget type
351376 widgetType = WidgetType . fromString ( widgetType ) ;
352377
353378 if ( userWidget ) {
354- WidgetUtils . setUserWidget ( widgetId , widgetType , widgetUrl , widgetName , widgetData ) . then ( ( ) => {
379+ try {
380+ await WidgetUtils . setUserWidget ( widgetId , widgetType , widgetUrl , widgetName , widgetData ) ;
381+
355382 sendResponse ( event , {
356383 success : true ,
357384 } ) ;
358385
359386 dis . dispatch ( { action : "user_widget_updated" } ) ;
360- } ) . catch ( ( e ) => {
387+ } catch ( e ) {
361388 sendError ( event , _t ( 'Unable to create widget.' ) , e ) ;
362- } ) ;
389+ }
363390 } else { // Room widget
364391 if ( ! roomId ) {
365392 sendError ( event , _t ( 'Missing roomId.' ) , null ) ;
366393 }
367- WidgetUtils . setRoomWidget ( roomId , widgetId , widgetType , widgetUrl , widgetName , widgetData ) . then ( ( ) => {
394+ try {
395+ await WidgetUtils . setRoomWidget ( roomId , widgetId , widgetType , widgetUrl , widgetName , widgetData ,
396+ widgetAvatarUrl ) ;
397+
368398 sendResponse ( event , {
369399 success : true ,
370400 } ) ;
371- } , ( err ) => {
401+ } catch ( err ) {
372402 sendError ( event , _t ( 'Failed to send request.' ) , err ) ;
373- } ) ;
403+ }
374404 }
375405}
376406
0 commit comments