2021-09-13 20:41:05 +00:00
|
|
|
import { forwardRef, useImperativeHandle } from 'react';
|
2022-03-22 14:29:45 +00:00
|
|
|
import Model from '@nozbe/watermelondb/Model';
|
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';
|
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';
|
2022-03-22 14:29:45 +00:00
|
|
|
import { TMessageModel } from '../definitions';
|
2017-12-13 15:00:26 +00:00
|
|
|
|
2022-03-22 14:29:45 +00:00
|
|
|
const MessageErrorActions = forwardRef(({ tmid }: { tmid: string }, ref) => {
|
|
|
|
// TODO - remove this any after merge ActionSheet evaluate
|
2021-09-13 20:41:05 +00:00
|
|
|
const { showActionSheet }: any = useActionSheet();
|
2017-12-13 15:00:26 +00:00
|
|
|
|
2022-03-22 14:29:45 +00:00
|
|
|
const handleResend = protectedFunction(async (message: TMessageModel) => {
|
2019-10-08 12:36:15 +00:00
|
|
|
await RocketChat.resendMessage(message, tmid);
|
2019-09-16 20:26:32 +00:00
|
|
|
});
|
|
|
|
|
2022-03-22 14:29:45 +00:00
|
|
|
const handleDelete = async (message: TMessageModel) => {
|
2019-10-08 12:36:15 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2022-03-22 14:29:45 +00:00
|
|
|
const deleteBatch: Model[] = [];
|
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);
|
2022-02-01 13:39:09 +00:00
|
|
|
if (msg?.tcount && msg.tcount <= 1) {
|
2019-10-08 12:36:15 +00:00
|
|
|
deleteBatch.push(
|
2022-03-22 14:29:45 +00:00
|
|
|
msg.prepareUpdate(m => {
|
2019-10-08 12:36:15 +00:00
|
|
|
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(
|
2022-03-22 14:29:45 +00:00
|
|
|
msg.prepareUpdate(m => {
|
|
|
|
if (m.tcount) {
|
|
|
|
m.tcount -= 1;
|
|
|
|
}
|
2019-10-08 12:36:15 +00:00
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2020-06-15 19:35:45 +00:00
|
|
|
} catch {
|
2019-10-08 12:36:15 +00:00
|
|
|
// Do nothing: message not found
|
|
|
|
}
|
|
|
|
}
|
2022-02-01 13:39:09 +00:00
|
|
|
await db.write(async () => {
|
2019-10-08 12:36:15 +00:00
|
|
|
await db.batch(...deleteBatch);
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2020-06-15 19:35:45 +00:00
|
|
|
};
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2022-03-22 14:29:45 +00:00
|
|
|
const showMessageErrorActions = (message: TMessageModel) => {
|
2020-06-15 19:35:45 +00:00
|
|
|
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
|
|
|
|
}));
|
2021-07-21 15:50:39 +00:00
|
|
|
|
|
|
|
return null;
|
2020-06-15 19:35:45 +00:00
|
|
|
});
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
export default MessageErrorActions;
|