Merge pull request #2317 from creesch/syncDefault

Enable sync on empty localstorage, force sync, sync on both load and reconnect.
This commit is contained in:
Jérémie Astori 2018-04-11 01:30:03 -04:00 committed by GitHub
commit 6f3c88663a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 10 deletions

View File

@ -18,6 +18,7 @@ const noCSSparamReg = /[?&]nocss/;
// Will be assigned when `initialize` is called. // Will be assigned when `initialize` is called.
let $syncWarningOverride; let $syncWarningOverride;
let $syncWarningBase; let $syncWarningBase;
let $forceSyncButton;
let $warningUnsupported; let $warningUnsupported;
let $warningBlocked; let $warningBlocked;
@ -48,12 +49,17 @@ const noSync = ["syncSettings"];
const alwaysSync = []; const alwaysSync = [];
// Process usersettings from localstorage. // Process usersettings from localstorage.
let userSettings = JSON.parse(storage.get("settings")) || {}; let userSettings = JSON.parse(storage.get("settings")) || false;
for (const key in settings) { if (!userSettings) {
// Enable sync by default if there are no user defined settings.
settings.syncSettings = true;
} else {
for (const key in settings) {
if (userSettings[key] !== undefined) { if (userSettings[key] !== undefined) {
settings[key] = userSettings[key]; settings[key] = userSettings[key];
} }
}
} }
// Apply custom CSS and themes on page load // Apply custom CSS and themes on page load
@ -76,7 +82,7 @@ module.exports = {
highlightsRE: null, highlightsRE: null,
settings, settings,
shouldOpenMessagePreview, shouldOpenMessagePreview,
noServerSettings, syncAllSettings,
processSetting, processSetting,
initialize, initialize,
}; };
@ -102,6 +108,7 @@ function updateDesktopNotificationStatus() {
function applySetting(name, value) { function applySetting(name, value) {
if (name === "syncSettings" && value) { if (name === "syncSettings" && value) {
$syncWarningOverride.hide(); $syncWarningOverride.hide();
$forceSyncButton.hide();
} else if (name === "motd") { } else if (name === "motd") {
$chat.toggleClass("hide-" + name, !value); $chat.toggleClass("hide-" + name, !value);
} else if (name === "statusMessages") { } else if (name === "statusMessages") {
@ -198,8 +205,10 @@ function updateSetting(name, value, sync) {
socket.emit("setting:get"); socket.emit("setting:get");
$syncWarningOverride.hide(); $syncWarningOverride.hide();
$syncWarningBase.hide(); $syncWarningBase.hide();
$forceSyncButton.hide();
} else if (name === "syncSettings") { } else if (name === "syncSettings") {
$syncWarningOverride.show(); $syncWarningOverride.show();
$forceSyncButton.show();
} }
if (settings.syncSettings && !noSync.includes(name) && sync) { if (settings.syncSettings && !noSync.includes(name) && sync) {
@ -210,9 +219,9 @@ function updateSetting(name, value, sync) {
} }
} }
function noServerSettings() { function syncAllSettings(force = false) {
// Sync is enabled but the server has no settings so we sync all settings from this client. // Sync all settings if sync is enabled or force is true.
if (settings.syncSettings) { if (settings.syncSettings || force) {
for (const name in settings) { for (const name in settings) {
if (!noSync.includes(name)) { if (!noSync.includes(name)) {
settingSetEmit(name, settings[name]); settingSetEmit(name, settings[name]);
@ -223,8 +232,10 @@ function noServerSettings() {
$syncWarningOverride.hide(); $syncWarningOverride.hide();
$syncWarningBase.hide(); $syncWarningBase.hide();
$forceSyncButton.hide();
} else { } else {
$syncWarningOverride.hide(); $syncWarningOverride.hide();
$forceSyncButton.hide();
$syncWarningBase.show(); $syncWarningBase.show();
} }
} }
@ -262,6 +273,7 @@ function initialize() {
$syncWarningOverride = $settings.find(".sync-warning-override"); $syncWarningOverride = $settings.find(".sync-warning-override");
$syncWarningBase = $settings.find(".sync-warning-base"); $syncWarningBase = $settings.find(".sync-warning-base");
$forceSyncButton = $settings.find(".force-sync-button");
$warningBlocked.hide(); $warningBlocked.hide();
module.exports.initialized = true; module.exports.initialized = true;
@ -300,8 +312,12 @@ function initialize() {
} }
}); });
$settings.find("#forceSync").on("click", () => {
syncAllSettings(true);
});
// Local init is done, let's sync // Local init is done, let's sync
// We always ask for synced settings even if it is disabled. // We always ask for synced settings even if it is disabled.
// Settings can be mandatory to sync and it is used to determine sync base state. // Settings can be mandatory to sync and it is used to determine sync base state.
socket.emit("settings:get"); socket.emit("setting:get");
} }

View File

@ -8,6 +8,8 @@ const webpush = require("../webpush");
socket.on("configuration", function(data) { socket.on("configuration", function(data) {
if (options.initialized) { if (options.initialized) {
// Likely a reconnect, request sync for possibly missed settings.
socket.emit("setting:get");
return; return;
} }

View File

@ -19,7 +19,7 @@ socket.on("setting:new", function(data) {
socket.on("setting:all", function(settings) { socket.on("setting:all", function(settings) {
if (Object.keys(settings).length === 0) { if (Object.keys(settings).length === 0) {
options.noServerSettings(); options.syncAllSettings();
} else { } else {
for (const name in settings) { for (const name in settings) {
evaluateSetting(name, settings[name]); evaluateSetting(name, settings[name]);

View File

@ -28,6 +28,10 @@
</label> </label>
<p class="sync-warning-override"><strong>Warning</strong> Checking this box will override the settings of this client with those stored on the server.</p> <p class="sync-warning-override"><strong>Warning</strong> Checking this box will override the settings of this client with those stored on the server.</p>
<p class="sync-warning-base"><strong>Warning</strong> No settings have been synced before. Enabling this will sync all settings of this client as the base for other clients.</p> <p class="sync-warning-base"><strong>Warning</strong> No settings have been synced before. Enabling this will sync all settings of this client as the base for other clients.</p>
<label class="opt force-sync-button">
<button type="button" class="btn" id="forceSync">Force sync settings</button><br>
This will override any settings already synced to the server.
</label>
</div> </div>
{{/unless}} {{/unless}}
<div class="col-sm-12"> <div class="col-sm-12">