Skip to content

Commit 5934018

Browse files
committed
feat: move new message handling to llc
1 parent 4f345e9 commit 5934018

File tree

12 files changed

+276
-83
lines changed

12 files changed

+276
-83
lines changed

package/expo-package/yarn.lock

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3589,6 +3589,11 @@ linkifyjs@^4.1.1:
35893589
resolved "https://registry.yarnpkg.com/linkifyjs/-/linkifyjs-4.1.1.tgz#73d427e3bbaaf4ca8e71c589ad4ffda11a9a5fde"
35903590
integrity sha512-zFN/CTVmbcVef+WaDXT63dNzzkfRBKT1j464NJQkV7iSgJU0sLBus9W0HBwnXK13/hf168pbrx/V/bjEHOXNHA==
35913591

3592+
linkifyjs@^4.2.0:
3593+
version "4.2.0"
3594+
resolved "https://registry.yarnpkg.com/linkifyjs/-/linkifyjs-4.2.0.tgz#9dd30222b9cbabec9c950e725ec00031c7fa3f08"
3595+
integrity sha512-pCj3PrQyATaoTYKHrgWRF3SJwsm61udVh+vuls/Rl6SptiDhgE7ziUIudAedRY9QEfynmM7/RmLEfPUyw1HPCw==
3596+
35923597
locate-path@^3.0.0:
35933598
version "3.0.0"
35943599
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
@@ -3759,6 +3764,11 @@ [email protected]:
37593764
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
37603765
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==
37613766

3767+
mime@^4.0.7:
3768+
version "4.0.7"
3769+
resolved "https://registry.yarnpkg.com/mime/-/mime-4.0.7.tgz#0b7a98b08c63bd3c10251e797d67840c9bde9f13"
3770+
integrity sha512-2OfDPL+e03E0LrXaGYOtTFIYhiuzep94NSsuhrNULq+stylcJedcHdzHtz0atMUuGwJfFYs0YL5xeC/Ca2x0eQ==
3771+
37623772
mimic-fn@^1.0.0:
37633773
version "1.2.0"
37643774
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022"
@@ -4772,9 +4782,9 @@ [email protected], stream-buffers@~2.2.0:
47724782
version "0.0.0"
47734783
uid ""
47744784

4775-
"stream-chat@https://github.com/GetStream/stream-chat-js.git#ab43f541954904c4efcf797a4fac8b162e3f342d":
4785+
"stream-chat@https://github.com/GetStream/stream-chat-js.git#4ff638045e434cc091a1200ecff4749b428d6936":
47764786
version "0.0.0-development"
4777-
resolved "https://github.com/GetStream/stream-chat-js.git#ab43f541954904c4efcf797a4fac8b162e3f342d"
4787+
resolved "https://github.com/GetStream/stream-chat-js.git#4ff638045e434cc091a1200ecff4749b428d6936"
47784788
dependencies:
47794789
"@types/jsonwebtoken" "^9.0.8"
47804790
"@types/ws" "^8.5.14"
@@ -4783,6 +4793,7 @@ [email protected], stream-buffers@~2.2.0:
47834793
form-data "^4.0.0"
47844794
isomorphic-ws "^5.0.0"
47854795
jsonwebtoken "^9.0.2"
4796+
linkifyjs "^4.2.0"
47864797
ws "^8.18.1"
47874798

47884799
"string-width-cjs@npm:string-width@^4.2.0":

package/native-package/yarn.lock

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2520,6 +2520,11 @@ linkifyjs@^4.1.1:
25202520
resolved "https://registry.yarnpkg.com/linkifyjs/-/linkifyjs-4.1.1.tgz#73d427e3bbaaf4ca8e71c589ad4ffda11a9a5fde"
25212521
integrity sha512-zFN/CTVmbcVef+WaDXT63dNzzkfRBKT1j464NJQkV7iSgJU0sLBus9W0HBwnXK13/hf168pbrx/V/bjEHOXNHA==
25222522

2523+
linkifyjs@^4.2.0:
2524+
version "4.2.0"
2525+
resolved "https://registry.yarnpkg.com/linkifyjs/-/linkifyjs-4.2.0.tgz#9dd30222b9cbabec9c950e725ec00031c7fa3f08"
2526+
integrity sha512-pCj3PrQyATaoTYKHrgWRF3SJwsm61udVh+vuls/Rl6SptiDhgE7ziUIudAedRY9QEfynmM7/RmLEfPUyw1HPCw==
2527+
25232528
locate-path@^3.0.0:
25242529
version "3.0.0"
25252530
resolved "https://registry.yarnpkg.com/locate-path/-/locate-path-3.0.0.tgz#dbec3b3ab759758071b58fe59fc41871af21400e"
@@ -2859,6 +2864,11 @@ [email protected]:
28592864
resolved "https://registry.yarnpkg.com/mime/-/mime-1.6.0.tgz#32cd9e5c64553bd58d19a568af452acff04981b1"
28602865
integrity sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==
28612866

2867+
mime@^4.0.7:
2868+
version "4.0.7"
2869+
resolved "https://registry.yarnpkg.com/mime/-/mime-4.0.7.tgz#0b7a98b08c63bd3c10251e797d67840c9bde9f13"
2870+
integrity sha512-2OfDPL+e03E0LrXaGYOtTFIYhiuzep94NSsuhrNULq+stylcJedcHdzHtz0atMUuGwJfFYs0YL5xeC/Ca2x0eQ==
2871+
28622872
minimatch@^3.0.4, minimatch@^3.1.1:
28632873
version "3.1.2"
28642874
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b"
@@ -3488,9 +3498,9 @@ statuses@~1.5.0:
34883498
version "0.0.0"
34893499
uid ""
34903500

3491-
"stream-chat@https://github.com/GetStream/stream-chat-js.git#ab43f541954904c4efcf797a4fac8b162e3f342d":
3501+
"stream-chat@https://github.com/GetStream/stream-chat-js.git#4ff638045e434cc091a1200ecff4749b428d6936":
34923502
version "0.0.0-development"
3493-
resolved "https://github.com/GetStream/stream-chat-js.git#ab43f541954904c4efcf797a4fac8b162e3f342d"
3503+
resolved "https://github.com/GetStream/stream-chat-js.git#4ff638045e434cc091a1200ecff4749b428d6936"
34943504
dependencies:
34953505
"@types/jsonwebtoken" "^9.0.8"
34963506
"@types/ws" "^8.5.14"
@@ -3499,6 +3509,7 @@ statuses@~1.5.0:
34993509
form-data "^4.0.0"
35003510
isomorphic-ws "^5.0.0"
35013511
jsonwebtoken "^9.0.2"
3512+
linkifyjs "^4.2.0"
35023513
ws "^8.18.1"
35033514

35043515
string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:

package/src/components/Channel/Channel.tsx

Lines changed: 52 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,27 +1498,29 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
14981498
/**
14991499
* Removes the message from local state
15001500
*/
1501-
const removeMessage: MessagesContextValue['removeMessage'] = useStableCallback(async (message) => {
1502-
if (channel) {
1503-
// TODO: See if it's easy to refactor this to be able to accept DB queries
1504-
channel.state.removeMessage(message);
1505-
copyMessagesStateFromChannel(channel);
1501+
const removeMessage: MessagesContextValue['removeMessage'] = useStableCallback(
1502+
async (message) => {
1503+
if (channel) {
1504+
// TODO: See if it's easy to refactor this to be able to accept DB queries
1505+
channel.state.removeMessage(message);
1506+
copyMessagesStateFromChannel(channel);
15061507

15071508
if (thread) {
15081509
setThreadMessages(channel.state.threads[thread.id] || []);
15091510
}
15101511
}
15111512

1512-
if (client.offlineDb) {
1513-
// FIXME: Batch these maybe ?.
1514-
await Promise.all([
1515-
client.offlineDb.dropPendingTasks({ messageId: message.id }),
1516-
client.offlineDb.hardDeleteMessage({
1517-
id: message.id,
1518-
}),
1519-
]);
1520-
}
1521-
});
1513+
if (client.offlineDb) {
1514+
// FIXME: Batch these maybe ?.
1515+
await Promise.all([
1516+
client.offlineDb.dropPendingTasks({ messageId: message.id }),
1517+
client.offlineDb.hardDeleteMessage({
1518+
id: message.id,
1519+
}),
1520+
]);
1521+
}
1522+
},
1523+
);
15221524

15231525
const sendReaction = useStableCallback(async (type: string, messageId: string) => {
15241526
if (!channel?.id || !client.user) {
@@ -1552,34 +1554,33 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
15521554
}
15531555
});
15541556

1555-
const deleteMessage: MessagesContextValue['deleteMessage'] = useStableCallback(async (
1556-
message,
1557-
hardDelete = false,
1558-
) => {
1559-
if (!channel.id) {
1560-
throw new Error('Channel has not been initialized yet');
1561-
}
1557+
const deleteMessage: MessagesContextValue['deleteMessage'] = useStableCallback(
1558+
async (message, hardDelete = false) => {
1559+
if (!channel.id) {
1560+
throw new Error('Channel has not been initialized yet');
1561+
}
15621562

1563-
if (message.status === MessageStatusTypes.FAILED) {
1564-
await removeMessage(message);
1565-
return;
1566-
}
1567-
const updatedMessage = {
1568-
...message,
1569-
cid: channel.cid,
1570-
deleted_at: new Date().toISOString(),
1571-
type: 'deleted' as MessageLabel,
1572-
};
1573-
updateMessage(updatedMessage);
1563+
if (message.status === MessageStatusTypes.FAILED) {
1564+
await removeMessage(message);
1565+
return;
1566+
}
1567+
const updatedMessage = {
1568+
...message,
1569+
cid: channel.cid,
1570+
deleted_at: new Date().toISOString(),
1571+
type: 'deleted' as MessageLabel,
1572+
};
1573+
updateMessage(updatedMessage);
15741574

1575-
threadInstance?.upsertReplyLocally({ message: updatedMessage });
1575+
threadInstance?.upsertReplyLocally({ message: updatedMessage });
15761576

1577-
const data = await client.deleteMessage(message.id, hardDelete);
1577+
const data = await client.deleteMessage(message.id, hardDelete);
15781578

1579-
if (data?.message) {
1580-
updateMessage({ ...data.message });
1581-
}
1582-
});
1579+
if (data?.message) {
1580+
updateMessage({ ...data.message });
1581+
}
1582+
},
1583+
);
15831584

15841585
const deleteReaction: MessagesContextValue['deleteReaction'] = useStableCallback(
15851586
async (type: string, messageId: string) => {
@@ -1589,14 +1590,20 @@ const ChannelWithContext = (props: PropsWithChildren<ChannelPropsWithContext>) =
15891590

15901591
const payload: Parameters<ChannelClass['deleteReaction']> = [messageId, type];
15911592

1592-
if (enableOfflineSupport) {
1593-
channel.state.removeReaction({ created_at: '', message_id: messageId, type, updated_at: '' });
1593+
if (enableOfflineSupport) {
1594+
channel.state.removeReaction({
1595+
created_at: '',
1596+
message_id: messageId,
1597+
type,
1598+
updated_at: '',
1599+
});
15941600

1595-
copyMessagesStateFromChannel(channel);
1596-
}
1601+
copyMessagesStateFromChannel(channel);
1602+
}
15971603

1598-
await channel.deleteReaction(...payload);
1599-
});
1604+
await channel.deleteReaction(...payload);
1605+
},
1606+
);
16001607

16011608
/**
16021609
* THREAD METHODS

package/src/store/OfflineDB.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,21 @@ export class OfflineDB extends AbstractOfflineDB {
2121
upsertChannels = api.upsertChannels;
2222

2323
// FIXME
24-
upsertUserSyncStatus = ({ userId, lastSyncedAt }: UpsertUserSyncStatusType) =>
25-
api.upsertUserSyncStatus({ currentUserId: userId, lastSyncedAt });
24+
upsertUserSyncStatus = ({ userId, lastSyncedAt, flush }: UpsertUserSyncStatusType) =>
25+
api.upsertUserSyncStatus({ currentUserId: userId, flush, lastSyncedAt });
2626

2727
// FIXME
2828
upsertAppSettings = ({ appSettings, userId, flush }: UpsertAppSettingsType) =>
2929
api.upsertAppSettings({ appSettings, currentUserId: userId, flush });
3030

3131
upsertPoll = api.upsertPoll;
3232

33+
upsertChannelData = api.upsertChannelData;
34+
35+
upsertReads = api.upsertReads;
36+
37+
upsertMessages = api.upsertMessages;
38+
3339
// FIXME
3440
getChannels = ({ cids, userId }: GetChannelsType) =>
3541
api.getChannels({ channelIds: cids, currentUserId: userId });
@@ -66,6 +72,8 @@ export class OfflineDB extends AbstractOfflineDB {
6672

6773
updateMessage = api.updateMessage;
6874

75+
channelExists = api.channelExists;
76+
6977
resetDB = SqliteClient.resetDB;
7078

7179
executeSqlBatch = SqliteClient.executeSqlBatch;

package/src/store/SqliteClient.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ export class SqliteClient {
9696
});
9797
await this.db.executeBatch(finalQueries);
9898
} catch (e) {
99+
this.db?.execute('ROLLBACK');
99100
this.logger?.('error', 'SqlBatch queries failed', {
100101
error: e,
101102
queries,
@@ -214,6 +215,7 @@ export class SqliteClient {
214215
static resetDB = async () => {
215216
this.logger?.('info', 'resetDB');
216217
if (this.db) {
218+
// await this.db.execute('ROLLBACK');
217219
await SqliteClient.dropTables();
218220
SqliteClient.closeDB();
219221
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { SqliteClient } from '../SqliteClient';
2+
3+
export const channelExists = async ({ cid }: { cid: string }) => {
4+
const channels = await SqliteClient.executeSql(
5+
'SELECT EXISTS(SELECT 1 FROM channels WHERE cid = ?)',
6+
[cid],
7+
);
8+
9+
SqliteClient.logger?.('info', 'channelExists', {
10+
cid,
11+
});
12+
13+
return channels.length > 0;
14+
};

package/src/store/apis/deleteReaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const deleteReaction = async ({
1717
const queries: PreparedQueries[] = [];
1818

1919
if (!message) {
20-
return;
20+
return [];
2121
}
2222

2323
queries.push(

package/src/store/apis/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ export * from './addPendingTask';
2929
export * from './deletePendingTask';
3030
export * from './getPendingTasks';
3131
export * from './softDeleteMessage';
32+
export * from './channelExists';

package/src/store/apis/upsertAppSettings.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ export const upsertAppSettings = async ({
1313
flush?: boolean;
1414
}) => {
1515
const storableAppSettings = JSON.stringify(appSettings);
16-
const query = createUpsertQuery('userSyncStatus', {
17-
appSettings: storableAppSettings,
18-
userId: currentUserId,
19-
});
16+
const queries = [
17+
createUpsertQuery('userSyncStatus', {
18+
appSettings: storableAppSettings,
19+
userId: currentUserId,
20+
}),
21+
];
2022

2123
SqliteClient.logger?.('info', 'upsertAppSettings', {
2224
appSettings: storableAppSettings,
@@ -25,6 +27,8 @@ export const upsertAppSettings = async ({
2527
});
2628

2729
if (flush) {
28-
await SqliteClient.executeSql.apply(null, query);
30+
await SqliteClient.executeSqlBatch(queries);
2931
}
32+
33+
return queries;
3034
};

package/src/store/apis/upsertChannels.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { ChannelAPIResponse, ChannelFilters, ChannelSort } from 'stream-chat';
1+
import type { ChannelAPIResponse } from 'stream-chat';
22

3-
import { upsertCidsForQuery } from './upsertCidsForQuery';
43
import { upsertMembers } from './upsertMembers';
54

65
import { upsertMessages } from './upsertMessages';
@@ -13,16 +12,12 @@ import type { PreparedQueries } from '../types';
1312

1413
export const upsertChannels = async ({
1514
channels,
16-
filters,
1715
flush = true,
1816
isLatestMessagesSet,
19-
sort,
2017
}: {
2118
channels: ChannelAPIResponse[];
22-
filters?: ChannelFilters;
2319
flush?: boolean;
2420
isLatestMessagesSet?: boolean;
25-
sort?: ChannelSort;
2621
}) => {
2722
// Update the database only if the query is provided.
2823
let queries: PreparedQueries[] = [];
@@ -33,17 +28,6 @@ export const upsertChannels = async ({
3328
channelIds,
3429
});
3530

36-
if (filters || sort) {
37-
queries = queries.concat(
38-
await upsertCidsForQuery({
39-
cids: channelIds,
40-
filters,
41-
flush: false,
42-
sort,
43-
}),
44-
);
45-
}
46-
4731
for (const channel of channels) {
4832
queries.push(createUpsertQuery('channels', mapChannelDataToStorable(channel.channel)));
4933

0 commit comments

Comments
 (0)