mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
feat(native): the app needs TREK 5.1.3 on the server and checks a remembered one once after the update
The fixes that keep the app's sign-in in the app live in the server's UI, so an updated app on a 5.1.2 server still ended in the browser. The server now reports nativeApi 2 and the app refuses anything below it, naming 5.1.3. A server the app remembers from before is not loaded unchecked: the first launch after the update shows the address screen filled in with it, says why and checks it right away. A server that is new enough loads again with its session; an older one gets the update hint.
This commit is contained in:
@@ -26,6 +26,7 @@ public class MainActivity extends BridgeActivity {
|
||||
// runs and on its own origin. BridgeActivity reads `config` while it
|
||||
// builds the bridge inside super.onCreate().
|
||||
boolean restarting = TrekServerStore.consumeRestart(this);
|
||||
TrekServerStore.recheckAfterUpdate(this);
|
||||
String server = TrekServerStore.getUrl(this);
|
||||
boolean onServer = server != null && !TrekServerStore.consumeShellOnce(this);
|
||||
config = TrekServerStore.configFor(this, onServer ? server : null, restarting);
|
||||
|
||||
@@ -22,6 +22,16 @@ final class TrekServerStore {
|
||||
|
||||
private static final String PREFS = "trek_shell";
|
||||
private static final String KEY_URL = "server_url";
|
||||
private static final String KEY_PREVIOUS_URL = "previous_server_url";
|
||||
private static final String KEY_SERVER_CHECK = "server_check";
|
||||
|
||||
/**
|
||||
* The server API this build needs (MIN_NATIVE_API in serverAddress.ts). A
|
||||
* server remembered under a lower value is checked once more: the first
|
||||
* launch after an update that raised it puts the address screen back up,
|
||||
* filled in with that server (getPreviousUrl). Raise both together.
|
||||
*/
|
||||
private static final int SERVER_CHECK = 2;
|
||||
private static final String KEY_SIGN_IN_HOSTS = "sign_in_hosts";
|
||||
private static final String KEY_SHELL_ONCE = "shell_once";
|
||||
private static final String KEY_RESTART = "restart";
|
||||
@@ -56,6 +66,21 @@ final class TrekServerStore {
|
||||
return prefs(context).getString(KEY_URL, null);
|
||||
}
|
||||
|
||||
/** Runs before the bridge is built, so a server picked under an older minimum is not loaded unchecked. */
|
||||
static void recheckAfterUpdate(Context context) {
|
||||
SharedPreferences prefs = prefs(context);
|
||||
if (prefs.getInt(KEY_SERVER_CHECK, 0) >= SERVER_CHECK) return;
|
||||
String server = getUrl(context);
|
||||
SharedPreferences.Editor editor = prefs.edit().putInt(KEY_SERVER_CHECK, SERVER_CHECK);
|
||||
if (server != null) editor.remove(KEY_URL).remove(KEY_SIGN_IN_HOSTS).putString(KEY_PREVIOUS_URL, server);
|
||||
editor.commit();
|
||||
}
|
||||
|
||||
/** The server an update asked to check again, until a server is set. */
|
||||
static String getPreviousUrl(Context context) {
|
||||
return prefs(context).getString(KEY_PREVIOUS_URL, null);
|
||||
}
|
||||
|
||||
/** Remembers the server; its sign-in hosts go with it and are replaced or dropped too. */
|
||||
static void setUrl(Context context, String url) {
|
||||
setServer(context, url, null);
|
||||
@@ -66,7 +91,7 @@ final class TrekServerStore {
|
||||
if (url == null) {
|
||||
editor.remove(KEY_URL);
|
||||
} else {
|
||||
editor.putString(KEY_URL, url);
|
||||
editor.putString(KEY_URL, url).remove(KEY_PREVIOUS_URL);
|
||||
}
|
||||
if (signInHosts == null || signInHosts.isEmpty()) {
|
||||
editor.remove(KEY_SIGN_IN_HOSTS);
|
||||
|
||||
@@ -310,8 +310,10 @@ public class TrekShellPlugin extends Plugin {
|
||||
@PluginMethod
|
||||
public void getServer(PluginCall call) {
|
||||
String url = TrekServerStore.getUrl(getContext());
|
||||
String previous = TrekServerStore.getPreviousUrl(getContext());
|
||||
JSObject result = new JSObject();
|
||||
result.put("url", url == null ? JSONObject.NULL : url);
|
||||
result.put("previousUrl", previous == null ? JSONObject.NULL : previous);
|
||||
call.resolve(result);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ import WebKit
|
||||
class TrekBridgeViewController: CAPBridgeViewController {
|
||||
override func instanceDescriptor() -> InstanceDescriptor {
|
||||
let descriptor = super.instanceDescriptor()
|
||||
TrekServerStore.recheckAfterUpdate()
|
||||
if let server = TrekServerStore.url {
|
||||
// The trailing slash matters: Capacitor treats every URL that starts
|
||||
// with this string as the app's own, and without it that would take
|
||||
@@ -157,13 +158,44 @@ enum TrekKeyCommands {
|
||||
enum TrekServerStore {
|
||||
private static let key = "TrekServerURL"
|
||||
|
||||
private static let previousKey = "TrekPreviousServerURL"
|
||||
|
||||
private static let checkedKey = "TrekServerCheck"
|
||||
|
||||
/// The server API this build needs (MIN_NATIVE_API in serverAddress.ts).
|
||||
/// A server remembered under a lower value is checked once more: the first
|
||||
/// launch after an update that raised it puts the address screen back up,
|
||||
/// filled in with that server (previousURL). Raise both together.
|
||||
private static let serverCheck = 2
|
||||
|
||||
/// Runs before the first bridge is built, so a server picked under an older
|
||||
/// minimum is not loaded unchecked.
|
||||
static func recheckAfterUpdate() {
|
||||
let defaults = UserDefaults.standard
|
||||
guard defaults.integer(forKey: checkedKey) < serverCheck else { return }
|
||||
defaults.set(serverCheck, forKey: checkedKey)
|
||||
guard let server = url else { return }
|
||||
url = nil
|
||||
defaults.set(server, forKey: previousKey)
|
||||
}
|
||||
|
||||
/// The server an update asked to check again, until a server is set.
|
||||
static var previousURL: String? {
|
||||
UserDefaults.standard.string(forKey: previousKey)
|
||||
}
|
||||
|
||||
private static let signInHostsKey = "TrekServerSignInHosts"
|
||||
|
||||
/// Setting the server drops the sign-in hosts of the one before it.
|
||||
static var url: String? {
|
||||
get { UserDefaults.standard.string(forKey: key) }
|
||||
set {
|
||||
if let newValue { UserDefaults.standard.set(newValue, forKey: key) } else { UserDefaults.standard.removeObject(forKey: key) }
|
||||
if let newValue {
|
||||
UserDefaults.standard.set(newValue, forKey: key)
|
||||
UserDefaults.standard.removeObject(forKey: previousKey)
|
||||
} else {
|
||||
UserDefaults.standard.removeObject(forKey: key)
|
||||
}
|
||||
UserDefaults.standard.removeObject(forKey: signInHostsKey)
|
||||
try? FileManager.default.removeItem(at: offlineCopyURL)
|
||||
}
|
||||
|
||||
@@ -348,7 +348,10 @@ public class TrekShellPlugin: CAPPlugin, CAPBridgedPlugin, ASWebAuthenticationPr
|
||||
private var authSession: ASWebAuthenticationSession?
|
||||
|
||||
@objc func getServer(_ call: CAPPluginCall) {
|
||||
call.resolve(["url": TrekServerStore.url ?? NSNull()])
|
||||
call.resolve([
|
||||
"url": TrekServerStore.url ?? NSNull(),
|
||||
"previousUrl": TrekServerStore.previousURL ?? NSNull(),
|
||||
])
|
||||
}
|
||||
|
||||
@objc func setServer(_ call: CAPPluginCall) {
|
||||
|
||||
@@ -275,7 +275,12 @@ function showWelcome(opts: { back?: boolean } = {}): void {
|
||||
}
|
||||
|
||||
// ── Connect (address) ───────────────────────────────────────────────────────
|
||||
function showConnect(prefill = '', opts: { back?: boolean } = {}): void {
|
||||
/**
|
||||
* `recheck`: an app update needs a newer server than the one it remembered, so
|
||||
* the address screen comes back once with it filled in, says why and checks it
|
||||
* right away. A server that is new enough loads again without another tap.
|
||||
*/
|
||||
function showConnect(prefill = '', opts: { back?: boolean; recheck?: boolean } = {}): void {
|
||||
const input = el('input', {
|
||||
type: 'url',
|
||||
placeholder: t('native.connect.placeholder'),
|
||||
@@ -344,14 +349,15 @@ function showConnect(prefill = '', opts: { back?: boolean } = {}): void {
|
||||
[
|
||||
topbar(() => showWelcome({ back: true })),
|
||||
el('h1', { className: 'title-sm' }, t('native.connect.title')),
|
||||
el('p', { className: 'sub' }, t('native.connect.hint')),
|
||||
el('p', { className: 'sub' }, t(opts.recheck ? 'native.connect.recheckHint' : 'native.connect.hint')),
|
||||
],
|
||||
[form, footLinks(externalLink(t('native.connect.whatIsServer'), SERVER_DOCS_URL))]
|
||||
)
|
||||
),
|
||||
opts
|
||||
);
|
||||
requestAnimationFrame(() => input.focus());
|
||||
if (opts.recheck && prefill) form.requestSubmit();
|
||||
else requestAnimationFrame(() => input.focus());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1322,8 +1328,9 @@ async function start(): Promise<void> {
|
||||
followBackGesture();
|
||||
letKeyboardClose();
|
||||
let url: string | null;
|
||||
let previousUrl: string | null | undefined;
|
||||
try {
|
||||
({ url } = await TrekShell.getServer());
|
||||
({ url, previousUrl } = await TrekShell.getServer());
|
||||
} catch {
|
||||
// No native shell under us (a web preview, or a simulator without the
|
||||
// plugin): there is no remembered server, so start at the welcome screen
|
||||
@@ -1332,7 +1339,9 @@ async function start(): Promise<void> {
|
||||
await SplashScreen.hide().catch(() => {});
|
||||
return;
|
||||
}
|
||||
if (!url) {
|
||||
if (!url && previousUrl) {
|
||||
showConnect(previousUrl, { recheck: true });
|
||||
} else if (!url) {
|
||||
showWelcome();
|
||||
} else {
|
||||
// Only reached when loading the server failed. A proxy that wanted a client
|
||||
|
||||
@@ -3,7 +3,7 @@ import { candidateUrls, probeDemo, probeServer, type ProbeResponse } from './ser
|
||||
|
||||
const features = (
|
||||
url: string,
|
||||
data: unknown = { bookingImport: false, aiParsing: false, nativeApp: true }
|
||||
data: unknown = { bookingImport: false, aiParsing: false, nativeApp: true, nativeApi: 2 }
|
||||
): ProbeResponse => ({ status: 200, data, url });
|
||||
|
||||
describe('candidateUrls', () => {
|
||||
@@ -26,13 +26,21 @@ describe('candidateUrls', () => {
|
||||
|
||||
describe('probeServer', () => {
|
||||
it('accepts a server at the minimum native API version or above', async () => {
|
||||
const data = { bookingImport: false, nativeApp: true, nativeApi: 1 };
|
||||
const get = vi.fn().mockResolvedValue(features('https://trek.example.com/api/health/features', data));
|
||||
expect(await probeServer('https://trek.example.com', get)).toEqual({ ok: true, url: 'https://trek.example.com' });
|
||||
for (const nativeApi of [2, 3]) {
|
||||
const data = { bookingImport: false, nativeApp: true, nativeApi };
|
||||
const get = vi.fn().mockResolvedValue(features('https://trek.example.com/api/health/features', data));
|
||||
expect(await probeServer('https://trek.example.com', get)).toEqual({ ok: true, url: 'https://trek.example.com' });
|
||||
}
|
||||
});
|
||||
|
||||
it('calls a server below the minimum native API version too old', async () => {
|
||||
const data = { bookingImport: false, nativeApp: true, nativeApi: 0 };
|
||||
const data = { bookingImport: false, nativeApp: true, nativeApi: 1 };
|
||||
const get = vi.fn().mockResolvedValue(features('https://trek.example.com/api/health/features', data));
|
||||
expect(await probeServer('https://trek.example.com', get)).toEqual({ ok: false, reason: 'tooOld' });
|
||||
});
|
||||
|
||||
it('calls a server that speaks to the app but reports no native API version too old', async () => {
|
||||
const data = { bookingImport: false, nativeApp: true };
|
||||
const get = vi.fn().mockResolvedValue(features('https://trek.example.com/api/health/features', data));
|
||||
expect(await probeServer('https://trek.example.com', get)).toEqual({ ok: false, reason: 'tooOld' });
|
||||
});
|
||||
@@ -173,7 +181,7 @@ describe('probeServer behind mutual TLS', () => {
|
||||
});
|
||||
|
||||
it('connects where the proxy asked and TREK answered, with or without a certificate', async () => {
|
||||
const trek = { bookingImport: false, nativeApp: true, nativeApi: 1 };
|
||||
const trek = { bookingImport: false, nativeApp: true, nativeApi: 2 };
|
||||
for (const sent of [true, false]) {
|
||||
const get = vi.fn().mockResolvedValue(asked(200, sent, trek));
|
||||
expect(await probeServer('https://trek.example.com', get)).toEqual({ ok: true, url: 'https://trek.example.com' });
|
||||
|
||||
@@ -43,10 +43,12 @@ const FEATURES_PATH = '/api/health/features';
|
||||
|
||||
/**
|
||||
* The oldest server API this app can work with (`nativeApi` in the features
|
||||
* response). A server from before the field existed answers without it and is
|
||||
* accepted while the minimum is 1.
|
||||
* response). 2 is TREK 5.1.3, the first server whose sign-in from the app does
|
||||
* not end in the browser. A server without the field is older still. Raising it
|
||||
* goes with SERVER_CHECK in TrekServerStore (iOS and Android), so a server the
|
||||
* app remembers from before is checked against the new minimum once.
|
||||
*/
|
||||
const MIN_NATIVE_API = 1;
|
||||
export const MIN_NATIVE_API = 2;
|
||||
|
||||
function normalize(raw: string): string | null {
|
||||
let parsed: URL;
|
||||
@@ -80,7 +82,7 @@ function judge(response: ProbeResponse): ProbeResult {
|
||||
if (response.status !== 200 || !isRecord(response.data)) return { ok: false, reason: 'notTrek' };
|
||||
if (response.data.nativeApp === true) {
|
||||
const api = response.data.nativeApi;
|
||||
if (typeof api === 'number' && api < MIN_NATIVE_API) return { ok: false, reason: 'tooOld' };
|
||||
if (typeof api !== 'number' || api < MIN_NATIVE_API) return { ok: false, reason: 'tooOld' };
|
||||
// Follow a redirect (http to https, a canonical host) so the app loads the
|
||||
// address the server actually answers on.
|
||||
const base = response.url.endsWith(FEATURES_PATH) ? response.url.slice(0, -FEATURES_PATH.length) : null;
|
||||
|
||||
@@ -13,7 +13,12 @@ import type { NativeCapabilities } from './capabilities';
|
||||
* worker.
|
||||
*/
|
||||
export interface TrekShellPlugin {
|
||||
getServer(): Promise<{ url: string | null }>;
|
||||
/**
|
||||
* `previousUrl`: the server this app remembered before an update asked for a
|
||||
* newer one (SERVER_CHECK in TrekServerStore). Set only while no server is,
|
||||
* so the address screen can check it again, pre-filled.
|
||||
*/
|
||||
getServer(): Promise<{ url: string | null; previousUrl?: string | null }>;
|
||||
/**
|
||||
* Remembers the server and loads it. The current page is gone afterwards.
|
||||
* `signInHosts` are the hosts of a proxy's sign-in page in front of it
|
||||
|
||||
Reference in New Issue
Block a user