From b1324bcf13cbeae2fef0b2effc6f02c77988b35d Mon Sep 17 00:00:00 2001 From: Ryan Davis Date: Thu, 25 Apr 2019 06:17:56 -0700 Subject: [PATCH] MM-14907 RN: iOS - No MattermostShare error when sharing text that is over server's max post size (#2713) * RN: iOS - No MattermostShare error when sharing text that is over server's max post size * MM-14907 Allow editing if over maxPostSize. * MM-14907 Always On Character Count * Readded work from MM-14836 Post-Merge * Refactor of getMaxPostSize to match getMaxFileSize --- ios/MattermostShare/ShareViewController.swift | 29 ++++++++++++++----- .../UploadAttachments/Constants.h | 1 + .../UploadAttachments/Constants.m | 1 + .../UploadAttachments/StoreManager.h | 1 + .../UploadAttachments/StoreManager.m | 10 +++++++ 5 files changed, 35 insertions(+), 7 deletions(-) diff --git a/ios/MattermostShare/ShareViewController.swift b/ios/MattermostShare/ShareViewController.swift index 898a1ee13..ebeb46f7a 100644 --- a/ios/MattermostShare/ShareViewController.swift +++ b/ios/MattermostShare/ShareViewController.swift @@ -19,6 +19,7 @@ class ShareViewController: SLComposeServiceViewController { private var serverURL: String? private var message: String? private var publicURL: String? + private var maxPostAlertShown: Bool = false private var tempContainerURL: URL? = UploadSessionManager.shared.tempContainerURL() as URL? fileprivate var selectedChannel: Item? @@ -42,21 +43,28 @@ class ShareViewController: SLComposeServiceViewController { } override func isContentValid() -> Bool { - // Do validation of contentText and/or NSExtensionContext attachments here - if (attachments.count > 0) { + let maxMessageSize = store.getMaxPostSize() + self.charactersRemaining = NSNumber(value: Int(maxMessageSize) - contentText.count) + //Check content text size is not above max + if (contentText.count > maxMessageSize) { + if !maxPostAlertShown { + maxPostAlertShown = true + showErrorMessageAndStayOpen(title: "", message: "Content text shared in Mattermost must be less than \(maxMessageSize+1) characters.", VC: self) + } + return false + } else if (attachments.count > 0) { // Do validation of contentText and/or NSExtensionContext attachments here let maxImagePixels = store.getMaxImagePixels() if attachments.hasImageLargerThan(pixels: maxImagePixels) { let readableMaxImagePixels = formatImagePixels(pixels: maxImagePixels) showErrorMessage(title: "", message: "Image attachments shared in Mattermost must be less than \(readableMaxImagePixels).", VC: self) } - let maxFileSize = store.getMaxFileSize() if attachments.hasAttachementLargerThan(fileSize: maxFileSize) { let readableMaxFileSize = formatFileSize(fileSize: maxFileSize) showErrorMessage(title: "", message: "File attachments shared in Mattermost must be less than \(readableMaxFileSize).", VC: self) } } - + return serverURL != nil && sessionToken != nil && attachmentsCount() == attachments.count && @@ -416,19 +424,26 @@ class ShareViewController: SLComposeServiceViewController { alert.addAction(okAction) VC.present(alert, animated: true, completion: nil) } - + + func showErrorMessageAndStayOpen(title: String, message: String, VC: UIViewController) { + let alert: UIAlertController = UIAlertController(title: title, message: message, preferredStyle: UIAlertController.Style.alert) + let okAction = UIAlertAction(title: "OK", style: UIAlertAction.Style.default) + alert.addAction(okAction) + VC.present(alert, animated: true, completion: nil) + } + func formatImagePixels(pixels: UInt64) -> String { let suffixes = ["pixels", "KP", "MP", "GP", "TP", "PP", "EP", "ZP", "YP"] let k: Double = 1000 return formatSize(size: Double(pixels), k: k, suffixes: suffixes) } - + func formatFileSize(fileSize: UInt64) -> String { let suffixes = ["bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"] let k: Double = 1024 return formatSize(size: Double(fileSize), k: k, suffixes: suffixes) } - + func formatSize(size: Double, k: Double, suffixes: Array) -> String { guard size > 0 else { return "0 \(suffixes[0])" diff --git a/ios/UploadAttachments/UploadAttachments/Constants.h b/ios/UploadAttachments/UploadAttachments/Constants.h index 65d1d1fad..830540790 100644 --- a/ios/UploadAttachments/UploadAttachments/Constants.h +++ b/ios/UploadAttachments/UploadAttachments/Constants.h @@ -5,4 +5,5 @@ extern NSString *APP_GROUP_ID; extern UInt64 DEFAULT_SERVER_MAX_IMAGE_PIXELS; extern UInt64 DEFAULT_SERVER_MAX_FILE_SIZE; +extern UInt64 DEFAULT_SERVER_MAX_POST_SIZE; @end diff --git a/ios/UploadAttachments/UploadAttachments/Constants.m b/ios/UploadAttachments/UploadAttachments/Constants.m index 4c641870a..fe47cacf8 100644 --- a/ios/UploadAttachments/UploadAttachments/Constants.m +++ b/ios/UploadAttachments/UploadAttachments/Constants.m @@ -4,4 +4,5 @@ NSString *APP_GROUP_ID = @"group.com.mattermost.rnbeta"; UInt64 DEFAULT_SERVER_MAX_IMAGE_PIXELS = 6048 * 4032; UInt64 DEFAULT_SERVER_MAX_FILE_SIZE = 50 * 1024 * 1024; +UInt64 DEFAULT_SERVER_MAX_POST_SIZE = 4000; @end diff --git a/ios/UploadAttachments/UploadAttachments/StoreManager.h b/ios/UploadAttachments/UploadAttachments/StoreManager.h index 5d6eba79c..25e4b46ad 100644 --- a/ios/UploadAttachments/UploadAttachments/StoreManager.h +++ b/ios/UploadAttachments/UploadAttachments/StoreManager.h @@ -16,6 +16,7 @@ -(NSDictionary *)getEntities:(BOOL)loadFromFile; -(UInt64)getMaxImagePixels; -(UInt64)getMaxFileSize; +-(UInt64)getMaxPostSize; -(NSArray *)getMyTeams; -(NSString *)getServerUrl; -(NSString *)getToken; diff --git a/ios/UploadAttachments/UploadAttachments/StoreManager.m b/ios/UploadAttachments/UploadAttachments/StoreManager.m index df9c00376..dc5f834c3 100644 --- a/ios/UploadAttachments/UploadAttachments/StoreManager.m +++ b/ios/UploadAttachments/UploadAttachments/StoreManager.m @@ -178,6 +178,16 @@ return DEFAULT_SERVER_MAX_FILE_SIZE; } +-(UInt64)getMaxPostSize { + NSDictionary *config = [self getConfig]; + NSString *key = @"MaxPostSize"; + if (config != nil && [config objectForKey:key]) { + return [self scanValueFromConfig:config key:key]; + } + + return DEFAULT_SERVER_MAX_POST_SIZE; +} + -(void)updateEntities:(NSString *)content { [self.bucket writeToFile:@"entities" content:content]; }