Skip to content

Commit 78a53b5

Browse files
committed
feat: add hard and soft deletes in the db
1 parent ffa1335 commit 78a53b5

File tree

7 files changed

+61
-45
lines changed

7 files changed

+61
-45
lines changed

package/src/components/Channel/Channel.tsx

Lines changed: 26 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,7 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14501450
*/
14511451
const removeMessage: MessagesContextValue['removeMessage'] = async (message) => {
14521452
if (channel) {
1453+
// TODO: See if it's easy to refactor this to be able to accept DB queries
14531454
channel.state.removeMessage(message);
14541455
copyMessagesStateFromChannel(channel);
14551456

@@ -1458,10 +1459,14 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14581459
}
14591460
}
14601461

1461-
if (enableOfflineSupport) {
1462-
await dbApi.deleteMessage({
1463-
id: message.id,
1464-
});
1462+
if (client.offlineDb) {
1463+
// FIXME: Batch these maybe ?.
1464+
await Promise.all([
1465+
client.offlineDb.dropPendingTasks({ messageId: message.id }),
1466+
client.offlineDb.hardDeleteMessage({
1467+
id: message.id,
1468+
}),
1469+
]);
14651470
}
14661471
};
14671472

@@ -1497,47 +1502,32 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14971502
}
14981503
};
14991504

1500-
const deleteMessage: MessagesContextValue['deleteMessage'] = async (message) => {
1505+
const deleteMessage: MessagesContextValue['deleteMessage'] = async (
1506+
message,
1507+
hardDelete = false,
1508+
) => {
15011509
if (!channel.id) {
15021510
throw new Error('Channel has not been initialized yet');
15031511
}
15041512

1505-
if (!enableOfflineSupport) {
1506-
if (message.status === MessageStatusTypes.FAILED) {
1507-
await removeMessage(message);
1508-
return;
1509-
}
1510-
await client.deleteMessage(message.id);
1511-
return;
1512-
}
1513-
15141513
if (message.status === MessageStatusTypes.FAILED) {
1515-
await client.offlineDb?.syncManager.dropPendingTasks({ messageId: message.id });
15161514
await removeMessage(message);
1517-
} else {
1518-
const updatedMessage = {
1519-
...message,
1520-
cid: channel.cid,
1521-
deleted_at: new Date().toISOString(),
1522-
type: 'deleted' as MessageLabel,
1523-
};
1524-
updateMessage(updatedMessage);
1515+
return;
1516+
}
1517+
const updatedMessage = {
1518+
...message,
1519+
cid: channel.cid,
1520+
deleted_at: new Date().toISOString(),
1521+
type: 'deleted' as MessageLabel,
1522+
};
1523+
updateMessage(updatedMessage);
15251524

1526-
threadInstance?.upsertReplyLocally({ message: updatedMessage });
1525+
threadInstance?.upsertReplyLocally({ message: updatedMessage });
15271526

1528-
const data = await client.offlineDb?.syncManager.queueTask({
1529-
task: {
1530-
channelId: channel.id,
1531-
channelType: channel.type,
1532-
messageId: message.id,
1533-
payload: [message.id],
1534-
type: 'delete-message',
1535-
},
1536-
});
1527+
const data = await client.deleteMessage(message.id, hardDelete);
15371528

1538-
if (data?.message) {
1539-
updateMessage({ ...data.message });
1540-
}
1529+
if (data?.message) {
1530+
updateMessage({ ...data.message });
15411531
}
15421532
};
15431533

package/src/contexts/messagesContext/MessagesContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export type MessagesContextValue = Pick<MessageContextValue, 'isMessageAIGenerat
107107
* Defaults to: [DateHeader](https://github.com/GetStream/stream-chat-react-native/blob/main/package/src/components/MessageList/DateHeader.tsx)
108108
**/
109109
DateHeader: React.ComponentType<DateHeaderProps>;
110-
deleteMessage: (message: MessageResponse) => Promise<void>;
110+
deleteMessage: (message: MessageResponse, hardDelete?: boolean) => Promise<void>;
111111
deleteReaction: (type: string, messageId: string) => Promise<void>;
112112

113113
/** Should keyboard be dismissed when messaged is touched */

package/src/store/OfflineDB.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type {
33
GetChannelsForQueryType,
44
GetChannelsType,
55
GetLastSyncedAtType,
6-
UpsertReactionType,
76
UpsertUserSyncStatusType,
87
} from 'stream-chat';
98

@@ -32,17 +31,16 @@ export class OfflineDB extends AbstractOfflineDB {
3231
upsertUserSyncStatus = ({ userId, lastSyncedAt }: UpsertUserSyncStatusType) =>
3332
api.upsertUserSyncStatus({ currentUserId: userId, lastSyncedAt });
3433

35-
/* eslint-disable @typescript-eslint/no-unused-vars */
36-
// @ts-expect-error Not yet impelmented
37-
upsertReaction = async (options: UpsertReactionType) => {};
38-
/* eslint-enable */
39-
4034
addPendingTask = api.addPendingTask;
4135

4236
deletePendingTask = api.deletePendingTask;
4337

4438
deleteReaction = api.deleteReaction;
4539

40+
hardDeleteMessage = api.deleteMessage;
41+
42+
softDeleteMessage = api.softDeleteMessage;
43+
4644
getPendingTasks = api.getPendingTasks;
4745

4846
resetDB = SqliteClient.resetDB;

package/src/store/apis/addPendingTask.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1+
import type { PendingTask } from 'stream-chat';
2+
13
import { mapTaskToStorable } from '../mappers/mapTaskToStorable';
24
import { createDeleteQuery } from '../sqlite-utils/createDeleteQuery';
35
import { createUpsertQuery } from '../sqlite-utils/createUpsertQuery';
46
import { SqliteClient } from '../SqliteClient';
5-
import type { PendingTask } from '../types';
67

78
/*
89
* addPendingTask - Adds a pending task to the database

package/src/store/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export * from './updatePollMessage';
2727
export * from './addPendingTask';
2828
export * from './deletePendingTask';
2929
export * from './getPendingTasks';
30+
export * from './softDeleteMessage';
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { MessageLabel } from 'stream-chat';
2+
3+
import { createUpdateQuery } from '../sqlite-utils/createUpdateQuery';
4+
import { SqliteClient } from '../SqliteClient';
5+
6+
export const softDeleteMessage = async ({ flush = true, id }: { id: string; flush?: boolean }) => {
7+
const query = createUpdateQuery(
8+
'messages',
9+
{
10+
deletedAt: new Date().toISOString(),
11+
type: 'deleted' as MessageLabel,
12+
},
13+
{ id },
14+
);
15+
16+
SqliteClient.logger?.('info', 'softDeleteMessage', {
17+
flush,
18+
id,
19+
});
20+
21+
if (flush) {
22+
await SqliteClient.executeSql.apply(null, query);
23+
}
24+
25+
return [query];
26+
};

package/src/store/mappers/mapTaskToStorable.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { PendingTask } from '../types';
1+
import type { PendingTask } from 'stream-chat';
22

33
export const mapTaskToStorable = (task: PendingTask) => ({
44
...task,

0 commit comments

Comments
 (0)