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_SUCCESS',
'PERMALINK_FAILURE',
'PERMALINK_CLEAR',
'TOGGLE_PIN_REQUEST',
'TOGGLE_PIN_SUCCESS',
'TOGGLE_PIN_FAILURE',
'SET_INPUT'
'SET_INPUT',
'CLEAR_INPUT'
]);
export const CREATE_CHANNEL = createRequestTypes('CREATE_CHANNEL', [
...defaultTypes,

View File

@ -106,6 +106,12 @@ export function permalinkFailure(err) {
};
}
export function permalinkClear() {
return {
type: types.MESSAGES.PERMALINK_CLEAR
};
}
export function togglePinRequest(message) {
return {
type: types.MESSAGES.TOGGLE_PIN_REQUEST,
@ -132,3 +138,9 @@ export function setInput(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 { userTyping } from '../actions/room';
import RocketChat from '../lib/rocketchat';
import { editRequest } from '../actions/messages';
import { editRequest, clearInput } from '../actions/messages';
const styles = StyleSheet.create({
textBox: {
@ -41,7 +41,8 @@ const styles = StyleSheet.create({
editing: state.messages.editing
}), dispatch => ({
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 {
static propTypes = {
@ -50,11 +51,12 @@ export default class MessageBox extends React.Component {
editRequest: PropTypes.func.isRequired,
message: PropTypes.object,
editing: PropTypes.bool,
typing: PropTypes.bool
typing: PropTypes.func,
clearInput: PropTypes.func
}
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.focus();
}
@ -75,6 +77,7 @@ export default class MessageBox extends React.Component {
this.props.onSubmit(message);
}
this.component.setNativeProps({ text: '' });
this.props.clearInput();
}
addFile = () => {

View File

@ -5,6 +5,7 @@ import { emojify } from 'react-emojione';
import Markdown from 'react-native-easy-markdown'; // eslint-disable-line
import ActionSheet from 'react-native-actionsheet';
import { connect } from 'react-redux';
import * as moment from 'moment';
import Card from './Card';
import User from './User';
@ -14,15 +15,11 @@ import {
editInit,
starRequest,
permalinkRequest,
permalinkClear,
togglePinRequest,
setInput
} 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({
content: {
flexGrow: 1,
@ -47,14 +44,21 @@ const styles = StyleSheet.create({
@connect(state => ({
message: state.messages.message,
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 => ({
deleteRequest: message => dispatch(deleteRequest(message)),
editInit: message => dispatch(editInit(message)),
starRequest: message => dispatch(starRequest(message)),
permalinkRequest: message => dispatch(permalinkRequest(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 {
static propTypes = {
@ -66,11 +70,21 @@ export default class Message extends React.Component {
editInit: PropTypes.func.isRequired,
starRequest: PropTypes.func.isRequired,
permalinkRequest: PropTypes.func.isRequired,
permalinkClear: PropTypes.func.isRequired,
togglePinRequest: PropTypes.func.isRequired,
setInput: PropTypes.func.isRequired,
user: PropTypes.object.isRequired,
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) {
@ -82,16 +96,51 @@ export default class Message extends React.Component {
};
this.handleActionPress = this.handleActionPress.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) {
if (this.props.permalink !== nextProps.permalink) {
if (this.props.permalink !== nextProps.permalink && nextProps.permalink) {
// copy permalink
if (this.state.copyPermalink) {
this.setState({ copyPermalink: false });
await Clipboard.setString(nextProps.permalink);
Alert.alert('Permalink copied to clipboard!');
this.props.permalinkClear();
// quote
} else if (this.state.quote) {
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() {
return !this.props.item.msg;
}
@ -166,7 +262,7 @@ export default class Message extends React.Component {
this.props.permalinkRequest(this.props.item);
}
handleTogglePin() {
handlePin() {
this.props.togglePinRequest(this.props.item);
}
@ -181,30 +277,33 @@ export default class Message extends React.Component {
}
handleActionPress = (actionIndex) => {
// reply
if (actionIndex === 1) {
this.handleReply();
// edit
} else if (actionIndex === 2) {
this.handleEdit();
// permalink
} else if (actionIndex === 3) {
this.handlePermalink();
// copy
} else if (actionIndex === 4) {
this.handleCopy();
// quote
} else if (actionIndex === 5) {
this.handleQuote();
// star
} else if (actionIndex === 6) {
this.handleStar();
// toggle pin
} else if (actionIndex === 7) {
this.handleTogglePin();
// delete
} else if (actionIndex === 8) {
this.handleDelete();
switch (actionIndex) {
case this.REPLY_INDEX:
this.handleReply();
break;
case this.EDIT_INDEX:
this.handleEdit();
break;
case this.PERMALINK_INDEX:
this.handlePermalink();
break;
case this.COPY_INDEX:
this.handleCopy();
break;
case this.QUOTE_INDEX:
this.handleQuote();
break;
case this.STAR_INDEX:
this.handleStar();
break;
case this.PIN_INDEX:
this.handlePin();
break;
case this.DELETE_INDEX:
this.handleDelete();
break;
default:
break;
}
}
@ -258,10 +357,10 @@ export default class Message extends React.Component {
</View>
<ActionSheet
ref={o => this.ActionSheet = o}
title={title}
options={options}
cancelButtonIndex={CANCEL_INDEX}
destructiveButtonIndex={DESTRUCTIVE_INDEX}
title='Messages actions'
options={this.options}
cancelButtonIndex={this.CANCEL_INDEX}
destructiveButtonIndex={this.DELETE_INDEX}
onPress={this.handleActionPress}
/>
</View>

View File

@ -490,6 +490,13 @@ const RocketChat = {
}
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) {
const room = await RocketChat.getRoom(message.rid);
const roomType = {

View File

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

View File

@ -49,12 +49,13 @@ const styles = StyleSheet.create({
@connect(
state => ({
username: state.login.user.username,
user: state.login.user,
usersTyping: state.room.usersTyping,
server: state.server.server,
Site_Url: state.settings.Site_Url,
Message_TimeFormat: state.settings.Message_TimeFormat,
loading: state.messages.isFetching
loading: state.messages.isFetching,
permissions: state.permissions
}),
dispatch => ({
actions: bindActionCreators(actions, dispatch),
@ -73,7 +74,8 @@ export default class RoomView extends React.Component {
Message_TimeFormat: PropTypes.string,
loading: PropTypes.bool,
usersTyping: PropTypes.array,
username: PropTypes.string
user: PropTypes.object,
permissions: PropTypes.object.isRequired
};
constructor(props) {
@ -96,6 +98,18 @@ export default class RoomView extends React.Component {
loaded: true,
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() {
@ -143,7 +157,7 @@ export default class RoomView extends React.Component {
}
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;
}
@ -176,6 +190,9 @@ export default class RoomView extends React.Component {
baseUrl={this.props.Site_Url}
Message_TimeFormat={this.props.Message_TimeFormat}
room={this.room}
hasEditPermission={this.hasEditPermission}
hasDeletePermission={this.hasDeletePermission}
hasForceDeletePermission={this.hasForceDeletePermission}
/>
);