2017-11-24 20:44:52 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
Alert, Clipboard, Vibration, Share
|
|
|
|
} from 'react-native';
|
2017-11-24 20:44:52 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import ActionSheet from 'react-native-actionsheet';
|
|
|
|
import * as moment from 'moment';
|
|
|
|
|
|
|
|
import {
|
2018-09-25 19:28:42 +00:00
|
|
|
actionsHide as actionsHideAction,
|
|
|
|
deleteRequest as deleteRequestAction,
|
|
|
|
editInit as editInitAction,
|
|
|
|
replyInit as replyInitAction,
|
|
|
|
togglePinRequest as togglePinRequestAction,
|
|
|
|
toggleReactionPicker as toggleReactionPickerAction,
|
|
|
|
toggleStarRequest as toggleStarRequestAction
|
2017-11-24 20:44:52 +00:00
|
|
|
} from '../actions/messages';
|
2018-01-15 20:43:52 +00:00
|
|
|
import { showToast } from '../utils/info';
|
2018-03-29 17:55:37 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2017-11-24 20:44:52 +00:00
|
|
|
|
|
|
|
@connect(
|
|
|
|
state => ({
|
|
|
|
actionMessage: state.messages.actionMessage,
|
|
|
|
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 => ({
|
2018-09-25 19:28:42 +00:00
|
|
|
actionsHide: () => dispatch(actionsHideAction()),
|
|
|
|
deleteRequest: message => dispatch(deleteRequestAction(message)),
|
|
|
|
editInit: message => dispatch(editInitAction(message)),
|
|
|
|
toggleStarRequest: message => dispatch(toggleStarRequestAction(message)),
|
|
|
|
togglePinRequest: message => dispatch(togglePinRequestAction(message)),
|
|
|
|
toggleReactionPicker: message => dispatch(toggleReactionPickerAction(message)),
|
|
|
|
replyInit: (message, mention) => dispatch(replyInitAction(message, mention))
|
2017-11-24 20:44:52 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
export default class MessageActions extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
actionsHide: PropTypes.func.isRequired,
|
2018-07-10 13:40:32 +00:00
|
|
|
room: PropTypes.object.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
actionMessage: PropTypes.object,
|
2018-07-20 19:54:46 +00:00
|
|
|
// user: PropTypes.object.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
deleteRequest: PropTypes.func.isRequired,
|
|
|
|
editInit: PropTypes.func.isRequired,
|
|
|
|
toggleStarRequest: PropTypes.func.isRequired,
|
|
|
|
togglePinRequest: PropTypes.func.isRequired,
|
2018-01-30 19:48:26 +00:00
|
|
|
toggleReactionPicker: PropTypes.func.isRequired,
|
2018-07-20 19:54:46 +00:00
|
|
|
replyInit: PropTypes.func.isRequired,
|
2017-11-24 20:44:52 +00:00
|
|
|
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
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.handleActionPress = this.handleActionPress.bind(this);
|
2018-06-18 13:30:36 +00:00
|
|
|
this.setPermissions();
|
2017-11-24 20:44:52 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
const { Message_AllowStarring, Message_AllowPinning } = this.props;
|
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
// Cancel
|
|
|
|
this.options = [I18n.t('Cancel')];
|
|
|
|
this.CANCEL_INDEX = 0;
|
|
|
|
|
|
|
|
// Reply
|
|
|
|
if (!this.isRoomReadOnly()) {
|
|
|
|
this.options.push(I18n.t('Reply'));
|
|
|
|
this.REPLY_INDEX = this.options.length - 1;
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
// Edit
|
|
|
|
if (this.allowEdit(props)) {
|
|
|
|
this.options.push(I18n.t('Edit'));
|
|
|
|
this.EDIT_INDEX = this.options.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Permalink
|
|
|
|
this.options.push(I18n.t('Copy_Permalink'));
|
|
|
|
this.PERMALINK_INDEX = this.options.length - 1;
|
|
|
|
|
|
|
|
// Copy
|
|
|
|
this.options.push(I18n.t('Copy_Message'));
|
|
|
|
this.COPY_INDEX = this.options.length - 1;
|
|
|
|
|
|
|
|
// Share
|
|
|
|
this.options.push(I18n.t('Share_Message'));
|
|
|
|
this.SHARE_INDEX = this.options.length - 1;
|
|
|
|
|
|
|
|
// Quote
|
|
|
|
if (!this.isRoomReadOnly()) {
|
|
|
|
this.options.push(I18n.t('Quote'));
|
|
|
|
this.QUOTE_INDEX = this.options.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Star
|
2018-09-25 19:28:42 +00:00
|
|
|
if (Message_AllowStarring) {
|
2018-06-18 13:30:36 +00:00
|
|
|
this.options.push(I18n.t(props.actionMessage.starred ? 'Unstar' : 'Star'));
|
|
|
|
this.STAR_INDEX = this.options.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pin
|
2018-09-25 19:28:42 +00:00
|
|
|
if (Message_AllowPinning) {
|
2018-06-18 13:30:36 +00:00
|
|
|
this.options.push(I18n.t(props.actionMessage.pinned ? 'Unpin' : 'Pin'));
|
|
|
|
this.PIN_INDEX = this.options.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reaction
|
|
|
|
if (!this.isRoomReadOnly() || this.canReactWhenReadOnly()) {
|
|
|
|
this.options.push(I18n.t('Add_Reaction'));
|
|
|
|
this.REACTION_INDEX = this.options.length - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete
|
|
|
|
if (this.allowDelete(props)) {
|
|
|
|
this.options.push(I18n.t('Delete'));
|
|
|
|
this.DELETE_INDEX = this.options.length - 1;
|
|
|
|
}
|
|
|
|
setTimeout(() => {
|
2018-08-01 19:35:06 +00:00
|
|
|
if (this.actionSheet && this.actionSheet.show) {
|
|
|
|
this.actionSheet.show();
|
|
|
|
}
|
2018-06-18 13:30:36 +00:00
|
|
|
Vibration.vibrate(50);
|
|
|
|
});
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
setPermissions() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { room } = this.props;
|
2018-03-29 17:55:37 +00:00
|
|
|
const permissions = ['edit-message', 'delete-message', 'force-delete-message'];
|
2018-09-25 19:28:42 +00:00
|
|
|
const result = RocketChat.hasPermission(permissions, room.rid);
|
2018-03-29 17:55:37 +00:00
|
|
|
this.hasEditPermission = result[permissions[0]];
|
|
|
|
this.hasDeletePermission = result[permissions[1]];
|
|
|
|
this.hasForceDeletePermission = result[permissions[2]];
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-06-18 13:30:36 +00:00
|
|
|
getPermalink = async(message) => {
|
|
|
|
try {
|
|
|
|
return await RocketChat.getPermalink(message);
|
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
isOwn = props => props.actionMessage.u && props.actionMessage.u._id === props.user.id;
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
isRoomReadOnly = () => {
|
|
|
|
const { room } = this.props;
|
|
|
|
return room.ro;
|
|
|
|
}
|
2018-01-17 16:42:30 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
canReactWhenReadOnly = () => {
|
|
|
|
const { room } = this.props;
|
|
|
|
return room.reactWhenReadOnly;
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
allowEdit = (props) => {
|
2018-01-17 16:42:30 +00:00
|
|
|
if (this.isRoomReadOnly()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-24 20:44:52 +00:00
|
|
|
const editOwn = this.isOwn(props);
|
2018-09-25 19:28:42 +00:00
|
|
|
const { Message_AllowEditing: isEditAllowed, Message_AllowEditing_BlockEditInMinutes } = this.props;
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
if (!(this.hasEditPermission || (isEditAllowed && editOwn))) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
const blockEditInMinutes = Message_AllowEditing_BlockEditInMinutes;
|
2017-11-24 20:44:52 +00:00
|
|
|
if (blockEditInMinutes) {
|
|
|
|
let msgTs;
|
|
|
|
if (props.actionMessage.ts != null) {
|
|
|
|
msgTs = moment(props.actionMessage.ts);
|
|
|
|
}
|
|
|
|
let currentTsDiff;
|
|
|
|
if (msgTs != null) {
|
|
|
|
currentTsDiff = moment().diff(msgTs, 'minutes');
|
|
|
|
}
|
|
|
|
return currentTsDiff < blockEditInMinutes;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
allowDelete = (props) => {
|
2018-01-17 16:42:30 +00:00
|
|
|
if (this.isRoomReadOnly()) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-24 20:44:52 +00:00
|
|
|
const deleteOwn = this.isOwn(props);
|
2018-09-25 19:28:42 +00:00
|
|
|
const { Message_AllowDeleting: isDeleteAllowed, Message_AllowDeleting_BlockDeleteInMinutes } = this.props;
|
2017-11-24 20:44:52 +00:00
|
|
|
if (!(this.hasDeletePermission || (isDeleteAllowed && deleteOwn) || this.hasForceDeletePermission)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (this.hasForceDeletePermission) {
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
const blockDeleteInMinutes = Message_AllowDeleting_BlockDeleteInMinutes;
|
2017-11-24 20:44:52 +00:00
|
|
|
if (blockDeleteInMinutes != null && blockDeleteInMinutes !== 0) {
|
|
|
|
let msgTs;
|
|
|
|
if (props.actionMessage.ts != null) {
|
|
|
|
msgTs = moment(props.actionMessage.ts);
|
|
|
|
}
|
|
|
|
let currentTsDiff;
|
|
|
|
if (msgTs != null) {
|
|
|
|
currentTsDiff = moment().diff(msgTs, 'minutes');
|
|
|
|
}
|
|
|
|
return currentTsDiff < blockDeleteInMinutes;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
handleDelete = () => {
|
|
|
|
const { deleteRequest, actionMessage } = this.props;
|
2017-11-24 20:44:52 +00:00
|
|
|
Alert.alert(
|
2018-06-01 17:38:13 +00:00
|
|
|
I18n.t('Are_you_sure_question_mark'),
|
|
|
|
I18n.t('You_will_not_be_able_to_recover_this_message'),
|
2017-11-24 20:44:52 +00:00
|
|
|
[
|
|
|
|
{
|
2018-06-01 17:38:13 +00:00
|
|
|
text: I18n.t('Cancel'),
|
2017-11-24 20:44:52 +00:00
|
|
|
style: 'cancel'
|
|
|
|
},
|
|
|
|
{
|
2018-06-01 17:38:13 +00:00
|
|
|
text: I18n.t('Yes_action_it', { action: 'delete' }),
|
2017-11-24 20:44:52 +00:00
|
|
|
style: 'destructive',
|
2018-09-25 19:28:42 +00:00
|
|
|
onPress: () => deleteRequest(actionMessage)
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
],
|
|
|
|
{ cancelable: false }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
handleEdit = () => {
|
|
|
|
const { actionMessage, editInit } = this.props;
|
|
|
|
const { _id, msg, rid } = actionMessage;
|
|
|
|
editInit({ _id, msg, rid });
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
handleCopy = async() => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { actionMessage } = this.props;
|
|
|
|
await Clipboard.setString(actionMessage.msg);
|
2018-06-01 17:38:13 +00:00
|
|
|
showToast(I18n.t('Copied_to_clipboard'));
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
handleShare = async() => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { actionMessage } = this.props;
|
2018-12-05 20:52:08 +00:00
|
|
|
const permalink = await this.getPermalink(actionMessage);
|
2018-02-01 12:48:58 +00:00
|
|
|
Share.share({
|
2018-12-05 20:52:08 +00:00
|
|
|
message: permalink
|
2018-02-01 12:48:58 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
handleStar = () => {
|
|
|
|
const { actionMessage, toggleStarRequest } = this.props;
|
|
|
|
toggleStarRequest(actionMessage);
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
handlePermalink = async() => {
|
|
|
|
const { actionMessage } = this.props;
|
|
|
|
const permalink = await this.getPermalink(actionMessage);
|
2018-06-18 13:30:36 +00:00
|
|
|
Clipboard.setString(permalink);
|
|
|
|
showToast(I18n.t('Permalink_copied_to_clipboard'));
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
handlePin = () => {
|
|
|
|
const { actionMessage, togglePinRequest } = this.props;
|
|
|
|
togglePinRequest(actionMessage);
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
handleReply = () => {
|
|
|
|
const { actionMessage, replyInit } = this.props;
|
|
|
|
replyInit(actionMessage, true);
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
handleQuote = () => {
|
|
|
|
const { actionMessage, replyInit } = this.props;
|
|
|
|
replyInit(actionMessage, false);
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
handleReaction = () => {
|
|
|
|
const { actionMessage, toggleReactionPicker } = this.props;
|
|
|
|
toggleReactionPicker(actionMessage);
|
2018-01-30 19:48:26 +00:00
|
|
|
}
|
|
|
|
|
2017-11-24 20:44:52 +00:00
|
|
|
handleActionPress = (actionIndex) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { actionsHide } = this.props;
|
2017-11-24 20:44:52 +00:00
|
|
|
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;
|
2018-02-01 12:48:58 +00:00
|
|
|
case this.SHARE_INDEX:
|
|
|
|
this.handleShare();
|
|
|
|
break;
|
2017-11-24 20:44:52 +00:00
|
|
|
case this.QUOTE_INDEX:
|
|
|
|
this.handleQuote();
|
|
|
|
break;
|
|
|
|
case this.STAR_INDEX:
|
|
|
|
this.handleStar();
|
|
|
|
break;
|
|
|
|
case this.PIN_INDEX:
|
|
|
|
this.handlePin();
|
|
|
|
break;
|
2018-01-30 19:48:26 +00:00
|
|
|
case this.REACTION_INDEX:
|
|
|
|
this.handleReaction();
|
|
|
|
break;
|
2017-11-24 20:44:52 +00:00
|
|
|
case this.DELETE_INDEX:
|
|
|
|
this.handleDelete();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
actionsHide();
|
2017-11-24 20:44:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<ActionSheet
|
2018-08-01 19:35:06 +00:00
|
|
|
ref={o => this.actionSheet = o}
|
2018-06-01 17:38:13 +00:00
|
|
|
title={I18n.t('Message_actions')}
|
2018-05-23 13:39:18 +00:00
|
|
|
testID='message-actions'
|
2017-11-24 20:44:52 +00:00
|
|
|
options={this.options}
|
|
|
|
cancelButtonIndex={this.CANCEL_INDEX}
|
|
|
|
destructiveButtonIndex={this.DELETE_INDEX}
|
|
|
|
onPress={this.handleActionPress}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|