From 7c336cd00afbe0487cbacb05d8fe22a2349ea0c1 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 16 Aug 2017 17:46:20 +0100 Subject: [PATCH 1/5] Implement TextualEvent tiles for im.vector.modular.widgets E.g. "Bob added a Acme widget", "Susan removed a Giraffe widget" --- src/TextForEvent.js | 21 +++++++++++++++++++++ src/components/views/rooms/EventTile.js | 2 ++ src/i18n/strings/en_EN.json | 4 +++- 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/TextForEvent.js b/src/TextForEvent.js index de12cec502..ed06cc2199 100644 --- a/src/TextForEvent.js +++ b/src/TextForEvent.js @@ -248,6 +248,25 @@ function textForPowerEvent(event) { }); } +function textForWidgetEvent(event) { + const senderName = event.sender ? event.sender.name : event.getSender(); + const prevWidgetName = event.getPrevContent() ? event.getPrevContent().name : null; + let widgetName = event.getContent() ? event.getContent().name : prevWidgetName; + + // Apply sentence case + widgetName = widgetName ? widgetName[0].toUpperCase() + widgetName.slice(1).toLowerCase() + ' ' : ''; + + if (event.getContent().url) { + return _t('%(senderName)s added a %(widgetName)swidget', { + senderName, widgetName, + }); + } else { + return _t('%(senderName)s removed a %(widgetName)swidget', { + senderName, widgetName, + }); + } +} + var handlers = { 'm.room.message': textForMessageEvent, 'm.room.name': textForRoomNameEvent, @@ -260,6 +279,8 @@ var handlers = { 'm.room.history_visibility': textForHistoryVisibilityEvent, 'm.room.encryption': textForEncryptionEvent, 'm.room.power_levels': textForPowerEvent, + + 'im.vector.modular.widgets': textForWidgetEvent, }; module.exports = { diff --git a/src/components/views/rooms/EventTile.js b/src/components/views/rooms/EventTile.js index 776d8a264b..a6f8ed5542 100644 --- a/src/components/views/rooms/EventTile.js +++ b/src/components/views/rooms/EventTile.js @@ -44,6 +44,8 @@ var eventTileTypes = { 'm.room.history_visibility' : 'messages.TextualEvent', 'm.room.encryption' : 'messages.TextualEvent', 'm.room.power_levels' : 'messages.TextualEvent', + + 'im.vector.modular.widgets': 'messages.TextualEvent', }; var MAX_READ_AVATARS = 5; diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 83be31de4b..0cd463fe8b 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -969,5 +969,7 @@ "Automatically replace plain text Emoji": "Automatically replace plain text Emoji", "Failed to upload image": "Failed to upload image", "Failed to update group": "Failed to update group", - "Hide avatars in user and room mentions": "Hide avatars in user and room mentions" + "Hide avatars in user and room mentions": "Hide avatars in user and room mentions", + "%(senderName)s added a %(widgetName)swidget": "%(senderName)s added a %(widgetName)swidget", + "%(senderName)s removed a %(widgetName)swidget": "%(senderName)s removed a %(widgetName)swidget" } From acc54b97f1c5e98bcaa0cd0dc481824b5bd998e7 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 16 Aug 2017 17:46:20 +0100 Subject: [PATCH 2/5] Implement TextualEvent tiles for im.vector.modular.widgets E.g. "Bob added a Acme widget", "Susan removed a Giraffe widget" The name is calculated by taking the `name` in the event content, falling back on the `type`, falling back on the previous content `type`. This is then capitalised. --- src/TextForEvent.js | 22 ++++++++++++++++++++++ src/components/views/rooms/EventTile.js | 2 ++ src/i18n/strings/en_EN.json | 4 +++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/TextForEvent.js b/src/TextForEvent.js index de12cec502..d55e7f95c2 100644 --- a/src/TextForEvent.js +++ b/src/TextForEvent.js @@ -248,6 +248,26 @@ function textForPowerEvent(event) { }); } +function textForWidgetEvent(event) { + const senderName = event.sender ? event.sender.name : event.getSender(); + const previousContent = event.getPrevContent() ? event.getPrevContent() : {}; + const {name, type} = event.getContent() ? event.getContent() : {}; + let widgetName = widgetName || name || type || previousContent.type || ''; + + // Apply sentence case + widgetName = widgetName ? widgetName[0].toUpperCase() + widgetName.slice(1).toLowerCase() + ' ' : ''; + + if (event.getContent().url) { + return _t('%(senderName)s added a %(widgetName)swidget', { + senderName, widgetName, + }); + } else { + return _t('%(senderName)s removed a %(widgetName)swidget', { + senderName, widgetName, + }); + } +} + var handlers = { 'm.room.message': textForMessageEvent, 'm.room.name': textForRoomNameEvent, @@ -260,6 +280,8 @@ var handlers = { 'm.room.history_visibility': textForHistoryVisibilityEvent, 'm.room.encryption': textForEncryptionEvent, 'm.room.power_levels': textForPowerEvent, + + 'im.vector.modular.widgets': textForWidgetEvent, }; module.exports = { diff --git a/src/components/views/rooms/EventTile.js b/src/components/views/rooms/EventTile.js index 776d8a264b..a6f8ed5542 100644 --- a/src/components/views/rooms/EventTile.js +++ b/src/components/views/rooms/EventTile.js @@ -44,6 +44,8 @@ var eventTileTypes = { 'm.room.history_visibility' : 'messages.TextualEvent', 'm.room.encryption' : 'messages.TextualEvent', 'm.room.power_levels' : 'messages.TextualEvent', + + 'im.vector.modular.widgets': 'messages.TextualEvent', }; var MAX_READ_AVATARS = 5; diff --git a/src/i18n/strings/en_EN.json b/src/i18n/strings/en_EN.json index 83be31de4b..0cd463fe8b 100644 --- a/src/i18n/strings/en_EN.json +++ b/src/i18n/strings/en_EN.json @@ -969,5 +969,7 @@ "Automatically replace plain text Emoji": "Automatically replace plain text Emoji", "Failed to upload image": "Failed to upload image", "Failed to update group": "Failed to update group", - "Hide avatars in user and room mentions": "Hide avatars in user and room mentions" + "Hide avatars in user and room mentions": "Hide avatars in user and room mentions", + "%(senderName)s added a %(widgetName)swidget": "%(senderName)s added a %(widgetName)swidget", + "%(senderName)s removed a %(widgetName)swidget": "%(senderName)s removed a %(widgetName)swidget" } From 20a0ec35aff3fefaef52ba6babafec107b7f8b03 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Fri, 18 Aug 2017 10:45:43 +0100 Subject: [PATCH 3/5] Use short form to get event URL --- src/TextForEvent.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TextForEvent.js b/src/TextForEvent.js index d55e7f95c2..8d6ef41180 100644 --- a/src/TextForEvent.js +++ b/src/TextForEvent.js @@ -251,13 +251,13 @@ function textForPowerEvent(event) { function textForWidgetEvent(event) { const senderName = event.sender ? event.sender.name : event.getSender(); const previousContent = event.getPrevContent() ? event.getPrevContent() : {}; - const {name, type} = event.getContent() ? event.getContent() : {}; + const {name, type, url} = event.getContent() ? event.getContent() : {}; let widgetName = widgetName || name || type || previousContent.type || ''; // Apply sentence case widgetName = widgetName ? widgetName[0].toUpperCase() + widgetName.slice(1).toLowerCase() + ' ' : ''; - if (event.getContent().url) { + if (url) { return _t('%(senderName)s added a %(widgetName)swidget', { senderName, widgetName, }); From 6c37a9a3aff74bc59646fe20c1f372c3e8892940 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Fri, 18 Aug 2017 12:03:29 +0100 Subject: [PATCH 4/5] Simplify getting of content, prev content for widget event --- src/TextForEvent.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TextForEvent.js b/src/TextForEvent.js index 8d6ef41180..d75ef34c1c 100644 --- a/src/TextForEvent.js +++ b/src/TextForEvent.js @@ -250,8 +250,8 @@ function textForPowerEvent(event) { function textForWidgetEvent(event) { const senderName = event.sender ? event.sender.name : event.getSender(); - const previousContent = event.getPrevContent() ? event.getPrevContent() : {}; - const {name, type, url} = event.getContent() ? event.getContent() : {}; + const previousContent = event.getPrevContent() || {}; + const {name, type, url} = event.getContent() || {}; let widgetName = widgetName || name || type || previousContent.type || ''; // Apply sentence case From 1bba1f0fa61034804e7a6052c0c383c5c87fdb28 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Fri, 18 Aug 2017 12:04:34 +0100 Subject: [PATCH 5/5] Add comment --- src/TextForEvent.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/TextForEvent.js b/src/TextForEvent.js index d75ef34c1c..f99cf7bde5 100644 --- a/src/TextForEvent.js +++ b/src/TextForEvent.js @@ -257,6 +257,8 @@ function textForWidgetEvent(event) { // Apply sentence case widgetName = widgetName ? widgetName[0].toUpperCase() + widgetName.slice(1).toLowerCase() + ' ' : ''; + // If the widget was removed, its content should be {}, but this is sufficiently + // equivalent to that condition. if (url) { return _t('%(senderName)s added a %(widgetName)swidget', { senderName, widgetName,