2016-03-05 03:30:18 +01: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-02 18:39:00 +01:00
var MatrixClientPeg = require ( "./MatrixClientPeg" ) ;
2016-03-05 03:30:18 +01:00
var CallHandler = require ( "./CallHandler" ) ;
2017-05-25 12:39:08 +02:00
import { _t } from './languageHandler' ;
2017-04-10 11:09:26 +02:00
import * as Roles from './Roles' ;
2017-04-06 18:02:35 +02:00
2015-09-16 15:48:49 +02:00
function textForMemberEvent ( ev ) {
2015-10-30 03:07:04 +01:00
// XXX: SYJS-16 "sender is sometimes null for join messages"
2015-09-16 15:48:49 +02:00
var senderName = ev . sender ? ev . sender . name : ev . getSender ( ) ;
var targetName = ev . target ? ev . target . name : ev . getStateKey ( ) ;
2016-03-05 03:30:18 +01:00
var ConferenceHandler = CallHandler . getConferenceHandler ( ) ;
2015-09-16 15:48:49 +02:00
var reason = ev . getContent ( ) . reason ? (
2017-05-23 16:16:31 +02:00
_t ( 'Reason' ) + ': ' + ev . getContent ( ) . reason
2015-09-16 15:48:49 +02:00
) : "" ;
switch ( ev . getContent ( ) . membership ) {
case 'invite' :
2015-12-17 16:48:14 +01:00
var threePidContent = ev . getContent ( ) . third _party _invite ;
if ( threePidContent ) {
2016-03-02 17:04:24 +01:00
if ( threePidContent . display _name ) {
2017-05-25 20:21:18 +02:00
return _t ( '%(targetName)s accepted the invitation for %(displayName)s.' , { targetName : targetName , displayName : threePidContent . display _name } ) ;
2016-03-02 17:04:24 +01:00
} else {
2017-05-25 20:21:18 +02:00
return _t ( '%(targetName)s accepted an invitation.' , { targetName : targetName } ) ;
2016-03-02 17:04:24 +01:00
}
2015-12-17 16:48:14 +01:00
}
else {
2016-03-05 03:30:18 +01:00
if ( ConferenceHandler && ConferenceHandler . isConferenceUser ( ev . getStateKey ( ) ) ) {
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s requested a VoIP conference.' , { senderName : senderName } ) ;
2016-03-05 03:30:18 +01:00
}
else {
2017-05-25 20:21:18 +02:00
return _t ( '%(senderName)s invited %(targetName)s.' , { senderName : senderName , targetName : targetName } ) ;
2016-03-05 03:30:18 +01:00
}
2015-12-17 16:48:14 +01:00
}
2015-09-16 15:48:49 +02:00
case 'ban' :
2017-05-25 20:21:18 +02:00
return _t (
2017-05-25 20:43:34 +02:00
'%(senderName)s banned %(targetName)s.' ,
{ senderName : senderName , targetName : targetName }
) + ' ' + reason ;
2015-09-16 15:48:49 +02:00
case 'join' :
if ( ev . getPrevContent ( ) && ev . getPrevContent ( ) . membership == 'join' ) {
if ( ev . getPrevContent ( ) . displayname && ev . getContent ( ) . displayname && ev . getPrevContent ( ) . displayname != ev . getContent ( ) . displayname ) {
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s changed their display name from %(oldDisplayName)s to %(displayName)s.' , { senderName : ev . getSender ( ) , oldDisplayName : ev . getPrevContent ( ) . displayname , displayName : ev . getContent ( ) . displayname } ) ;
2015-09-16 15:48:49 +02:00
} else if ( ! ev . getPrevContent ( ) . displayname && ev . getContent ( ) . displayname ) {
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s set their display name to %(displayName)s.' , { senderName : ev . getSender ( ) , displayName : ev . getContent ( ) . displayname } ) ;
2015-09-16 15:48:49 +02:00
} else if ( ev . getPrevContent ( ) . displayname && ! ev . getContent ( ) . displayname ) {
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s removed their display name (%(oldDisplayName)s).' , { senderName : ev . getSender ( ) , oldDisplayName : ev . getPrevContent ( ) . displayname } ) ;
2015-09-16 15:48:49 +02:00
} else if ( ev . getPrevContent ( ) . avatar _url && ! ev . getContent ( ) . avatar _url ) {
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s removed their profile picture.' , { senderName : senderName } ) ;
2015-09-16 15:48:49 +02:00
} else if ( ev . getPrevContent ( ) . avatar _url && ev . getContent ( ) . avatar _url && ev . getPrevContent ( ) . avatar _url != ev . getContent ( ) . avatar _url ) {
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s changed their profile picture.' , { senderName : senderName } ) ;
2015-09-16 15:48:49 +02:00
} else if ( ! ev . getPrevContent ( ) . avatar _url && ev . getContent ( ) . avatar _url ) {
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s set a profile picture.' , { senderName : senderName } ) ;
2016-09-02 17:54:27 +02:00
} else {
2017-05-06 02:45:28 +02:00
// suppress null rejoins
return '' ;
2015-09-16 15:48:49 +02:00
}
} else {
if ( ! ev . target ) console . warn ( "Join message has no target! -- " + ev . getContent ( ) . state _key ) ;
2016-03-05 03:30:18 +01:00
if ( ConferenceHandler && ConferenceHandler . isConferenceUser ( ev . getStateKey ( ) ) ) {
2017-05-27 15:55:55 +02:00
return _t ( 'VoIP conference started.' ) ;
2016-03-05 03:30:18 +01:00
}
else {
2017-05-25 20:21:18 +02:00
return _t ( '%(targetName)s joined the room.' , { targetName : targetName } ) ;
2016-03-05 03:30:18 +01:00
}
2015-09-16 15:48:49 +02:00
}
case 'leave' :
if ( ev . getSender ( ) === ev . getStateKey ( ) ) {
2016-03-05 03:30:18 +01:00
if ( ConferenceHandler && ConferenceHandler . isConferenceUser ( ev . getStateKey ( ) ) ) {
2017-05-27 15:55:55 +02:00
return _t ( 'VoIP conference finished.' ) ;
2016-03-05 03:30:18 +01:00
}
2016-03-16 02:16:15 +01:00
else if ( ev . getPrevContent ( ) . membership === "invite" ) {
2017-05-25 20:21:18 +02:00
return _t ( '%(targetName)s rejected the invitation.' , { targetName : targetName } ) ;
2016-03-16 02:16:15 +01:00
}
2016-03-05 03:30:18 +01:00
else {
2017-05-25 20:21:18 +02:00
return _t ( '%(targetName)s left the room.' , { targetName : targetName } ) ;
2016-03-05 03:30:18 +01:00
}
2015-09-16 15:48:49 +02:00
}
else if ( ev . getPrevContent ( ) . membership === "ban" ) {
2017-05-25 20:43:34 +02:00
return _t ( '%(senderName)s unbanned %(targetName)s.' , { senderName : senderName , targetName : targetName } ) ;
2015-09-16 15:48:49 +02:00
}
else if ( ev . getPrevContent ( ) . membership === "join" ) {
2017-05-25 20:21:18 +02:00
return _t (
2017-05-25 20:25:06 +02:00
'%(senderName)s kicked %(targetName)s.' ,
{ senderName : senderName , targetName : targetName }
2017-05-25 20:43:34 +02:00
) + ' ' + reason ;
2015-09-16 15:48:49 +02:00
}
2015-10-05 17:44:50 +02:00
else if ( ev . getPrevContent ( ) . membership === "invite" ) {
2017-05-25 20:21:18 +02:00
return _t (
2017-05-25 20:25:06 +02:00
'%(senderName)s withdrew %(targetName)s\'s inivitation.' ,
{ senderName : senderName , targetName : targetName }
2017-05-25 20:43:34 +02:00
) + ' ' + reason ;
2015-10-05 17:44:50 +02:00
}
2015-09-16 15:48:49 +02:00
else {
2017-05-25 20:21:18 +02:00
return _t ( '%(targetName)s left the room.' , { targetName : targetName } ) ;
2015-09-16 15:48:49 +02:00
}
}
2016-09-15 18:01:02 +02:00
}
2015-09-16 15:48:49 +02:00
function textForTopicEvent ( ev ) {
var senderDisplayName = ev . sender && ev . sender . name ? ev . sender . name : ev . getSender ( ) ;
2017-05-27 15:55:55 +02:00
return _t ( '%(senderDisplayName)s changed the topic to "%(topic)s".' , { senderDisplayName : senderDisplayName , topic : ev . getContent ( ) . topic } ) ;
2016-09-15 18:01:02 +02:00
}
2015-09-16 15:48:49 +02:00
2015-10-30 03:07:04 +01:00
function textForRoomNameEvent ( ev ) {
var senderDisplayName = ev . sender && ev . sender . name ? ev . sender . name : ev . getSender ( ) ;
2017-05-27 15:55:55 +02:00
return _t ( '%(senderDisplayName)s changed the room name to %(roomName)s.' , { senderDisplayName : senderDisplayName , roomName : ev . getContent ( ) . name } ) ;
2016-09-15 18:01:02 +02:00
}
2015-10-30 03:07:04 +01:00
2015-09-16 15:48:49 +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" ) {
2017-05-25 20:21:18 +02:00
message = _t ( '%(senderDisplayName)s sent an image.' , { senderDisplayName : senderDisplayName } ) ;
2015-09-16 15:48:49 +02:00
}
return message ;
2016-09-15 18:01:02 +02:00
}
2015-09-16 15:48:49 +02:00
function textForCallAnswerEvent ( event ) {
2017-05-23 16:16:31 +02:00
var senderName = event . sender ? event . sender . name : _t ( 'Someone' ) ;
var supported = MatrixClientPeg . get ( ) . supportsVoip ( ) ? "" : _t ( '(not supported by this browser)' ) ;
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s answered the call.' , { senderName : senderName } ) + ' ' + supported ;
2016-09-15 18:01:02 +02:00
}
2015-09-16 15:48:49 +02:00
function textForCallHangupEvent ( event ) {
2017-05-23 16:16:31 +02:00
var senderName = event . sender ? event . sender . name : _t ( 'Someone' ) ;
var supported = MatrixClientPeg . get ( ) . supportsVoip ( ) ? "" : _t ( '(not supported by this browser)' ) ;
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s ended the call.' , { senderName : senderName } ) + ' ' + supported ;
2016-09-15 18:01:02 +02:00
}
2015-09-16 15:48:49 +02:00
function textForCallInviteEvent ( event ) {
2017-05-23 16:16:31 +02:00
var senderName = event . sender ? event . sender . name : _t ( 'Someone' ) ;
2015-09-16 15:48:49 +02:00
// 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" ;
}
2017-05-23 16:16:31 +02:00
var supported = MatrixClientPeg . get ( ) . supportsVoip ( ) ? "" : _t ( '(not supported by this browser)' ) ;
2017-05-25 20:43:34 +02:00
return _t ( '%(senderName)s placed a %(callType)s call.' , { senderName : senderName , callType : type } ) + ' ' + supported ;
2016-09-15 18:01:02 +02:00
}
2015-09-16 15:48:49 +02:00
2015-12-17 16:48:14 +01:00
function textForThreePidInviteEvent ( event ) {
var senderName = event . sender ? event . sender . name : event . getSender ( ) ;
2017-05-25 20:21:18 +02:00
return _t ( '%(senderName)s sent an invitation to %(targetDisplayName)s to join the room.' , { senderName : senderName , targetDisplayName : event . getContent ( ) . display _name } ) ;
2016-09-15 18:01:02 +02:00
}
2015-12-17 16:48:14 +01:00
2016-03-16 00:47:40 +01:00
function textForHistoryVisibilityEvent ( event ) {
var senderName = event . sender ? event . sender . name : event . getSender ( ) ;
var vis = event . getContent ( ) . history _visibility ;
2017-05-25 20:21:18 +02:00
// XXX: This i18n just isn't going to work for languages with different sentence structure.
2017-05-23 16:16:31 +02:00
var text = _t ( '%(senderName)s made future room history visible to' , { senderName : senderName } ) + ' ' ;
2016-03-16 00:47:40 +01:00
if ( vis === "invited" ) {
2017-05-23 16:16:31 +02:00
text += _t ( 'all room members, from the point they are invited' ) + '.' ;
2016-03-16 00:47:40 +01:00
}
else if ( vis === "joined" ) {
2017-05-23 16:16:31 +02:00
text += _t ( 'all room members, from the point they joined' ) + '.' ;
2016-03-16 00:47:40 +01:00
}
else if ( vis === "shared" ) {
2017-05-23 16:16:31 +02:00
text += _t ( 'all room members' ) + '.' ;
2016-03-16 00:47:40 +01:00
}
else if ( vis === "world_readable" ) {
2017-05-23 16:16:31 +02:00
text += _t ( 'anyone' ) + '.' ;
2016-03-16 00:47:40 +01:00
}
else {
2017-05-23 16:16:31 +02:00
text += ' ' + _t ( 'unknown' ) + ' (' + vis + ')' ;
2016-03-16 00:47:40 +01:00
}
return text ;
2016-09-15 18:01:02 +02:00
}
function textForEncryptionEvent ( event ) {
var senderName = event . sender ? event . sender . name : event . getSender ( ) ;
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s turned on end-to-end encryption (algorithm %(algorithm)s).' , { senderName : senderName , algorithm : event . getContent ( ) . algorithm } ) ;
2016-09-15 18:01:02 +02:00
}
2016-03-16 00:47:40 +01:00
2017-04-06 18:02:35 +02:00
// Currently will only display a change if a user's power level is changed
function textForPowerEvent ( event ) {
const senderName = event . sender ? event . sender . name : event . getSender ( ) ;
if ( ! event . getPrevContent ( ) || ! event . getPrevContent ( ) . users ) {
return '' ;
}
const userDefault = event . getContent ( ) . users _default || 0 ;
// Construct set of userIds
let users = [ ] ;
Object . keys ( event . getContent ( ) . users ) . forEach (
( userId ) => {
if ( users . indexOf ( userId ) === - 1 ) users . push ( userId ) ;
}
) ;
Object . keys ( event . getPrevContent ( ) . users ) . forEach (
( userId ) => {
if ( users . indexOf ( userId ) === - 1 ) users . push ( userId ) ;
}
) ;
let diff = [ ] ;
2017-05-25 20:21:18 +02:00
// XXX: This is also surely broken for i18n
2017-04-06 18:02:35 +02:00
users . forEach ( ( userId ) => {
// Previous power level
const from = event . getPrevContent ( ) . users [ userId ] ;
// Current power level
const to = event . getContent ( ) . users [ userId ] ;
if ( to !== from ) {
diff . push (
2017-05-23 16:16:31 +02:00
_t ( '%(userId)s from %(fromPowerLevel)s to %(toPowerLevel)s' , { userId : userId , fromPowerLevel : Roles . textualPowerLevel ( from , userDefault ) , toPowerLevel : Roles . textualPowerLevel ( to , userDefault ) } )
2017-04-06 18:02:35 +02:00
) ;
}
} ) ;
if ( ! diff . length ) {
return '' ;
}
2017-05-27 15:55:55 +02:00
return _t ( '%(senderName)s changed the power level of %(powerLevelDiffText)s.' , { senderName : senderName , powerLevelDiffText : diff . join ( ", " ) } ) ;
2017-04-06 18:02:35 +02:00
}
2015-09-16 15:48:49 +02:00
var handlers = {
'm.room.message' : textForMessageEvent ,
2015-10-30 03:07:04 +01: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 00:47:40 +01:00
'm.room.third_party_invite' : textForThreePidInviteEvent ,
'm.room.history_visibility' : textForHistoryVisibilityEvent ,
2016-09-15 18:01:02 +02:00
'm.room.encryption' : textForEncryptionEvent ,
2017-04-06 18:02:35 +02:00
'm.room.power_levels' : textForPowerEvent ,
2015-09-16 15:48:49 +02:00
} ;
module . exports = {
textForEvent : function ( ev ) {
var hdlr = handlers [ ev . getType ( ) ] ;
if ( ! hdlr ) return "" ;
return hdlr ( ev ) ;
}
2017-01-20 15:22:27 +01:00
} ;