Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reload channel state if frozen flag changed #1196

Merged
merged 8 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1026,9 +1026,20 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
// add any messages to our channel state
const { messageSet } = this._initializeState(state, messageSetToAddToIfDoesNotExist);

const areCapabilitiesChanged =
[...(state.channel.own_capabilities || [])].sort().join() !==
[...(Array.isArray(this.data?.own_capabilities) ? (this.data?.own_capabilities as string[]) : [])].sort().join();
this.data = state.channel;
this.offlineMode = false;

if (areCapabilitiesChanged) {
this.getClient().dispatchEvent({
type: 'capabilities.changed',
cid: this.cid,
own_capabilities: state.channel.own_capabilities,
});
}

this.getClient().dispatchEvent({
type: 'channels.queried',
queriedChannels: {
Expand Down Expand Up @@ -1351,6 +1362,10 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
break;
case 'channel.updated':
if (event.channel) {
const isFrozenChanged = event.channel?.frozen !== undefined && event.channel.frozen !== channel.data?.frozen;
if (isFrozenChanged) {
szuperaz marked this conversation as resolved.
Show resolved Hide resolved
this.query({ state: false, members: { limit: 0 }, messages: { limit: 0 }, watchers: { limit: 0 } });
}
channel.data = {
...event.channel,
hidden: event.channel?.hidden ?? channel.data?.hidden,
Expand Down
1 change: 1 addition & 0 deletions src/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ export const EVENT_MAP = {
'connection.changed': true,
'connection.recovered': true,
'transport.changed': true,
'capabilities.changed': true,
};
49 changes: 49 additions & 0 deletions test/unit/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,12 @@ import { generateMsg } from './test-utils/generateMessage';
import { generateUser } from './test-utils/generateUser';
import { getClientWithUser } from './test-utils/getClient';
import { getOrCreateChannelApi } from './test-utils/getOrCreateChannelApi';
import sinon from 'sinon';
import { mockChannelQueryResponse } from './test-utils/mockChannelQueryResponse';

import { StreamChat } from '../../src/client';
import { ChannelState } from '../../src';
import exp from 'constants';

const expect = chai.expect;

Expand Down Expand Up @@ -478,6 +481,52 @@ describe('Channel _handleChannelEvent', function () {
expect(channel.data.hidden).eq(true);
});

it('should update the frozen flag and reload channel state to update `own_capabilities`', () => {
const event = {
channel: { frozen: true },
type: 'channel.updated',
};
channel.data.frozen = false;
sinon.spy(channel, 'query');
szuperaz marked this conversation as resolved.
Show resolved Hide resolved

channel._handleChannelEvent(event);
expect(channel.data.frozen).eq(true);
expect(channel.query.called).to.be.true;

channel._handleChannelEvent(event);
expect(channel.query.calledOnce).to.be.true;
});

it('should dispatch "capabilities.changed" event', async () => {
const mock = sinon.mock(client);
const response = mockChannelQueryResponse;
channel.data.own_capabilities = response.channel.own_capabilities.slice(0, 1);
mock.expects('post').returns(Promise.resolve(response));
const spy = sinon.spy();
channel.on('capabilities.changed', spy);

await channel.query();

expect(spy.calledOnce).to.be.true;

const arg = spy.firstCall.args[0];
// We don't care about received_at in the assertion
delete arg.received_at;
sinon.assert.match(arg, {
type: 'capabilities.changed',
cid: channel.cid,
own_capabilities: response.channel.own_capabilities,
});

channel.data.own_capabilities = response.channel.own_capabilities;
mock.expects('post').returns(Promise.resolve(response));
spy.resetHistory();

await channel.query();

expect(spy.notCalled).to.be.true;
});

it('should update channel member ban state on user.banned and user.unbanned events', () => {
const user = { id: 'user_id' };
const shadowBanEvent = {
Expand Down
190 changes: 190 additions & 0 deletions test/unit/test-utils/mockChannelQueryResponse.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
export const mockChannelQueryResponse = {
channel: {
id: '!members-VTb-DIRK8V2pHHguhdYktHnmOGCfRzphbD2BDcXjItE',
type: 'messaging',
cid: 'messaging:!members-VTb-DIRK8V2pHHguhdYktHnmOGCfRzphbD2BDcXjItE',
last_message_at: '2023-11-14T12:39:29.396443Z',
created_at: '2023-08-18T09:06:45.421888Z',
updated_at: '2023-11-15T08:26:11.595394Z',
created_by: {
id: 'zitaszuperagetstreamio',
role: 'admin',
created_at: '2021-09-14T13:07:45.764353Z',
updated_at: '2023-11-02T10:43:16.258959Z',
last_active: '2022-07-15T07:08:03.807199Z',
banned: false,
online: false,
first_name: 'Zita',
staff_user: true,
dashboard_user: true,
name: 'Zita Szupera',
email: 'zita.szupera@getstream.io',
image: 'https://getstream.io/random_png/?id=little-wood-9\u0026name=little-wood-9',
last_name: 'Szupera',
},
frozen: true,
disabled: false,
member_count: 2,
config: {
created_at: '2021-11-10T13:31:08.527632Z',
updated_at: '2023-10-25T11:22:29.482779Z',
name: 'messaging',
typing_events: true,
read_events: true,
connect_events: true,
search: true,
reactions: true,
replies: true,
quotes: true,
mutes: true,
uploads: true,
url_enrichment: true,
custom_events: true,
push_notifications: true,
reminders: false,
mark_messages_pending: false,
message_retention: 'infinite',
max_message_length: 5000,
automod: 'AI',
automod_behavior: 'flag',
blocklist: 'profanity_en_2020_v1',
blocklist_behavior: 'flag',
automod_thresholds: {},
commands: [
{
name: 'giphy',
description: 'Post a random gif to the channel',
args: '[text]',
set: 'fun_set',
},
{
name: 'ban',
description: 'Ban a user',
args: '[@username] [text]',
set: 'moderation_set',
},
{
name: 'mute',
description: 'Mute a user',
args: '[@username]',
set: 'moderation_set',
},
{
name: 'unmute',
description: 'Unmute a user',
args: '[@username]',
set: 'moderation_set',
},
{
name: 'unban',
description: 'Unban a user',
args: '[@username]',
set: 'moderation_set',
},
],
},
cooldown: 30,
own_capabilities: [
'ban-channel-members',
'connect-events',
'delete-any-message',
'delete-channel',
'delete-own-message',
'flag-message',
'freeze-channel',
'join-channel',
'leave-channel',
'mute-channel',
'read-events',
'search-messages',
'send-custom-events',
'send-typing-events',
'set-channel-cooldown',
'skip-slow-mode',
'typing-events',
'update-any-message',
'update-channel',
'update-own-message',
'upload-file',
],
hidden: false,
name: 'Edit channel2',
},
messages: [],
pinned_messages: [],
members: [
{
user_id: 'sara-angular-test',
user: {
id: 'sara-angular-test',
role: 'user',
created_at: '2023-06-27T10:13:12.796304Z',
updated_at: '2023-11-03T09:48:35.748166Z',
last_active: '2023-11-14T07:47:29.732473872Z',
banned: false,
online: false,
name: 'Jack',
email: '',
},
created_at: '2023-08-18T09:06:45.431978Z',
updated_at: '2023-08-18T09:06:45.431978Z',
banned: false,
shadow_banned: false,
role: 'member',
channel_role: 'channel_member',
notifications_muted: false,
},
{
user_id: 'zitaszuperagetstreamio',
user: {
id: 'zitaszuperagetstreamio',
role: 'admin',
created_at: '2021-09-14T13:07:45.764353Z',
updated_at: '2023-11-02T10:43:16.258959Z',
last_active: '2022-07-15T07:08:03.807199Z',
banned: false,
online: false,
first_name: 'Zita',
staff_user: true,
dashboard_user: true,
name: 'Zita Szupera',
email: 'zita.szupera@getstream.io',
image: 'https://getstream.io/random_png/?id=little-wood-9\u0026name=little-wood-9',
last_name: 'Szupera',
},
created_at: '2023-08-18T09:06:45.431978Z',
updated_at: '2023-08-18T09:06:45.431978Z',
banned: false,
shadow_banned: false,
role: 'owner',
channel_role: 'channel_member',
notifications_muted: false,
},
],
membership: {
user: {
id: 'zitaszuperagetstreamio',
role: 'admin',
created_at: '2021-09-14T13:07:45.764353Z',
updated_at: '2023-11-02T10:43:16.258959Z',
last_active: '2022-07-15T07:08:03.807199Z',
banned: false,
online: false,
last_name: 'Szupera',
first_name: 'Zita',
staff_user: true,
dashboard_user: true,
name: 'Zita Szupera',
email: 'zita.szupera@getstream.io',
image: 'https://getstream.io/random_png/?id=little-wood-9\u0026name=little-wood-9',
},
created_at: '2023-08-18T09:06:45.431978Z',
updated_at: '2023-08-18T09:06:45.431978Z',
banned: false,
shadow_banned: false,
role: 'owner',
channel_role: 'channel_member',
notifications_muted: false,
},
duration: '3.22ms',
};
Loading