add unit test; refactor function; not remove on logout but on server remove

This commit is contained in:
Pablo Andrés Vélez Vidal 2023-08-21 17:31:16 +02:00
parent 7d3115c920
commit 7244f95fea
6 changed files with 51 additions and 41 deletions

View file

@ -21,7 +21,7 @@ import {getCurrentUser} from '@queries/servers/user';
import {getThemeFromState} from '@screens/navigation';
import EphemeralStore from '@store/ephemeral_store';
import {deleteFileCache, deleteFileCacheByDir} from '@utils/file';
import {extractCleanDomain, isMainActivity} from '@utils/helpers';
import {createKeyFromServerUrl, isMainActivity} from '@utils/helpers';
import {addNewServer} from '@utils/server';
import type {LaunchType} from '@typings/launch';
@ -121,6 +121,7 @@ class SessionManager {
WebsocketManager.invalidateClient(serverUrl);
if (removeServer) {
await removePushDisabledInServerAcknowledged(createKeyFromServerUrl(serverUrl));
await DatabaseManager.destroyServerDatabase(serverUrl);
} else {
await DatabaseManager.deleteServerDatabase(serverUrl);
@ -168,8 +169,6 @@ class SessionManager {
await this.terminateSession(serverUrl, removeServer);
await removePushDisabledInServerAcknowledged(extractCleanDomain(serverUrl));
if (activeServerUrl === serverUrl) {
let displayName = '';
let launchType: LaunchType = Launch.AddServer;

View file

@ -3,7 +3,7 @@
import React from 'react';
import {PUSH_PROXY_STATUS_VERIFIED} from '@constants/push_proxy';
import {PUSH_PROXY_RESPONSE_NOT_AVAILABLE, PUSH_PROXY_STATUS_VERIFIED} from '@constants/push_proxy';
import {renderWithIntl} from '@test/intl-test-helper';
import Header from './header';
@ -23,4 +23,34 @@ describe('components/channel_list/header', () => {
expect(toJSON()).toMatchSnapshot();
});
it('Push notifications disabled and not having ackoledge show alert icon', () => {
const wrapper = renderWithIntl(
<Header
pushProxyStatus={PUSH_PROXY_RESPONSE_NOT_AVAILABLE}
canCreateChannels={true}
canJoinChannels={true}
canInvitePeople={true}
displayName={'Test!'}
pushDisabledAck={false}
/>,
);
expect(wrapper.getByTestId('channel_list_header.push_alert')).toBeTruthy();
});
it('Push notifications disabled but after ackoledging do not show alert icon', () => {
const wrapper = renderWithIntl(
<Header
pushProxyStatus={PUSH_PROXY_RESPONSE_NOT_AVAILABLE}
canCreateChannels={true}
canJoinChannels={true}
canInvitePeople={true}
displayName={'Test!'}
pushDisabledAck={true}
/>,
);
expect(wrapper.queryByTestId('channel_list_header.push_alert')).toBeNull();
});
});

View file

@ -7,7 +7,7 @@ import {combineLatest, of as of$} from 'rxjs';
import {distinctUntilChanged, switchMap} from 'rxjs/operators';
import {observePushDisabledInServerAcknowledged} from '@app/queries/app/global';
import {extractCleanDomain} from '@app/utils/helpers';
import {createKeyFromServerUrl} from '@app/utils/helpers';
import {Permissions} from '@constants';
import {withServerUrl} from '@context/server';
import {observePermissionForTeam} from '@queries/servers/role';
@ -64,7 +64,7 @@ const enhanced = withObservables([], ({serverUrl, database}: Props) => {
distinctUntilChanged(),
),
pushProxyStatus: observePushVerificationStatus(database),
pushDisabledAck: observePushDisabledInServerAcknowledged(extractCleanDomain(serverUrl)),
pushDisabledAck: observePushDisabledInServerAcknowledged(createKeyFromServerUrl(serverUrl)),
};
});

View file

@ -4,7 +4,7 @@
import withObservables from '@nozbe/with-observables';
import {of as of$} from 'rxjs';
import {extractCleanDomain} from '@app/utils/helpers';
import {createKeyFromServerUrl} from '@app/utils/helpers';
import {Tutorial} from '@constants';
import {PUSH_PROXY_STATUS_UNKNOWN} from '@constants/push_proxy';
import DatabaseManager from '@database/manager';
@ -27,7 +27,7 @@ const enhance = withObservables(['highlight'], ({highlight, server}: {highlight:
server: server.observe(),
tutorialWatched,
pushProxyStatus: serverDatabase ? observePushVerificationStatus(serverDatabase) : of$(PUSH_PROXY_STATUS_UNKNOWN),
pushDisabledAck: observePushDisabledInServerAcknowledged(extractCleanDomain(server.url)),
pushDisabledAck: observePushDisabledInServerAcknowledged(createKeyFromServerUrl(server.url)),
};
});

