From c81182c9cfac7e159135d7801844ca999313e666 Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Wed, 17 Jan 2018 14:42:30 -0200 Subject: [PATCH] Read only room (#198) * Reactive read only --- app/containers/MessageActions.js | 20 ++++++++++++++++---- app/lib/realm.js | 3 ++- app/lib/rocketchat.js | 2 ++ app/views/RoomView/index.js | 19 +++++++++++++++++-- app/views/RoomView/styles.js | 3 +++ 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/app/containers/MessageActions.js b/app/containers/MessageActions.js index c13ffba1..12be855e 100644 --- a/app/containers/MessageActions.js +++ b/app/containers/MessageActions.js @@ -89,8 +89,10 @@ export default class MessageActions extends React.Component { this.options = ['Cancel']; this.CANCEL_INDEX = 0; // Reply - this.options.push('Reply'); - this.REPLY_INDEX = this.options.length - 1; + if (!this.isRoomReadOnly()) { + this.options.push('Reply'); + this.REPLY_INDEX = this.options.length - 1; + } // Edit if (this.allowEdit(nextProps)) { this.options.push('Edit'); @@ -103,8 +105,10 @@ export default class MessageActions extends React.Component { this.options.push('Copy Message'); this.COPY_INDEX = this.options.length - 1; // Quote - this.options.push('Quote'); - this.QUOTE_INDEX = this.options.length - 1; + if (!this.isRoomReadOnly()) { + this.options.push('Quote'); + this.QUOTE_INDEX = this.options.length - 1; + } // Star if (this.props.Message_AllowStarring) { this.options.push(actionMessage.starred ? 'Unstar' : 'Star'); @@ -165,7 +169,12 @@ export default class MessageActions extends React.Component { isOwn = props => props.actionMessage.u && props.actionMessage.u._id === props.user.id; + isRoomReadOnly = () => this.props.room.ro; + allowEdit = (props) => { + if (this.isRoomReadOnly()) { + return false; + } const editOwn = this.isOwn(props); const { Message_AllowEditing: isEditAllowed } = this.props; if (!(this.hasEditPermission || (isEditAllowed && editOwn))) { @@ -187,6 +196,9 @@ export default class MessageActions extends React.Component { } allowDelete = (props) => { + if (this.isRoomReadOnly()) { + return false; + } const deleteOwn = this.isOwn(props); const { Message_AllowDeleting: isDeleteAllowed } = this.props; if (!(this.hasDeletePermission || (isDeleteAllowed && deleteOwn) || this.hasForceDeletePermission)) { diff --git a/app/lib/realm.js b/app/lib/realm.js index 4cf45933..17d30e26 100644 --- a/app/lib/realm.js +++ b/app/lib/realm.js @@ -79,7 +79,8 @@ const subscriptionSchema = { userMentions: { type: 'int', optional: true }, // userMentions: 0, // groupMentions: 0, - roomUpdatedAt: { type: 'date', optional: true } + roomUpdatedAt: { type: 'date', optional: true }, + ro: { type: 'bool', optional: true } } }; diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index f6e7077b..dc0bf1bd 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -127,6 +127,7 @@ const RocketChat = { const sub = database.objects('subscriptions').filtered('rid == $0', data._id)[0]; database.write(() => { sub.roomUpdatedAt = data._updatedAt; + sub.ro = data.ro; }); } }); @@ -424,6 +425,7 @@ const RocketChat = { const room = rooms.find(({ _id }) => _id === subscription.rid); if (room) { subscription.roomUpdatedAt = room._updatedAt; + subscription.ro = room.ro; } if (subscription.roles) { subscription.roles = subscription.roles.map(role => ({ value: role })); diff --git a/app/views/RoomView/index.js b/app/views/RoomView/index.js index 8f754b8b..c960c74c 100644 --- a/app/views/RoomView/index.js +++ b/app/views/RoomView/index.js @@ -73,11 +73,12 @@ export default class RoomView extends React.Component { .filtered('rid = $0', this.rid) .sorted('ts', true); const rowIds = this.data.map((row, index) => index); - [this.room] = database.objects('subscriptions').filtered('rid = $0', this.rid); + this.rooms = database.objects('subscriptions').filtered('rid = $0', this.rid); this.state = { dataSource: ds.cloneWithRows(this.data, rowIds), loaded: true, - joined: typeof props.rid === 'undefined' + joined: typeof props.rid === 'undefined', + readOnly: false }; } @@ -85,6 +86,7 @@ export default class RoomView extends React.Component { this.props.navigation.setParams({ title: this.name }); + this.updateRoom(); this.props.openRoom({ rid: this.rid, name: this.name, ls: this.room.ls }); if (this.room.alert || this.room.unread || this.room.userMentions) { this.props.setLastOpen(this.room.ls); @@ -92,6 +94,7 @@ export default class RoomView extends React.Component { this.props.setLastOpen(null); } this.data.addListener(this.updateState); + this.rooms.addListener(this.updateRoom); } shouldComponentUpdate(nextProps, nextState) { return !(equal(this.props, nextProps) && equal(this.state, nextState)); @@ -134,6 +137,11 @@ export default class RoomView extends React.Component { }); }, 50); + updateRoom = () => { + [this.room] = this.rooms; + this.setState({ readOnly: this.room.ro }); + } + sendMessage = message => RocketChat.sendMessage(this.rid, message).then(() => { this.props.setLastOpen(null); }); @@ -167,6 +175,13 @@ export default class RoomView extends React.Component { ); } + if (this.state.readOnly) { + return ( + + This room is read only + + ); + } return (this.box = box)} onSubmit={this.sendMessage} rid={this.rid} />; }; diff --git a/app/views/RoomView/styles.js b/app/views/RoomView/styles.js index 66ddd313..2e2713d0 100644 --- a/app/views/RoomView/styles.js +++ b/app/views/RoomView/styles.js @@ -30,5 +30,8 @@ export default StyleSheet.create({ textAlign: 'center', padding: 5, color: '#ccc' + }, + readOnly: { + padding: 10 } });