@@ -148,6 +148,8 @@ Request:
148
148
can configure/lay out the widget in different ways. All widgets must have a type.
149
149
- `name` (String) is an optional human-readable string about the widget.
150
150
- `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.
151
153
Response:
152
154
{
153
155
success: true
@@ -313,12 +315,13 @@ function inviteUser(event: MessageEvent<any>, roomId: string, userId: string): v
313
315
} ) ;
314
316
}
315
317
316
- function setWidget ( event : MessageEvent < any > , roomId : string ) : void {
318
+ async function setWidget ( event : MessageEvent < any > , roomId : string ) : void {
317
319
const widgetId = event . data . widget_id ;
318
320
let widgetType = event . data . type ;
319
321
const widgetUrl = event . data . url ;
320
322
const widgetName = event . data . name ; // optional
321
323
const widgetData = event . data . data ; // optional
324
+ let widgetAvatarUrl = event . data . avatar_url ; // optional
322
325
const userWidget = event . data . userWidget ;
323
326
324
327
// both adding/removing widgets need these checks
@@ -337,6 +340,10 @@ function setWidget(event: MessageEvent<any>, roomId: string): void {
337
340
sendError ( event , _t ( "Unable to create widget." ) , new Error ( "Optional field 'data' must be an Object." ) ) ;
338
341
return ;
339
342
}
343
+ if ( widgetAvatarUrl !== undefined && typeof widgetAvatarUrl !== 'string' && ! ( widgetAvatarUrl instanceof Blob ) ) {
344
+ sendError ( event , _t ( "Unable to create widget." ) , new Error ( "Optional field 'avatar_url' must be a string or Blob." ) ) ;
345
+ return ;
346
+ }
340
347
if ( typeof widgetType !== 'string' ) {
341
348
sendError ( event , _t ( "Unable to create widget." ) , new Error ( "Field 'type' must be a string." ) ) ;
342
349
return ;
@@ -347,30 +354,47 @@ function setWidget(event: MessageEvent<any>, roomId: string): void {
347
354
}
348
355
}
349
356
357
+ // A blob was transmitted as a widget avatar url, so we have to upload it to
358
+ // the room first
359
+ if ( widgetAvatarUrl && widgetAvatarUrl instanceof Blob ) {
360
+ try {
361
+ const client = MatrixClientPeg . get ( ) ;
362
+ widgetAvatarUrl = await client . uploadContent ( widgetAvatarUrl , {
363
+ type : widgetAvatarUrl . type ,
364
+ } ) ;
365
+ } catch ( err ) {
366
+ sendError ( event , _t ( 'Unable to create widget.' ) , err ) ;
367
+ }
368
+ }
369
+
350
370
// convert the widget type to a known widget type
351
371
widgetType = WidgetType . fromString ( widgetType ) ;
352
372
353
373
if ( userWidget ) {
354
- WidgetUtils . setUserWidget ( widgetId , widgetType , widgetUrl , widgetName , widgetData ) . then ( ( ) => {
374
+ try {
375
+ await WidgetUtils . setUserWidget ( widgetId , widgetType , widgetUrl , widgetName , widgetData ) ;
376
+
355
377
sendResponse ( event , {
356
378
success : true ,
357
379
} ) ;
358
380
359
381
dis . dispatch ( { action : "user_widget_updated" } ) ;
360
- } ) . catch ( ( e ) => {
382
+ } catch ( e ) {
361
383
sendError ( event , _t ( 'Unable to create widget.' ) , e ) ;
362
- } ) ;
384
+ }
363
385
} else { // Room widget
364
386
if ( ! roomId ) {
365
387
sendError ( event , _t ( 'Missing roomId.' ) , null ) ;
366
388
}
367
- WidgetUtils . setRoomWidget ( roomId , widgetId , widgetType , widgetUrl , widgetName , widgetData ) . then ( ( ) => {
389
+ try {
390
+ await WidgetUtils . setRoomWidget ( roomId , widgetId , widgetType , widgetUrl , widgetName , widgetData , widgetAvatarUrl )
391
+
368
392
sendResponse ( event , {
369
393
success : true ,
370
394
} ) ;
371
- } , ( err ) => {
395
+ } catch ( err ) {
372
396
sendError ( event , _t ( 'Failed to send request.' ) , err ) ;
373
- } ) ;
397
+ }
374
398
}
375
399
}
376
400
0 commit comments