Skip to content

Commit 1365dc0

Browse files
committed
refactor: keep hardDelete and deleteForMe options mutually exclusive in client.deleteMessage
1 parent c7cc175 commit 1365dc0

File tree

2 files changed

+45
-66
lines changed

2 files changed

+45
-66
lines changed

src/client.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3116,16 +3116,18 @@ export class StreamChat {
31163116
messageID: string,
31173117
optionsOrHardDelete?: DeleteMessageOptions | boolean,
31183118
): Promise<APIResponse & { message: MessageResponse }> {
3119-
const { hardDelete } = (
3120-
typeof optionsOrHardDelete === 'boolean'
3121-
? { hardDelete: optionsOrHardDelete }
3122-
: (optionsOrHardDelete ?? {})
3123-
) as DeleteMessageOptions;
3119+
let options: DeleteMessageOptions = {};
3120+
if (typeof optionsOrHardDelete === 'boolean') {
3121+
options = optionsOrHardDelete ? { hardDelete: true } : {};
3122+
} else if (optionsOrHardDelete?.deleteForMe) {
3123+
options = { deleteForMe: true };
3124+
} else if (optionsOrHardDelete?.hardDelete) {
3125+
options = { hardDelete: true };
3126+
}
31243127

31253128
try {
31263129
if (this.offlineDb) {
3127-
// todo: write code for this.offlineDb.deleteForMe
3128-
if (hardDelete) {
3130+
if (options.hardDelete) {
31293131
await this.offlineDb.hardDeleteMessage({ id: messageID });
31303132
} else {
31313133
await this.offlineDb.softDeleteMessage({ id: messageID });
@@ -3134,7 +3136,7 @@ export class StreamChat {
31343136
{
31353137
task: {
31363138
messageId: messageID,
3137-
payload: [messageID, optionsOrHardDelete],
3139+
payload: [messageID, options],
31383140
type: 'delete-message',
31393141
},
31403142
},
@@ -3147,14 +3149,16 @@ export class StreamChat {
31473149
});
31483150
}
31493151

3150-
return this._deleteMessage(messageID, optionsOrHardDelete);
3152+
return this._deleteMessage(messageID, options);
31513153
}
31523154

31533155
// fixme: remove the signature with optionsOrHardDelete boolean with the next major release
31543156
async _deleteMessage(
31553157
messageID: string,
31563158
optionsOrHardDelete?: DeleteMessageOptions | boolean,
31573159
): Promise<APIResponse & { message: MessageResponse }> {
3160+
// this is a API call method, we do not route hardDelete: true and deleteForMe: true to deleteForMe: true
3161+
// and expect to receive error response from the server
31583162
const { deleteForMe, hardDelete } = (
31593163
typeof optionsOrHardDelete === 'boolean'
31603164
? { hardDelete: optionsOrHardDelete }

test/unit/client.test.js

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -953,64 +953,37 @@ describe('message deletion', () => {
953953
vi.resetAllMocks();
954954
});
955955

956-
it('should soft delete the message and queue task if hardDelete is false', async () => {
957-
await client.deleteMessage(messageId, false);
958-
959-
expect(client.offlineDb.softDeleteMessage).toHaveBeenCalledTimes(1);
960-
expect(client.offlineDb.softDeleteMessage).toHaveBeenCalledWith({ id: messageId });
961-
expect(client.offlineDb.hardDeleteMessage).not.toHaveBeenCalled();
962-
expect(queueTaskSpy).toHaveBeenCalledTimes(1);
963-
964-
const taskArg = queueTaskSpy.mock.calls[0][0];
965-
expect(taskArg).to.deep.equal({
966-
task: {
967-
messageId,
968-
payload: [messageId, false],
969-
type: 'delete-message',
970-
},
971-
});
972-
expect(_deleteMessageSpy).not.toHaveBeenCalled();
973-
});
974-
975-
it('should hard delete the message and queue task if hardDelete is true', async () => {
976-
await client.deleteMessage(messageId, true);
977-
978-
expect(client.offlineDb.hardDeleteMessage).toHaveBeenCalledTimes(1);
979-
expect(client.offlineDb.hardDeleteMessage).toHaveBeenCalledWith({ id: messageId });
980-
expect(client.offlineDb.softDeleteMessage).not.toHaveBeenCalled();
981-
expect(queueTaskSpy).toHaveBeenCalledTimes(1);
982-
983-
const taskArg = queueTaskSpy.mock.calls[0][0];
984-
expect(taskArg).to.deep.equal({
985-
task: {
986-
messageId,
987-
payload: [messageId, true],
988-
type: 'delete-message',
989-
},
990-
});
991-
expect(_deleteMessageSpy).not.toHaveBeenCalled();
992-
});
993-
994956
it.each([
957+
['undefined', undefined, {}],
958+
['true', true, { hardDelete: true }],
959+
['false', false, {}],
960+
['{ hardDelete: false }', { hardDelete: false }, {}],
961+
['{ hardDelete: true }', { hardDelete: true }, { hardDelete: true }],
962+
['{ deleteForMe: true }', { deleteForMe: true }, { deleteForMe: true }],
963+
['{ deleteForMe: false }', { deleteForMe: false }, {}],
995964
[
996965
'{ hardDelete: false, deleteForMe: true }',
997966
{ hardDelete: false, deleteForMe: true },
967+
{ deleteForMe: true },
998968
],
999969
[
1000970
'{ hardDelete: true, deleteForMe: true }',
1001971
{ hardDelete: true, deleteForMe: true },
972+
{ deleteForMe: true },
1002973
],
1003974
[
1004975
'{ hardDelete: false, deleteForMe: false }',
1005976
{ hardDelete: false, deleteForMe: false },
977+
{},
1006978
],
1007979
[
1008-
'{ hardDelete: false, deleteForMe: false }',
1009-
{ hardDelete: false, deleteForMe: false },
980+
'{ hardDelete: true, deleteForMe: false }',
981+
{ hardDelete: true, deleteForMe: false },
982+
{ hardDelete: true },
1010983
],
1011-
])('should parse delete message options %s', async (_, options) => {
984+
])('should parse delete message options %s', async (_, options, expectedOptions) => {
1012985
await client.deleteMessage(messageId, options);
1013-
if (options.hardDelete) {
986+
if (expectedOptions.hardDelete) {
1014987
expect(client.offlineDb.hardDeleteMessage).toHaveBeenCalledTimes(1);
1015988
expect(client.offlineDb.hardDeleteMessage).toHaveBeenCalledWith({
1016989
id: messageId,
@@ -1030,48 +1003,50 @@ describe('message deletion', () => {
10301003
expect(taskArg).to.deep.equal({
10311004
task: {
10321005
messageId,
1033-
payload: [messageId, options],
1006+
payload: [messageId, expectedOptions],
10341007
type: 'delete-message',
10351008
},
10361009
});
10371010
expect(_deleteMessageSpy).not.toHaveBeenCalled();
10381011
});
10391012

1040-
it('should fall back to _deleteMessage if offlineDb is not set', async () => {
1041-
client.offlineDb = undefined;
1042-
1043-
await client.deleteMessage(messageId, true);
1044-
1045-
expect(_deleteMessageSpy).toHaveBeenCalledTimes(1);
1046-
expect(_deleteMessageSpy).toHaveBeenCalledWith(messageId, true);
1047-
});
1048-
10491013
it.each([
1014+
['undefined', undefined, {}],
1015+
['true', true, { hardDelete: true }],
1016+
['false', false, {}],
1017+
['{ hardDelete: false }', { hardDelete: false }, {}],
1018+
['{ hardDelete: true }', { hardDelete: true }, { hardDelete: true }],
1019+
['{ deleteForMe: true }', { deleteForMe: true }, { deleteForMe: true }],
1020+
['{ deleteForMe: false }', { deleteForMe: false }, {}],
10501021
[
10511022
'{ hardDelete: false, deleteForMe: true }',
10521023
{ hardDelete: false, deleteForMe: true },
1024+
{ deleteForMe: true },
10531025
],
10541026
[
10551027
'{ hardDelete: true, deleteForMe: true }',
10561028
{ hardDelete: true, deleteForMe: true },
1029+
{ deleteForMe: true },
10571030
],
10581031
[
10591032
'{ hardDelete: false, deleteForMe: false }',
10601033
{ hardDelete: false, deleteForMe: false },
1034+
{},
10611035
],
10621036
[
1063-
'{ hardDelete: false, deleteForMe: false }',
1064-
{ hardDelete: false, deleteForMe: false },
1037+
'{ hardDelete: true, deleteForMe: false }',
1038+
{ hardDelete: true, deleteForMe: false },
1039+
{ hardDelete: true },
10651040
],
10661041
])(
1067-
'should fall back to _deleteMessage if offlineDb is not set and delete options object is provided as %s',
1068-
async (_, options) => {
1042+
'should fall back to _deleteMessage if offlineDb is not set and delete options is %s',
1043+
async (_, options, expectedOptions) => {
10691044
client.offlineDb = undefined;
10701045

10711046
await client.deleteMessage(messageId, options);
10721047

10731048
expect(_deleteMessageSpy).toHaveBeenCalledTimes(1);
1074-
expect(_deleteMessageSpy).toHaveBeenCalledWith(messageId, options);
1049+
expect(_deleteMessageSpy).toHaveBeenCalledWith(messageId, expectedOptions);
10751050
},
10761051
);
10771052

0 commit comments

Comments
 (0)