2017-12-13 15:00:26 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2019-01-30 12:11:02 +00:00
|
|
|
import ActionSheet from 'react-native-action-sheet';
|
2017-12-13 15:00:26 +00:00
|
|
|
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2018-05-18 17:55:08 +00:00
|
|
|
import protectedFunction from '../lib/methods/helpers/protectedFunction';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2019-10-08 12:36:15 +00:00
|
|
|
import log from '../utils/log';
|
2017-12-13 15:00:26 +00:00
|
|
|
|
2019-08-07 13:51:34 +00:00
|
|
|
class MessageErrorActions extends React.Component {
|
2017-12-13 15:00:26 +00:00
|
|
|
static propTypes = {
|
2019-09-16 20:26:32 +00:00
|
|
|
actionsHide: PropTypes.func.isRequired,
|
2019-10-08 12:36:15 +00:00
|
|
|
message: PropTypes.object,
|
|
|
|
tmid: PropTypes.string
|
2017-12-13 15:00:26 +00:00
|
|
|
};
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2017-12-13 15:00:26 +00:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.handleActionPress = this.handleActionPress.bind(this);
|
2018-06-01 17:38:13 +00:00
|
|
|
this.options = [I18n.t('Cancel'), I18n.t('Delete'), I18n.t('Resend')];
|
2017-12-13 15:00:26 +00:00
|
|
|
this.CANCEL_INDEX = 0;
|
|
|
|
this.DELETE_INDEX = 1;
|
|
|
|
this.RESEND_INDEX = 2;
|
2018-11-16 11:06:29 +00:00
|
|
|
setTimeout(() => {
|
2019-01-30 12:11:02 +00:00
|
|
|
this.showActionSheet();
|
2018-11-16 11:06:29 +00:00
|
|
|
});
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
handleResend = protectedFunction(async() => {
|
2019-10-08 12:36:15 +00:00
|
|
|
const { message, tmid } = this.props;
|
|
|
|
await RocketChat.resendMessage(message, tmid);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
|
2019-10-08 12:36:15 +00:00
|
|
|
handleDelete = async() => {
|
|
|
|
try {
|
|
|
|
const { message, tmid } = this.props;
|
|
|
|
const db = database.active;
|
|
|
|
const deleteBatch = [];
|
|
|
|
const msgCollection = db.collections.get('messages');
|
|
|
|
const threadCollection = db.collections.get('threads');
|
|
|
|
|
|
|
|
// Delete the object (it can be Message or ThreadMessage instance)
|
|
|
|
deleteBatch.push(message.prepareDestroyPermanently());
|
|
|
|
|
|
|
|
// If it's a thread, we find and delete the whole tree, if necessary
|
|
|
|
if (tmid) {
|
|
|
|
try {
|
|
|
|
const msg = await msgCollection.find(message.id);
|
|
|
|
deleteBatch.push(msg.prepareDestroyPermanently());
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing: message not found
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Find the thread header and update it
|
|
|
|
const msg = await msgCollection.find(tmid);
|
|
|
|
if (msg.tcount <= 1) {
|
|
|
|
deleteBatch.push(
|
|
|
|
msg.prepareUpdate((m) => {
|
|
|
|
m.tcount = null;
|
|
|
|
m.tlm = null;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
try {
|
|
|
|
// If the whole thread was removed, delete the thread
|
|
|
|
const thread = await threadCollection.find(tmid);
|
|
|
|
deleteBatch.push(thread.prepareDestroyPermanently());
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing: thread not found
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deleteBatch.push(
|
|
|
|
msg.prepareUpdate((m) => {
|
|
|
|
m.tcount -= 1;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
// Do nothing: message not found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(...deleteBatch);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2019-01-30 12:11:02 +00:00
|
|
|
showActionSheet = () => {
|
|
|
|
ActionSheet.showActionSheetWithOptions({
|
|
|
|
options: this.options,
|
|
|
|
cancelButtonIndex: this.CANCEL_INDEX,
|
|
|
|
destructiveButtonIndex: this.DELETE_INDEX,
|
|
|
|
title: I18n.t('Message_actions')
|
|
|
|
}, (actionIndex) => {
|
|
|
|
this.handleActionPress(actionIndex);
|
2017-12-13 15:00:26 +00:00
|
|
|
});
|
2019-01-30 12:11:02 +00:00
|
|
|
}
|
2017-12-13 15:00:26 +00:00
|
|
|
|
|
|
|
handleActionPress = (actionIndex) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { actionsHide } = this.props;
|
2017-12-13 15:00:26 +00:00
|
|
|
switch (actionIndex) {
|
|
|
|
case this.RESEND_INDEX:
|
|
|
|
this.handleResend();
|
|
|
|
break;
|
|
|
|
case this.DELETE_INDEX:
|
|
|
|
this.handleDelete();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
actionsHide();
|
2017-12-13 15:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2019-01-30 12:11:02 +00:00
|
|
|
null
|
2017-12-13 15:00:26 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export default MessageErrorActions;
|