Skip to content

Commit 6cdc989

Browse files
authored
Merge pull request #438 from GetStream/custom-reactions
feat: allow reactions customization #437
2 parents 8c25eac + 914074d commit 6cdc989

File tree

7 files changed

+107
-19
lines changed

7 files changed

+107
-19
lines changed

docusaurus/docs/Angular/components/MessageReactionsComponent.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ export class CustomMessageComponent {
3737

3838
## Customization
3939

40+
You can override the default reactions using the [`MessageReactionsService`](../services/MessageReactionsService.mdx)
41+
4042
You can provide your own message reactions component by the [`CustomTemplatesService`](../services/CustomTemplatesService.mdx)
4143

4244
[//]: # "Start of generated content"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { TestBed } from '@angular/core/testing';
2+
3+
import { MessageReactionsService } from './message-reactions.service';
4+
5+
describe('MessageReactionsService', () => {
6+
let service: MessageReactionsService;
7+
8+
beforeEach(() => {
9+
TestBed.configureTestingModule({});
10+
service = TestBed.inject(MessageReactionsService);
11+
});
12+
13+
it('should be created', () => {
14+
expect(service).toBeTruthy();
15+
});
16+
});
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { Injectable } from '@angular/core';
2+
import { MessageReactionType } from './types';
3+
import { BehaviorSubject } from 'rxjs';
4+
5+
/**
6+
* The `MessageReactionsService` allows you to set which [reactions](https://getstream.io/chat/docs/javascript/send_reaction/?language=javascript) are enabled and their associated emoji.
7+
*
8+
*/
9+
@Injectable({
10+
providedIn: 'root',
11+
})
12+
export class MessageReactionsService {
13+
/**
14+
* The enabled [reactions](https://getstream.io/chat/docs/javascript/send_reaction/?language=javascript) and the associated emoji
15+
*
16+
* You can provide any string as a reaction. The emoji can be provided as a string, if you want to use custom images for reactions you have to provide a [custom reactions UI](../../services/CustomTemplatesService/#messagereactionstemplate)
17+
*/
18+
reactions$ = new BehaviorSubject<{ [key in MessageReactionType]: string }>({
19+
like: '👍',
20+
angry: '😠',
21+
love: '❤️',
22+
haha: '😂',
23+
wow: '😮',
24+
sad: '😞',
25+
});
26+
27+
constructor() {}
28+
29+
/**
30+
* Sets the enabled reactions
31+
*/
32+
set reactions(reactions: { [key in MessageReactionType]: string }) {
33+
this.reactions$.next(reactions);
34+
}
35+
36+
/**
37+
* Get the currently enabled reactions
38+
*/
39+
get reactions() {
40+
return this.reactions$.getValue();
41+
}
42+
}

projects/stream-chat-angular/src/lib/message-reactions/message-reactions.component.spec.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { ChannelService } from '../channel.service';
1313
import { SimpleChange } from '@angular/core';
1414
import { AvatarPlaceholderComponent } from '../avatar-placeholder/avatar-placeholder.component';
1515
import { MessageReactionType } from '../types';
16+
import { MessageReactionsService } from '../message-reactions.service';
1617

1718
describe('MessageReactionsComponent', () => {
1819
let component: MessageReactionsComponent;
@@ -38,15 +39,29 @@ describe('MessageReactionsComponent', () => {
3839
// eslint-disable-next-line no-unused-vars
3940
removeReaction: (id: string, type: MessageReactionType) => {},
4041
};
42+
const reactionsServiceMock = {
43+
reactions: {},
44+
};
4145

4246
beforeEach(async () => {
47+
reactionsServiceMock.reactions = {
48+
like: '👍',
49+
angry: '😠',
50+
love: '❤️',
51+
haha: '😂',
52+
wow: '😮',
53+
sad: '😞',
54+
};
4355
await TestBed.configureTestingModule({
4456
declarations: [
4557
MessageReactionsComponent,
4658
AvatarComponent,
4759
AvatarPlaceholderComponent,
4860
],
49-
providers: [{ provide: ChannelService, useValue: channelServiceMock }],
61+
providers: [
62+
{ provide: ChannelService, useValue: channelServiceMock },
63+
{ provide: MessageReactionsService, useValue: reactionsServiceMock },
64+
],
5065
}).compileComponents();
5166
});
5267

@@ -350,4 +365,22 @@ describe('MessageReactionsComponent', () => {
350365
);
351366
/* eslint-disable jasmine/new-line-before-expect */
352367
});
368+
369+
it('should filter not supported reactions', () => {
370+
reactionsServiceMock.reactions = {
371+
angry: '😠',
372+
haha: '😂',
373+
};
374+
component.messageReactionCounts = {
375+
angry: 1,
376+
haha: 2,
377+
like: 1,
378+
};
379+
fixture.detectChanges();
380+
const reactionCounts = queryReactionCountsFromReactionList();
381+
382+
expect(queryEmojis().length).toBe(2);
383+
expect(reactionCounts[0].textContent).toContain('1');
384+
expect(reactionCounts[1].textContent).toContain('2');
385+
});
353386
});

projects/stream-chat-angular/src/lib/message-reactions/message-reactions.component.ts

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,7 @@ import { ReactionResponse } from 'stream-chat';
1414
import { ChannelService } from '../channel.service';
1515
import { MessageReactionType, DefaultStreamChatGenerics } from '../types';
1616
import { NgxPopperjsTriggers, NgxPopperjsPlacements } from 'ngx-popperjs';
17-
18-
const emojiReactionsMapping: { [key in MessageReactionType]: string } = {
19-
like: '👍',
20-
angry: '😠',
21-
love: '❤️',
22-
haha: '😂',
23-
wow: '😮',
24-
sad: '😞',
25-
};
17+
import { MessageReactionsService } from '../message-reactions.service';
2618

2719
/**
2820
* The `MessageReactions` component displays the reactions of a message, the current user can add and remove reactions. You can read more about [message reactions](https://getstream.io/chat/docs/javascript/send_reaction/?language=javascript) in the platform documentation.
@@ -72,7 +64,8 @@ export class MessageReactionsComponent implements AfterViewChecked, OnChanges {
7264

7365
constructor(
7466
private cdRef: ChangeDetectorRef,
75-
private channelService: ChannelService
67+
private channelService: ChannelService,
68+
private messageReactionsService: MessageReactionsService
7669
) {}
7770

7871
ngOnChanges(changes: SimpleChanges): void {
@@ -91,20 +84,20 @@ export class MessageReactionsComponent implements AfterViewChecked, OnChanges {
9184
}
9285

9386
get existingReactions(): MessageReactionType[] {
94-
return Object.keys(this.messageReactionCounts).filter(
95-
(k) => this.messageReactionCounts[k as MessageReactionType]! > 0
96-
) as MessageReactionType[];
87+
return Object.keys(this.messageReactionCounts)
88+
.filter((k) => this.reactionOptions.indexOf(k) !== -1)
89+
.filter((k) => this.messageReactionCounts[k]! > 0);
9790
}
9891

9992
get reactionsCount() {
100-
return Object.values(this.messageReactionCounts).reduce(
101-
(total, count) => total + count,
93+
return this.existingReactions.reduce(
94+
(total, reaction) => total + this.messageReactionCounts[reaction]!,
10295
0
10396
);
10497
}
10598

10699
get reactionOptions(): MessageReactionType[] {
107-
return ['like', 'love', 'haha', 'wow', 'sad', 'angry'];
100+
return Object.keys(this.messageReactionsService.reactions);
108101
}
109102

110103
getLatestUserByReaction(reactionType: MessageReactionType) {
@@ -113,7 +106,7 @@ export class MessageReactionsComponent implements AfterViewChecked, OnChanges {
113106
}
114107

115108
getEmojiByReaction(reactionType: MessageReactionType) {
116-
return emojiReactionsMapping[reactionType];
109+
return this.messageReactionsService.reactions[reactionType];
117110
}
118111

119112
getUsersByReaction(reactionType: MessageReactionType) {

projects/stream-chat-angular/src/lib/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ export type MessageReactionType =
289289
| 'like'
290290
| 'love'
291291
| 'sad'
292-
| 'wow';
292+
| 'wow'
293+
| string;
293294

294295
export type AttachmentConfigration = {
295296
url: string;

projects/stream-chat-angular/src/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ export * from './lib/stream-autocomplete-textarea.module';
5454
export * from './lib/stream-textarea.module';
5555
export * from './lib/injection-tokens';
5656
export * from './lib/custom-templates.service';
57+
export * from './lib/message-reactions.service';
5758
export * from './lib/types';

0 commit comments

Comments
 (0)