serverSideConfig.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { get, set } from 'lodash'
  2. const defaultApi = ({ rootState, commit }, { path, value }) => {
  3. const params = {}
  4. set(params, path, value)
  5. return rootState
  6. .api
  7. .backendInteractor
  8. .updateProfile({ params })
  9. .then(result => {
  10. commit('addNewUsers', [result])
  11. commit('setCurrentUser', result)
  12. })
  13. }
  14. const notificationsApi = ({ rootState, commit }, { path, value, oldValue }) => {
  15. const settings = {}
  16. set(settings, path, value)
  17. return rootState
  18. .api
  19. .backendInteractor
  20. .updateNotificationSettings({ settings })
  21. .then(result => {
  22. if (result.status === 'success') {
  23. commit('confirmServerSideOption', { name, value })
  24. } else {
  25. commit('confirmServerSideOption', { name, value: oldValue })
  26. }
  27. })
  28. }
  29. /**
  30. * Map that stores relation between path for reading (from user profile),
  31. * for writing (into API) an what API to use.
  32. *
  33. * Shorthand - instead of { get, set, api? } object it's possible to use string
  34. * in case default api is used and get = set
  35. *
  36. * If no api is specified, defaultApi is used (see above)
  37. */
  38. export const settingsMap = {
  39. 'defaultScope': 'source.privacy',
  40. 'defaultNSFW': 'source.sensitive', // BROKEN: pleroma/pleroma#2837
  41. 'stripRichContent': {
  42. get: 'source.pleroma.no_rich_text',
  43. set: 'no_rich_text'
  44. },
  45. // Privacy
  46. 'locked': 'locked',
  47. 'acceptChatMessages': {
  48. get: 'pleroma.accepts_chat_messages',
  49. set: 'accepts_chat_messages'
  50. },
  51. 'allowFollowingMove': {
  52. get: 'pleroma.allow_following_move',
  53. set: 'allow_following_move'
  54. },
  55. 'discoverable': 'source.discoverable',
  56. 'hideFavorites': {
  57. get: 'pleroma.hide_favorites',
  58. set: 'hide_favorites'
  59. },
  60. 'hideFollowers': {
  61. get: 'pleroma.hide_followers',
  62. set: 'hide_followers'
  63. },
  64. 'hideFollows': {
  65. get: 'pleroma.hide_follows',
  66. set: 'hide_follows'
  67. },
  68. 'hideFollowersCount': {
  69. get: 'pleroma.hide_followers_count',
  70. set: 'hide_followers_count'
  71. },
  72. 'hideFollowsCount': {
  73. get: 'pleroma.hide_follows_count',
  74. set: 'hide_follows_count'
  75. },
  76. // NotificationSettingsAPIs
  77. 'webPushHideContents': {
  78. get: 'pleroma.notification_settings.hide_notification_contents',
  79. set: 'hide_notification_contents',
  80. api: notificationsApi
  81. },
  82. 'blockNotificationsFromStrangers': {
  83. get: 'pleroma.notification_settings.block_from_strangers',
  84. set: 'block_from_strangers',
  85. api: notificationsApi
  86. }
  87. }
  88. export const defaultState = Object.fromEntries(Object.keys(settingsMap).map(key => [key, null]))
  89. const serverSideConfig = {
  90. state: { ...defaultState },
  91. mutations: {
  92. confirmServerSideOption (state, { name, value }) {
  93. set(state, name, value)
  94. },
  95. wipeServerSideOption (state, { name }) {
  96. set(state, name, null)
  97. },
  98. wipeAllServerSideOptions (state) {
  99. Object.keys(settingsMap).forEach(key => {
  100. set(state, key, null)
  101. })
  102. },
  103. // Set the settings based on their path location
  104. setCurrentUser (state, user) {
  105. Object.entries(settingsMap).forEach((map) => {
  106. const [name, value] = map
  107. const { get: path = value } = value
  108. set(state, name, get(user._original, path))
  109. })
  110. }
  111. },
  112. actions: {
  113. setServerSideOption ({ rootState, state, commit, dispatch }, { name, value }) {
  114. const oldValue = get(state, name)
  115. const map = settingsMap[name]
  116. if (!map) throw new Error('Invalid server-side setting')
  117. const { set: path = map, api = defaultApi } = map
  118. commit('wipeServerSideOption', { name })
  119. api({ rootState, commit }, { path, value, oldValue })
  120. .catch((e) => {
  121. console.warn('Error setting server-side option:', e)
  122. commit('confirmServerSideOption', { name, value: oldValue })
  123. })
  124. },
  125. logout ({ commit }) {
  126. commit('wipeAllServerSideOptions')
  127. }
  128. }
  129. }
  130. export default serverSideConfig