Skip to content

Commit

Permalink
Merge branch 'master' into feature/real-time-indicators
Browse files Browse the repository at this point in the history
  • Loading branch information
kanat authored Apr 30, 2024
2 parents 739878d + 7183154 commit ee9cd48
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .lintstagedrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"{**/*.{js,ts,md,css,scss,json}, .eslintrc.json, .prettierrc, .babelrc}": "prettier --list-different",
"**/*.{js,md,ts}": "eslint --max-warnings 0"
"**/*.{js,md,ts}, !test": "eslint --max-warnings 0"
}
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,6 @@ export type MessageResponse<
export type MessageResponseBase<
StreamChatGenerics extends ExtendableGenerics = DefaultGenerics
> = MessageBase<StreamChatGenerics> & {
reaction_groups: Record<string, ReactionGroupResponse>;
type: MessageLabel;
args?: string;
before_message_send_failed?: boolean;
Expand All @@ -651,6 +650,7 @@ export type MessageResponseBase<
pinned_by?: UserResponse<StreamChatGenerics> | null;
poll?: PollResponse<StreamChatGenerics>;
reaction_counts?: { [key: string]: number } | null;
reaction_groups?: { [key: string]: ReactionGroupResponse } | null;
reaction_scores?: { [key: string]: number } | null;
reply_count?: number;
shadowed?: boolean;
Expand Down
31 changes: 31 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
UserResponse,
MessageResponse,
FormatMessageResponse,
ReactionGroupResponse,
} from './types';
import { AxiosRequestConfig } from 'axios';

Expand Down Expand Up @@ -296,6 +297,11 @@ export function formatMessage<StreamChatGenerics extends ExtendableGenerics = De
created_at: message.created_at ? new Date(message.created_at) : new Date(),
updated_at: message.updated_at ? new Date(message.updated_at) : new Date(),
status: message.status || 'received',
reaction_groups: maybeGetReactionGroupsFallback(
message.reaction_groups,
message.reaction_counts,
message.reaction_scores,
),
};
}

Expand Down Expand Up @@ -365,3 +371,28 @@ export function addToMessageList<StreamChatGenerics extends ExtendableGenerics =
}
return [...messageArr];
}

function maybeGetReactionGroupsFallback(
groups: { [key: string]: ReactionGroupResponse } | null | undefined,
counts: { [key: string]: number } | null | undefined,
scores: { [key: string]: number } | null | undefined,
): { [key: string]: ReactionGroupResponse } | null {
if (groups) {
return groups;
}

if (counts && scores) {
const fallback: { [key: string]: ReactionGroupResponse } = {};

for (const type of Object.keys(counts)) {
fallback[type] = {
count: counts[type],
sum_scores: scores[type],
};
}

return fallback;
}

return null;
}
41 changes: 40 additions & 1 deletion test/unit/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import chai from 'chai';
import { axiosParamsSerializer, generateUUIDv4, normalizeQuerySort } from '../../src/utils';
import { axiosParamsSerializer, formatMessage, normalizeQuerySort } from '../../src/utils';
import sinon from 'sinon';

const expect = chai.expect;
Expand Down Expand Up @@ -100,3 +100,42 @@ describe('axiosParamsSerializer', () => {
}
});
});

describe('reaction groups fallback', () => {
it('uses groups if present', () => {
const date = '2024-04-30T11:03:39.217974Z';
const groups = {
love: {
count: 1,
sum_scores: 1,
first_reaction_at: date,
last_reaction_at: date,
},
};

const message = formatMessage({ reaction_groups: groups });
expect(message.reaction_groups).to.be.equal(groups);
});

it('falls back to counts + scores', () => {
const counts = { love: 1, sad: 1 };
const scores = { love: 1, sad: 2 };

const message = formatMessage({
reaction_groups: null,
reaction_counts: counts,
reaction_scores: scores,
});

expect(message.reaction_groups).to.deep.equal({
love: {
count: 1,
sum_scores: 1,
},
sad: {
count: 1,
sum_scores: 2,
},
});
});
});

0 comments on commit ee9cd48

Please sign in to comment.