Skip to content

Commit

Permalink
MM-14907 RN: iOS - No MattermostShare error when sharing text that is…
Browse files Browse the repository at this point in the history
… over server's max post size (mattermost#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
  • Loading branch information
RyPoints authored and enahum committed Apr 25, 2019
1 parent 89c0d1b commit fffd31b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
29 changes: 22 additions & 7 deletions ios/MattermostShare/ShareViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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 &&
Expand Down Expand Up @@ -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>) -> String {
guard size > 0 else {
return "0 \(suffixes[0])"
Expand Down
1 change: 1 addition & 0 deletions ios/UploadAttachments/UploadAttachments/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
1 change: 1 addition & 0 deletions ios/UploadAttachments/UploadAttachments/Constants.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ @implementation Constants
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
1 change: 1 addition & 0 deletions ios/UploadAttachments/UploadAttachments/StoreManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
-(NSDictionary *)getEntities:(BOOL)loadFromFile;
-(UInt64)getMaxImagePixels;
-(UInt64)getMaxFileSize;
-(UInt64)getMaxPostSize;
-(NSArray *)getMyTeams;
-(NSString *)getServerUrl;
-(NSString *)getToken;
Expand Down
10 changes: 10 additions & 0 deletions ios/UploadAttachments/UploadAttachments/StoreManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,16 @@ -(UInt64)getMaxFileSize {
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];
}
Expand Down

0 comments on commit fffd31b

Please sign in to comment.