Prettier for everything else left

This commit is contained in:
Scott Idem
2026-03-24 12:28:28 -04:00
parent 12a9472064
commit 23d25bf65a
4 changed files with 552 additions and 389 deletions

View File

@@ -17,7 +17,7 @@ import {
PUBLIC_AE_API_PATH,
PUBLIC_AE_API_SECRET_KEY,
PUBLIC_AE_API_CRUD_SUPER_KEY,
PUBLIC_AE_BOOTSTRAP_KEY,
PUBLIC_AE_BOOTSTRAP_KEY
// PUBLIC_AE_NO_ACCOUNT_ID,
// PUBLIC_AE_NO_ACCOUNT_ID_TOKEN
} from '$env/static/public';
@@ -130,32 +130,54 @@ export async function load({ fetch, params, parent, route, url }) {
try {
// Use the db_core instance directly for a quick lookup
const { db_core } = await import('$lib/ae_core/db_core');
const cached = await db_core.site_domain.where('fqdn').equals(fqdn).first();
const cached = await db_core.site_domain
.where('fqdn')
.equals(fqdn)
.first();
if (cached) {
if (log_lvl) console.log('ROOT LOAD: Found cached site domain. Unblocking layout.');
if (log_lvl)
console.log(
'ROOT LOAD: Found cached site domain. Unblocking layout.'
);
result = cached;
}
} catch (e) {
if (log_lvl) console.warn('ROOT LOAD: Failed to read from Dexie cache.', e);
if (log_lvl)
console.warn('ROOT LOAD: Failed to read from Dexie cache.', e);
}
}
// Detect Aether Native Bridge (Electron)
if (typeof window !== 'undefined' && (window as any).aetherNative) {
is_native = true;
if (log_lvl) console.log('ROOT LOAD: Detected Aether Native Bridge. Requesting device config...');
if (log_lvl)
console.log(
'ROOT LOAD: Detected Aether Native Bridge. Requesting device config...'
);
try {
const native_device_config = await (window as any).aetherNative.get_device_config();
const native_device_config = await (
window as any
).aetherNative.get_device_config();
if (native_device_config) {
if (log_lvl) console.log('ROOT LOAD: Native device config received:', native_device_config);
if (log_lvl)
console.log(
'ROOT LOAD: Native device config received:',
native_device_config
);
// Map native device config to the expected result structure
// Use native as source of truth if available
result = {
...native_device_config,
// Ensure naming consistency
account_id: native_device_config.account_id || native_device_config.account_id_random,
site_id: native_device_config.site_id || native_device_config.site_id_random,
site_domain_id: native_device_config.site_domain_id || native_device_config.site_domain_id_random,
account_id:
native_device_config.account_id ||
native_device_config.account_id_random,
site_id:
native_device_config.site_id ||
native_device_config.site_id_random,
site_domain_id:
native_device_config.site_domain_id ||
native_device_config.site_domain_id_random
};
// Inject native device metadata into the location state with SAFE MERGE
@@ -163,50 +185,76 @@ export async function load({ fetch, params, parent, route, url }) {
const incoming_dev = native_device_config.native_device;
// String-Only ID Vision: Ensure semantic fields use the random string ID
if (incoming_dev.event_device_id_random) incoming_dev.event_device_id = incoming_dev.event_device_id_random;
if (incoming_dev.event_id_random) incoming_dev.event_id = incoming_dev.event_id_random;
if (incoming_dev.id_random) incoming_dev.id = incoming_dev.id_random;
if (incoming_dev.event_device_id_random)
incoming_dev.event_device_id =
incoming_dev.event_device_id_random;
if (incoming_dev.event_id_random)
incoming_dev.event_id = incoming_dev.event_id_random;
if (incoming_dev.id_random)
incoming_dev.id = incoming_dev.id_random;
// 1. Recover existing user overrides from localStorage
let existing_dev = {};
try {
const raw = localStorage.getItem('ae_loc');
if (raw) existing_dev = JSON.parse(raw).native_device || {};
if (raw)
existing_dev = JSON.parse(raw).native_device || {};
} catch (e) {}
// 2. Merge: Priority to EXISTING overrides for specific timers
ae_loc_init['native_device'] = {
...incoming_dev,
// Persist these specific user-controlled fields
check_event_loop_period: (existing_dev as any).check_event_loop_period || incoming_dev.check_event_loop_period,
check_event_device_loop_period: (existing_dev as any).check_event_device_loop_period || incoming_dev.check_event_device_loop_period,
check_event_location_loop_period: (existing_dev as any).check_event_location_loop_period || incoming_dev.check_event_location_loop_period,
check_event_session_loop_period: (existing_dev as any).check_event_session_loop_period || incoming_dev.check_event_session_loop_period,
check_event_loop_period:
(existing_dev as any).check_event_loop_period ||
incoming_dev.check_event_loop_period,
check_event_device_loop_period:
(existing_dev as any)
.check_event_device_loop_period ||
incoming_dev.check_event_device_loop_period,
check_event_location_loop_period:
(existing_dev as any)
.check_event_location_loop_period ||
incoming_dev.check_event_location_loop_period,
check_event_session_loop_period:
(existing_dev as any)
.check_event_session_loop_period ||
incoming_dev.check_event_session_loop_period,
// Use API value if present; default to 2 (never preserve from localStorage — stale values cause orphaned cache dirs)
hash_prefix_length: incoming_dev.hash_prefix_length || 2
};
// Map specific operational paths
ae_loc_init['local_file_cache_path'] = incoming_dev.local_file_cache_path;
ae_loc_init['host_file_temp_path'] = incoming_dev.host_file_temp_path;
ae_loc_init['local_file_cache_path'] =
incoming_dev.local_file_cache_path;
ae_loc_init['host_file_temp_path'] =
incoming_dev.host_file_temp_path;
ae_loc_init['recording_path'] = incoming_dev.recording_path;
}
// IMPORTANT: Update API settings with the native-authorized key if present
if (native_device_config.aether_api_key) {
ae_api_init['api_secret_key'] = native_device_config.aether_api_key;
ae_api_headers['x-aether-api-key'] = native_device_config.aether_api_key;
ae_api_init['api_secret_key'] =
native_device_config.aether_api_key;
ae_api_headers['x-aether-api-key'] =
native_device_config.aether_api_key;
}
}
} catch (err) {
console.error('ROOT LOAD: Failed to fetch native device config.', err);
console.error(
'ROOT LOAD: Failed to fetch native device config.',
err
);
}
}
// 2. SLOW PATH: Wait for API site lookup only if we have no result yet
if (!result) {
try {
if (log_lvl) console.log(`ROOT LOAD: No cache. Starting site lookup V3 for ${fqdn}...`);
if (log_lvl)
console.log(
`ROOT LOAD: No cache. Starting site lookup V3 for ${fqdn}...`
);
// Use dedicated Bootstrap key — limited permissions, no account_id required.
// Key is injected at build time from PUBLIC_AE_BOOTSTRAP_KEY in .env.
@@ -226,14 +274,24 @@ export async function load({ fetch, params, parent, route, url }) {
view: 'base',
log_lvl
});
if (log_lvl) console.log(`ROOT LOAD: Site lookup result for ${fqdn}:`, result);
if (log_lvl)
console.log(
`ROOT LOAD: Site lookup result for ${fqdn}:`,
result
);
} catch (err) {
console.error(`ROOT LOAD: Site lookup critical failure for ${fqdn}.`, err);
console.error(
`ROOT LOAD: Site lookup critical failure for ${fqdn}.`,
err
);
api_error = true;
}
} else {
// We have a result (cache or native), fire off the refresh in the background to update Dexie
if (log_lvl) console.log('ROOT LOAD: Result already obtained. Background refresh triggered.');
if (log_lvl)
console.log(
'ROOT LOAD: Result already obtained. Background refresh triggered.'
);
lookup_site_domain({
api_cfg: ae_api_init,
fqdn,
@@ -243,14 +301,22 @@ export async function load({ fetch, params, parent, route, url }) {
}
// Defensive check: if result is false (common from API helper) or null, use emergency ghost
if (!result || typeof result !== 'object' || result.account_id === 'ghost') {
console.warn(`ROOT LOAD: Falsy or Ghost result for ${fqdn}. Forcing fallback message.`);
if (
!result ||
typeof result !== 'object' ||
result.account_id === 'ghost'
) {
console.warn(
`ROOT LOAD: Falsy or Ghost result for ${fqdn}. Forcing fallback message.`
);
result = {
id: 'ghost',
id_random: 'ghost',
account_id_random: 'ghost',
account_code: 'ghost',
account_name: api_error ? 'API Connection Failed' : 'Domain Not Registered',
account_name: api_error
? 'API Connection Failed'
: 'Domain Not Registered',
site_id_random: 'ghost',
site_domain_id_random: 'ghost',
enable: '1',
@@ -278,7 +344,8 @@ export async function load({ fetch, params, parent, route, url }) {
ae_loc_init['account_name'] = json_data.account_name || 'Ghost Account';
ae_loc_init['site_id'] = json_data.site_id || 'ghost';
ae_loc_init['site_domain_id'] = json_data.site_domain_id || json_data.site_domain_id || 'ghost';
ae_loc_init['site_domain_id'] =
json_data.site_domain_id || json_data.site_domain_id || 'ghost';
ae_loc_init['site_enable'] = json_data.enable || '1';
ae_loc_init['site_header_image_path'] = json_data.header_image_path || '';
ae_loc_init['site_style_href'] = json_data.style_href || '';
@@ -286,12 +353,16 @@ export async function load({ fetch, params, parent, route, url }) {
ae_loc_init['site_access_code_kv'] = json_data.access_code_kv_json || {};
ae_loc_init['site_cfg_json'] = json_data.cfg_json || {};
ae_loc_init['site_access_key'] = json_data.access_key || '';
ae_loc_init['site_domain_access_key'] = json_data.site_domain_access_key || '';
ae_loc_init['site_domain_access_key'] =
json_data.site_domain_access_key || '';
ae_loc_init['base_url'] = url.origin;
ae_loc_init['hostname'] = url.hostname;
if (!ae_loc_init['site_access_key'] && !ae_loc_init['site_domain_access_key']) {
if (
!ae_loc_init['site_access_key'] &&
!ae_loc_init['site_domain_access_key']
) {
ae_loc_init['key_checked'] = true;
ae_loc_init['allow_access'] = true;
} else {
@@ -304,10 +375,11 @@ export async function load({ fetch, params, parent, route, url }) {
if (access_key == ae_loc_init['site_access_key']) {
ae_loc_init['key_checked'] = ae_loc_init['site_access_key'];
ae_loc_init['allow_access'] = ae_loc_init['site_access_key'];
}
else if (access_key == ae_loc_init['site_domain_access_key']) {
ae_loc_init['key_checked'] = ae_loc_init['site_domain_access_key'];
ae_loc_init['allow_access'] = ae_loc_init['site_domain_access_key'];
} else if (access_key == ae_loc_init['site_domain_access_key']) {
ae_loc_init['key_checked'] =
ae_loc_init['site_domain_access_key'];
ae_loc_init['allow_access'] =
ae_loc_init['site_domain_access_key'];
} else {
ae_loc_init['key_checked'] = true;
ae_loc_init['allow_access'] = false;
@@ -323,7 +395,8 @@ export async function load({ fetch, params, parent, route, url }) {
// });
// }
ae_loc_init['account_name'] = json_data.account_name || 'Account Name Not Set';
ae_loc_init['account_name'] =
json_data.account_name || 'Account Name Not Set';
// ae_acct['api'] = ae_api_init; // DO NOT USE: This overwrites our isolated clone from line 65
ae_acct['loc'] = ae_loc_init;
@@ -331,7 +404,7 @@ export async function load({ fetch, params, parent, route, url }) {
ae_acct['slct'] = {
account_id: account_id,
site_domain_id: ae_loc_init.site_domain_id,
site_id: ae_loc_init.site_id,
site_id: ae_loc_init.site_id
// event_id: ae_loc_init.site_cfg_json?.slct__event_id,
// event_badge_template_id: ae_loc_init.site_cfg_json?.slct__event_badge_template_id,
// sponsorship_cfg_id: ae_loc_init.site_cfg_json?.slct__sponsorship_cfg_id
@@ -339,7 +412,11 @@ export async function load({ fetch, params, parent, route, url }) {
data_struct[account_id] = ae_acct;
if (log_lvl) console.log('ROOT LOAD: Final data_struct structure ready.', Object.keys(data_struct));
if (log_lvl)
console.log(
'ROOT LOAD: Final data_struct structure ready.',
Object.keys(data_struct)
);
return data_struct;
}
}