From 1b03aa8119a5d20fca9fa7b878c56c89c1fdf152 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 17 Jul 2015 11:24:54 +0100 Subject: [PATCH 1/2] Display nick changes and factor out getting text for events into a common place. --- skins/base/views/molecules/MRoomMemberTile.js | 14 +------ skins/base/views/organisms/Notifier.js | 22 +---------- src/TextForEvent.js | 38 +++++++++++++++++++ 3 files changed, 42 insertions(+), 32 deletions(-) create mode 100644 src/TextForEvent.js diff --git a/skins/base/views/molecules/MRoomMemberTile.js b/skins/base/views/molecules/MRoomMemberTile.js index 996b6f9c..e5a7f715 100644 --- a/skins/base/views/molecules/MRoomMemberTile.js +++ b/skins/base/views/molecules/MRoomMemberTile.js @@ -22,6 +22,7 @@ var MRoomMemberTileController = require("../../../../src/controllers/molecules/M var MatrixClientPeg = require("../../../../src/MatrixClientPeg"); var ComponentBroker = require('../../../../src/ComponentBroker'); +var TextForEvent = require('../../../../src/TextForEvent'); var MessageTimestamp = ComponentBroker.get('atoms/MessageTimestamp'); module.exports = React.createClass({ @@ -29,18 +30,7 @@ module.exports = React.createClass({ mixins: [MRoomMemberTileController], getMemberEventText: function() { - var ev = this.props.mxEvent; - // XXX: SYJS-16 - var senderName = ev.sender ? ev.sender.name : "Someone"; - var targetName = ev.target ? ev.target.name : "Someone"; - switch (ev.getContent().membership) { - case 'invite': - return senderName + " invited " + targetName + "."; - case 'join': - return targetName + " joined the room."; - case 'leave': - return targetName + " left the room."; - } + return TextForEvent.textForEvent(this.props.mxEvent); }, render: function() { diff --git a/skins/base/views/organisms/Notifier.js b/skins/base/views/organisms/Notifier.js index 09f1921a..34b37246 100644 --- a/skins/base/views/organisms/Notifier.js +++ b/skins/base/views/organisms/Notifier.js @@ -19,32 +19,14 @@ limitations under the License. var NotifierController = require("../../../../src/controllers/organisms/Notifier"); var MatrixClientPeg = require("../../../../src/MatrixClientPeg"); +var TextForEvent = require("../../../../src/TextForEvent"); var extend = require("../../../../src/extend"); var dis = require("../../../../src/dispatcher"); var NotifierView = { notificationMessageForEvent: function(ev) { - var senderDisplayName = ev.sender ? ev.sender.name : ''; - var message = null; - - if (ev.event.type === "m.room.message") { - message = ev.getContent().body; - if (ev.getContent().msgtype === "m.emote") { - message = "* " + senderDisplayName + " " + message; - } else if (ev.getContent().msgtype === "m.image") { - message = senderDisplayName + " sent an image."; - } - } else if (ev.event.type == "m.room.member") { - if (ev.event.state_key !== MatrixClientPeg.get().credentials.userId && "join" === ev.getContent().membership) { - // Notify when another user joins - message = senderDisplayName + " joined"; - } else if (ev.event.state_key === MatrixClientPeg.get().credentials.userId && "invite" === ev.getContent().membership) { - // notify when you are invited - message = senderDisplayName + " invited you to a room"; - } - } - return message; + return TextForEvent.textForEvent(ev); }, displayNotification: function(ev, room) { diff --git a/src/TextForEvent.js b/src/TextForEvent.js new file mode 100644 index 00000000..e53bb3ef --- /dev/null +++ b/src/TextForEvent.js @@ -0,0 +1,38 @@ + +function textForMemberEvent(ev) { + // XXX: SYJS-16 + var senderName = ev.sender ? ev.sender.name : ev.getSender(); + var targetName = ev.target ? ev.target.name : ev.getContent().target; + switch (ev.getContent().membership) { + case 'invite': + return senderName + " invited " + targetName + "."; + case 'join': + if (ev.getPrevContent() && ev.getPrevContent().membership == 'join') { + if (ev.getPrevContent().displayname && ev.getContent().displayname) { + return ev.getSender() + " changed their display name from " + + ev.getPrevContent().displayname + " to " + + ev.getContent().displayname; + } else if (!ev.getPrevContent().displayname && ev.getContent().displayname) { + return ev.getSender() + " set their display name to " + ev.getContent().displayname; + } else if (ev.getPrevContent().displayname && !ev.getContent().displayname) { + return ev.getSender() + " removed their display name"; + } + } else { + return targetName + " joined the room."; + } + case 'leave': + return targetName + " left the room."; + } +}; + +var handlers = { + 'm.room.member': textForMemberEvent +}; + +module.exports = { + textForEvent(ev) { + var hdlr = handlers[ev.getType()]; + if (!hdlr) return "Unknown entry event"; + return hdlr(ev); + } +} From 37108ac56c071c4516827ffe7dd9907dbaaa7ab9 Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 17 Jul 2015 11:56:55 +0100 Subject: [PATCH 2/2] Make notifications sensible again and have information once and only once. --- skins/base/views/organisms/Notifier.js | 10 ++++++++++ src/TextForEvent.js | 13 +++++++++++++ 2 files changed, 23 insertions(+) diff --git a/skins/base/views/organisms/Notifier.js b/skins/base/views/organisms/Notifier.js index 34b37246..7df76e29 100644 --- a/skins/base/views/organisms/Notifier.js +++ b/skins/base/views/organisms/Notifier.js @@ -43,8 +43,18 @@ var NotifierView = { var title; if (!ev.sender || room.name == ev.sender.name) { title = room.name; + // notificationMessageForEvent includes sender, + // but we already have the sender here + if (ev.getContent().body) msg = ev.getContent().body; + } else if (ev.getType() == 'm.room.member') { + // context is all in the message here, we don't need + // to display sender info + title = room.name; } else if (ev.sender) { title = ev.sender.name + " (" + room.name + ")"; + // notificationMessageForEvent includes sender, + // but we've just out sender in the title + if (ev.getContent().body) msg = ev.getContent().body; } var notification = new global.Notification( diff --git a/src/TextForEvent.js b/src/TextForEvent.js index e53bb3ef..4788c9e8 100644 --- a/src/TextForEvent.js +++ b/src/TextForEvent.js @@ -25,7 +25,20 @@ function textForMemberEvent(ev) { } }; +function textForMessageEvent(ev) { + var senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender(); + + var message = senderDisplayName + ': ' + ev.getContent().body; + if (ev.getContent().msgtype === "m.emote") { + message = "* " + senderDisplayName + " " + message; + } else if (ev.getContent().msgtype === "m.image") { + message = senderDisplayName + " sent an image."; + } + return message; +}; + var handlers = { + 'm.room.message': textForMessageEvent, 'm.room.member': textForMemberEvent };