Read only room (#198)

* Reactive read only
This commit is contained in:
Diego Mello 2018-01-17 14:42:30 -02:00 committed by Guilherme Gazzo
parent d3acc17fc1
commit c81182c9cf
5 changed files with 40 additions and 7 deletions

View File

@ -89,8 +89,10 @@ export default class MessageActions extends React.Component {
this.options = ['Cancel']; this.options = ['Cancel'];
this.CANCEL_INDEX = 0; this.CANCEL_INDEX = 0;
// Reply // Reply
if (!this.isRoomReadOnly()) {
this.options.push('Reply'); this.options.push('Reply');
this.REPLY_INDEX = this.options.length - 1; this.REPLY_INDEX = this.options.length - 1;
}
// Edit // Edit
if (this.allowEdit(nextProps)) { if (this.allowEdit(nextProps)) {
this.options.push('Edit'); this.options.push('Edit');
@ -103,8 +105,10 @@ export default class MessageActions extends React.Component {
this.options.push('Copy Message'); this.options.push('Copy Message');
this.COPY_INDEX = this.options.length - 1; this.COPY_INDEX = this.options.length - 1;
// Quote // Quote
if (!this.isRoomReadOnly()) {
this.options.push('Quote'); this.options.push('Quote');
this.QUOTE_INDEX = this.options.length - 1; this.QUOTE_INDEX = this.options.length - 1;
}
// Star // Star
if (this.props.Message_AllowStarring) { if (this.props.Message_AllowStarring) {
this.options.push(actionMessage.starred ? 'Unstar' : 'Star'); 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; isOwn = props => props.actionMessage.u && props.actionMessage.u._id === props.user.id;
isRoomReadOnly = () => this.props.room.ro;
allowEdit = (props) => { allowEdit = (props) => {
if (this.isRoomReadOnly()) {
return false;
}
const editOwn = this.isOwn(props); const editOwn = this.isOwn(props);
const { Message_AllowEditing: isEditAllowed } = this.props; const { Message_AllowEditing: isEditAllowed } = this.props;
if (!(this.hasEditPermission || (isEditAllowed && editOwn))) { if (!(this.hasEditPermission || (isEditAllowed && editOwn))) {
@ -187,6 +196,9 @@ export default class MessageActions extends React.Component {
} }
allowDelete = (props) => { allowDelete = (props) => {
if (this.isRoomReadOnly()) {
return false;
}
const deleteOwn = this.isOwn(props); const deleteOwn = this.isOwn(props);
const { Message_AllowDeleting: isDeleteAllowed } = this.props; const { Message_AllowDeleting: isDeleteAllowed } = this.props;
if (!(this.hasDeletePermission || (isDeleteAllowed && deleteOwn) || this.hasForceDeletePermission)) { if (!(this.hasDeletePermission || (isDeleteAllowed && deleteOwn) || this.hasForceDeletePermission)) {

View File

@ -79,7 +79,8 @@ const subscriptionSchema = {
userMentions: { type: 'int', optional: true }, userMentions: { type: 'int', optional: true },
// userMentions: 0, // userMentions: 0,
// groupMentions: 0, // groupMentions: 0,
roomUpdatedAt: { type: 'date', optional: true } roomUpdatedAt: { type: 'date', optional: true },
ro: { type: 'bool', optional: true }
} }
}; };

View File

@ -127,6 +127,7 @@ const RocketChat = {
const sub = database.objects('subscriptions').filtered('rid == $0', data._id)[0]; const sub = database.objects('subscriptions').filtered('rid == $0', data._id)[0];
database.write(() => { database.write(() => {
sub.roomUpdatedAt = data._updatedAt; sub.roomUpdatedAt = data._updatedAt;
sub.ro = data.ro;
}); });
} }
}); });
@ -424,6 +425,7 @@ const RocketChat = {
const room = rooms.find(({ _id }) => _id === subscription.rid); const room = rooms.find(({ _id }) => _id === subscription.rid);
if (room) { if (room) {
subscription.roomUpdatedAt = room._updatedAt; subscription.roomUpdatedAt = room._updatedAt;
subscription.ro = room.ro;
} }
if (subscription.roles) { if (subscription.roles) {
subscription.roles = subscription.roles.map(role => ({ value: role })); subscription.roles = subscription.roles.map(role => ({ value: role }));

View File

@ -73,11 +73,12 @@ export default class RoomView extends React.Component {
.filtered('rid = $0', this.rid) .filtered('rid = $0', this.rid)
.sorted('ts', true); .sorted('ts', true);
const rowIds = this.data.map((row, index) => index); 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 = { this.state = {
dataSource: ds.cloneWithRows(this.data, rowIds), dataSource: ds.cloneWithRows(this.data, rowIds),
loaded: true, 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({ this.props.navigation.setParams({
title: this.name title: this.name
}); });
this.updateRoom();
this.props.openRoom({ rid: this.rid, name: this.name, ls: this.room.ls }); this.props.openRoom({ rid: this.rid, name: this.name, ls: this.room.ls });
if (this.room.alert || this.room.unread || this.room.userMentions) { if (this.room.alert || this.room.unread || this.room.userMentions) {
this.props.setLastOpen(this.room.ls); this.props.setLastOpen(this.room.ls);
@ -92,6 +94,7 @@ export default class RoomView extends React.Component {
this.props.setLastOpen(null); this.props.setLastOpen(null);
} }
this.data.addListener(this.updateState); this.data.addListener(this.updateState);
this.rooms.addListener(this.updateRoom);
} }
shouldComponentUpdate(nextProps, nextState) { shouldComponentUpdate(nextProps, nextState) {
return !(equal(this.props, nextProps) && equal(this.state, nextState)); return !(equal(this.props, nextProps) && equal(this.state, nextState));
@ -134,6 +137,11 @@ export default class RoomView extends React.Component {
}); });
}, 50); }, 50);
updateRoom = () => {
[this.room] = this.rooms;
this.setState({ readOnly: this.room.ro });
}
sendMessage = message => RocketChat.sendMessage(this.rid, message).then(() => { sendMessage = message => RocketChat.sendMessage(this.rid, message).then(() => {
this.props.setLastOpen(null); this.props.setLastOpen(null);
}); });
@ -167,6 +175,13 @@ export default class RoomView extends React.Component {
</View> </View>
); );
} }
if (this.state.readOnly) {
return (
<View style={styles.readOnly}>
<Text>This room is read only</Text>
</View>
);
}
return <MessageBox ref={box => (this.box = box)} onSubmit={this.sendMessage} rid={this.rid} />; return <MessageBox ref={box => (this.box = box)} onSubmit={this.sendMessage} rid={this.rid} />;
}; };

View File

@ -30,5 +30,8 @@ export default StyleSheet.create({
textAlign: 'center', textAlign: 'center',
padding: 5, padding: 5,
color: '#ccc' color: '#ccc'
},
readOnly: {
padding: 10
} }
}); });