mirror of https://github.com/oxen-io/session-ios
Don't auto-download attachments from untrusted contacts
parent
e170bfedf1
commit
960e500acd
@ -0,0 +1,50 @@
|
|||||||
|
|
||||||
|
final class MediaPlaceholderView : UIView {
|
||||||
|
private let viewItem: ConversationViewItem
|
||||||
|
private let textColor: UIColor
|
||||||
|
|
||||||
|
// MARK: Settings
|
||||||
|
private static let iconSize: CGFloat = 24
|
||||||
|
private static let iconImageViewSize: CGFloat = 40
|
||||||
|
|
||||||
|
// MARK: Lifecycle
|
||||||
|
init(viewItem: ConversationViewItem, textColor: UIColor) {
|
||||||
|
self.viewItem = viewItem
|
||||||
|
self.textColor = textColor
|
||||||
|
super.init(frame: CGRect.zero)
|
||||||
|
setUpViewHierarchy()
|
||||||
|
}
|
||||||
|
|
||||||
|
override init(frame: CGRect) {
|
||||||
|
preconditionFailure("Use init(viewItem:textColor:) instead.")
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder: NSCoder) {
|
||||||
|
preconditionFailure("Use init(viewItem:textColor:) instead.")
|
||||||
|
}
|
||||||
|
|
||||||
|
private func setUpViewHierarchy() {
|
||||||
|
// Image view
|
||||||
|
let iconSize = MediaPlaceholderView.iconSize
|
||||||
|
let icon = UIImage(named: "actionsheet_camera_roll_black")?.withTint(textColor)?.resizedImage(to: CGSize(width: iconSize, height: iconSize))
|
||||||
|
let imageView = UIImageView(image: icon)
|
||||||
|
imageView.contentMode = .center
|
||||||
|
let iconImageViewSize = MediaPlaceholderView.iconImageViewSize
|
||||||
|
imageView.set(.width, to: iconImageViewSize)
|
||||||
|
imageView.set(.height, to: iconImageViewSize)
|
||||||
|
// Body label
|
||||||
|
let titleLabel = UILabel()
|
||||||
|
titleLabel.lineBreakMode = .byTruncatingTail
|
||||||
|
titleLabel.text = "Download Media?"
|
||||||
|
titleLabel.textColor = textColor
|
||||||
|
titleLabel.font = .systemFont(ofSize: Values.mediumFontSize)
|
||||||
|
// Stack view
|
||||||
|
let stackView = UIStackView(arrangedSubviews: [ imageView, titleLabel ])
|
||||||
|
stackView.axis = .horizontal
|
||||||
|
stackView.alignment = .center
|
||||||
|
stackView.isLayoutMarginsRelativeArrangement = true
|
||||||
|
stackView.layoutMargins = UIEdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 12)
|
||||||
|
addSubview(stackView)
|
||||||
|
stackView.pin(to: self, withInset: Values.smallSpacing)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
|
||||||
|
final class DownloadAttachmentModal : Modal {
|
||||||
|
private let viewItem: ConversationViewItem
|
||||||
|
|
||||||
|
// MARK: Lifecycle
|
||||||
|
init(viewItem: ConversationViewItem) {
|
||||||
|
self.viewItem = viewItem
|
||||||
|
super.init(nibName: nil, bundle: nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
override init(nibName: String?, bundle: Bundle?) {
|
||||||
|
preconditionFailure("Use init(viewItem:) instead.")
|
||||||
|
}
|
||||||
|
|
||||||
|
required init?(coder: NSCoder) {
|
||||||
|
preconditionFailure("Use init(viewItem:) instead.")
|
||||||
|
}
|
||||||
|
|
||||||
|
override func populateContentView() {
|
||||||
|
guard let publicKey = (viewItem.interaction as? TSIncomingMessage)?.authorId else { return }
|
||||||
|
// Name
|
||||||
|
let name = Storage.shared.getContact(with: publicKey)?.displayName(for: .regular) ?? publicKey
|
||||||
|
// Title
|
||||||
|
let titleLabel = UILabel()
|
||||||
|
titleLabel.textColor = Colors.text
|
||||||
|
titleLabel.font = .boldSystemFont(ofSize: Values.largeFontSize)
|
||||||
|
titleLabel.text = "Trust \(name)?"
|
||||||
|
titleLabel.textAlignment = .center
|
||||||
|
// Message
|
||||||
|
let messageLabel = UILabel()
|
||||||
|
messageLabel.textColor = Colors.text
|
||||||
|
messageLabel.font = .systemFont(ofSize: Values.smallFontSize)
|
||||||
|
let message = "Are you sure you want to download media sent by \(name)?"
|
||||||
|
let attributedMessage = NSMutableAttributedString(string: message)
|
||||||
|
attributedMessage.addAttributes([ .font : UIFont.boldSystemFont(ofSize: Values.smallFontSize) ], range: (message as NSString).range(of: name))
|
||||||
|
messageLabel.attributedText = attributedMessage
|
||||||
|
messageLabel.numberOfLines = 0
|
||||||
|
messageLabel.lineBreakMode = .byWordWrapping
|
||||||
|
messageLabel.textAlignment = .center
|
||||||
|
// Unblock button
|
||||||
|
let unblockButton = UIButton()
|
||||||
|
unblockButton.set(.height, to: Values.mediumButtonHeight)
|
||||||
|
unblockButton.layer.cornerRadius = Modal.buttonCornerRadius
|
||||||
|
unblockButton.backgroundColor = Colors.buttonBackground
|
||||||
|
unblockButton.titleLabel!.font = .systemFont(ofSize: Values.smallFontSize)
|
||||||
|
unblockButton.setTitleColor(Colors.text, for: UIControl.State.normal)
|
||||||
|
unblockButton.setTitle("Download", for: UIControl.State.normal)
|
||||||
|
unblockButton.addTarget(self, action: #selector(trust), for: UIControl.Event.touchUpInside)
|
||||||
|
// Button stack view
|
||||||
|
let buttonStackView = UIStackView(arrangedSubviews: [ cancelButton, unblockButton ])
|
||||||
|
buttonStackView.axis = .horizontal
|
||||||
|
buttonStackView.spacing = Values.mediumSpacing
|
||||||
|
buttonStackView.distribution = .fillEqually
|
||||||
|
// Main stack view
|
||||||
|
let mainStackView = UIStackView(arrangedSubviews: [ titleLabel, messageLabel, buttonStackView ])
|
||||||
|
mainStackView.axis = .vertical
|
||||||
|
mainStackView.spacing = Values.largeSpacing
|
||||||
|
contentView.addSubview(mainStackView)
|
||||||
|
mainStackView.pin(.leading, to: .leading, of: contentView, withInset: Values.largeSpacing)
|
||||||
|
mainStackView.pin(.top, to: .top, of: contentView, withInset: Values.largeSpacing)
|
||||||
|
contentView.pin(.trailing, to: .trailing, of: mainStackView, withInset: Values.largeSpacing)
|
||||||
|
contentView.pin(.bottom, to: .bottom, of: mainStackView, withInset: Values.largeSpacing)
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Interaction
|
||||||
|
@objc private func trust() {
|
||||||
|
guard let message = viewItem.interaction as? TSIncomingMessage else { return }
|
||||||
|
let publicKey = message.authorId
|
||||||
|
let contact = Storage.shared.getContact(with: publicKey) ?? Contact(sessionID: publicKey)
|
||||||
|
contact.isTrusted = true
|
||||||
|
Storage.write(with: { transaction in
|
||||||
|
Storage.shared.setContact(contact, using: transaction)
|
||||||
|
message.touch(with: transaction)
|
||||||
|
}, completion: {
|
||||||
|
Storage.shared.resumeAttachmentDownloadJobsIfNeeded(for: message.uniqueId!)
|
||||||
|
})
|
||||||
|
presentingViewController?.dismiss(animated: true, completion: nil)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue