Skip to content

Commit

Permalink
fix: remove reaction_counts from offline DB
Browse files Browse the repository at this point in the history
  • Loading branch information
khushal87 committed May 31, 2024
1 parent e014bb2 commit 54cccba
Show file tree
Hide file tree
Showing 9 changed files with 3 additions and 73 deletions.
3 changes: 1 addition & 2 deletions package/src/components/Chat/hooks/handleEventToSyncDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,7 @@ export const handleEventToSyncDB = <
if (type === 'reaction.updated') {
const message = event.message;
if (message && event.reaction) {
// We update the entire message to make sure we also update
// reaction_counts.
// We update the entire message to make sure we also update reaction_groups
return queriesWithChannelGuard((flushOverride) =>
updateMessage({
flush: flushOverride,
Expand Down
2 changes: 1 addition & 1 deletion package/src/store/QuickSqliteClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type { PreparedQueries, Table } from './types';
*
*/
export class QuickSqliteClient {
static dbVersion = 3;
static dbVersion = 4;

static dbName = DB_NAME;
static dbLocation = DB_LOCATION;
Expand Down
2 changes: 0 additions & 2 deletions package/src/store/apis/insertReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ export const insertReaction = ({

queries.push(createUpsertQuery('reactions', storableReaction));

const stringifiedNewReactionCounts = JSON.stringify(message.reaction_counts);
const stringifiedNewReactionGroups = JSON.stringify(message.reaction_groups);

queries.push(
createUpdateQuery(
'messages',
{
reactionCounts: stringifiedNewReactionCounts,
reactionGroups: stringifiedNewReactionGroups,
},
{ id: reaction.message_id },
Expand Down
17 changes: 0 additions & 17 deletions package/src/store/apis/updateReaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,6 @@ export const updateReaction = ({

let updatedReactionCounts: string | undefined;

if (message.reaction_counts) {
const { reactionCounts } = mapMessageToStorable(message);
updatedReactionCounts = reactionCounts;

queries.push(
createUpdateQuery(
'messages',
{
reactionCounts,
},
{
id: message.id,
},
),
);
}

let updatedReactionGroups: string | undefined;
if (message.reaction_groups) {
const { reactionGroups } = mapMessageToStorable(message);
Expand Down
2 changes: 0 additions & 2 deletions package/src/store/mappers/mapMessageToStorable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ export const mapMessageToStorable = (
latest_reactions,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
own_reactions,
reaction_counts,
reaction_groups,
text,
type,
Expand All @@ -33,7 +32,6 @@ export const mapMessageToStorable = (
deletedAt: mapDateTimeToStorable(deleted_at),
extraData: JSON.stringify(extraData),
id,
reactionCounts: JSON.stringify(reaction_counts),
reactionGroups: JSON.stringify(reaction_groups),
text,
type,
Expand Down
12 changes: 1 addition & 11 deletions package/src/store/mappers/mapStorableToMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,7 @@ export const mapStorableToMessage = <
messageRow: TableRowJoinedUser<'messages'>;
reactionRows: TableRowJoinedUser<'reactions'>[];
}): MessageResponse<StreamChatGenerics> => {
const {
createdAt,
deletedAt,
extraData,
reactionCounts,
reactionGroups,
updatedAt,
user,
...rest
} = messageRow;
const { createdAt, deletedAt, extraData, reactionGroups, updatedAt, user, ...rest } = messageRow;
const latestReactions =
reactionRows?.map((reaction) => mapStorableToReaction<StreamChatGenerics>(reaction)) || [];

Expand All @@ -41,7 +32,6 @@ export const mapStorableToMessage = <
deleted_at: deletedAt,
latest_reactions: latestReactions,
own_reactions: ownReactions,
reaction_counts: reactionCounts ? JSON.parse(reactionCounts) : {},
reaction_groups: reactionGroups ? JSON.parse(reactionGroups) : {},
updated_at: updatedAt,
user: mapStorableToUser(user),
Expand Down
2 changes: 0 additions & 2 deletions package/src/store/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export const tables: Tables = {
deletedAt: 'TEXT',
extraData: 'TEXT',
id: 'TEXT',
reactionCounts: 'TEXT',
reactionGroups: 'TEXT',
text: "TEXT DEFAULT ''",
type: 'TEXT',
Expand Down Expand Up @@ -263,7 +262,6 @@ export type Schema = {
deletedAt: string;
extraData: string;
id: string;
reactionCounts: string;
reactionGroups: string;
type: MessageLabel;
updatedAt: string;
Expand Down
25 changes: 0 additions & 25 deletions package/src/utils/addReactionToLocalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,6 @@ export const addReactionToLocalState = <
message.latest_reactions = [];
}
message.latest_reactions = message.latest_reactions.filter((r) => r.user_id !== user.id);
if (
currentReaction &&
message.reaction_counts &&
message.reaction_counts[currentReaction.type] &&
message.reaction_counts[currentReaction.type] > 0
) {
message.reaction_counts[currentReaction.type] =
message.reaction_counts[currentReaction.type] - 1;
}

if (
currentReaction &&
Expand All @@ -72,14 +63,6 @@ export const addReactionToLocalState = <
message.reaction_groups[currentReaction.type].sum_scores - 1;
}

if (!message.reaction_counts) {
message.reaction_counts = {
[reactionType]: 1,
};
} else {
message.reaction_counts[reactionType] = (message.reaction_counts?.[reactionType] || 0) + 1;
}

if (!message.reaction_groups) {
message.reaction_groups = {
[reactionType]: {
Expand Down Expand Up @@ -107,14 +90,6 @@ export const addReactionToLocalState = <
}
}
} else {
if (!message.reaction_counts) {
message.reaction_counts = {
[reactionType]: 1,
};
} else {
message.reaction_counts[reactionType] = (message.reaction_counts?.[reactionType] || 0) + 1;
}

if (!message.reaction_groups) {
message.reaction_groups = {
[reactionType]: {
Expand Down
11 changes: 0 additions & 11 deletions package/src/utils/removeReactionFromLocalState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,6 @@ export const removeReactionFromLocalState = <
}
}

if (message.reaction_counts?.[reactionType]) {
if (message.reaction_counts[reactionType] > 0) {
message.reaction_counts[reactionType] = message.reaction_counts[reactionType] - 1;
if (message.reaction_counts[reactionType] === 0) {
delete message.reaction_counts[reactionType];
}
} else {
delete message.reaction_counts[reactionType];
}
}

deleteReaction({
messageId,
reactionType,
Expand Down

0 comments on commit 54cccba

Please sign in to comment.