Skip to content
This repository was archived by the owner on Sep 11, 2024. It is now read-only.

Commit 21c52c2

Browse files
committed
Support avatar_url in the scalar client API
Signed-off-by: Oliver Sand <[email protected]>
1 parent c1579f7 commit 21c52c2

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

src/ScalarMessaging.ts

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
151153
Response:
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,10 @@ 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' && !(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+
}
340347
if (typeof widgetType !== 'string') {
341348
sendError(event, _t("Unable to create widget."), new Error("Field 'type' must be a string."));
342349
return;
@@ -347,30 +354,47 @@ function setWidget(event: MessageEvent<any>, roomId: string): void {
347354
}
348355
}
349356

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+
350370
// convert the widget type to a known widget type
351371
widgetType = WidgetType.fromString(widgetType);
352372

353373
if (userWidget) {
354-
WidgetUtils.setUserWidget(widgetId, widgetType, widgetUrl, widgetName, widgetData).then(() => {
374+
try {
375+
await WidgetUtils.setUserWidget(widgetId, widgetType, widgetUrl, widgetName, widgetData);
376+
355377
sendResponse(event, {
356378
success: true,
357379
});
358380

359381
dis.dispatch({ action: "user_widget_updated" });
360-
}).catch((e) => {
382+
} catch(e) {
361383
sendError(event, _t('Unable to create widget.'), e);
362-
});
384+
}
363385
} else { // Room widget
364386
if (!roomId) {
365387
sendError(event, _t('Missing roomId.'), null);
366388
}
367-
WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData).then(() => {
389+
try {
390+
await WidgetUtils.setRoomWidget(roomId, widgetId, widgetType, widgetUrl, widgetName, widgetData, widgetAvatarUrl)
391+
368392
sendResponse(event, {
369393
success: true,
370394
});
371-
}, (err) => {
395+
} catch(err) {
372396
sendError(event, _t('Failed to send request.'), err);
373-
});
397+
}
374398
}
375399
}
376400

src/utils/WidgetUtils.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ export default class WidgetUtils {
286286
widgetUrl?: string,
287287
widgetName?: string,
288288
widgetData?: object,
289+
widgetAvatarUrl?: string,
289290
) {
290291
let content;
291292

@@ -299,6 +300,7 @@ export default class WidgetUtils {
299300
url: widgetUrl,
300301
name: widgetName,
301302
data: widgetData,
303+
avatar_url: widgetAvatarUrl
302304
};
303305
} else {
304306
content = {};

0 commit comments

Comments
 (0)