Skip to content

Commit

Permalink
feat: reflect user ban events in channel members state (#1128)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinCupela authored Jun 9, 2023
1 parent 9ecd345 commit ff0e134
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1395,6 +1395,24 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
case 'channel.visible':
channel.data = { ...channel.data, hidden: false };
break;
case 'user.banned':
if (!event.user?.id) break;
channelState.members[event.user.id] = {
...(channelState.members[event.user.id] || {}),
shadow_banned: !!event.shadow,
banned: !event.shadow,
user: { ...(channelState.members[event.user.id]?.user || {}), ...event.user },
};
break;
case 'user.unbanned':
if (!event.user?.id) break;
channelState.members[event.user.id] = {
...(channelState.members[event.user.id] || {}),
shadow_banned: false,
banned: false,
user: { ...(channelState.members[event.user.id]?.user || {}), ...event.user },
};
break;
default:
}

Expand Down
53 changes: 53 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,59 @@ describe('Channel _handleChannelEvent', function () {
channel._handleChannelEvent(channelVisibleEvent);
expect(channel.data.hidden).eq(true);
});

it('should update channel member ban state on user.banned and user.unbanned events', () => {
const user = { id: 'user_id' };
const shadowBanEvent = {
type: 'user.banned',
shadow: true,
user,
};
const shadowUnbanEvent = {
type: 'user.unbanned',
shadow: true,
user,
};
const banEvent = {
type: 'user.banned',
user,
};
const unbanEvent = {
type: 'user.unbanned',
user,
};

[
[shadowBanEvent, banEvent, { shadow_banned: true, banned: false }, { shadow_banned: false, banned: true }],
[
shadowBanEvent,
shadowUnbanEvent,
{ shadow_banned: true, banned: false },
{ shadow_banned: false, banned: false },
],
[
shadowBanEvent,
unbanEvent,
{ shadow_banned: true, banned: false },
{ shadow_banned: false, banned: false },
],
[banEvent, shadowBanEvent, { shadow_banned: false, banned: true }, { shadow_banned: true, banned: false }],
[
banEvent,
shadowUnbanEvent,
{ shadow_banned: false, banned: true },
{ shadow_banned: false, banned: false },
],
[banEvent, unbanEvent, { shadow_banned: false, banned: true }, { shadow_banned: false, banned: false }],
].forEach(([firstEvent, secondEvent, expectAfterFirst, expectAfterSecond]) => {
channel._handleChannelEvent(firstEvent);
expect(channel.state.members[user.id].banned).eq(expectAfterFirst.banned);
expect(channel.state.members[user.id].shadow_banned).eq(expectAfterFirst.shadow_banned);
channel._handleChannelEvent(secondEvent);
expect(channel.state.members[user.id].banned).eq(expectAfterSecond.banned);
expect(channel.state.members[user.id].shadow_banned).eq(expectAfterSecond.shadow_banned);
});
});
});

describe('Channels - Constructor', function () {
Expand Down

0 comments on commit ff0e134

Please sign in to comment.