diff --git a/app/managers/session_manager.ts b/app/managers/session_manager.ts index 09b0c1301..f5bbe2562 100644 --- a/app/managers/session_manager.ts +++ b/app/managers/session_manager.ts @@ -7,6 +7,7 @@ import FastImage from 'react-native-fast-image'; import {removePushDisabledInServerAcknowledged, storeOnboardingViewedValue} from '@actions/app/global'; import {cancelSessionNotification, logout, scheduleSessionNotification} from '@actions/remote/session'; +import {urlSafeBase64Encode} from '@app/utils/security'; import {Events, Launch} from '@constants'; import DatabaseManager from '@database/manager'; import {resetMomentLocale} from '@i18n'; @@ -21,7 +22,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 {createKeyFromServerUrl, isMainActivity} from '@utils/helpers'; +import {isMainActivity} from '@utils/helpers'; import {addNewServer} from '@utils/server'; import type {LaunchType} from '@typings/launch'; @@ -121,7 +122,7 @@ class SessionManager { WebsocketManager.invalidateClient(serverUrl); if (removeServer) { - await removePushDisabledInServerAcknowledged(createKeyFromServerUrl(serverUrl)); + await removePushDisabledInServerAcknowledged(urlSafeBase64Encode(serverUrl)); await DatabaseManager.destroyServerDatabase(serverUrl); } else { await DatabaseManager.deleteServerDatabase(serverUrl); 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 5959534ca..8414a7a71 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 @@ -24,7 +24,7 @@ describe('components/channel_list/header', () => { expect(toJSON()).toMatchSnapshot(); }); - it('Push notifications disabled and not having ackoledge show alert icon', () => { + it('Push notifications disabled and not having acknoledged it 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', () => { + it('Push notifications are disabled, but even after acknowledging them, the alert icon does not appear', () => { const wrapper = renderWithIntl(
{ distinctUntilChanged(), ), pushProxyStatus: observePushVerificationStatus(database), - pushDisabledAck: observePushDisabledInServerAcknowledged(createKeyFromServerUrl(serverUrl)), + pushDisabledAck: observePushDisabledInServerAcknowledged(urlSafeBase64Encode(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 dd0d83ff0..be9090cc2 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,12 +4,12 @@ import withObservables from '@nozbe/with-observables'; import {of as of$} from 'rxjs'; -import {createKeyFromServerUrl} from '@app/utils/helpers'; import {Tutorial} from '@constants'; import {PUSH_PROXY_STATUS_UNKNOWN} from '@constants/push_proxy'; import DatabaseManager from '@database/manager'; import {observePushDisabledInServerAcknowledged, observeTutorialWatched} from '@queries/app/global'; import {observePushVerificationStatus} from '@queries/servers/system'; +import {urlSafeBase64Encode} from '@utils/security'; import ServerItem from './server_item'; @@ -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(createKeyFromServerUrl(server.url)), + pushDisabledAck: observePushDisabledInServerAcknowledged(urlSafeBase64Encode(server.url)), }; }); diff --git a/app/utils/helpers.ts b/app/utils/helpers.ts index dabfafd30..cb1915c0c 100644 --- a/app/utils/helpers.ts +++ b/app/utils/helpers.ts @@ -162,29 +162,3 @@ export function isMainActivity() { android: ShareModule?.getCurrentActivityName() === 'MainActivity', }); } - -/** - * 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: 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 createKeyFromServerUrl(input: string) { - const regex = /^(https?:\/\/)?([\w.-]+)(:\d+)?(\/\S*)?/; - const match = input.match(regex); - - if (match) { - const domainWithPortAndSubpath = match[2] + (match[3] || '') + (match[4] || ''); - return domainWithPortAndSubpath.replace(/[^\w]/g, ''); - } - - return ''; -} diff --git a/app/utils/push_proxy.ts b/app/utils/push_proxy.ts index e44a35882..9ce7cb80a 100644 --- a/app/utils/push_proxy.ts +++ b/app/utils/push_proxy.ts @@ -4,18 +4,17 @@ import {Alert} from 'react-native'; import {storePushDisabledInServerAcknowledged} from '@actions/app/global'; -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 {getPushDisabledInServerAcknowledged} from '@queries/app/global'; import EphemeralStore from '@store/ephemeral_store'; -import {createKeyFromServerUrl} from './helpers'; +import {urlSafeBase64Encode} from './security'; import type {IntlShape} from 'react-intl'; -export async function pushDisabledInServerAck(serverUrl: string) { - const extractedDomain = createKeyFromServerUrl(serverUrl); - const pushServerDisabledAck = await getPushDisabledInServerAcknowledged(extractedDomain); - return pushServerDisabledAck; +export function pushDisabledInServerAck(serverUrl: string) { + const extractedDomain = urlSafeBase64Encode(serverUrl); + return getPushDisabledInServerAcknowledged(extractedDomain); } export async function canReceiveNotifications(serverUrl: string, verification: string, intl: IntlShape) { @@ -40,7 +39,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(createKeyFromServerUrl(serverUrl)); + await storePushDisabledInServerAcknowledged(urlSafeBase64Encode(serverUrl)); } };