utils.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { _converse } from '@converse/headless/core.js';
  2. import assignIn from 'lodash-es/assignIn';
  3. import isEqual from "lodash-es/isEqual.js";
  4. import isObject from 'lodash-es/isObject';
  5. import log from '@converse/headless/log';
  6. import pick from 'lodash-es/pick';
  7. import u from '@converse/headless/utils/core';
  8. import { DEFAULT_SETTINGS } from './constants.js';
  9. import { Events } from '@converse/skeletor/src/events.js';
  10. import { Model } from '@converse/skeletor/src/model.js';
  11. import { initStorage } from '@converse/headless/utils/storage.js';
  12. let app_settings;
  13. let init_settings = {}; // Container for settings passed in via converse.initialize
  14. let user_settings; // User settings, populated via api.users.settings
  15. export function getAppSettings () {
  16. return app_settings;
  17. }
  18. export function initAppSettings (settings) {
  19. init_settings = settings;
  20. app_settings = {};
  21. Object.assign(app_settings, Events);
  22. // Allow only whitelisted settings to be overwritten via converse.initialize
  23. const allowed_settings = pick(settings, Object.keys(DEFAULT_SETTINGS));
  24. assignIn(app_settings, DEFAULT_SETTINGS, allowed_settings);
  25. }
  26. export function getInitSettings () {
  27. return init_settings;
  28. }
  29. export function getAppSetting (key) {
  30. if (Object.keys(DEFAULT_SETTINGS).includes(key)) {
  31. return app_settings[key];
  32. }
  33. }
  34. export function extendAppSettings (settings) {
  35. u.merge(DEFAULT_SETTINGS, settings);
  36. // When updating the settings, we need to avoid overwriting the
  37. // initialization_settings (i.e. the settings passed in via converse.initialize).
  38. const allowed_keys = Object.keys(pick(settings,Object.keys(DEFAULT_SETTINGS)));
  39. const allowed_site_settings = pick(init_settings, allowed_keys);
  40. const updated_settings = assignIn(pick(settings, allowed_keys), allowed_site_settings);
  41. u.merge(app_settings, updated_settings);
  42. }
  43. export function registerListener (name, func, context) {
  44. app_settings.on(name, func, context)
  45. }
  46. export function unregisterListener (name, func) {
  47. app_settings.off(name, func);
  48. }
  49. export function updateAppSettings (key, val) {
  50. if (key == null) return this; // eslint-disable-line no-eq-null
  51. let attrs;
  52. if (isObject(key)) {
  53. attrs = key;
  54. } else if (typeof key === 'string') {
  55. attrs = {};
  56. attrs[key] = val;
  57. }
  58. const allowed_keys = Object.keys(pick(attrs, Object.keys(DEFAULT_SETTINGS)));
  59. const changed = {};
  60. allowed_keys.forEach(k => {
  61. const val = attrs[k];
  62. if (!isEqual(app_settings[k], val)) {
  63. changed[k] = val;
  64. app_settings[k] = val;
  65. }
  66. });
  67. Object.keys(changed).forEach(k => app_settings.trigger('change:' + k, changed[k]));
  68. app_settings.trigger('change', changed);
  69. }
  70. /**
  71. * @async
  72. */
  73. function initUserSettings () {
  74. if (!_converse.bare_jid) {
  75. const msg = "No JID to fetch user settings for";
  76. log.error(msg);
  77. throw Error(msg);
  78. }
  79. if (!user_settings?.fetched) {
  80. const id = `converse.user-settings.${_converse.bare_jid}`;
  81. user_settings = new Model({id});
  82. initStorage(user_settings, id);
  83. user_settings.fetched = user_settings.fetch({'promise': true});
  84. }
  85. return user_settings.fetched;
  86. }
  87. export async function getUserSettings () {
  88. await initUserSettings();
  89. return user_settings;
  90. }
  91. export async function updateUserSettings (data, options) {
  92. await initUserSettings();
  93. return user_settings.save(data, options);
  94. }
  95. export async function clearUserSettings () {
  96. await initUserSettings();
  97. return user_settings.clear();
  98. }