From 8b70fee072382bb49c11159069b6f21a740b4751 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Tue, 21 Feb 2023 09:04:47 +0100 Subject: [PATCH] Moved rl.fetch and rl.fetchJSON to boot.js so that AppData can be fetched as JSON --- dev/boot.js | 76 ++++++++++++++++++- dev/bootstrap.js | 74 ------------------ .../app/libraries/RainLoop/ServiceActions.php | 14 ++-- 3 files changed, 80 insertions(+), 84 deletions(-) diff --git a/dev/boot.js b/dev/boot.js index be27098dd..b8a626a2e 100644 --- a/dev/boot.js +++ b/dev/boot.js @@ -82,7 +82,78 @@ window.rl = { rl.app.refresh(); }, - loadScript: loadScript + loadScript: loadScript, + + fetch: (resource, init, postData) => { + init = Object.assign({ + mode: 'same-origin', + cache: 'no-cache', + redirect: 'error', + referrerPolicy: 'no-referrer', + credentials: 'same-origin', + headers: {} + }, init); + let asJSON = 1, + XToken = RL_APP_DATA.System?.token, + object = {}; + if (postData) { + init.method = 'POST'; + if (postData instanceof FormData) { + postData.forEach((value, key) => { + if (value instanceof File) { + asJSON = 0; + } else if (!Reflect.has(object, key)) { + object[key] = value; + } else { + Array.isArray(object[key]) || (object[key] = [object[key]]); + object[key].push(value); + } + }); + if (asJSON) { + postData = object; +// postData.XToken = XToken; + } else { + XToken && postData.set('XToken', XToken); + } + } + if (asJSON) { + init.headers['Content-Type'] = 'application/json'; + postData = JSON.stringify(postData); + } + init.body = postData; + } + XToken && (init.headers['X-SM-Token'] = XToken); +// init.headers = new Headers(init.headers); + return fetch(resource, init); + }, + + fetchJSON: (resource, init, postData) => { + init = Object.assign({ headers: {} }, init); + init.headers.Accept = 'application/json'; + return rl.fetch(resource, init, postData).then(response => { + if (!response.ok) { + return Promise.reject('Network response error: ' + response.status); + } + /* TODO: use this for non-developers? + response.clone() + let data = response.text(); + try { + return JSON.parse(data); + } catch (e) { + console.error(e); +// console.log(data); + return Promise.reject(Notifications.JsonParse); + return { + Result: false, + ErrorCode: 952, // Notifications.JsonParse + ErrorMessage: e.message, + ErrorMessageAdditional: data + } + } + */ + return response.json(); + }); + } }; if (!navigator.cookieEnabled) { @@ -92,7 +163,8 @@ if (!navigator.cookieEnabled) { eId('loading').hidden = true; eId('BadBrowser').hidden = false; } else { - loadScript(qUri(`${admin ? 'Admin' : ''}AppData/0/${Math.random().toString().slice(2)}/`)) + rl.fetchJSON(qUri(`${admin ? 'Admin' : ''}AppData/0/${Math.random().toString().slice(2)}/`)) + .then(data => rl.initData(data)) .catch(e => showError(e)); } diff --git a/dev/bootstrap.js b/dev/bootstrap.js index 21a3497a6..dd2cd0218 100644 --- a/dev/bootstrap.js +++ b/dev/bootstrap.js @@ -1,10 +1,6 @@ -import { Settings } from 'Common/Globals'; import { i18n } from 'Common/Translator'; - import { root } from 'Common/Links'; -import { isArray } from 'Common/Utils'; - export default App => { rl.app = App; @@ -33,74 +29,4 @@ export default App => { on: () => hasher.active = true }; - rl.fetch = (resource, init, postData) => { - init = Object.assign({ - mode: 'same-origin', - cache: 'no-cache', - redirect: 'error', - referrerPolicy: 'no-referrer', - credentials: 'same-origin', - headers: {} - }, init); - if (postData) { - init.method = 'POST'; - let asJSON = 1, - XToken = Settings.app('token'), - object = {}; - if (postData instanceof FormData) { - postData.forEach((value, key) => { - if (value instanceof File) { - asJSON = 0; - } else if (!Reflect.has(object, key)) { - object[key] = value; - } else { - isArray(object[key]) || (object[key] = [object[key]]); - object[key].push(value); - } - }); - if (asJSON) { - postData = object; - } else { - postData.set('XToken', XToken); - } - } - if (asJSON) { - init.headers['Content-Type'] = 'application/json'; - postData = JSON.stringify(postData); - } - init.body = postData; - } - init.headers['X-SM-Token'] = Settings.app('token'); -// init.headers = new Headers(init.headers); - return fetch(resource, init); - }; - - rl.fetchJSON = (resource, init, postData) => { - init = Object.assign({ headers: {} }, init); - init.headers.Accept = 'application/json'; - return rl.fetch(resource, init, postData).then(response => { - if (!response.ok) { - return Promise.reject('Network response error: ' + response.status); - } - /* TODO: use this for non-developers? - response.clone() - let data = response.text(); - try { - return JSON.parse(data); - } catch (e) { - console.error(e); -// console.log(data); - return Promise.reject(Notifications.JsonParse); - return { - Result: false, - ErrorCode: 952, // Notifications.JsonParse - ErrorMessage: e.message, - ErrorMessageAdditional: data - } - } - */ - return response.json(); - }); - }; - }; diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php index 532732d63..7163b1cb7 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ServiceActions.php @@ -631,18 +631,16 @@ class ServiceActions private function localAppData(bool $bAdmin = false) : string { - \header('Content-Type: application/javascript; charset=utf-8'); + \header('Content-Type: application/json; charset=utf-8'); $this->oHttp->ServerNoCache(); try { - $sResult = 'rl.initData(' - . Utils::jsonEncode($this->oActions->AppData($bAdmin)) - . ');'; - + $sResult = Utils::jsonEncode($this->oActions->AppData($bAdmin)); $this->Logger()->Write($sResult, \LOG_INFO, 'APPDATA'); - return $sResult; - } catch (\Throwable $e) { - return 'alert(' . \json_encode('ERROR: ' . $e->getMessage()) . ');'; + } catch (\Throwable $oException) { + $self->Logger()->WriteExceptionShort($oException); + \MailSo\Base\Http::StatusHeader(500); + return $oException->getMessage(); } }