2016-03-05 10:30:18 +08:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2015-11-03 01:39:00 +08:00
|
|
|
var MatrixClientPeg = require("./MatrixClientPeg");
|
2016-03-05 10:30:18 +08:00
|
|
|
var CallHandler = require("./CallHandler");
|
2015-09-16 21:48:49 +08:00
|
|
|
|
|
|
|
function textForMemberEvent(ev) {
|
2015-10-30 10:07:04 +08:00
|
|
|
// XXX: SYJS-16 "sender is sometimes null for join messages"
|
2015-09-16 21:48:49 +08:00
|
|
|
var senderName = ev.sender ? ev.sender.name : ev.getSender();
|
|
|
|
var targetName = ev.target ? ev.target.name : ev.getStateKey();
|
2016-03-05 10:30:18 +08:00
|
|
|
var ConferenceHandler = CallHandler.getConferenceHandler();
|
2015-09-16 21:48:49 +08:00
|
|
|
var reason = ev.getContent().reason ? (
|
|
|
|
" Reason: " + ev.getContent().reason
|
|
|
|
) : "";
|
|
|
|
switch (ev.getContent().membership) {
|
|
|
|
case 'invite':
|
2015-12-17 23:48:14 +08:00
|
|
|
var threePidContent = ev.getContent().third_party_invite;
|
|
|
|
if (threePidContent) {
|
2016-03-03 00:04:24 +08:00
|
|
|
if (threePidContent.display_name) {
|
2016-03-04 19:37:37 +08:00
|
|
|
return targetName + " accepted the invitation for " +
|
2016-03-03 00:04:24 +08:00
|
|
|
threePidContent.display_name + ".";
|
|
|
|
} else {
|
|
|
|
return targetName + " accepted an invitation.";
|
|
|
|
}
|
2015-12-17 23:48:14 +08:00
|
|
|
}
|
|
|
|
else {
|
2016-03-05 10:30:18 +08:00
|
|
|
if (ConferenceHandler && ConferenceHandler.isConferenceUser(ev.getStateKey())) {
|
|
|
|
return senderName + " requested a VoIP conference";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return senderName + " invited " + targetName + ".";
|
|
|
|
}
|
2015-12-17 23:48:14 +08:00
|
|
|
}
|
2015-09-16 21:48:49 +08:00
|
|
|
case 'ban':
|
|
|
|
return senderName + " banned " + targetName + "." + reason;
|
|
|
|
case 'join':
|
|
|
|
if (ev.getPrevContent() && ev.getPrevContent().membership == 'join') {
|
|
|
|
if (ev.getPrevContent().displayname && ev.getContent().displayname && 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) {
|
2016-03-18 03:05:39 +08:00
|
|
|
return ev.getSender() + " removed their display name (" + ev.getPrevContent().displayname + ")";
|
2015-09-16 21:48:49 +08:00
|
|
|
} else if (ev.getPrevContent().avatar_url && !ev.getContent().avatar_url) {
|
2016-03-18 03:05:39 +08:00
|
|
|
return senderName + " removed their profile picture";
|
2015-09-16 21:48:49 +08:00
|
|
|
} else if (ev.getPrevContent().avatar_url && ev.getContent().avatar_url && ev.getPrevContent().avatar_url != ev.getContent().avatar_url) {
|
2016-03-18 03:05:39 +08:00
|
|
|
return senderName + " changed their profile picture";
|
2015-09-16 21:48:49 +08:00
|
|
|
} else if (!ev.getPrevContent().avatar_url && ev.getContent().avatar_url) {
|
2016-03-18 03:05:39 +08:00
|
|
|
return senderName + " set a profile picture";
|
2015-09-16 21:48:49 +08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!ev.target) console.warn("Join message has no target! -- " + ev.getContent().state_key);
|
2016-03-05 10:30:18 +08:00
|
|
|
if (ConferenceHandler && ConferenceHandler.isConferenceUser(ev.getStateKey())) {
|
|
|
|
return "VoIP conference started";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return targetName + " joined the room.";
|
|
|
|
}
|
2015-09-16 21:48:49 +08:00
|
|
|
}
|
|
|
|
return '';
|
|
|
|
case 'leave':
|
|
|
|
if (ev.getSender() === ev.getStateKey()) {
|
2016-03-05 10:30:18 +08:00
|
|
|
if (ConferenceHandler && ConferenceHandler.isConferenceUser(ev.getStateKey())) {
|
|
|
|
return "VoIP conference finished";
|
|
|
|
}
|
2016-03-16 09:16:15 +08:00
|
|
|
else if (ev.getPrevContent().membership === "invite") {
|
|
|
|
return targetName + " rejected the invitation.";
|
|
|
|
}
|
2016-03-05 10:30:18 +08:00
|
|
|
else {
|
|
|
|
return targetName + " left the room.";
|
|
|
|
}
|
2015-09-16 21:48:49 +08:00
|
|
|
}
|
|
|
|
else if (ev.getPrevContent().membership === "ban") {
|
|
|
|
return senderName + " unbanned " + targetName + ".";
|
|
|
|
}
|
|
|
|
else if (ev.getPrevContent().membership === "join") {
|
|
|
|
return senderName + " kicked " + targetName + "." + reason;
|
|
|
|
}
|
2015-10-05 23:44:50 +08:00
|
|
|
else if (ev.getPrevContent().membership === "invite") {
|
|
|
|
return senderName + " withdrew " + targetName + "'s invitation." + reason;
|
|
|
|
}
|
2015-09-16 21:48:49 +08:00
|
|
|
else {
|
|
|
|
return targetName + " left the room.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function textForTopicEvent(ev) {
|
|
|
|
var senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
|
|
|
|
2016-01-17 10:48:55 +08:00
|
|
|
return senderDisplayName + ' changed the topic to "' + ev.getContent().topic + '"';
|
2015-09-16 21:48:49 +08:00
|
|
|
};
|
|
|
|
|
2015-10-30 10:07:04 +08:00
|
|
|
function textForRoomNameEvent(ev) {
|
|
|
|
var senderDisplayName = ev.sender && ev.sender.name ? ev.sender.name : ev.getSender();
|
|
|
|
|
|
|
|
return senderDisplayName + ' changed the room name to "' + ev.getContent().name + '"';
|
|
|
|
};
|
|
|
|
|
2015-09-16 21:48:49 +08: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;
|
|
|
|
};
|
|
|
|
|
|
|
|
function textForCallAnswerEvent(event) {
|
|
|
|
var senderName = event.sender ? event.sender.name : "Someone";
|
2015-11-03 01:39:00 +08:00
|
|
|
var supported = MatrixClientPeg.get().supportsVoip() ? "" : " (not supported by this browser)";
|
|
|
|
return senderName + " answered the call." + supported;
|
2015-09-16 21:48:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function textForCallHangupEvent(event) {
|
|
|
|
var senderName = event.sender ? event.sender.name : "Someone";
|
2015-11-03 01:39:00 +08:00
|
|
|
var supported = MatrixClientPeg.get().supportsVoip() ? "" : " (not supported by this browser)";
|
|
|
|
return senderName + " ended the call." + supported;
|
2015-09-16 21:48:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
function textForCallInviteEvent(event) {
|
|
|
|
var senderName = event.sender ? event.sender.name : "Someone";
|
|
|
|
// FIXME: Find a better way to determine this from the event?
|
|
|
|
var type = "voice";
|
|
|
|
if (event.getContent().offer && event.getContent().offer.sdp &&
|
|
|
|
event.getContent().offer.sdp.indexOf('m=video') !== -1) {
|
|
|
|
type = "video";
|
|
|
|
}
|
2015-11-03 01:39:00 +08:00
|
|
|
var supported = MatrixClientPeg.get().supportsVoip() ? "" : " (not supported by this browser)";
|
|
|
|
return senderName + " placed a " + type + " call." + supported;
|
2015-09-16 21:48:49 +08:00
|
|
|
};
|
|
|
|
|
2015-12-17 23:48:14 +08:00
|
|
|
function textForThreePidInviteEvent(event) {
|
|
|
|
var senderName = event.sender ? event.sender.name : event.getSender();
|
|
|
|
return senderName + " sent an invitation to " + event.getContent().display_name +
|
|
|
|
" to join the room.";
|
|
|
|
};
|
|
|
|
|
2016-03-16 07:47:40 +08:00
|
|
|
function textForHistoryVisibilityEvent(event) {
|
|
|
|
var senderName = event.sender ? event.sender.name : event.getSender();
|
|
|
|
var vis = event.getContent().history_visibility;
|
|
|
|
var text = senderName + " made future room history visible to ";
|
|
|
|
if (vis === "invited") {
|
|
|
|
text += "all room members, from the point they are invited.";
|
|
|
|
}
|
|
|
|
else if (vis === "joined") {
|
|
|
|
text += "all room members, from the point they joined.";
|
|
|
|
}
|
|
|
|
else if (vis === "shared") {
|
|
|
|
text += "all room members.";
|
|
|
|
}
|
|
|
|
else if (vis === "world_readable") {
|
|
|
|
text += "anyone.";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
text += " unknown (" + vis + ")";
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2015-09-16 21:48:49 +08:00
|
|
|
var handlers = {
|
|
|
|
'm.room.message': textForMessageEvent,
|
2015-10-30 10:07:04 +08:00
|
|
|
'm.room.name': textForRoomNameEvent,
|
|
|
|
'm.room.topic': textForTopicEvent,
|
|
|
|
'm.room.member': textForMemberEvent,
|
|
|
|
'm.call.invite': textForCallInviteEvent,
|
|
|
|
'm.call.answer': textForCallAnswerEvent,
|
|
|
|
'm.call.hangup': textForCallHangupEvent,
|
2016-03-16 07:47:40 +08:00
|
|
|
'm.room.third_party_invite': textForThreePidInviteEvent,
|
|
|
|
'm.room.history_visibility': textForHistoryVisibilityEvent,
|
2015-09-16 21:48:49 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
textForEvent: function(ev) {
|
|
|
|
var hdlr = handlers[ev.getType()];
|
|
|
|
if (!hdlr) return "";
|
|
|
|
return hdlr(ev);
|
|
|
|
}
|
|
|
|
}
|