From d8a76946f3fd464a17750ce5ef4b13ba48fef78b Mon Sep 17 00:00:00 2001 From: Elias Nahum Date: Mon, 6 May 2019 12:20:08 -0400 Subject: [PATCH] MM-15428 Return empty object for native managed configuration when not set (#2770) --- .../rnbeta/MattermostManagedModule.java | 4 ++-- app/components/at_mention/at_mention.js | 2 +- .../markdown_code_block/markdown_code_block.js | 2 +- .../markdown/markdown_image/markdown_image.js | 2 +- .../markdown/markdown_link/markdown_link.js | 2 +- app/components/post_list/post_list.android.js | 1 - app/components/post_list/post_list.ios.js | 1 - app/mattermost_managed/mattermost-managed.ios.js | 3 +-- app/screens/post_options/index.js | 2 +- ios/Mattermost/MattermostManaged.m | 15 +++++++++++---- 10 files changed, 19 insertions(+), 15 deletions(-) diff --git a/android/app/src/main/java/com/mattermost/rnbeta/MattermostManagedModule.java b/android/app/src/main/java/com/mattermost/rnbeta/MattermostManagedModule.java index 78808e2db..3992990c2 100644 --- a/android/app/src/main/java/com/mattermost/rnbeta/MattermostManagedModule.java +++ b/android/app/src/main/java/com/mattermost/rnbeta/MattermostManagedModule.java @@ -56,10 +56,10 @@ public class MattermostManagedModule extends ReactContextBaseJavaModule { Object result = Arguments.fromBundle(config); promise.resolve(result); } else { - throw new Exception("The MDM vendor has not sent any Managed configuration"); + promise.resolve(Arguments.createMap()); } } catch (Exception e) { - promise.reject("no managed configuration", e); + promise.resolve(Arguments.createMap()); } } } diff --git a/app/components/at_mention/at_mention.js b/app/components/at_mention/at_mention.js index 1b4605daa..e096c5e1b 100644 --- a/app/components/at_mention/at_mention.js +++ b/app/components/at_mention/at_mention.js @@ -99,7 +99,7 @@ export default class AtMention extends React.PureComponent { const config = mattermostManaged.getCachedConfig(); - if (config.copyAndPasteProtection !== 'true') { + if (config?.copyAndPasteProtection !== 'true') { const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'}); const actionText = formatMessage({id: 'mobile.mention.copy_mention', defaultMessage: 'Copy Mention'}); diff --git a/app/components/markdown/markdown_code_block/markdown_code_block.js b/app/components/markdown/markdown_code_block/markdown_code_block.js index 73666c348..d6ff764ed 100644 --- a/app/components/markdown/markdown_code_block/markdown_code_block.js +++ b/app/components/markdown/markdown_code_block/markdown_code_block.js @@ -84,7 +84,7 @@ export default class MarkdownCodeBlock extends React.PureComponent { const config = mattermostManaged.getCachedConfig(); - if (config.copyAndPasteProtection !== 'true') { + if (config?.copyAndPasteProtection !== 'true') { const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'}); const actionText = formatMessage({id: 'mobile.markdown.code.copy_code', defaultMessage: 'Copy Code'}); BottomSheet.showBottomSheetWithOptions({ diff --git a/app/components/markdown/markdown_image/markdown_image.js b/app/components/markdown/markdown_image/markdown_image.js index 4e9700f21..e1e384a53 100644 --- a/app/components/markdown/markdown_image/markdown_image.js +++ b/app/components/markdown/markdown_image/markdown_image.js @@ -146,7 +146,7 @@ export default class MarkdownImage extends React.Component { const config = mattermostManaged.getCachedConfig(); - if (config.copyAndPasteProtection !== 'true') { + if (config?.copyAndPasteProtection !== 'true') { const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'}); const actionText = formatMessage({id: 'mobile.markdown.link.copy_url', defaultMessage: 'Copy URL'}); BottomSheet.showBottomSheetWithOptions({ diff --git a/app/components/markdown/markdown_link/markdown_link.js b/app/components/markdown/markdown_link/markdown_link.js index 20a00badb..abcdc7b76 100644 --- a/app/components/markdown/markdown_link/markdown_link.js +++ b/app/components/markdown/markdown_link/markdown_link.js @@ -101,7 +101,7 @@ export default class MarkdownLink extends PureComponent { const config = mattermostManaged.getCachedConfig(); - if (config.copyAndPasteProtection !== 'true') { + if (config?.copyAndPasteProtection !== 'true') { const cancelText = formatMessage({id: 'mobile.post.cancel', defaultMessage: 'Cancel'}); const actionText = formatMessage({id: 'mobile.markdown.link.copy_url', defaultMessage: 'Copy URL'}); BottomSheet.showBottomSheetWithOptions({ diff --git a/app/components/post_list/post_list.android.js b/app/components/post_list/post_list.android.js index 015d1797d..3aaddd23d 100644 --- a/app/components/post_list/post_list.android.js +++ b/app/components/post_list/post_list.android.js @@ -19,7 +19,6 @@ export default class PostList extends PostListBase { this.state = { refreshing: false, - managedConfig: {}, dataSource: new DataSource(props.postIds, this.keyExtractor), }; } diff --git a/app/components/post_list/post_list.ios.js b/app/components/post_list/post_list.ios.js index 4be27931f..ceeb8e43f 100644 --- a/app/components/post_list/post_list.ios.js +++ b/app/components/post_list/post_list.ios.js @@ -31,7 +31,6 @@ export default class PostList extends PostListBase { this.makeExtraData = makeExtraData(); this.state = { - managedConfig: {}, postListHeight: 0, }; } diff --git a/app/mattermost_managed/mattermost-managed.ios.js b/app/mattermost_managed/mattermost-managed.ios.js index dc0074e6c..4c4c64d8a 100644 --- a/app/mattermost_managed/mattermost-managed.ios.js +++ b/app/mattermost_managed/mattermost-managed.ios.js @@ -8,7 +8,6 @@ const {BlurAppScreen, MattermostManaged} = NativeModules; const mattermostManagedEmitter = new NativeEventEmitter(MattermostManaged); const listeners = []; -const emptyObject = {}; let cachedConfig = {}; export default { @@ -39,7 +38,7 @@ export default { blurAppScreen: BlurAppScreen.enabled, getConfig: async () => { try { - cachedConfig = await MattermostManaged.getConfig() || emptyObject; + cachedConfig = await MattermostManaged.getConfig(); } catch (error) { // do nothing... } diff --git a/app/screens/post_options/index.js b/app/screens/post_options/index.js index 891ffdd49..a17962bde 100644 --- a/app/screens/post_options/index.js +++ b/app/screens/post_options/index.js @@ -97,7 +97,7 @@ function mapStateToProps(state, ownProps) { canAddReaction = false; } - if (!ownProps.isSystemMessage && ownProps.managedConfig.copyAndPasteProtection !== 'true' && post.message) { + if (!ownProps.isSystemMessage && ownProps.managedConfig?.copyAndPasteProtection !== 'true' && post.message) { canCopyText = true; } diff --git a/ios/Mattermost/MattermostManaged.m b/ios/Mattermost/MattermostManaged.m index 985b617ad..0df7cc7e1 100644 --- a/ios/Mattermost/MattermostManaged.m +++ b/ios/Mattermost/MattermostManaged.m @@ -69,14 +69,22 @@ static NSString * const feedbackKey = @"com.apple.feedback.managed"; { NSDictionary *response = [[NSUserDefaults standardUserDefaults] dictionaryForKey:configurationKey]; if (hasListeners) { - [self sendEventWithName:@"managedConfigDidChange" body:response]; + @try { + [self sendEventWithName:@"managedConfigDidChange" body:response]; + } @catch (NSException *exception) { + NSLog(@"Error sending event managedConfigDidChange to JS details=%@", exception.reason); + } } } - (void) remoteConfigChanged { NSDictionary *response = [[NSUserDefaults standardUserDefaults] dictionaryForKey:configurationKey]; if (hasListeners) { - [self sendEventWithName:@"managedConfigDidChange" body:response]; + @try { + [self sendEventWithName:@"managedConfigDidChange" body:response]; + } @catch (NSException *exception) { + NSLog(@"Error sending event managedConfigDidChange to JS details=%@", exception.reason); + } } } @@ -87,8 +95,7 @@ RCT_EXPORT_METHOD(getConfig:(RCTPromiseResolveBlock)resolve resolve(response); } else { - NSError *error = [NSError errorWithDomain:@"Mattermost Managed" code:-1 userInfo:nil]; - reject(@"no managed configuration", @"The MDM vendor has not sent any Managed configuration", error); + resolve(@{}); } }