logo

pleroma-fe

My custom branche(s) on git.pleroma.social/pleroma/pleroma-fe git clone https://anongit.hacktivis.me/git/pleroma-fe.git/

profileConfig.js (3884B)


  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('confirmProfileOption', { name, value })
  24. } else {
  25. commit('confirmProfileOption', { 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: {
  56. get: 'source.pleroma.discoverable',
  57. set: 'discoverable'
  58. },
  59. hideFavorites: {
  60. get: 'pleroma.hide_favorites',
  61. set: 'hide_favorites'
  62. },
  63. hideFollowers: {
  64. get: 'pleroma.hide_followers',
  65. set: 'hide_followers'
  66. },
  67. hideFollows: {
  68. get: 'pleroma.hide_follows',
  69. set: 'hide_follows'
  70. },
  71. hideFollowersCount: {
  72. get: 'pleroma.hide_followers_count',
  73. set: 'hide_followers_count'
  74. },
  75. hideFollowsCount: {
  76. get: 'pleroma.hide_follows_count',
  77. set: 'hide_follows_count'
  78. },
  79. // NotificationSettingsAPIs
  80. webPushHideContents: {
  81. get: 'pleroma.notification_settings.hide_notification_contents',
  82. set: 'hide_notification_contents',
  83. api: notificationsApi
  84. },
  85. blockNotificationsFromStrangers: {
  86. get: 'pleroma.notification_settings.block_from_strangers',
  87. set: 'block_from_strangers',
  88. api: notificationsApi
  89. }
  90. }
  91. export const defaultState = Object.fromEntries(Object.keys(settingsMap).map(key => [key, null]))
  92. const profileConfig = {
  93. state: { ...defaultState },
  94. mutations: {
  95. confirmProfileOption (state, { name, value }) {
  96. set(state, name, value)
  97. },
  98. wipeProfileOption (state, { name }) {
  99. set(state, name, null)
  100. },
  101. wipeAllProfileOptions (state) {
  102. Object.keys(settingsMap).forEach(key => {
  103. set(state, key, null)
  104. })
  105. },
  106. // Set the settings based on their path location
  107. setCurrentUser (state, user) {
  108. Object.entries(settingsMap).forEach((map) => {
  109. const [name, value] = map
  110. const { get: path = value } = value
  111. set(state, name, get(user._original, path))
  112. })
  113. }
  114. },
  115. actions: {
  116. setProfileOption ({ rootState, state, commit, dispatch }, { name, value }) {
  117. const oldValue = get(state, name)
  118. const map = settingsMap[name]
  119. if (!map) throw new Error('Invalid server-side setting')
  120. const { set: path = map, api = defaultApi } = map
  121. commit('wipeProfileOption', { name })
  122. api({ rootState, commit }, { path, value, oldValue })
  123. .catch((e) => {
  124. console.warn('Error setting server-side option:', e)
  125. commit('confirmProfileOption', { name, value: oldValue })
  126. })
  127. },
  128. logout ({ commit }) {
  129. commit('wipeAllProfileOptions')
  130. }
  131. }
  132. }
  133. export default profileConfig