2015-07-17 12:24:54 +02:00
|
|
|
|
|
|
|
function textForMemberEvent(ev) {
|
|
|
|
// XXX: SYJS-16
|
|
|
|
var senderName = ev.sender ? ev.sender.name : ev.getSender();
|
2015-07-20 14:30:01 +02:00
|
|
|
var targetName = ev.target ? ev.target.name : ev.getStateKey();
|
2015-07-17 13:03:10 +02:00
|
|
|
var reason = ev.getContent().reason ? (
|
|
|
|
" Reason: " + ev.getContent().reason
|
|
|
|
) : "";
|
2015-07-17 12:24:54 +02:00
|
|
|
switch (ev.getContent().membership) {
|
|
|
|
case 'invite':
|
|
|
|
return senderName + " invited " + targetName + ".";
|
2015-07-17 13:03:10 +02:00
|
|
|
case 'ban':
|
|
|
|
return senderName + " banned " + targetName + "." + reason;
|
2015-07-17 12:24:54 +02:00
|
|
|
case 'join':
|
|
|
|
if (ev.getPrevContent() && ev.getPrevContent().membership == 'join') {
|
2015-07-17 18:13:10 +02:00
|
|
|
if (ev.getPrevContent().displayname && ev.getContent().displayname && ev.getPrevContent().displayname != ev.getContent().displayname) {
|
2015-07-17 12:24:54 +02:00
|
|
|
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";
|
2015-07-17 17:41:27 +02:00
|
|
|
} else if (ev.getPrevContent().avatar_url && !ev.getContent().avatar_url) {
|
|
|
|
return ev.getSender() + " removed their profile picture";
|
2015-07-17 18:13:10 +02:00
|
|
|
} else if (ev.getPrevContent().avatar_url && ev.getContent().avatar_url && ev.getPrevContent().avatar_url != ev.getContent().avatar_url) {
|
2015-07-17 17:41:27 +02:00
|
|
|
return ev.getSender() + " changed their profile picture";
|
|
|
|
} else if (!ev.getPrevContent().avatar_url && ev.getContent().avatar_url) {
|
|
|
|
return ev.getSender() + " set a profile picture";
|
2015-07-17 12:24:54 +02:00
|
|
|
}
|
|
|
|
} else {
|
2015-07-17 16:55:36 +02:00
|
|
|
if (!ev.target) console.warn("Join message has no target! -- " + ev.getContent().state_key);
|
2015-07-17 12:24:54 +02:00
|
|
|
return targetName + " joined the room.";
|
|
|
|
}
|
2015-07-18 20:06:36 +02:00
|
|
|
return '';
|
2015-07-17 12:24:54 +02:00
|
|
|
case 'leave':
|
2015-07-17 13:03:10 +02:00
|
|
|
if (ev.getSender() === ev.getStateKey()) {
|
|
|
|
return targetName + " left the room.";
|
|
|
|
}
|
|
|
|
else if (ev.getPrevContent().membership === "ban") {
|
|
|
|
return senderName + " unbanned " + targetName + ".";
|
|
|
|
}
|
|
|
|
else if (ev.getPrevContent().membership === "join") {
|
|
|
|
return senderName + " kicked " + targetName + "." + reason;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return targetName + " left the room.";
|
|
|
|
}
|
2015-07-17 12:24:54 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-17 19:48:53 +02:00
|
|
|
function textForTopicEvent(ev) {
|
|
|
|
var senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
|
|
|
|
|
|
|
return senderDisplayName + ' changed the topic to, "' + ev.getContent().topic + '"';
|
|
|
|
};
|
|
|
|
|
2015-07-17 12:56:55 +02:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2015-07-17 12:24:54 +02:00
|
|
|
var handlers = {
|
2015-07-17 12:56:55 +02:00
|
|
|
'm.room.message': textForMessageEvent,
|
2015-07-17 19:48:53 +02:00
|
|
|
'm.room.topic': textForTopicEvent,
|
2015-07-17 12:24:54 +02:00
|
|
|
'm.room.member': textForMemberEvent
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2015-07-17 15:42:36 +02:00
|
|
|
textForEvent: function(ev) {
|
2015-07-17 12:24:54 +02:00
|
|
|
var hdlr = handlers[ev.getType()];
|
2015-07-17 13:23:24 +02:00
|
|
|
if (!hdlr) return "";
|
2015-07-17 12:24:54 +02:00
|
|
|
return hdlr(ev);
|
|
|
|
}
|
|
|
|
}
|