Don't send text files as text messgaes.

This commit is contained in:
Matthew Chen 2018-01-17 16:40:57 -05:00
parent 64e4f054b8
commit d1c17167c6
5 changed files with 17 additions and 66 deletions

View File

@ -110,11 +110,6 @@ public class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate {
createVideoPreview()
} else if attachment.isAudio {
createAudioPreview()
// We handle the isOversizeText case before isText.
} else if attachment.isOversizeText {
createOversizeTextPreview()
} else if attachment.isUrl || attachment.isText {
createTextPreview()
} else {
createGenericPreview()
}
@ -291,63 +286,6 @@ public class MediaMessageView: UIView, OWSAudioAttachmentPlayerDelegate {
}
}
private func createOversizeTextPreview() {
let data = attachment.data
guard let messageText = String(data: data, encoding: String.Encoding.utf8) else {
createGenericPreview()
return
}
let messageBubbleView = UIImageView()
messageBubbleView.layoutMargins = .zero
let bubbleImageData =
OWSMessagesBubbleImageFactory.shared.outgoing
messageBubbleView.image = bubbleImageData.messageBubbleImage
let textColor = UIColor.white
let messageTextView = UITextView()
messageTextView.font = UIFont.ows_dynamicTypeBody()
messageTextView.backgroundColor = UIColor.clear
messageTextView.isOpaque = false
messageTextView.isEditable = false
messageTextView.isSelectable = false
messageTextView.textContainerInset = UIEdgeInsets.zero
messageTextView.contentInset = UIEdgeInsets.zero
messageTextView.isScrollEnabled = false
messageTextView.showsHorizontalScrollIndicator = false
messageTextView.showsVerticalScrollIndicator = false
messageTextView.isUserInteractionEnabled = false
messageTextView.textColor = textColor
messageTextView.linkTextAttributes = [NSForegroundColorAttributeName : textColor,
NSUnderlineStyleAttributeName : [NSUnderlineStyle.styleSingle,
NSUnderlineStyle.patternSolid]
]
messageTextView.dataDetectorTypes = [.link, .address, .calendarEvent]
messageTextView.text = messageText
messageBubbleView.layoutMargins = .zero
self.layoutMargins = .zero
self.addSubview(messageBubbleView)
messageBubbleView.autoVCenterInSuperview()
messageBubbleView.autoHCenterInSuperview()
messageBubbleView.autoPinEdge(toSuperviewEdge: .leading, withInset: 25, relation: .greaterThanOrEqual)
messageBubbleView.autoPinEdge(toSuperviewEdge: .trailing, withInset: 25, relation: .greaterThanOrEqual)
messageBubbleView.addSubview(messageTextView)
messageTextView.autoPinTopToSuperview(withMargin:10)
messageTextView.autoPinBottomToSuperview(withMargin:10)
messageTextView.autoPinLeadingToSuperview(withMargin:10)
messageTextView.autoPinTrailingToSuperview(withMargin:15)
}
private func createTextPreview() {
// Show nothing; URLs should only appear in the attachment approval view
// of the SAE and in this context the URL will be placed in the caption field.
}
private func createGenericPreview() {
var subviews = [UIView]()

View File

@ -164,10 +164,6 @@ public class MessageApprovalViewController: OWSViewController, UITextViewDelegat
nameLabel.setCompressionResistanceHorizontalLow()
nameLabel.autoPinTopToSuperview(withMargin: vMargin)
recipientRow.logFrameLater(withLabel: "recipientRow")
toLabel.logFrameLater(withLabel: "toLabel")
nameLabel.logFrameLater(withLabel: "nameLabel")
if let groupThread = self.thread as? TSGroupThread {
let groupName = (groupThread.name().count > 0
? groupThread.name()

View File

@ -131,6 +131,9 @@ typedef void (^SendMessageBlock)(SendCompletionBlock completion);
if (!(self.attachment.isUrl || self.attachment.isText)) {
return nil;
}
if (!self.attachment.isConvertibleToTextMessage) {
return nil;
}
NSData *data = self.attachment.data;
NSString *_Nullable messageText = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
DDLogVerbose(@"%@ messageTextForAttachment: %@", self.logTag, messageText);

View File

@ -138,6 +138,10 @@ public class SignalAttachment: NSObject {
return dataSource.isValidImage()
}
// This flag should be set for text attachments that can be sent as text messages.
@objc
public var isConvertibleToTextMessage = false
// Attachment types are identified using UTIs.
//
// See: https://developer.apple.com/library/content/documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html

View File

@ -546,6 +546,7 @@ public class ShareViewController: UINavigationController, ShareViewDelegate, SAE
let (promise, fulfill, reject) = Promise<(URL, String)>.pending()
var customFileName: String?
var isConvertibleToTextMessage = false
itemProvider.loadItem(forTypeIdentifier: srcUtiType, options: nil, completionHandler: {
(provider, error) in
@ -589,13 +590,18 @@ public class ShareViewController: UINavigationController, ShareViewDelegate, SAE
reject(writeError)
return
}
let fileUrl = URL(fileURLWithPath:tempFilePath)
isConvertibleToTextMessage = !itemProvider.registeredTypeIdentifiers.contains(kUTTypeFileURL as String)
if UTTypeConformsTo(srcUtiType as CFString, kUTTypeText) {
fulfill((fileUrl, srcUtiType))
} else {
fulfill((fileUrl, kUTTypeText as String))
}
} else if let url = provider as? URL {
isConvertibleToTextMessage = !itemProvider.registeredTypeIdentifiers.contains(kUTTypeFileURL as String)
fulfill((url, srcUtiType))
} else {
let unexpectedTypeError = ShareViewControllerError.assertionError(description: "unexpected item type: \(String(describing: provider))")
@ -661,6 +667,10 @@ public class ShareViewController: UINavigationController, ShareViewDelegate, SAE
}
let attachment = SignalAttachment.attachment(dataSource: dataSource, dataUTI: specificUTIType, imageQuality: .medium)
if isConvertibleToTextMessage {
Logger.info("\(self.logTag) isConvertibleToTextMessage")
attachment.isConvertibleToTextMessage = isConvertibleToTextMessage
}
return Promise(value: attachment)
}
}