From 9c1814d12243f45cb67a797780a8c393f301080c Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Tue, 22 Feb 2022 23:31:40 +0200 Subject: expert settings toggle + server-side settings --- src/modules/serverSideConfig.js | 111 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 src/modules/serverSideConfig.js (limited to 'src/modules/serverSideConfig.js') diff --git a/src/modules/serverSideConfig.js b/src/modules/serverSideConfig.js new file mode 100644 index 00000000..75ea91be --- /dev/null +++ b/src/modules/serverSideConfig.js @@ -0,0 +1,111 @@ +import { get, set } from 'lodash' + +export const settingsMapGet = { + 'defaultScope': 'source.privacy', + 'defaultNSFW': 'source.sensitive', + 'stripRichContent': 'source.pleroma.no_rich_content', + // Privacy + 'locked': 'locked', + 'acceptChatMessages': 'pleroma.accepts_chat_messages', + 'allowFollowingMove': 'pleroma.allow_following_move', + 'discoverable': 'source.discoverable', + 'hideFavorites': 'pleroma.hide_favorites', + 'hideFollowers': 'pleroma.hide_followers', + 'hideFollows': 'pleroma.hide_follows', + 'hideFollowersCount': 'pleroma.hide_followers_count', + 'hideFollowsCount': 'pleroma.hide_follows_count', + // NotificationSettingsAPIs + 'webPushHideContents': 'pleroma.notification_settings.hide_notification_contents', + 'blockNotificationsFromStrangers': 'pleroma.notification_settings.block_from_strangers' +} + +export const settingsMapSet = { + 'defaultScope': 'source.privacy', + 'defaultNSFW': 'source.sensitive', + 'stripRichContent': 'source.pleroma.no_rich_content', + // 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, undefined])) + +const serverSideConfig = { + state: { ...defaultState }, + mutations: { + confirmServerSideOption (state, { name, value }) { + set(state, name, value) + }, + wipeServerSideOption (state, { name }) { + set(state, name, undefined) + }, + // Set the settings based on their path location + setCurrentUser (state, user) { + Object.entries(settingsMapGet).forEach(([name, path]) => { + set(state, name, get(user._original, path)) + }) + } + }, + actions: { + setServerSideOption ({ rootState, state, commit, dispatch }, { name, value }) { + const oldValue = get(state, name) + const params = {} + const path = settingsMapSet[name] + if (!path) throw new Error('Invalid server-side setting') + 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(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) => { + console.warn('Error setting server-side option:', e) + commit('confirmServerSideOption', { name, value: oldValue }) + }) + } + } +} + +export default serverSideConfig -- cgit v1.2.3-70-g09d2 From f626da838a7abeb3a2d3cd5f71ee4eb2ca272361 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Thu, 24 Feb 2022 15:00:08 +0200 Subject: revert to using local setting for default nsfw since backend is broken --- src/components/post_status_form/post_status_form.js | 1 + src/components/settings_modal/tabs/general_tab.vue | 3 ++- src/modules/serverSideConfig.js | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) (limited to 'src/modules/serverSideConfig.js') diff --git a/src/components/post_status_form/post_status_form.js b/src/components/post_status_form/post_status_form.js index fe07309f..9d7bbd75 100644 --- a/src/components/post_status_form/post_status_form.js +++ b/src/components/post_status_form/post_status_form.js @@ -117,6 +117,7 @@ const PostStatusForm = { ? this.copyMessageScope : this.$store.state.users.currentUser.default_scope + // const { defaultNSFW: sensitiveByDefault } = this.$store.state.serverSideConfig const { postContentType: contentType, sensitiveByDefault } = this.$store.getters.mergedConfig return { diff --git a/src/components/settings_modal/tabs/general_tab.vue b/src/components/settings_modal/tabs/general_tab.vue index 4accf0c1..5db70d77 100644 --- a/src/components/settings_modal/tabs/general_tab.vue +++ b/src/components/settings_modal/tabs/general_tab.vue @@ -221,7 +221,8 @@
  • - + + {{ $t('settings.sensitive_by_default') }}
  • diff --git a/src/modules/serverSideConfig.js b/src/modules/serverSideConfig.js index 75ea91be..ea2dc5e3 100644 --- a/src/modules/serverSideConfig.js +++ b/src/modules/serverSideConfig.js @@ -2,7 +2,7 @@ import { get, set } from 'lodash' export const settingsMapGet = { 'defaultScope': 'source.privacy', - 'defaultNSFW': 'source.sensitive', + 'defaultNSFW': 'source.sensitive', // BROKEN: pleroma/pleroma#2837 'stripRichContent': 'source.pleroma.no_rich_content', // Privacy 'locked': 'locked', -- cgit v1.2.3-70-g09d2 From 3a5ad18aca6a7303e6f6b97dd3f0919d5532184c Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 28 Feb 2022 18:00:38 +0200 Subject: fix stripping rich content not working --- src/modules/serverSideConfig.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/modules/serverSideConfig.js') diff --git a/src/modules/serverSideConfig.js b/src/modules/serverSideConfig.js index ea2dc5e3..c9d148d1 100644 --- a/src/modules/serverSideConfig.js +++ b/src/modules/serverSideConfig.js @@ -3,7 +3,7 @@ import { get, set } from 'lodash' export const settingsMapGet = { 'defaultScope': 'source.privacy', 'defaultNSFW': 'source.sensitive', // BROKEN: pleroma/pleroma#2837 - 'stripRichContent': 'source.pleroma.no_rich_content', + 'stripRichContent': 'source.pleroma.no_rich_text', // Privacy 'locked': 'locked', 'acceptChatMessages': 'pleroma.accepts_chat_messages', @@ -22,7 +22,7 @@ export const settingsMapGet = { export const settingsMapSet = { 'defaultScope': 'source.privacy', 'defaultNSFW': 'source.sensitive', - 'stripRichContent': 'source.pleroma.no_rich_content', + 'stripRichContent': 'no_rich_text', // Privacy 'locked': 'locked', 'acceptChatMessages': 'accepts_chat_messages', -- cgit v1.2.3-70-g09d2 From 8bb97fbfeb8f34c36aec96ee175d6eced49c1fb4 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 28 Feb 2022 18:01:41 +0200 Subject: fix settings behaving erratically and not updating properly --- src/modules/serverSideConfig.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/modules/serverSideConfig.js') diff --git a/src/modules/serverSideConfig.js b/src/modules/serverSideConfig.js index c9d148d1..2db6fc06 100644 --- a/src/modules/serverSideConfig.js +++ b/src/modules/serverSideConfig.js @@ -44,7 +44,7 @@ export const customAPIs = { 'blockNotificationsFromStrangers': 'updateNotificationSettings' } -export const defaultState = Object.fromEntries(Object.keys(settingsMapGet).map(key => [key, undefined])) +export const defaultState = Object.fromEntries(Object.keys(settingsMapGet).map(key => [key, null])) const serverSideConfig = { state: { ...defaultState }, @@ -53,7 +53,7 @@ const serverSideConfig = { set(state, name, value) }, wipeServerSideOption (state, { name }) { - set(state, name, undefined) + set(state, name, null) }, // Set the settings based on their path location setCurrentUser (state, user) { -- cgit v1.2.3-70-g09d2 From 39909c8a8590e866082da5d2528b4df2898f2bf5 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Mon, 28 Feb 2022 18:17:13 +0200 Subject: pre-emptively wipe serverside settings on logout --- src/modules/serverSideConfig.js | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'src/modules/serverSideConfig.js') diff --git a/src/modules/serverSideConfig.js b/src/modules/serverSideConfig.js index 2db6fc06..4f0cd965 100644 --- a/src/modules/serverSideConfig.js +++ b/src/modules/serverSideConfig.js @@ -55,6 +55,11 @@ const serverSideConfig = { wipeServerSideOption (state, { name }) { set(state, name, null) }, + wipeAllServerSideOptions (state) { + Object.keys(settingsMapGet).forEach(key => { + set(state, key, null) + }) + }, // Set the settings based on their path location setCurrentUser (state, user) { Object.entries(settingsMapGet).forEach(([name, path]) => { @@ -104,6 +109,9 @@ const serverSideConfig = { console.warn('Error setting server-side option:', e) commit('confirmServerSideOption', { name, value: oldValue }) }) + }, + logout ({ commit }) { + commit('wipeAllServerSideOptions') } } } -- cgit v1.2.3-70-g09d2 From 5930b667a147a194993dba604dd617f926bb9d31 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Sun, 13 Mar 2022 11:30:38 +0200 Subject: reduce the copypaste by making it more functional-style --- src/modules/serverSideConfig.js | 160 ++++++++++++++++++++++------------------ 1 file changed, 89 insertions(+), 71 deletions(-) (limited to 'src/modules/serverSideConfig.js') diff --git a/src/modules/serverSideConfig.js b/src/modules/serverSideConfig.js index 4f0cd965..5c1baedb 100644 --- a/src/modules/serverSideConfig.js +++ b/src/modules/serverSideConfig.js @@ -1,50 +1,95 @@ import { get, set } from 'lodash' -export const settingsMapGet = { - 'defaultScope': 'source.privacy', - 'defaultNSFW': 'source.sensitive', // BROKEN: pleroma/pleroma#2837 - 'stripRichContent': 'source.pleroma.no_rich_text', - // Privacy - 'locked': 'locked', - 'acceptChatMessages': 'pleroma.accepts_chat_messages', - 'allowFollowingMove': 'pleroma.allow_following_move', - 'discoverable': 'source.discoverable', - 'hideFavorites': 'pleroma.hide_favorites', - 'hideFollowers': 'pleroma.hide_followers', - 'hideFollows': 'pleroma.hide_follows', - 'hideFollowersCount': 'pleroma.hide_followers_count', - 'hideFollowsCount': 'pleroma.hide_follows_count', - // NotificationSettingsAPIs - 'webPushHideContents': 'pleroma.notification_settings.hide_notification_contents', - 'blockNotificationsFromStrangers': 'pleroma.notification_settings.block_from_strangers' +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) + }) } -export const settingsMapSet = { +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', - 'defaultNSFW': 'source.sensitive', - 'stripRichContent': 'no_rich_text', + 'defaultNSFW': 'source.sensitive', // BROKEN: pleroma/pleroma#2837 + 'stripRichContent': { + get: 'source.pleroma.no_rich_text', + set: 'no_rich_text' + }, // Privacy 'locked': 'locked', - 'acceptChatMessages': 'accepts_chat_messages', - 'allowFollowingMove': 'allow_following_move', + 'acceptChatMessages': { + get: 'pleroma.accepts_chat_messages', + set: 'accepts_chat_messages' + }, + 'allowFollowingMove': { + get: 'pleroma.allow_following_move', + set: 'allow_following_move' + }, 'discoverable': 'source.discoverable', - 'hideFavorites': 'hide_favorites', - 'hideFollowers': 'hide_followers', - 'hideFollows': 'hide_follows', - 'hideFollowersCount': 'hide_followers_count', - 'hideFollowsCount': 'hide_follows_count', + 'hideFavorites': { + get: 'pleroma.hide_favorites', + set: 'hide_favorites' + }, + '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 - 'webPushHideContents': 'hide_notification_contents', - 'blockNotificationsFromStrangers': 'block_from_strangers' -} - -export const customAPIs = { - __defaultApi: 'updateProfile', - 'webPushHideContents': 'updateNotificationSettings', - 'blockNotificationsFromStrangers': 'updateNotificationSettings' + 'webPushHideContents': { + 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 defaultState = Object.fromEntries(Object.keys(settingsMapGet).map(key => [key, null])) +export const defaultState = Object.fromEntries(Object.keys(settingsMap).map(key => [key, null])) const serverSideConfig = { state: { ...defaultState }, @@ -56,13 +101,15 @@ const serverSideConfig = { set(state, name, null) }, wipeAllServerSideOptions (state) { - Object.keys(settingsMapGet).forEach(key => { + Object.keys(settingsMap).forEach(key => { set(state, key, null) }) }, // Set the settings based on their path location 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)) }) } @@ -70,41 +117,12 @@ const serverSideConfig = { actions: { setServerSideOption ({ rootState, state, commit, dispatch }, { name, value }) { const oldValue = get(state, name) - const params = {} - const path = settingsMapSet[name] - if (!path) throw new Error('Invalid server-side setting') + const map = settingsMap[name] + if (!map) throw new Error('Invalid server-side setting') + const { set: path = map, api = defaultApi } = map 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(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) - }) + api({ rootState, commit }, { path, value, oldValue }) .catch((e) => { console.warn('Error setting server-side option:', e) commit('confirmServerSideOption', { name, value: oldValue }) -- cgit v1.2.3-70-g09d2