Permissions working

This commit is contained in:
Diego Mello 2017-11-22 17:30:02 -02:00
parent 86059e13d2
commit 9b9271e833
7 changed files with 198 additions and 50 deletions

View File

@ -44,10 +44,12 @@ export const MESSAGES = createRequestTypes('MESSAGES', [
'PERMALINK_REQUEST', 'PERMALINK_REQUEST',
'PERMALINK_SUCCESS', 'PERMALINK_SUCCESS',
'PERMALINK_FAILURE', 'PERMALINK_FAILURE',
'PERMALINK_CLEAR',
'TOGGLE_PIN_REQUEST', 'TOGGLE_PIN_REQUEST',
'TOGGLE_PIN_SUCCESS', 'TOGGLE_PIN_SUCCESS',
'TOGGLE_PIN_FAILURE', 'TOGGLE_PIN_FAILURE',
'SET_INPUT' 'SET_INPUT',
'CLEAR_INPUT'
]); ]);
export const CREATE_CHANNEL = createRequestTypes('CREATE_CHANNEL', [ export const CREATE_CHANNEL = createRequestTypes('CREATE_CHANNEL', [
...defaultTypes, ...defaultTypes,

View File

@ -106,6 +106,12 @@ export function permalinkFailure(err) {
}; };
} }
export function permalinkClear() {
return {
type: types.MESSAGES.PERMALINK_CLEAR
};
}
export function togglePinRequest(message) { export function togglePinRequest(message) {
return { return {
type: types.MESSAGES.TOGGLE_PIN_REQUEST, type: types.MESSAGES.TOGGLE_PIN_REQUEST,
@ -132,3 +138,9 @@ export function setInput(message) {
message message
}; };
} }
export function clearInput() {
return {
type: types.MESSAGES.CLEAR_INPUT
};
}

View File

@ -6,7 +6,7 @@ import ImagePicker from 'react-native-image-picker';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { userTyping } from '../actions/room'; import { userTyping } from '../actions/room';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
import { editRequest } from '../actions/messages'; import { editRequest, clearInput } from '../actions/messages';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
textBox: { textBox: {
@ -41,7 +41,8 @@ const styles = StyleSheet.create({
editing: state.messages.editing editing: state.messages.editing
}), dispatch => ({ }), dispatch => ({
editRequest: message => dispatch(editRequest(message)), editRequest: message => dispatch(editRequest(message)),
typing: status => dispatch(userTyping(status)) typing: status => dispatch(userTyping(status)),
clearInput: () => dispatch(clearInput())
})) }))
export default class MessageBox extends React.Component { export default class MessageBox extends React.Component {
static propTypes = { static propTypes = {
@ -50,11 +51,12 @@ export default class MessageBox extends React.Component {
editRequest: PropTypes.func.isRequired, editRequest: PropTypes.func.isRequired,
message: PropTypes.object, message: PropTypes.object,
editing: PropTypes.bool, editing: PropTypes.bool,
typing: PropTypes.bool typing: PropTypes.func,
clearInput: PropTypes.func
} }
componentWillReceiveProps(nextProps) { componentWillReceiveProps(nextProps) {
if (this.props.message !== nextProps.message) { if (this.props.message !== nextProps.message && nextProps.message) {
this.component.setNativeProps({ text: nextProps.message.msg }); this.component.setNativeProps({ text: nextProps.message.msg });
this.component.focus(); this.component.focus();
} }
@ -75,6 +77,7 @@ export default class MessageBox extends React.Component {
this.props.onSubmit(message); this.props.onSubmit(message);
} }
this.component.setNativeProps({ text: '' }); this.component.setNativeProps({ text: '' });
this.props.clearInput();
} }
addFile = () => { addFile = () => {

View File

@ -5,6 +5,7 @@ import { emojify } from 'react-emojione';
import Markdown from 'react-native-easy-markdown'; // eslint-disable-line import Markdown from 'react-native-easy-markdown'; // eslint-disable-line
import ActionSheet from 'react-native-actionsheet'; import ActionSheet from 'react-native-actionsheet';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import * as moment from 'moment';
import Card from './Card'; import Card from './Card';
import User from './User'; import User from './User';
@ -14,15 +15,11 @@ import {
editInit, editInit,
starRequest, starRequest,
permalinkRequest, permalinkRequest,
permalinkClear,
togglePinRequest, togglePinRequest,
setInput setInput
} from '../../actions/messages'; } from '../../actions/messages';
const title = 'Message actions';
const options = ['Cancel', 'Reply', 'Edit', 'Permalink', 'Copy', 'Quote', 'Star Message', 'Pin Message', 'Delete'];
const CANCEL_INDEX = 0;
const DESTRUCTIVE_INDEX = 8;
const styles = StyleSheet.create({ const styles = StyleSheet.create({
content: { content: {
flexGrow: 1, flexGrow: 1,
@ -47,14 +44,21 @@ const styles = StyleSheet.create({
@connect(state => ({ @connect(state => ({
message: state.messages.message, message: state.messages.message,
permalink: state.messages.permalink, permalink: state.messages.permalink,
user: state.login.user user: state.login.user,
Message_AllowDeleting: state.settings.Message_AllowDeleting,
Message_AllowDeleting_BlockDeleteInMinutes: state.settings.Message_AllowDeleting_BlockDeleteInMinutes,
Message_AllowEditing: state.settings.Message_AllowEditing,
Message_AllowEditing_BlockEditInMinutes: state.settings.Message_AllowEditing_BlockEditInMinutes,
Message_AllowPinning: state.settings.Message_AllowPinning,
Message_AllowStarring: state.settings.Message_AllowStarring
}), dispatch => ({ }), dispatch => ({
deleteRequest: message => dispatch(deleteRequest(message)), deleteRequest: message => dispatch(deleteRequest(message)),
editInit: message => dispatch(editInit(message)), editInit: message => dispatch(editInit(message)),
starRequest: message => dispatch(starRequest(message)), starRequest: message => dispatch(starRequest(message)),
permalinkRequest: message => dispatch(permalinkRequest(message)), permalinkRequest: message => dispatch(permalinkRequest(message)),
togglePinRequest: message => dispatch(togglePinRequest(message)), togglePinRequest: message => dispatch(togglePinRequest(message)),
setInput: message => dispatch(setInput(message)) setInput: message => dispatch(setInput(message)),
permalinkClear: () => dispatch(permalinkClear())
})) }))
export default class Message extends React.Component { export default class Message extends React.Component {
static propTypes = { static propTypes = {
@ -66,11 +70,21 @@ export default class Message extends React.Component {
editInit: PropTypes.func.isRequired, editInit: PropTypes.func.isRequired,
starRequest: PropTypes.func.isRequired, starRequest: PropTypes.func.isRequired,
permalinkRequest: PropTypes.func.isRequired, permalinkRequest: PropTypes.func.isRequired,
permalinkClear: PropTypes.func.isRequired,
togglePinRequest: PropTypes.func.isRequired, togglePinRequest: PropTypes.func.isRequired,
setInput: PropTypes.func.isRequired, setInput: PropTypes.func.isRequired,
user: PropTypes.object.isRequired, user: PropTypes.object.isRequired,
message: PropTypes.object, message: PropTypes.object,
permalink: PropTypes.string permalink: PropTypes.string,
Message_AllowDeleting: PropTypes.bool,
Message_AllowDeleting_BlockDeleteInMinutes: PropTypes.number,
Message_AllowEditing: PropTypes.bool,
Message_AllowEditing_BlockEditInMinutes: PropTypes.number,
Message_AllowPinning: PropTypes.bool,
Message_AllowStarring: PropTypes.bool,
hasEditPermission: PropTypes.bool,
hasDeletePermission: PropTypes.bool,
hasForceDeletePermission: PropTypes.bool
} }
constructor(props) { constructor(props) {
@ -82,16 +96,51 @@ export default class Message extends React.Component {
}; };
this.handleActionPress = this.handleActionPress.bind(this); this.handleActionPress = this.handleActionPress.bind(this);
this.showActions = this.showActions.bind(this); this.showActions = this.showActions.bind(this);
// Cancel
this.options = ['Cancel'];
this.CANCEL_INDEX = 0;
// Reply
this.options.push('Reply');
this.REPLY_INDEX = this.options.length - 1;
// Edit
if (this.allowEdit()) {
this.options.push('Edit');
this.EDIT_INDEX = this.options.length - 1;
}
// Permalink
this.options.push('Permalink');
this.PERMALINK_INDEX = this.options.length - 1;
// Copy
this.options.push('Copy');
this.COPY_INDEX = this.options.length - 1;
// Quote
this.options.push('Quote');
this.QUOTE_INDEX = this.options.length - 1;
// Star
if (this.props.Message_AllowStarring) {
this.options.push('Star');
this.STAR_INDEX = this.options.length - 1;
}
// Pin
if (this.props.Message_AllowPinning) {
this.options.push('Pin');
this.PIN_INDEX = this.options.length - 1;
}
// Delete
if (this.allowDelete()) {
this.options.push('Delete');
this.DELETE_INDEX = this.options.length - 1;
}
} }
async componentWillReceiveProps(nextProps) { async componentWillReceiveProps(nextProps) {
if (this.props.permalink !== nextProps.permalink) { if (this.props.permalink !== nextProps.permalink && nextProps.permalink) {
// copy permalink // copy permalink
if (this.state.copyPermalink) { if (this.state.copyPermalink) {
this.setState({ copyPermalink: false }); this.setState({ copyPermalink: false });
await Clipboard.setString(nextProps.permalink); await Clipboard.setString(nextProps.permalink);
Alert.alert('Permalink copied to clipboard!'); Alert.alert('Permalink copied to clipboard!');
this.props.permalinkClear();
// quote // quote
} else if (this.state.quote) { } else if (this.state.quote) {
this.setState({ quote: false }); this.setState({ quote: false });
@ -112,6 +161,53 @@ export default class Message extends React.Component {
} }
} }
isOwn = () => this.props.item.u && this.props.item.u._id === this.props.user.id;
allowEdit = () => {
const isEditAllowed = this.props.Message_AllowEditing;
const editOwn = this.isOwn();
if (!(this.props.hasEditPermission || (isEditAllowed && editOwn))) {
return false;
}
const blockEditInMinutes = this.props.Message_AllowEditing_BlockEditInMinutes;
if (blockEditInMinutes) {
let msgTs;
if (this.props.item.ts != null) {
msgTs = moment(this.props.item.ts);
}
let currentTsDiff;
if (msgTs != null) {
currentTsDiff = moment().diff(msgTs, 'minutes');
}
return currentTsDiff < blockEditInMinutes;
}
return true;
}
allowDelete = () => {
const deleteOwn = this.isOwn();
const { hasDeletePermission, hasForceDeletePermission, Message_AllowDeleting: isDeleteAllowed } = this.props;
if (!(hasDeletePermission || (isDeleteAllowed && deleteOwn) || this.props.hasForceDeletePermission)) {
return false;
}
if (hasForceDeletePermission) {
return true;
}
const blockDeleteInMinutes = this.props.Message_AllowDeleting_BlockDeleteInMinutes;
if (blockDeleteInMinutes != null && blockDeleteInMinutes !== 0) {
let msgTs;
if (this.props.item.ts != null) {
msgTs = moment(this.props.item.ts);
}
let currentTsDiff;
if (msgTs != null) {
currentTsDiff = moment().diff(msgTs, 'minutes');
}
return currentTsDiff < blockDeleteInMinutes;
}
return true;
}
isDeleted() { isDeleted() {
return !this.props.item.msg; return !this.props.item.msg;
} }
@ -166,7 +262,7 @@ export default class Message extends React.Component {
this.props.permalinkRequest(this.props.item); this.props.permalinkRequest(this.props.item);
} }
handleTogglePin() { handlePin() {
this.props.togglePinRequest(this.props.item); this.props.togglePinRequest(this.props.item);
} }
@ -181,30 +277,33 @@ export default class Message extends React.Component {
} }
handleActionPress = (actionIndex) => { handleActionPress = (actionIndex) => {
// reply switch (actionIndex) {
if (actionIndex === 1) { case this.REPLY_INDEX:
this.handleReply(); this.handleReply();
// edit break;
} else if (actionIndex === 2) { case this.EDIT_INDEX:
this.handleEdit(); this.handleEdit();
// permalink break;
} else if (actionIndex === 3) { case this.PERMALINK_INDEX:
this.handlePermalink(); this.handlePermalink();
// copy break;
} else if (actionIndex === 4) { case this.COPY_INDEX:
this.handleCopy(); this.handleCopy();
// quote break;
} else if (actionIndex === 5) { case this.QUOTE_INDEX:
this.handleQuote(); this.handleQuote();
// star break;
} else if (actionIndex === 6) { case this.STAR_INDEX:
this.handleStar(); this.handleStar();
// toggle pin break;
} else if (actionIndex === 7) { case this.PIN_INDEX:
this.handleTogglePin(); this.handlePin();
// delete break;
} else if (actionIndex === 8) { case this.DELETE_INDEX:
this.handleDelete(); this.handleDelete();
break;
default:
break;
} }
} }
@ -258,10 +357,10 @@ export default class Message extends React.Component {
</View> </View>
<ActionSheet <ActionSheet
ref={o => this.ActionSheet = o} ref={o => this.ActionSheet = o}
title={title} title='Messages actions'
options={options} options={this.options}
cancelButtonIndex={CANCEL_INDEX} cancelButtonIndex={this.CANCEL_INDEX}
destructiveButtonIndex={DESTRUCTIVE_INDEX} destructiveButtonIndex={this.DELETE_INDEX}
onPress={this.handleActionPress} onPress={this.handleActionPress}
/> />
</View> </View>

View File

@ -490,6 +490,13 @@ const RocketChat = {
} }
return call('pinMessage', message); return call('pinMessage', message);
}, },
getRoom(rid) {
const result = realm.objects('subscriptions').filtered('rid = $0', rid);
if (result.length === 0) {
return Promise.reject(new Error('Room not found'));
}
return Promise.resolve(result[0]);
},
async getPermalink(message) { async getPermalink(message) {
const room = await RocketChat.getRoom(message.rid); const room = await RocketChat.getRoom(message.rid);
const roomType = { const roomType = {

View File

@ -50,13 +50,21 @@ export default function messages(state = initialState, action) {
...state, ...state,
permalink: action.permalink permalink: action.permalink
}; };
case types.MESSAGES.PERMALINK_CLEAR:
return {
...state,
permalink: ''
};
case types.MESSAGES.SET_INPUT: case types.MESSAGES.SET_INPUT:
return { return {
...state, ...state,
message: action.message message: action.message
}; };
// case types.LOGOUT: case types.MESSAGES.CLEAR_INPUT:
// return initialState; return {
...state,
message: {}
};
default: default:
return state; return state;
} }

View File

@ -49,12 +49,13 @@ const styles = StyleSheet.create({
@connect( @connect(
state => ({ state => ({
username: state.login.user.username, user: state.login.user,
usersTyping: state.room.usersTyping, usersTyping: state.room.usersTyping,
server: state.server.server, server: state.server.server,
Site_Url: state.settings.Site_Url, Site_Url: state.settings.Site_Url,
Message_TimeFormat: state.settings.Message_TimeFormat, Message_TimeFormat: state.settings.Message_TimeFormat,
loading: state.messages.isFetching loading: state.messages.isFetching,
permissions: state.permissions
}), }),
dispatch => ({ dispatch => ({
actions: bindActionCreators(actions, dispatch), actions: bindActionCreators(actions, dispatch),
@ -73,7 +74,8 @@ export default class RoomView extends React.Component {
Message_TimeFormat: PropTypes.string, Message_TimeFormat: PropTypes.string,
loading: PropTypes.bool, loading: PropTypes.bool,
usersTyping: PropTypes.array, usersTyping: PropTypes.array,
username: PropTypes.string user: PropTypes.object,
permissions: PropTypes.object.isRequired
}; };
constructor(props) { constructor(props) {
@ -96,6 +98,18 @@ export default class RoomView extends React.Component {
loaded: true, loaded: true,
joined: typeof props.rid === 'undefined' joined: typeof props.rid === 'undefined'
}; };
// permissions
const { roles } = this.room[0];
const roomRoles = Array.from(Object.keys(roles), i => roles[i].value);
const userRoles = this.props.user.roles || [];
const mergedRoles = [...new Set([...roomRoles, ...userRoles])];
this.hasEditPermission = this.props.permissions['edit-message']
.some(item => mergedRoles.indexOf(item) !== -1);
this.hasDeletePermission = this.props.permissions['delete-message']
.some(item => mergedRoles.indexOf(item) !== -1);
this.hasForceDeletePermission = this.props.permissions['force-delete-message']
.some(item => mergedRoles.indexOf(item) !== -1);
} }
componentWillMount() { componentWillMount() {
@ -143,7 +157,7 @@ export default class RoomView extends React.Component {
} }
get usersTyping() { get usersTyping() {
const users = this.props.usersTyping.filter(_username => this.props.username !== _username); const users = this.props.usersTyping.filter(_username => this.props.user.username !== _username);
return users.length ? `${ users.join(' ,') } ${ users.length > 1 ? 'are' : 'is' } typing` : null; return users.length ? `${ users.join(' ,') } ${ users.length > 1 ? 'are' : 'is' } typing` : null;
} }
@ -176,6 +190,9 @@ export default class RoomView extends React.Component {
baseUrl={this.props.Site_Url} baseUrl={this.props.Site_Url}
Message_TimeFormat={this.props.Message_TimeFormat} Message_TimeFormat={this.props.Message_TimeFormat}
room={this.room} room={this.room}
hasEditPermission={this.hasEditPermission}
hasDeletePermission={this.hasDeletePermission}
hasForceDeletePermission={this.hasForceDeletePermission}
/> />
); );