From a33859326e21d1c3b7ff650d50f20c155174e2f8 Mon Sep 17 00:00:00 2001 From: Richard Lewis Date: Fri, 9 Mar 2018 09:15:16 +0000 Subject: [PATCH] Use getUserWidgets where possible. --- src/ScalarMessaging.js | 7 ++----- src/utils/widgets.js | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/ScalarMessaging.js b/src/ScalarMessaging.js index 7a9f85a9e0..2411fc6e6d 100644 --- a/src/ScalarMessaging.js +++ b/src/ScalarMessaging.js @@ -335,10 +335,7 @@ function setWidget(event, roomId) { if (userWidget) { const client = MatrixClientPeg.get(); - let userWidgets = {}; - if (client.getAccountData('m.widgets')) { - userWidgets = client.getAccountData('m.widgets').getContent(); - } + const userWidgets = Widgets.getUserWidgets(); // Delete existing widget with ID try { @@ -408,7 +405,7 @@ function getWidgets(event, roomId) { } // Add user widgets (not linked to a specific room) - const userWidgets = Widgets.getUserWidgets(); + const userWidgets = Widgets.getUserWidgetsArray(); widgetStateEvents = widgetStateEvents.concat(userWidgets); sendResponse(event, widgetStateEvents); diff --git a/src/utils/widgets.js b/src/utils/widgets.js index da68abb4a9..c2eae88b17 100644 --- a/src/utils/widgets.js +++ b/src/utils/widgets.js @@ -7,7 +7,7 @@ import MatrixClientPeg from '../MatrixClientPeg'; */ function getWidgets(room) { const widgets = getRoomWidgets(room); - widgets.concat(getUserWidgets()); + widgets.concat(getUserWidgetsArray()); return widgets; } @@ -29,7 +29,7 @@ function getRoomWidgets(room) { /** * Get user specific widgets (not linked to a specific room) - * @return {[object]} Array containing current / active user widgets + * @return {object} Event content object containing current / active user widgets */ function getUserWidgets() { const client = MatrixClientPeg.get(); @@ -41,7 +41,16 @@ function getUserWidgets() { if (userWidgets && userWidgets.getContent()) { userWidgetContent = userWidgets.getContent(); } - return Object.keys(userWidgetContent).map((key) => userWidgetContent[key]); + return userWidgetContent; +} + +/** + * Get user specific widgets (not linked to a specific room) as an array + * @return {[object]} Array containing current / active user widgets + */ +function getUserWidgetsArray() { + const userWidgetContent = getUserWidgets(); + return Object.keys(userWidgetContent).map((key) => userWidgetContent[key]); } /** @@ -49,7 +58,7 @@ function getUserWidgets() { * @return {[object]} Array containing current / active stickerpicker widgets */ function getStickerpickerWidgets() { - const widgets = getUserWidgets(); + const widgets = getUserWidgetsArray(); const stickerpickerWidgets = widgets.filter((widget) => widget.type='m.stickerpicker'); return stickerpickerWidgets; } @@ -76,6 +85,7 @@ export default { getWidgets, getRoomWidgets, getUserWidgets, + getUserWidgetsArray, getStickerpickerWidgets, removeStickerpickerWidgets, };