2016-07-20 14:03:13 +03:00
/ *
Copyright 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 .
* /
2017-07-12 15:58:14 +03:00
import Promise from 'bluebird' ;
2017-10-11 19:56:17 +03:00
const React = require ( 'react' ) ;
const MatrixClientPeg = require ( '../../../MatrixClientPeg' ) ;
const sdk = require ( "../../../index" ) ;
const Modal = require ( "../../../Modal" ) ;
const UserSettingsStore = require ( '../../../UserSettingsStore' ) ;
2017-06-02 12:18:31 +03:00
import { _t , _tJsx } from '../../../languageHandler' ;
2016-07-20 14:03:13 +03:00
module . exports = React . createClass ( {
displayName : 'UrlPreviewSettings' ,
propTypes : {
room : React . PropTypes . object ,
} ,
getInitialState : function ( ) {
2017-10-11 19:56:17 +03:00
const cli = MatrixClientPeg . get ( ) ;
const roomState = this . props . room . currentState ;
2016-07-20 14:03:13 +03:00
2017-10-11 19:56:17 +03:00
const roomPreviewUrls = this . props . room . currentState . getStateEvents ( 'org.matrix.room.preview_urls' , '' ) ;
const userPreviewUrls = this . props . room . getAccountData ( "org.matrix.room.preview_urls" ) ;
2016-07-20 14:03:13 +03:00
return {
globalDisableUrlPreview : ( roomPreviewUrls && roomPreviewUrls . getContent ( ) . disable ) || false ,
userDisableUrlPreview : ( userPreviewUrls && ( userPreviewUrls . getContent ( ) . disable === true ) ) || false ,
userEnableUrlPreview : ( userPreviewUrls && ( userPreviewUrls . getContent ( ) . disable === false ) ) || false ,
} ;
} ,
componentDidMount : function ( ) {
this . originalState = Object . assign ( { } , this . state ) ;
} ,
saveSettings : function ( ) {
2017-10-11 19:56:17 +03:00
const promises = [ ] ;
2016-07-20 14:03:13 +03:00
if ( this . state . globalDisableUrlPreview !== this . originalState . globalDisableUrlPreview ) {
console . log ( "UrlPreviewSettings: Updating room's preview_urls state event" ) ;
promises . push (
MatrixClientPeg . get ( ) . sendStateEvent (
this . props . room . roomId , "org.matrix.room.preview_urls" , {
2017-10-11 19:56:17 +03:00
disable : this . state . globalDisableUrlPreview ,
} , "" ,
) ,
2016-07-20 14:03:13 +03:00
) ;
}
2017-10-11 19:56:17 +03:00
let content = undefined ;
2016-07-20 14:03:13 +03:00
if ( this . state . userDisableUrlPreview !== this . originalState . userDisableUrlPreview ) {
console . log ( "UrlPreviewSettings: Disabling user's per-room preview_urls" ) ;
2017-10-11 19:56:17 +03:00
content = this . state . userDisableUrlPreview ? { disable : true } : { } ;
2016-07-20 14:03:13 +03:00
}
if ( this . state . userEnableUrlPreview !== this . originalState . userEnableUrlPreview ) {
console . log ( "UrlPreviewSettings: Enabling user's per-room preview_urls" ) ;
if ( ! content || content . disable === undefined ) {
2017-10-11 19:56:17 +03:00
content = this . state . userEnableUrlPreview ? { disable : false } : { } ;
2016-07-20 14:03:13 +03:00
}
}
if ( content ) {
promises . push (
MatrixClientPeg . get ( ) . setRoomAccountData (
2017-10-11 19:56:17 +03:00
this . props . room . roomId , "org.matrix.room.preview_urls" , content ,
) ,
2016-07-20 14:03:13 +03:00
) ;
}
console . log ( "UrlPreviewSettings: saveSettings: " + JSON . stringify ( promises ) ) ;
return promises ;
} ,
onGlobalDisableUrlPreviewChange : function ( ) {
this . setState ( {
globalDisableUrlPreview : this . refs . globalDisableUrlPreview . checked ? true : false ,
} ) ;
} ,
onUserEnableUrlPreviewChange : function ( ) {
this . setState ( {
userDisableUrlPreview : false ,
userEnableUrlPreview : this . refs . userEnableUrlPreview . checked ? true : false ,
} ) ;
} ,
onUserDisableUrlPreviewChange : function ( ) {
this . setState ( {
userDisableUrlPreview : this . refs . userDisableUrlPreview . checked ? true : false ,
userEnableUrlPreview : false ,
} ) ;
} ,
render : function ( ) {
2017-10-11 19:56:17 +03:00
const self = this ;
const roomState = this . props . room . currentState ;
const cli = MatrixClientPeg . get ( ) ;
2016-07-20 14:03:13 +03:00
2017-10-11 19:56:17 +03:00
const maySetRoomPreviewUrls = roomState . mayClientSendStateEvent ( 'org.matrix.room.preview_urls' , cli ) ;
let disableRoomPreviewUrls ;
2016-07-20 14:03:13 +03:00
if ( maySetRoomPreviewUrls ) {
disableRoomPreviewUrls =
< label >
< input type = "checkbox" ref = "globalDisableUrlPreview"
2017-10-11 19:56:17 +03:00
onChange = { this . onGlobalDisableUrlPreviewChange }
checked = { this . state . globalDisableUrlPreview } / >
{ _t ( "Disable URL previews by default for participants in this room" ) }
2017-01-20 17:22:27 +03:00
< / l a b e l > ;
2017-10-11 19:56:17 +03:00
} else {
2016-07-20 14:03:13 +03:00
disableRoomPreviewUrls =
< label >
2017-10-11 19:56:17 +03:00
{ _t ( "URL previews are %(globalDisableUrlPreview)s by default for participants in this room." , { globalDisableUrlPreview : this . state . globalDisableUrlPreview ? _t ( "disabled" ) : _t ( "enabled" ) } ) }
2017-01-20 17:22:27 +03:00
< / l a b e l > ;
2016-07-20 14:03:13 +03:00
}
2017-06-02 12:18:31 +03:00
let urlPreviewText = null ;
if ( UserSettingsStore . getUrlPreviewsDisabled ( ) ) {
urlPreviewText = (
2017-10-11 19:56:17 +03:00
_tJsx ( "You have <a>disabled</a> URL previews by default." , /<a>(.*?)<\/a>/ , ( sub ) => < a href = "#/settings" > { sub } < / a > )
2017-06-02 12:18:31 +03:00
) ;
2017-10-11 19:56:17 +03:00
} else {
2017-06-02 12:18:31 +03:00
urlPreviewText = (
2017-10-11 19:56:17 +03:00
_tJsx ( "You have <a>enabled</a> URL previews by default." , /<a>(.*?)<\/a>/ , ( sub ) => < a href = "#/settings" > { sub } < / a > )
2017-06-02 12:18:31 +03:00
) ;
}
2016-07-20 14:03:13 +03:00
return (
< div className = "mx_RoomSettings_toggles" >
2017-10-11 19:56:17 +03:00
< h3 > { _t ( "URL Previews" ) } < / h 3 >
2016-07-20 14:03:13 +03:00
< label >
2017-10-11 19:56:17 +03:00
{ urlPreviewText }
2016-07-20 14:03:13 +03:00
< / l a b e l >
{ disableRoomPreviewUrls }
< label >
< input type = "checkbox" ref = "userEnableUrlPreview"
2017-10-11 19:56:17 +03:00
onChange = { this . onUserEnableUrlPreviewChange }
checked = { this . state . userEnableUrlPreview } / >
{ _t ( "Enable URL previews for this room (affects only you)" ) }
2016-07-20 14:03:13 +03:00
< / l a b e l >
< label >
< input type = "checkbox" ref = "userDisableUrlPreview"
2017-10-11 19:56:17 +03:00
onChange = { this . onUserDisableUrlPreviewChange }
checked = { this . state . userDisableUrlPreview } / >
{ _t ( "Disable URL previews for this room (affects only you)" ) }
2016-07-20 14:03:13 +03:00
< / l a b e l >
< / d i v >
) ;
2017-10-11 19:56:17 +03:00
} ,
2017-01-20 17:22:27 +03:00
} ) ;