diff --git a/app/managers/session_manager.ts b/app/managers/session_manager.ts index c79a69cff..09b0c1301 100644 --- a/app/managers/session_manager.ts +++ b/app/managers/session_manager.ts @@ -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; diff --git a/app/screens/home/channel_list/categories_list/header/header.test.tsx b/app/screens/home/channel_list/categories_list/header/header.test.tsx index af40f111a..5959534ca 100644 --- a/app/screens/home/channel_list/categories_list/header/header.test.tsx +++ b/app/screens/home/channel_list/categories_list/header/header.test.tsx @@ -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( +
, + ); + + expect(wrapper.getByTestId('channel_list_header.push_alert')).toBeTruthy(); + }); + + it('Push notifications disabled but after ackoledging do not show alert icon', () => { + const wrapper = renderWithIntl( +
, + ); + + expect(wrapper.queryByTestId('channel_list_header.push_alert')).toBeNull(); + }); }); diff --git a/app/screens/home/channel_list/categories_list/header/index.ts b/app/screens/home/channel_list/categories_list/header/index.ts index 305883204..8d72dda1a 100644 --- a/app/screens/home/channel_list/categories_list/header/index.ts +++ b/app/screens/home/channel_list/categories_list/header/index.ts @@ -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)), }; }); diff --git a/app/screens/home/channel_list/servers/servers_list/server_item/index.ts b/app/screens/home/channel_list/servers/servers_list/server_item/index.ts index 626f5121d..dd0d83ff0 100644 --- a/app/screens/home/channel_list/servers/servers_list/server_item/index.ts +++ b/app/screens/home/channel_list/servers/servers_list/server_item/index.ts @@ -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)), }; }); diff --git a/app/utils/helpers.ts b/app/utils/helpers.ts index 715951d38..dabfafd30 100644 --- a/app/utils/helpers.ts +++ b/app/utils/helpers.ts @@ -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, ''); -} diff --git a/app/utils/push_proxy.ts b/app/utils/push_proxy.ts index 7bfba76c2..e44a35882 100644 --- a/app/utils/push_proxy.ts +++ b/app/utils/push_proxy.ts @@ -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)); } };