Commit Graph

20 Commits

Author SHA1 Message Date
Michael Barrett
46897bdb3a
Ensure hidden settings return falsy when used with the @custom helper (#17920)
no refs

When a theme setting is hidden, it should return a falsy value when used
2023-09-13 08:38:31 +01:00
Fabien "egg" O'Carroll
104f84f252 Added eslint rule for file naming convention
As discussed with the product team we want to enforce kebab-case file names for
all files, with the exception of files which export a single class, in which
case they should be PascalCase and reflect the class which they export.

This will help find classes faster, and should push better naming for them too.

Some files and packages have been excluded from this linting, specifically when
a library or framework depends on the naming of a file for the functionality
e.g. Ember, knex-migrator, adapter-manager
2023-05-09 12:34:34 -04:00
Daniel Lockyer
8859d6298c
Added guard for activateTheme being called in parallel causing sync bugs (#15053)
refs https://github.com/TryGhost/Team/issues/1666

- it seems like we may have a situation where `.activateTheme()` can be called simultaneously resulting in unexpected behaviour in the sync such as duplicate theme setting records
- adjusted behaviour to keep track of the currently running activation within the service and if `.activateTheme()` is called again whilst it's in progress it will wait for completion of the first sync before exiting early or continuing with a new activation

**Note:** There is a known edge-case if there are _more_ than 2 parallel `.activateTheme()` calls. We don't believe that will be an issue but calling it out in case we do still see duplicated custom setting records being created.

Co-authored-by: Kevin Ansfield <kevin@lookingsideways.co.uk>
2022-08-04 11:29:58 +01:00
Kevin Ansfield
5c2f25d016 Fixed "Error parsing filter" when a theme name contains . chars
no issue

- we were passing the theme name directly in the model query filter string without any surrounding `'` chars meaning we were generating invalid filters
2021-10-26 10:14:39 +01:00
Kevin Ansfield
2951da002c Added name argument to .activateTheme()
no issue

Ghost allows multiple themes with the same `name` value in `package.json` so it assigns custom names based on the file name. When syncing we were only looking at the package.json `name` value so there could be a syncing clash when swapping between active themes.

- added `name` as the first argument to `.activateTheme(name, theme)` so that the service's consumer is in control of settings sync applying to it's known theme name
2021-10-20 11:04:44 +01:00
Thibaut Patel
e5e67fe6f0 Added validation for boolean and color
refs https://github.com/TryGhost/Team/issues/1107
refs https://github.com/TryGhost/Team/issues/1106
2021-10-20 11:23:14 +02:00
Kevin Ansfield
b708c7ea67 Changed "NotFoundError" and "BadRequestError" to "ValidationError"
refs https://github.com/TryGhost/Team/issues/1104

- better matches the context of the request as we're validating known keys and valid options from multiple settings
  - "404 Not Found" didn't make sense because `/custom_theme_settings/` _does_ exist, it's just that one setting key wasn't known
  - "400 Bad Request" didn't make sense because the request was understood, it just failed validation of the setting values
2021-10-08 15:40:44 +01:00
Kevin Ansfield
88fc9c7964 Added tests for Service class, fixed error when throwing invalid option error
refs https://github.com/TryGhost/Team/issues/1104

- there was a mismatch of template options when throwing error on invalid select value meaning we were erroring but with the wrong error type and message
- added missing `lodash` dependency in package.json
2021-10-07 16:57:52 +01:00
Kevin Ansfield
2583403b91 Renamed service internals with _ prefix to better delineate public API 2021-10-07 15:28:02 +01:00
Kevin Ansfield
dbf7d33c88 Added tests for Cache class, fixed singular .get('key') behaviour
refs https://github.com/TryGhost/Team/issues/1104

- `.get('key')` would error because it was incorrectly expecting the internal content object to contain `{key, value}` objects
- changed `.getAll()` to return a shallow copy of the internal content object so we don't leak references that can result in unexpected changes to the cache content
- changed the internal content object naming to `_content` to highlight that it's an internal/private implementation detail rather than a public API
2021-10-06 13:12:26 +01:00
Kevin Ansfield
1a9087cfa8 Fixed theme settings for non-active theme being destroyed/updated when syncing
no issue

- the `.browse()` call to fetch themes was not structured to filter out themes correctly so it was fetching all theme settings rather than only settings related to the current theme
2021-10-04 12:14:08 +01:00
Kevin Ansfield
ef136a2029 Fixed unexpected early-exit of settings sync
no issue

- `for .. of` loop contained a `return` rather than a `continue` so the whole function was being exited with an unexpected `undefined` return value after the first loop where an unknown setting is removed
2021-10-04 11:50:53 +01:00
Kevin Ansfield
306bae39c6 Fixed error when removing existing settings that no longer exist
no issue

- debug statement was using wrong setting variable, `themeSetting` won't exist when it's been removed
2021-10-04 11:50:53 +01:00
Kevin Ansfield
bc31453dde Added .updateSettings() to custom theme settings service
refs https://github.com/TryGhost/Team/issues/1070

- receives settings array as provided by through Ghost's API
- errors if a provided setting key does not already exist in the database (settings are only created when syncing with a theme)
- errors if a provided select setting value does not match the theme-provided options
- providing there were no errors, updates each value in the database if it's changed along with the internal cache of full setting objects and the public key/value cache
- returns the same list of settings as `.listSettings()` for API consistency
2021-09-28 15:50:46 +01:00
Kevin Ansfield
2bb5a9cb6e Fixed cache.populate() leaving behind old settings
refs https://github.com/TryGhost/Team/issues/1070

- if the cache is repopulated, keys that already existed in the cache but that don't exist in the new group of settings were left in the cache
- added a `.clear()` method that removes all items from the cache and call it when populating so the cache only contains what it was last populated with
  - deletes properties on the internal content object so that references aren't lost
2021-09-28 15:50:46 +01:00
Kevin Ansfield
6c526bfee2 Fixed missing await in async method
refs https://github.com/TryGhost/Team/issues/1070

- missing await was resulting in setting caches not being populated
2021-09-23 23:41:44 +01:00
Kevin Ansfield
e28415abf4 Added CustomThemeSettingsService.listSettings()
refs https://github.com/TryGhost/Team/issues/1070

- return re-fetched settings after sync so they can be used in later activation steps
- added internal `activeThemeSettings` cache that stores full setting data along with saved values for use in API output
- added `listSettings()` that converts internal cache object into a settings array that matches API format
2021-09-23 20:34:35 +01:00
Kevin Ansfield
c4f364996b Removed model dependency in custom theme settings cache
refs https://github.com/TryGhost/Team/issues/1070

- having a dependency on a model in the cache service meant that Ghost had to know about that and pre-initialize the cache during boot, even though that didn't actually do anything except create a cache instance
- by making the cache a simple key/value store able to be populated from the cache settings service when a theme is activated it means that Ghost doesn't need to perform any extra initialization when the cache is initialized via `require`
2021-09-23 09:16:59 +01:00
Kevin Ansfield
cf9cee0208 First pass at custom-theme-settings-service functionality
refs https://github.com/TryGhost/Team/issues/1070

- added `bread` util that acts as a wrapper for the provided model, if we have any business functionality needed when settings are added/removed then it will go here
- added primary "server" service that handles syncing of custom theme data extracted from a theme with the settings that are in the database and exported as "Service". Syncing rules on theme activation:
    - if a new setting is seen, create it with the default value
    - if a setting has it's type changed, remove it and create a new setting with the default value
    - if a select setting's value is not a valid option, reset it to the default value
- added shared "frontend/server" service that exposes an in-memory cache of key/value pairs for the currently active theme
2021-09-22 21:56:45 +01:00
Kevin Ansfield
56012f5f21 slimer new custom-theme-settings-service 2021-09-21 17:21:52 +01:00