Display nick changes and factor out getting text for events into a common place.

This commit is contained in:
David Baker 2015-07-17 11:24:54 +01:00
parent 891ba40114
commit 1b03aa8119
3 changed files with 42 additions and 32 deletions

View File

@ -22,6 +22,7 @@ var MRoomMemberTileController = require("../../../../src/controllers/molecules/M
var MatrixClientPeg = require("../../../../src/MatrixClientPeg"); var MatrixClientPeg = require("../../../../src/MatrixClientPeg");
var ComponentBroker = require('../../../../src/ComponentBroker'); var ComponentBroker = require('../../../../src/ComponentBroker');
var TextForEvent = require('../../../../src/TextForEvent');
var MessageTimestamp = ComponentBroker.get('atoms/MessageTimestamp'); var MessageTimestamp = ComponentBroker.get('atoms/MessageTimestamp');
module.exports = React.createClass({ module.exports = React.createClass({
@ -29,18 +30,7 @@ module.exports = React.createClass({
mixins: [MRoomMemberTileController], mixins: [MRoomMemberTileController],
getMemberEventText: function() { getMemberEventText: function() {
var ev = this.props.mxEvent; return TextForEvent.textForEvent(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.";
}
}, },
render: function() { render: function() {

View File

@ -19,32 +19,14 @@ limitations under the License.
var NotifierController = require("../../../../src/controllers/organisms/Notifier"); var NotifierController = require("../../../../src/controllers/organisms/Notifier");
var MatrixClientPeg = require("../../../../src/MatrixClientPeg"); var MatrixClientPeg = require("../../../../src/MatrixClientPeg");
var TextForEvent = require("../../../../src/TextForEvent");
var extend = require("../../../../src/extend"); var extend = require("../../../../src/extend");
var dis = require("../../../../src/dispatcher"); var dis = require("../../../../src/dispatcher");
var NotifierView = { var NotifierView = {
notificationMessageForEvent: function(ev) { notificationMessageForEvent: function(ev) {
var senderDisplayName = ev.sender ? ev.sender.name : ''; return TextForEvent.textForEvent(ev);
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;
}, },
displayNotification: function(ev, room) { displayNotification: function(ev, room) {

38
src/TextForEvent.js Normal file
View File

@ -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);
}
}