diff options
Diffstat (limited to 'src/stores/ServicesStore.js')
-rw-r--r-- | src/stores/ServicesStore.js | 144 |
1 files changed, 81 insertions, 63 deletions
diff --git a/src/stores/ServicesStore.js b/src/stores/ServicesStore.js index 4ccb995ae..8598c6234 100644 --- a/src/stores/ServicesStore.js +++ b/src/stores/ServicesStore.js | |||
@@ -2,7 +2,6 @@ import { shell } from 'electron'; | |||
2 | import { action, reaction, computed, observable } from 'mobx'; | 2 | import { action, reaction, computed, observable } from 'mobx'; |
3 | import { debounce, remove } from 'lodash'; | 3 | import { debounce, remove } from 'lodash'; |
4 | import ms from 'ms'; | 4 | import ms from 'ms'; |
5 | import { app } from '@electron/remote'; | ||
6 | import { ensureFileSync, pathExistsSync, writeFileSync } from 'fs-extra'; | 5 | import { ensureFileSync, pathExistsSync, writeFileSync } from 'fs-extra'; |
7 | import { join } from 'path'; | 6 | import { join } from 'path'; |
8 | 7 | ||
@@ -11,13 +10,11 @@ import Request from './lib/Request'; | |||
11 | import CachedRequest from './lib/CachedRequest'; | 10 | import CachedRequest from './lib/CachedRequest'; |
12 | import { matchRoute } from '../helpers/routing-helpers'; | 11 | import { matchRoute } from '../helpers/routing-helpers'; |
13 | import { isInTimeframe } from '../helpers/schedule-helpers'; | 12 | import { isInTimeframe } from '../helpers/schedule-helpers'; |
14 | import { | 13 | import { getRecipeDirectory, getDevRecipeDirectory } from '../helpers/recipe-helpers'; |
15 | getRecipeDirectory, | ||
16 | getDevRecipeDirectory, | ||
17 | } from '../helpers/recipe-helpers'; | ||
18 | import { workspaceStore } from '../features/workspaces'; | 14 | import { workspaceStore } from '../features/workspaces'; |
19 | import { KEEP_WS_LOADED_USID } from '../config'; | 15 | import { KEEP_WS_LOADED_USID } from '../config'; |
20 | import { SPELLCHECKER_LOCALES } from '../i18n/languages'; | 16 | import { SPELLCHECKER_LOCALES } from '../i18n/languages'; |
17 | import { ferdiVersion } from '../environment'; | ||
21 | 18 | ||
22 | const debug = require('debug')('Ferdi:ServiceStore'); | 19 | const debug = require('debug')('Ferdi:ServiceStore'); |
23 | 20 | ||
@@ -128,38 +125,66 @@ export default class ServicesStore extends Store { | |||
128 | setup() { | 125 | setup() { |
129 | // Single key reactions for the sake of your CPU | 126 | // Single key reactions for the sake of your CPU |
130 | reaction( | 127 | reaction( |
131 | () => this.stores.settings.app.enableSpellchecking, | 128 | () => ( |
132 | () => this._shareSettingsWithServiceProcess(), | 129 | this.stores.settings.app.enableSpellchecking |
130 | ), | ||
131 | () => { | ||
132 | this._shareSettingsWithServiceProcess(); | ||
133 | }, | ||
133 | ); | 134 | ); |
134 | 135 | ||
135 | reaction( | 136 | reaction( |
136 | () => this.stores.settings.app.spellcheckerLanguage, | 137 | () => ( |
137 | () => this._shareSettingsWithServiceProcess(), | 138 | this.stores.settings.app.spellcheckerLanguage |
139 | ), | ||
140 | () => { | ||
141 | this._shareSettingsWithServiceProcess(); | ||
142 | }, | ||
138 | ); | 143 | ); |
139 | 144 | ||
140 | reaction( | 145 | reaction( |
141 | () => this.stores.settings.app.darkMode, | 146 | () => ( |
142 | () => this._shareSettingsWithServiceProcess(), | 147 | this.stores.settings.app.darkMode |
148 | ), | ||
149 | () => { | ||
150 | this._shareSettingsWithServiceProcess(); | ||
151 | }, | ||
143 | ); | 152 | ); |
144 | 153 | ||
145 | reaction( | 154 | reaction( |
146 | () => this.stores.settings.app.adaptableDarkMode, | 155 | () => ( |
147 | () => this._shareSettingsWithServiceProcess(), | 156 | this.stores.settings.app.adaptableDarkMode |
157 | ), | ||
158 | () => { | ||
159 | this._shareSettingsWithServiceProcess(); | ||
160 | }, | ||
148 | ); | 161 | ); |
149 | 162 | ||
150 | reaction( | 163 | reaction( |
151 | () => this.stores.settings.app.universalDarkMode, | 164 | () => ( |
152 | () => this._shareSettingsWithServiceProcess(), | 165 | this.stores.settings.app.universalDarkMode |
166 | ), | ||
167 | () => { | ||
168 | this._shareSettingsWithServiceProcess(); | ||
169 | }, | ||
153 | ); | 170 | ); |
154 | 171 | ||
155 | reaction( | 172 | reaction( |
156 | () => this.stores.settings.app.searchEngine, | 173 | () => ( |
157 | () => this._shareSettingsWithServiceProcess(), | 174 | this.stores.settings.app.searchEngine |
175 | ), | ||
176 | () => { | ||
177 | this._shareSettingsWithServiceProcess(); | ||
178 | }, | ||
158 | ); | 179 | ); |
159 | 180 | ||
160 | reaction( | 181 | reaction( |
161 | () => this.stores.settings.app.clipboardNotifications, | 182 | () => ( |
162 | () => this._shareSettingsWithServiceProcess(), | 183 | this.stores.settings.app.clipboardNotifications |
184 | ), | ||
185 | () => { | ||
186 | this._shareSettingsWithServiceProcess(); | ||
187 | }, | ||
163 | ); | 188 | ); |
164 | } | 189 | } |
165 | 190 | ||
@@ -190,12 +215,12 @@ export default class ServicesStore extends Store { | |||
190 | * Run various maintenance tasks on services | 215 | * Run various maintenance tasks on services |
191 | */ | 216 | */ |
192 | _serviceMaintenance() { | 217 | _serviceMaintenance() { |
193 | this.all.forEach(service => { | 218 | this.enabled.forEach(service => { |
194 | // Defines which services should be hibernated or woken up | 219 | // Defines which services should be hibernated or woken up |
195 | if (!service.isActive) { | 220 | if (!service.isActive) { |
196 | if ( | 221 | if ( |
197 | !service.lastHibernated && | 222 | !service.lastHibernated && |
198 | Date.now() - service.lastUsed > | 223 | (Date.now() - service.lastUsed) > |
199 | ms(`${this.stores.settings.all.app.hibernationStrategy}s`) | 224 | ms(`${this.stores.settings.all.app.hibernationStrategy}s`) |
200 | ) { | 225 | ) { |
201 | // If service is stale, hibernate it. | 226 | // If service is stale, hibernate it. |
@@ -204,21 +229,18 @@ export default class ServicesStore extends Store { | |||
204 | 229 | ||
205 | if ( | 230 | if ( |
206 | service.lastHibernated && | 231 | service.lastHibernated && |
207 | Number(this.stores.settings.all.app.wakeUpStrategy) > 0 | 232 | Number(this.stores.settings.all.app.wakeUpStrategy) > 0 && |
233 | (Date.now() - service.lastHibernated) > | ||
234 | ms(`${this.stores.settings.all.app.wakeUpStrategy}s`) | ||
208 | ) { | 235 | ) { |
209 | // If service is in hibernation and the wakeup time has elapsed, wake it. | 236 | // If service is in hibernation and the wakeup time has elapsed, wake it. |
210 | if ( | 237 | this._awake({ serviceId: service.id }); |
211 | Date.now() - service.lastHibernated > | ||
212 | ms(`${this.stores.settings.all.app.wakeUpStrategy}s`) | ||
213 | ) { | ||
214 | this._awake({ serviceId: service.id }); | ||
215 | } | ||
216 | } | 238 | } |
217 | } | 239 | } |
218 | 240 | ||
219 | if ( | 241 | if ( |
220 | service.lastPoll && | 242 | service.lastPoll && |
221 | service.lastPoll - service.lastPollAnswer > ms('1m') | 243 | (service.lastPoll - service.lastPollAnswer) > ms('1m') |
222 | ) { | 244 | ) { |
223 | // If service did not reply for more than 1m try to reload. | 245 | // If service did not reply for more than 1m try to reload. |
224 | if (!service.isActive) { | 246 | if (!service.isActive) { |
@@ -378,27 +400,22 @@ export default class ServicesStore extends Store { | |||
378 | 400 | ||
379 | // set default values for serviceData | 401 | // set default values for serviceData |
380 | // eslint-disable-next-line prefer-object-spread | 402 | // eslint-disable-next-line prefer-object-spread |
381 | Object.assign( | 403 | // TODO: How is this different from the defaults of the recipe in 'src/models/Recipe' file? |
382 | { | 404 | serviceData = { |
383 | isEnabled: true, | 405 | isEnabled: true, |
384 | isHibernationEnabled: false, | 406 | isHibernationEnabled: false, |
385 | isNotificationEnabled: true, | 407 | isNotificationEnabled: true, |
386 | isBadgeEnabled: true, | 408 | isBadgeEnabled: true, |
387 | isMuted: false, | 409 | isMuted: false, |
388 | customIcon: false, | 410 | customIcon: false, |
389 | isDarkModeEnabled: false, | 411 | isDarkModeEnabled: false, |
390 | spellcheckerLanguage: | 412 | spellcheckerLanguage: |
391 | SPELLCHECKER_LOCALES[this.stores.settings.app.spellcheckerLanguage], | 413 | SPELLCHECKER_LOCALES[this.stores.settings.app.spellcheckerLanguage], |
392 | userAgentPref: '', | 414 | userAgentPref: '', |
393 | }, | 415 | ...serviceData, |
394 | serviceData, | 416 | }; |
395 | ); | ||
396 | |||
397 | let data = serviceData; | ||
398 | 417 | ||
399 | if (!skipCleanup) { | 418 | const data = skipCleanup ? serviceData : this._cleanUpTeamIdAndCustomUrl(recipeId, serviceData); |
400 | data = this._cleanUpTeamIdAndCustomUrl(recipeId, serviceData); | ||
401 | } | ||
402 | 419 | ||
403 | const response = await this.createServiceRequest.execute(recipeId, data) | 420 | const response = await this.createServiceRequest.execute(recipeId, data) |
404 | ._promise; | 421 | ._promise; |
@@ -430,12 +447,13 @@ export default class ServicesStore extends Store { | |||
430 | serviceData.name = data.name; | 447 | serviceData.name = data.name; |
431 | } | 448 | } |
432 | 449 | ||
433 | if (data.team && !data.customURL) { | 450 | if (data.team) { |
434 | serviceData.team = data.team; | 451 | if (!data.customURL) { |
435 | } | 452 | serviceData.team = data.team; |
436 | 453 | } else { | |
437 | if (data.team && data.customURL) { | 454 | // TODO: Is this correct? |
438 | serviceData.customUrl = data.team; | 455 | serviceData.customUrl = data.team; |
456 | } | ||
439 | } | 457 | } |
440 | 458 | ||
441 | this.actions.service.createService({ | 459 | this.actions.service.createService({ |
@@ -596,9 +614,8 @@ export default class ServicesStore extends Store { | |||
596 | this.allDisplayed.length, | 614 | this.allDisplayed.length, |
597 | ); | 615 | ); |
598 | 616 | ||
599 | // TODO: simplify this; | 617 | this.all.forEach(s => { |
600 | this.all.forEach((s, index) => { | 618 | s.isActive = false; |
601 | this.all[index].isActive = false; | ||
602 | }); | 619 | }); |
603 | this.allDisplayed[nextIndex].isActive = true; | 620 | this.allDisplayed[nextIndex].isActive = true; |
604 | } | 621 | } |
@@ -610,9 +627,8 @@ export default class ServicesStore extends Store { | |||
610 | this.allDisplayed.length, | 627 | this.allDisplayed.length, |
611 | ); | 628 | ); |
612 | 629 | ||
613 | // TODO: simplify this; | 630 | this.all.forEach(s => { |
614 | this.all.forEach((s, index) => { | 631 | s.isActive = false; |
615 | this.all[index].isActive = false; | ||
616 | }); | 632 | }); |
617 | this.allDisplayed[prevIndex].isActive = true; | 633 | this.allDisplayed[prevIndex].isActive = true; |
618 | } | 634 | } |
@@ -877,6 +893,7 @@ export default class ServicesStore extends Store { | |||
877 | ); | 893 | ); |
878 | 894 | ||
879 | const services = {}; | 895 | const services = {}; |
896 | // TODO: simplify this | ||
880 | this.all.forEach((s, index) => { | 897 | this.all.forEach((s, index) => { |
881 | services[this.all[index].id] = index; | 898 | services[this.all[index].id] = index; |
882 | }); | 899 | }); |
@@ -893,6 +910,7 @@ export default class ServicesStore extends Store { | |||
893 | 910 | ||
894 | @action _toggleNotifications({ serviceId }) { | 911 | @action _toggleNotifications({ serviceId }) { |
895 | const service = this.one(serviceId); | 912 | const service = this.one(serviceId); |
913 | service.isNotificationEnabled = !service.isNotificationEnabled; | ||
896 | 914 | ||
897 | this.actions.service.updateService({ | 915 | this.actions.service.updateService({ |
898 | serviceId, | 916 | serviceId, |
@@ -905,8 +923,7 @@ export default class ServicesStore extends Store { | |||
905 | 923 | ||
906 | @action _toggleAudio({ serviceId }) { | 924 | @action _toggleAudio({ serviceId }) { |
907 | const service = this.one(serviceId); | 925 | const service = this.one(serviceId); |
908 | 926 | service.isMuted = !service.isMuted; | |
909 | service.isNotificationEnabled = !service.isNotificationEnabled; | ||
910 | 927 | ||
911 | this.actions.service.updateService({ | 928 | this.actions.service.updateService({ |
912 | serviceId, | 929 | serviceId, |
@@ -919,6 +936,7 @@ export default class ServicesStore extends Store { | |||
919 | 936 | ||
920 | @action _toggleDarkMode({ serviceId }) { | 937 | @action _toggleDarkMode({ serviceId }) { |
921 | const service = this.one(serviceId); | 938 | const service = this.one(serviceId); |
939 | service.isDarkModeEnabled = !service.isDarkModeEnabled; | ||
922 | 940 | ||
923 | this.actions.service.updateService({ | 941 | this.actions.service.updateService({ |
924 | serviceId, | 942 | serviceId, |
@@ -1149,7 +1167,7 @@ export default class ServicesStore extends Store { | |||
1149 | 'initialize-recipe', | 1167 | 'initialize-recipe', |
1150 | { | 1168 | { |
1151 | ...shareWithWebview, | 1169 | ...shareWithWebview, |
1152 | franzVersion: app.getVersion(), | 1170 | franzVersion: ferdiVersion, |
1153 | }, | 1171 | }, |
1154 | service.recipe, | 1172 | service.recipe, |
1155 | ); | 1173 | ); |