View file

@ -164,46 +164,27 @@ export function isMainActivity() {
}
/**
* Extracts the domain name or IP address from a serverUrl.
* Uses the serverUrl to create a key.
*
* @param {string} input - The serverUrl, potentially with a port and protocol.
* @returns {string} The extracted domain name or IP address,
* or empty if no match is found.
*
* // Output:
* // Input: https://example-something.com => Result: example-something
* // Input: http://192.168.1.1 => Result: 192.168.1.1
* // Input: https://127.0.0.1:3000 => Result: 127.0.0.1
* // Input: https://subdomain.example.com/api => subdomain.example.com/api
* // Input: http://localhost:8080/app/v1 => localhost:8080/app/v1
* // Input: https://example-something.com => Result: examplesomething
* // Input: http://192.168.1.1 => Result: 19216811
* // Input: https://127.0.0.1:3000 => Result: 1270013000
* // Input: https://subdomain.example.com/api => subdomainexamplecomapi
* // Input: http://localhost:8080/app/v1 => localhost8080appv1
*/
export function extractDomain(input: string) {
export function createKeyFromServerUrl(input: string) {
const regex = /^(https?:\/\/)?([\w.-]+)(:\d+)?(\/\S*)?/;
const match = input.match(regex);
if (match) {
const modifiedString = match[2] + (match[4] || '');
return modifiedString;
const domainWithPortAndSubpath = match[2] + (match[3] || '') + (match[4] || '');
return domainWithPortAndSubpath.replace(/[^\w]/g, '');
}
return '';
}
/**
* Returns the domain name or IP address from a serverUrl without special characters.
*
* @param {string} domain - The extracted domain from a serverUrl.
* @returns {string} The extracted domain name or IP address, modified to remove special characters,
* or empty .
*
* // Output:
* // Input: example-something => Result: examplesomething
* // Input: 192.168.1.1 => Result: 19216811
* // Input: 127.0.0.1 => Result: 127001
*/
export function extractCleanDomain(domain: string) {
if (!domain) {
return '';
}
return extractDomain(domain).replace(/[\W.]/g, '');
}

View file

@ -8,23 +8,23 @@ import {getPushDisabledInServerAcknowledged} from '@app/queries/app/global';
import {PUSH_PROXY_RESPONSE_NOT_AVAILABLE, PUSH_PROXY_RESPONSE_UNKNOWN, PUSH_PROXY_STATUS_NOT_AVAILABLE, PUSH_PROXY_STATUS_UNKNOWN, PUSH_PROXY_STATUS_VERIFIED} from '@constants/push_proxy';
import EphemeralStore from '@store/ephemeral_store';
import {extractCleanDomain} from './helpers';
import {createKeyFromServerUrl} from './helpers';
import type {IntlShape} from 'react-intl';
export async function pushDisabledInServerAck(serverUrl: string) {
const extractedDomain = extractCleanDomain(serverUrl);
const extractedDomain = createKeyFromServerUrl(serverUrl);
const pushServerDisabledAck = await getPushDisabledInServerAcknowledged(extractedDomain);
return pushServerDisabledAck;
}
export async function canReceiveNotifications(serverUrl: string, verification: string, intl: IntlShape) {
const a = await pushDisabledInServerAck(serverUrl);
const hasAckNotification = await pushDisabledInServerAck(serverUrl);
switch (verification) {
case PUSH_PROXY_RESPONSE_NOT_AVAILABLE:
EphemeralStore.setPushProxyVerificationState(serverUrl, PUSH_PROXY_STATUS_NOT_AVAILABLE);
if (!a) {
if (!hasAckNotification) {
alertPushProxyError(intl, serverUrl);
}
break;
@ -40,7 +40,7 @@ export async function canReceiveNotifications(serverUrl: string, verification: s
const handleAlertResponse = async (buttonIndex: number, serverUrl: string) => {
if (buttonIndex === 0) {
// User clicked "Okay" acknowledging that the push notifications are disabled on that server
await storePushDisabledInServerAcknowledged(extractCleanDomain(serverUrl));
await storePushDisabledInServerAcknowledged(createKeyFromServerUrl(serverUrl));
}
};