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

Prevent /mine endpoint call when a user is anonymous #423

Merged
merged 2 commits into from
Mar 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 13 additions & 1 deletion src/platform-apps/channels/container.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ describe('ChannelsContainer', () => {
user: {
data: null,
},
context: {
isAuthenticated: true,
},
...props,
};

Expand All @@ -55,7 +58,7 @@ describe('ChannelsContainer', () => {
expect(fetchChannels).toHaveBeenCalledWith(domainId);
});

it('set receiveUnreadCount channels on mount', () => {
it('set receiveUnreadCount channels on mount when authenticated', () => {
const domainId = '0x000000000000000000000000000000000000000A';
const fetchChannels = jest.fn();
const receiveUnreadCount = jest.fn();
Expand All @@ -65,6 +68,15 @@ describe('ChannelsContainer', () => {
expect(receiveUnreadCount).toHaveBeenCalledWith(domainId);
});

it('do not set receiveUnreadCount channels on mount when anonymous', () => {
const fetchChannels = jest.fn();
const receiveUnreadCount = jest.fn();

subject({ fetchChannels, receiveUnreadCount, context: { isAuthenticated: false } });

expect(receiveUnreadCount).not.toHaveBeenCalled();
});

it('wraps ChannelList in AppContextPanel', () => {
const wrapper = subject();

Expand Down
32 changes: 26 additions & 6 deletions src/platform-apps/channels/container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { AppLayout, AppContextPanel, AppContent } from '@zer0-os/zos-component-l
import './styles.scss';
import { AuthenticationState } from '../../store/authentication/types';
import { ChatViewContainer } from '../../components/chat-view-container/chat-view-container';
import { withContext as withAuthenticationContext } from '../../components/authentication/context';

interface PublicProperties {
store: Store<RootState>;
Expand All @@ -33,6 +34,9 @@ export interface Properties extends PublicProperties {
receiveUnreadCount: (domainId: string) => void;
stopSyncChannels: () => void;
user: AuthenticationState['user'];
context: {
isAuthenticated: boolean;
};
}

export class Container extends React.Component<Properties> {
Expand All @@ -59,14 +63,30 @@ export class Container extends React.Component<Properties> {
}

componentDidMount() {
this.props.fetchChannels(this.props.domainId);
this.props.receiveUnreadCount(this.props.domainId);
const {
context: { isAuthenticated },
domainId,
} = this.props;

this.props.fetchChannels(domainId);

if (isAuthenticated) {
this.props.receiveUnreadCount(domainId);
}
}

componentDidUpdate(prevProps: Properties) {
if (prevProps.user.data !== this.props.user.data) {
this.props.fetchChannels(this.props.domainId);
this.props.receiveUnreadCount(this.props.domainId);
const {
context: { isAuthenticated },
user,
domainId,
} = this.props;

if (isAuthenticated) {
if (prevProps.user.data !== user.data) {
this.props.fetchChannels(domainId);
this.props.receiveUnreadCount(domainId);
}
}
}

Expand Down Expand Up @@ -104,4 +124,4 @@ export class Container extends React.Component<Properties> {
}
}

export const ChannelsContainer = connectContainer<PublicProperties>(Container);
export const ChannelsContainer = withAuthenticationContext<PublicProperties>(connectContainer<{}>(Container));