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

Commit 84c4bae

Browse files
authored
Merge pull request #1905 from matrix-org/dbkr/wait_for_user_widget
Wait for echo from server when adding user widgets
2 parents 7bfc50b + 9d5ba25 commit 84c4bae

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/ScalarMessaging.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,41 @@ function inviteUser(event, roomId, userId) {
286286
});
287287
}
288288

289+
/**
290+
* Returns a promise that resolves when a widget with the given
291+
* ID has been added as a user widget (ie. the accountData event
292+
* arrives) or rejects after a timeout
293+
*
294+
* @param {string} widgetId The ID of the widget to wait for
295+
* @returns {Promise} that resolves when the widget is available
296+
*/
297+
function waitForUserWidget(widgetId) {
298+
return new Promise((resolve, reject) => {
299+
const currentAccountDataEvent = MatrixClientPeg.get().getAccountData('m.widgets');
300+
if (
301+
currentAccountDataEvent &&
302+
currentAccountDataEvent.getContent() &&
303+
currentAccountDataEvent.getContent()[widgetId] !== undefined
304+
) {
305+
resolve();
306+
return;
307+
}
308+
309+
function onAccountData(ev) {
310+
if (ev.getType() === 'm.widgets' && ev.getContent() && ev.getContent()[widgetId] !== undefined) {
311+
MatrixClientPeg.get().removeListener('accountData', onAccountData);
312+
clearTimeout(timerId);
313+
resolve();
314+
}
315+
}
316+
const timerId = setTimeout(() => {
317+
MatrixClientPeg.get().removeListener('accountData', onAccountData);
318+
reject(new Error("Timed out waiting for widget ID " + widgetId + " to appear"));
319+
}, 10000);
320+
MatrixClientPeg.get().on('accountData', onAccountData);
321+
});
322+
}
323+
289324
function setWidget(event, roomId) {
290325
const widgetId = event.data.widget_id;
291326
const widgetType = event.data.type;
@@ -355,12 +390,20 @@ function setWidget(event, roomId) {
355390
};
356391
}
357392

393+
// This starts listening for when the echo comes back from the server
394+
// since the widget won't appear added until this happens. If we don't
395+
// wait for this, the action will complete but if the user is fast enough,
396+
// the widget still won't actually be there.
358397
client.setAccountData('m.widgets', userWidgets).then(() => {
398+
return waitForUserWidget(widgetId);
399+
}).then(() => {
359400
sendResponse(event, {
360401
success: true,
361402
});
362403

363404
dis.dispatch({ action: "user_widget_updated" });
405+
}).catch((e) => {
406+
sendError(event, _t('Unable to create widget.'), e);
364407
});
365408
} else { // Room widget
366409
if (!roomId) {
@@ -373,6 +416,8 @@ function setWidget(event, roomId) {
373416
// TODO - Room widgets need to be moved to 'm.widget' state events
374417
// https://docs.google.com/document/d/1uPF7XWY_dXTKVKV7jZQ2KmsI19wn9-kFRgQ1tFQP7wQ/edit?usp=sharing
375418
client.sendStateEvent(roomId, "im.vector.modular.widgets", content, widgetId).done(() => {
419+
// XXX: We should probably wait for the echo of the state event to come back from the server,
420+
// as we do with user widgets.
376421
sendResponse(event, {
377422
success: true,
378423
});

src/i18n/strings/en_EN.json

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@
190190
"Message Replies": "Message Replies",
191191
"Message Pinning": "Message Pinning",
192192
"Tag Panel": "Tag Panel",
193-
"Sticker Messages": "Sticker Messages",
194193
"Disable Emoji suggestions while typing": "Disable Emoji suggestions while typing",
195194
"Use compact timeline layout": "Use compact timeline layout",
196195
"Hide removed messages": "Hide removed messages",
@@ -566,8 +565,6 @@
566565
"Download %(text)s": "Download %(text)s",
567566
"Invalid file%(extra)s": "Invalid file%(extra)s",
568567
"Error decrypting image": "Error decrypting image",
569-
"This image cannot be displayed.": "This image cannot be displayed.",
570-
"Image '%(Body)s' cannot be displayed.": "Image '%(Body)s' cannot be displayed.",
571568
"Error decrypting video": "Error decrypting video",
572569
"%(senderDisplayName)s changed the avatar for %(roomName)s": "%(senderDisplayName)s changed the avatar for %(roomName)s",
573570
"%(senderDisplayName)s removed the room avatar.": "%(senderDisplayName)s removed the room avatar.",
@@ -815,8 +812,8 @@
815812
"Encryption key request": "Encryption key request",
816813
"Sign out": "Sign out",
817814
"Log out and remove encryption keys?": "Log out and remove encryption keys?",
818-
"Send Logs": "Send Logs",
819815
"Clear Storage and Sign Out": "Clear Storage and Sign Out",
816+
"Send Logs": "Send Logs",
820817
"Refresh": "Refresh",
821818
"Unable to restore session": "Unable to restore session",
822819
"We encountered an error trying to restore your previous session.": "We encountered an error trying to restore your previous session.",

0 commit comments

Comments
 (0)