2020-06-15 19:35:45 +00:00
|
|
|
import { useImperativeHandle, forwardRef } from 'react';
|
2017-12-13 15:00:26 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
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';
|
2020-06-15 19:35:45 +00:00
|
|
|
import { useActionSheet } from './ActionSheet';
|
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
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
const MessageErrorActions = forwardRef(({ tmid }, ref) => {
|
|
|
|
const { showActionSheet } = useActionSheet();
|
2017-12-13 15:00:26 +00:00
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
const handleResend = protectedFunction(async(message) => {
|
2019-10-08 12:36:15 +00:00
|
|
|
await RocketChat.resendMessage(message, tmid);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
const handleDelete = async(message) => {
|
2019-10-08 12:36:15 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
const deleteBatch = [];
|
2021-02-26 16:25:51 +00:00
|
|
|
const msgCollection = db.get('messages');
|
|
|
|
const threadCollection = db.get('threads');
|
2019-10-08 12:36:15 +00:00
|
|
|
|
|
|
|
// 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());
|
2020-06-15 19:35:45 +00:00
|
|
|
} catch {
|
2019-10-08 12:36:15 +00:00
|
|
|
// 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());
|
2020-06-15 19:35:45 +00:00
|
|
|
} catch {
|
2019-10-08 12:36:15 +00:00
|
|
|
// Do nothing: thread not found
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deleteBatch.push(
|
|
|
|
msg.prepareUpdate((m) => {
|
|
|
|
m.tcount -= 1;
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-06-15 19:35:45 +00:00
|
|
|
} catch {
|
2019-10-08 12:36:15 +00:00
|
|
|
// Do nothing: message not found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await db.action(async() => {
|
|
|
|
await db.batch(...deleteBatch);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2020-06-15 19:35:45 +00:00
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
const showMessageErrorActions = (message) => {
|
|
|
|
showActionSheet({
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
title: I18n.t('Resend'),
|
|
|
|
icon: 'send',
|
|
|
|
onPress: () => handleResend(message)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: I18n.t('Delete'),
|
2020-08-05 16:48:24 +00:00
|
|
|
icon: 'delete',
|
2020-06-15 19:35:45 +00:00
|
|
|
danger: true,
|
|
|
|
onPress: () => handleDelete(message)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
hasCancel: true
|
2017-12-13 15:00:26 +00:00
|
|
|
});
|
2020-06-15 19:35:45 +00:00
|
|
|
};
|
2017-12-13 15:00:26 +00:00
|
|
|
|
2020-06-15 19:35:45 +00:00
|
|
|
useImperativeHandle(ref, () => ({
|
|
|
|
showMessageErrorActions
|
|
|
|
}));
|
|
|
|
});
|
|
|
|
MessageErrorActions.propTypes = {
|
|
|
|
message: PropTypes.object,
|
|
|
|
tmid: PropTypes.string
|
|
|
|
};
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export default MessageErrorActions;
|