reduce the copypaste by making it more functional-style
This commit is contained in:
parent
726fdbea15
commit
5930b667a1
|
@ -1,50 +1,95 @@
|
||||||
import { get, set } from 'lodash'
|
import { get, set } from 'lodash'
|
||||||
|
|
||||||
export const settingsMapGet = {
|
const defaultApi = ({ rootState, commit }, { path, value }) => {
|
||||||
|
const params = {}
|
||||||
|
set(params, path, value)
|
||||||
|
return rootState
|
||||||
|
.api
|
||||||
|
.backendInteractor
|
||||||
|
.updateProfile({ params })
|
||||||
|
.then(result => {
|
||||||
|
commit('addNewUsers', [result])
|
||||||
|
commit('setCurrentUser', result)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const notificationsApi = ({ rootState, commit }, { path, value, oldValue }) => {
|
||||||
|
const settings = {}
|
||||||
|
set(settings, path, value)
|
||||||
|
return rootState
|
||||||
|
.api
|
||||||
|
.backendInteractor
|
||||||
|
.updateNotificationSettings({ settings })
|
||||||
|
.then(result => {
|
||||||
|
if (result.status === 'success') {
|
||||||
|
commit('confirmServerSideOption', { name, value })
|
||||||
|
} else {
|
||||||
|
commit('confirmServerSideOption', { name, value: oldValue })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map that stores relation between path for reading (from user profile),
|
||||||
|
* for writing (into API) an what API to use.
|
||||||
|
*
|
||||||
|
* Shorthand - instead of { get, set, api? } object it's possible to use string
|
||||||
|
* in case default api is used and get = set
|
||||||
|
*
|
||||||
|
* If no api is specified, defaultApi is used (see above)
|
||||||
|
*/
|
||||||
|
export const settingsMap = {
|
||||||
'defaultScope': 'source.privacy',
|
'defaultScope': 'source.privacy',
|
||||||
'defaultNSFW': 'source.sensitive', // BROKEN: pleroma/pleroma#2837
|
'defaultNSFW': 'source.sensitive', // BROKEN: pleroma/pleroma#2837
|
||||||
'stripRichContent': 'source.pleroma.no_rich_text',
|
'stripRichContent': {
|
||||||
|
get: 'source.pleroma.no_rich_text',
|
||||||
|
set: 'no_rich_text'
|
||||||
|
},
|
||||||
// Privacy
|
// Privacy
|
||||||
'locked': 'locked',
|
'locked': 'locked',
|
||||||
'acceptChatMessages': 'pleroma.accepts_chat_messages',
|
'acceptChatMessages': {
|
||||||
'allowFollowingMove': 'pleroma.allow_following_move',
|
get: 'pleroma.accepts_chat_messages',
|
||||||
|
set: 'accepts_chat_messages'
|
||||||
|
},
|
||||||
|
'allowFollowingMove': {
|
||||||
|
get: 'pleroma.allow_following_move',
|
||||||
|
set: 'allow_following_move'
|
||||||
|
},
|
||||||
'discoverable': 'source.discoverable',
|
'discoverable': 'source.discoverable',
|
||||||
'hideFavorites': 'pleroma.hide_favorites',
|
'hideFavorites': {
|
||||||
'hideFollowers': 'pleroma.hide_followers',
|
get: 'pleroma.hide_favorites',
|
||||||
'hideFollows': 'pleroma.hide_follows',
|
set: 'hide_favorites'
|
||||||
'hideFollowersCount': 'pleroma.hide_followers_count',
|
},
|
||||||
'hideFollowsCount': 'pleroma.hide_follows_count',
|
'hideFollowers': {
|
||||||
|
get: 'pleroma.hide_followers',
|
||||||
|
set: 'hide_followers'
|
||||||
|
},
|
||||||
|
'hideFollows': {
|
||||||
|
get: 'pleroma.hide_follows',
|
||||||
|
set: 'hide_follows'
|
||||||
|
},
|
||||||
|
'hideFollowersCount': {
|
||||||
|
get: 'pleroma.hide_followers_count',
|
||||||
|
set: 'hide_followers_count'
|
||||||
|
},
|
||||||
|
'hideFollowsCount': {
|
||||||
|
get: 'pleroma.hide_follows_count',
|
||||||
|
set: 'hide_follows_count'
|
||||||
|
},
|
||||||
// NotificationSettingsAPIs
|
// NotificationSettingsAPIs
|
||||||
'webPushHideContents': 'pleroma.notification_settings.hide_notification_contents',
|
'webPushHideContents': {
|
||||||
'blockNotificationsFromStrangers': 'pleroma.notification_settings.block_from_strangers'
|
get: 'pleroma.notification_settings.hide_notification_contents',
|
||||||
|
set: 'hide_notification_contents',
|
||||||
|
api: notificationsApi
|
||||||
|
},
|
||||||
|
'blockNotificationsFromStrangers': {
|
||||||
|
get: 'pleroma.notification_settings.block_from_strangers',
|
||||||
|
set: 'block_from_strangers',
|
||||||
|
api: notificationsApi
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const settingsMapSet = {
|
export const defaultState = Object.fromEntries(Object.keys(settingsMap).map(key => [key, null]))
|
||||||
'defaultScope': 'source.privacy',
|
|
||||||
'defaultNSFW': 'source.sensitive',
|
|
||||||
'stripRichContent': 'no_rich_text',
|
|
||||||
// Privacy
|
|
||||||
'locked': 'locked',
|
|
||||||
'acceptChatMessages': 'accepts_chat_messages',
|
|
||||||
'allowFollowingMove': 'allow_following_move',
|
|
||||||
'discoverable': 'source.discoverable',
|
|
||||||
'hideFavorites': 'hide_favorites',
|
|
||||||
'hideFollowers': 'hide_followers',
|
|
||||||
'hideFollows': 'hide_follows',
|
|
||||||
'hideFollowersCount': 'hide_followers_count',
|
|
||||||
'hideFollowsCount': 'hide_follows_count',
|
|
||||||
// NotificationSettingsAPIs
|
|
||||||
'webPushHideContents': 'hide_notification_contents',
|
|
||||||
'blockNotificationsFromStrangers': 'block_from_strangers'
|
|
||||||
}
|
|
||||||
|
|
||||||
export const customAPIs = {
|
|
||||||
__defaultApi: 'updateProfile',
|
|
||||||
'webPushHideContents': 'updateNotificationSettings',
|
|
||||||
'blockNotificationsFromStrangers': 'updateNotificationSettings'
|
|
||||||
}
|
|
||||||
|
|
||||||
export const defaultState = Object.fromEntries(Object.keys(settingsMapGet).map(key => [key, null]))
|
|
||||||
|
|
||||||
const serverSideConfig = {
|
const serverSideConfig = {
|
||||||
state: { ...defaultState },
|
state: { ...defaultState },
|
||||||
|
@ -56,13 +101,15 @@ const serverSideConfig = {
|
||||||
set(state, name, null)
|
set(state, name, null)
|
||||||
},
|
},
|
||||||
wipeAllServerSideOptions (state) {
|
wipeAllServerSideOptions (state) {
|
||||||
Object.keys(settingsMapGet).forEach(key => {
|
Object.keys(settingsMap).forEach(key => {
|
||||||
set(state, key, null)
|
set(state, key, null)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
// Set the settings based on their path location
|
// Set the settings based on their path location
|
||||||
setCurrentUser (state, user) {
|
setCurrentUser (state, user) {
|
||||||
Object.entries(settingsMapGet).forEach(([name, path]) => {
|
Object.entries(settingsMap).forEach((map) => {
|
||||||
|
const [name, value] = map
|
||||||
|
const { get: path = value } = value
|
||||||
set(state, name, get(user._original, path))
|
set(state, name, get(user._original, path))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -70,41 +117,12 @@ const serverSideConfig = {
|
||||||
actions: {
|
actions: {
|
||||||
setServerSideOption ({ rootState, state, commit, dispatch }, { name, value }) {
|
setServerSideOption ({ rootState, state, commit, dispatch }, { name, value }) {
|
||||||
const oldValue = get(state, name)
|
const oldValue = get(state, name)
|
||||||
const params = {}
|
const map = settingsMap[name]
|
||||||
const path = settingsMapSet[name]
|
if (!map) throw new Error('Invalid server-side setting')
|
||||||
if (!path) throw new Error('Invalid server-side setting')
|
const { set: path = map, api = defaultApi } = map
|
||||||
commit('wipeServerSideOption', { name })
|
commit('wipeServerSideOption', { name })
|
||||||
const customAPIName = customAPIs[name] || customAPIs.__defaultApi
|
|
||||||
const api = rootState.api.backendInteractor[customAPIName]
|
|
||||||
let prefix = ''
|
|
||||||
switch (customAPIName) {
|
|
||||||
case 'updateNotificationSettings':
|
|
||||||
prefix = 'settings.'
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
prefix = 'params.'
|
|
||||||
break
|
|
||||||
}
|
|
||||||
|
|
||||||
set(params, prefix + path, value)
|
api({ rootState, commit }, { path, value, oldValue })
|
||||||
api(params)
|
|
||||||
.then((result) => {
|
|
||||||
switch (customAPIName) {
|
|
||||||
case 'updateNotificationSettings':
|
|
||||||
console.log(result)
|
|
||||||
if (result.status === 'success') {
|
|
||||||
commit('confirmServerSideOption', { name, value })
|
|
||||||
} else {
|
|
||||||
commit('confirmServerSideOption', { name, value: oldValue })
|
|
||||||
}
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
commit('addNewUsers', [result])
|
|
||||||
commit('setCurrentUser', result)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
console.log(state)
|
|
||||||
})
|
|
||||||
.catch((e) => {
|
.catch((e) => {
|
||||||
console.warn('Error setting server-side option:', e)
|
console.warn('Error setting server-side option:', e)
|
||||||
commit('confirmServerSideOption', { name, value: oldValue })
|
commit('confirmServerSideOption', { name, value: oldValue })
|
||||||
|
|
Loading…
Reference in New Issue