From c42e2276a2ed4d863dd67f1d07c95e42d088ea2d Mon Sep 17 00:00:00 2001 From: enahum Date: Mon, 13 Nov 2017 13:32:38 -0300 Subject: [PATCH] Prevent notify props from causing a crash (#1127) --- .../settings/notification_settings/index.js | 3 +- .../notification_settings.js | 7 +++-- .../notification_settings_email/index.js | 3 +- .../notification_settings_email.js | 3 +- .../notification_settings_mentions/index.js | 3 +- .../notification_settings_mention_base.js | 6 ++-- .../index.js | 3 +- .../notification_settings_mobile_base.js | 4 ++- app/utils/notify_props.js | 28 +++++++++++++++++++ 9 files changed, 45 insertions(+), 15 deletions(-) create mode 100644 app/utils/notify_props.js diff --git a/app/screens/settings/notification_settings/index.js b/app/screens/settings/notification_settings/index.js index b9adfb02a4..cb6815c4bd 100644 --- a/app/screens/settings/notification_settings/index.js +++ b/app/screens/settings/notification_settings/index.js @@ -11,9 +11,8 @@ import {handleUpdateUserNotifyProps} from 'app/actions/views/account_notificatio import NotificationSettings from './notification_settings'; -function mapStateToProps(state, ownProps) { +function mapStateToProps(state) { return { - ...ownProps, config: state.entities.general.config, currentUser: getCurrentUser(state), myPreferences: getMyPreferences(state), diff --git a/app/screens/settings/notification_settings/notification_settings.js b/app/screens/settings/notification_settings/notification_settings.js index 1961c6a63f..1a4d2b6f19 100644 --- a/app/screens/settings/notification_settings/notification_settings.js +++ b/app/screens/settings/notification_settings/notification_settings.js @@ -21,6 +21,7 @@ import {RadioButton, RadioButtonGroup} from 'app/components/radio_button'; import StatusBar from 'app/components/status_bar'; import NotificationPreferences from 'app/notification_preferences'; import SettingsItem from 'app/screens/settings/settings_item'; +import {getNotificationProps} from 'app/utils/notify_props'; import {preventDoubleTap} from 'app/utils/tap'; import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme'; @@ -154,7 +155,7 @@ class NotificationSettings extends PureComponent { } this.saveNotificationProps({ - ...currentUser.notify_props, + ...getNotificationProps(currentUser), email, interval }); @@ -165,7 +166,7 @@ class NotificationSettings extends PureComponent { const {currentUser} = this.props; const {user_id} = notifyProps; const previousProps = { - ...currentUser.notify_props, + ...getNotificationProps(currentUser), user_id }; @@ -184,7 +185,7 @@ class NotificationSettings extends PureComponent { } const {config, currentUser, intl, myPreferences} = this.props; - const notifyProps = currentUser.notify_props || {}; + const notifyProps = getNotificationProps(currentUser); const sendEmailNotifications = config.SendEmailNotifications === 'true'; const emailBatchingEnabled = sendEmailNotifications && config.EnableEmailBatching === 'true'; diff --git a/app/screens/settings/notification_settings_email/index.js b/app/screens/settings/notification_settings_email/index.js index 48ef2a279c..7cc8d7e5fa 100644 --- a/app/screens/settings/notification_settings_email/index.js +++ b/app/screens/settings/notification_settings_email/index.js @@ -8,9 +8,8 @@ import {getMyPreferences, getTheme} from 'mattermost-redux/selectors/entities/pr import NotificationSettingsEmail from './notification_settings_email'; -function mapStateToProps(state, ownProps) { +function mapStateToProps(state) { return { - ...ownProps, config: getConfig(state), myPreferences: getMyPreferences(state), theme: getTheme(state) diff --git a/app/screens/settings/notification_settings_email/notification_settings_email.js b/app/screens/settings/notification_settings_email/notification_settings_email.js index 45da1c9788..9842793f3e 100644 --- a/app/screens/settings/notification_settings_email/notification_settings_email.js +++ b/app/screens/settings/notification_settings_email/notification_settings_email.js @@ -14,6 +14,7 @@ import {getPreferencesByCategory} from 'mattermost-redux/utils/preference_utils' import FormattedText from 'app/components/formatted_text'; import StatusBar from 'app/components/status_bar'; +import {getNotificationProps} from 'app/utils/notify_props'; import {changeOpacity, makeStyleSheetFromTheme} from 'app/utils/theme'; import Section from 'app/screens/settings/section'; @@ -33,7 +34,7 @@ export default class NotificationSettingsEmail extends PureComponent { super(props); const {currentUser} = props; - const notifyProps = currentUser.notify_props || {}; + const notifyProps = getNotificationProps(currentUser); props.navigator.setOnNavigatorEvent(this.onNavigatorEvent); this.state = this.setStateFromNotifyProps(notifyProps); diff --git a/app/screens/settings/notification_settings_mentions/index.js b/app/screens/settings/notification_settings_mentions/index.js index 0d946284e1..2f9bd18520 100644 --- a/app/screens/settings/notification_settings_mentions/index.js +++ b/app/screens/settings/notification_settings_mentions/index.js @@ -7,9 +7,8 @@ import {getTheme} from 'mattermost-redux/selectors/entities/preferences'; import NotificationSettingsMentions from './notification_settings_mentions'; -function mapStateToProps(state, ownProps) { +function mapStateToProps(state) { return { - ...ownProps, theme: getTheme(state) }; } diff --git a/app/screens/settings/notification_settings_mentions/notification_settings_mention_base.js b/app/screens/settings/notification_settings_mentions/notification_settings_mention_base.js index 99a17f773b..43d3b13574 100644 --- a/app/screens/settings/notification_settings_mentions/notification_settings_mention_base.js +++ b/app/screens/settings/notification_settings_mentions/notification_settings_mention_base.js @@ -5,6 +5,8 @@ import {PureComponent} from 'react'; import PropTypes from 'prop-types'; import {intlShape} from 'react-intl'; +import {getNotificationProps} from 'app/utils/notify_props'; + export default class NotificationSettingsMentionsBase extends PureComponent { static propTypes = { currentUser: PropTypes.object.isRequired, @@ -18,7 +20,7 @@ export default class NotificationSettingsMentionsBase extends PureComponent { super(props); const {currentUser} = props; - const notifyProps = currentUser.notify_props || {}; + const notifyProps = getNotificationProps(currentUser); props.navigator.setOnNavigatorEvent(this.onNavigatorEvent); @@ -43,7 +45,7 @@ export default class NotificationSettingsMentionsBase extends PureComponent { mentionKeys.splice(usernameMentionIndex, 1); } - const comments = notifyProps.comments || 'never'; + const comments = notifyProps.comments || 'any'; const newState = { ...notifyProps, diff --git a/app/screens/settings/notification_settings_mentions_keywords/index.js b/app/screens/settings/notification_settings_mentions_keywords/index.js index 61fb1faaa6..3688c09864 100644 --- a/app/screens/settings/notification_settings_mentions_keywords/index.js +++ b/app/screens/settings/notification_settings_mentions_keywords/index.js @@ -7,9 +7,8 @@ import {getTheme} from 'mattermost-redux/selectors/entities/preferences'; import NotificationSettingsMentionsKeywords from './notification_settings_mentions_keywords'; -function mapStateToProps(state, ownProps) { +function mapStateToProps(state) { return { - ...ownProps, theme: getTheme(state) }; } diff --git a/app/screens/settings/notification_settings_mobile/notification_settings_mobile_base.js b/app/screens/settings/notification_settings_mobile/notification_settings_mobile_base.js index d495d51737..679514986c 100644 --- a/app/screens/settings/notification_settings_mobile/notification_settings_mobile_base.js +++ b/app/screens/settings/notification_settings_mobile/notification_settings_mobile_base.js @@ -6,6 +6,8 @@ import {Platform} from 'react-native'; import PropTypes from 'prop-types'; import {intlShape} from 'react-intl'; +import {getNotificationProps} from 'app/utils/notify_props'; + export default class NotificationSettingsMobileBase extends PureComponent { static propTypes = { config: PropTypes.object.isRequired, @@ -21,7 +23,7 @@ export default class NotificationSettingsMobileBase extends PureComponent { super(props); const {currentUser} = props; - const notifyProps = currentUser.notify_props || {}; + const notifyProps = getNotificationProps(currentUser); this.state = { ...notifyProps, diff --git a/app/utils/notify_props.js b/app/utils/notify_props.js new file mode 100644 index 0000000000..611c4cc5e0 --- /dev/null +++ b/app/utils/notify_props.js @@ -0,0 +1,28 @@ +// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. +// See License.txt for license information. + +export function getNotificationProps(user) { + if (user && user.notify_props) { + return user.notify_props; + } + + const props = { + channel: 'true', + comments: 'any', + desktop: 'all', + desktop_sound: 'true', + email: 'true', + mention_keys: user ? `${user.username},@${user.username}` : '', + push: 'mention', + push_status: 'online' + }; + + if (!user || !user.first_name) { + props.first_name = 'false'; + } else { + props.first_name = 'true'; + } + + return props; +} +