From e74747c913ee43d2bf363a35da1fe274a15566c9 Mon Sep 17 00:00:00 2001 From: mathiiiiiis Date: Sun, 1 Mar 2026 05:53:08 +0100 Subject: [PATCH] feat(webview): implement useLatestURL cookie watcher and startup routing --- .env | 3 ++ .github/workflows/build-apk.yml | 3 ++ App.tsx | 51 ++++++++++++++++++++++---------- src/components/CustomWebView.tsx | 20 ++++++++++++- src/env.ts | 8 ++++- 5 files changed, 68 insertions(+), 17 deletions(-) diff --git a/.env b/.env index e066840..3032f25 100644 --- a/.env +++ b/.env @@ -1,4 +1,7 @@ SERVER_URL="https://nerimity.com" +DEFAULT_URL="https://nerimity.com" +LATEST_URL="https://latest.nerimity.com" +DEV_URL="http://local.nerimity.com:3000" DEV_MODE="true" TURNSTILE_SITEKEY="0x4AAAAAAABO1ilip_YaVHJk" EMOJI_URL="https://nerimity.com/twemojis/" diff --git a/.github/workflows/build-apk.yml b/.github/workflows/build-apk.yml index 6ecc23b..5c99e7a 100644 --- a/.github/workflows/build-apk.yml +++ b/.github/workflows/build-apk.yml @@ -31,6 +31,9 @@ jobs: uses: SpicyPizza/create-envfile@v1.3 with: envkey_SERVER_URL: "https://nerimity.com" + envkey_DEFAULT_URL: "https://nerimity.com" + envkey_LATEST_URL: "https://latest.nerimity.com" + envkey_DEV_URL: "http://local.nerimity.com:3000" envkey_DEV_MODE: "false" envkey_APP_VERSION: "dev" envkey_TURNSTILE_SITEKEY: "0x4AAAAAAABO1ilip_YaVHJk" diff --git a/App.tsx b/App.tsx index 69bb544..160c83a 100644 --- a/App.tsx +++ b/App.tsx @@ -1,5 +1,6 @@ import React, {useCallback, useEffect, useRef, useState} from 'react'; -import {Alert, AppState, BackHandler, Linking, Platform} from 'react-native'; +import {Alert, AppState, BackHandler, Linking, Platform, TouchableOpacity, Text, StyleSheet} from 'react-native'; +import EncryptedStorage from 'react-native-encrypted-storage'; import Show from './src/components/ui/Show'; import { currentUrl, @@ -9,7 +10,10 @@ import { import {CustomVideo, CustomVideoRef} from './src/components/ui/CustomVideo'; import TrackPlayer from 'react-native-track-player'; -import {handlePushNotification, registerNotificationChannels} from './src/pushNotifications'; +import { + handlePushNotification, + registerNotificationChannels, +} from './src/pushNotifications'; import messaging, { FirebaseMessagingTypes, @@ -40,18 +44,28 @@ notifee.onBackgroundEvent(async ({type, detail}) => { } }); +function getUrl(useLatest: boolean) { + //if (env.DEV_MODE) { return env.DEV_URL + '/login'; } + const base = useLatest ? env.LATEST_URL : env.DEFAULT_URL; + return base + '/login'; +} + function App(): JSX.Element { const videoRef = useRef(null); const [videoUrl, setVideoUrl] = useState(null); const webViewRef = useRef(null); const [authenticated, setAuthenticated] = useState(false); const [url, setUrl] = useState(null); + const [startupUrl, setStartupUrl] = useState(getUrl(false)); const runIfAuthenticated = useWaitFor(authenticated); useUpdateChecker(); useEffect(() => { registerNotificationChannels(); - }, []) + EncryptedStorage.getItem('useLatestURL').then(val => { + setStartupUrl(getUrl(val === 'true')); + }).catch(() => {}); + }, []); const handleNotificationClick = useCallback( async (notification: any) => { @@ -135,14 +149,21 @@ function App(): JSX.Element { setAuthenticated(true)} ref={webViewRef} - url={url || 'https://nerimity.com/login'} + url={url || startupUrl} onVideoClick={setVideoUrl} + onUseLatestURL={useLatest => { + EncryptedStorage.setItem('useLatestURL', String(useLatest)); + setStartupUrl(getUrl(useLatest)); + setUrl(null); + }} /> setVideoUrl(null)} + onVideoEnd={() => { + setVideoUrl(null); + }} /> @@ -162,16 +183,16 @@ function useUpdateChecker() { 'Update Available', `Current: ${env.APP_VERSION}\nLatest: ${release.tag_name}`, [ - {text: 'Later'}, - { - text: 'View Changelog', - onPress: onViewChangelog, - }, - { - isPreferred: true, - text: 'Update Now', - onPress: onUpdateNow, - }, + {text: 'Later'}, + { + text: 'View Changelog', + onPress: onViewChangelog, + }, + { + isPreferred: true, + text: 'Update Now', + onPress: onUpdateNow, + }, ], ); }, []); diff --git a/src/components/CustomWebView.tsx b/src/components/CustomWebView.tsx index 02bf210..a6b65c0 100644 --- a/src/components/CustomWebView.tsx +++ b/src/components/CustomWebView.tsx @@ -24,6 +24,7 @@ export interface CustomWebViewProps { url?: string | null; onVideoClick: (url: string) => void; onAuthenticated: (userId: string) => void; + onUseLatestURL: (useLatest: boolean) => void; } export let currentUrl = ''; @@ -146,8 +147,22 @@ export const CustomWebView = forwardRef( emit, }; + const _cookieDesc = Object.getOwnPropertyDescriptor(Document.prototype, 'cookie') + || Object.getOwnPropertyDescriptor(HTMLDocument.prototype, 'cookie'); + if (_cookieDesc) { + Object.defineProperty(document, 'cookie', { + configurable: true, + set(v) { + _cookieDesc.set.call(this, v); + const m = v.match(/^([^=]+)=([^;]*)/); + if (m && m[1].trim() === 'useLatestURL') { + post('useLatestURL', {value: m[2]}); + } + }, + get() { return _cookieDesc.get.call(this); } + }); + } - })(); true; // note: this is required, or you'll sometimes get silent failures @@ -191,6 +206,9 @@ export const CustomWebView = forwardRef( if (event === 'pauseAudio') { TrackPlayer.pause(); } + if (event === 'useLatestURL') { + props.onUseLatestURL(payload?.value === 'true'); + } if (event === 'logout') { console.log('logged out'); await EncryptedStorage.clear(); diff --git a/src/env.ts b/src/env.ts index 2b69da4..acd7baa 100644 --- a/src/env.ts +++ b/src/env.ts @@ -1,5 +1,8 @@ import { SERVER_URL, + DEFAULT_URL, + LATEST_URL, + DEV_URL, DEV_MODE, TURNSTILE_SITEKEY, EMOJI_URL, @@ -8,7 +11,10 @@ import { } from '@env'; export default { - SERVER_URL: SERVER_URL, + SERVER_URL: SERVER_URL || 'https://nerimity.com', + DEFAULT_URL: DEFAULT_URL || 'https://nerimity.com', + LATEST_URL: LATEST_URL || 'https://latest.nerimity.com', + DEV_URL: DEV_URL || 'http://local.nerimity.com:3000', APP_VERSION: APP_VERSION as string | undefined, DEV_MODE: DEV_MODE === 'true', TURNSTILE_SITEKEY: TURNSTILE_SITEKEY,