@@ -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,15 @@ 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'
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
+ }
340
352
if ( typeof widgetType !== 'string' ) {
341
353
sendError ( event , _t ( "Unable to create widget." ) , new Error ( "Field 'type' must be a string." ) ) ;
342
354
return ;
@@ -347,30 +359,48 @@ function setWidget(event: MessageEvent<any>, roomId: string): void {
347
359
}
348
360
}
349
361
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
+
350
375
// convert the widget type to a known widget type
351
376
widgetType = WidgetType . fromString ( widgetType ) ;
352
377
353
378
if ( userWidget ) {
354
- WidgetUtils . setUserWidget ( widgetId , widgetType , widgetUrl , widgetName , widgetData ) . then ( ( ) => {
379
+ try {
380
+ await WidgetUtils . setUserWidget ( widgetId , widgetType , widgetUrl , widgetName , widgetData ) ;
381
+
355
382
sendResponse ( event , {
356
383
success : true ,
357
384
} ) ;
358
385
359
386
dis . dispatch ( { action : "user_widget_updated" } ) ;
360
- } ) . catch ( ( e ) => {
387
+ } catch ( e ) {
361
388
sendError ( event , _t ( 'Unable to create widget.' ) , e ) ;
362
- } ) ;
389
+ }
363
390
} else { // Room widget
364
391
if ( ! roomId ) {
365
392
sendError ( event , _t ( 'Missing roomId.' ) , null ) ;
366
393
}
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
+
368
398
sendResponse ( event , {
369
399
success : true ,
370
400
} ) ;
371
- } , ( err ) => {
401
+ } catch ( err ) {
372
402
sendError ( event , _t ( 'Failed to send request.' ) , err ) ;
373
- } ) ;
403
+ }
374
404
}
375
405
}
376
406
0 commit comments