Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open i.delta.chat-links in App (#2134) #2295

Merged
merged 5 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2411,6 +2411,10 @@ extension ChatViewController: BaseMessageCellDelegate {
if Utils.isEmail(url: url) {
let email = Utils.getEmailFrom(url)
self.askToChatWith(email: email)
} else if url.isDeltaChatInvitation,
let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let appCoordinator = appDelegate.appCoordinator {
appCoordinator.handleDeltaChatInvitation(url: url, from: self)
} else {
UIApplication.shared.open(url)
}
Expand Down
4 changes: 4 additions & 0 deletions deltachat-ios/Controller/ContactDetailViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,10 @@ extension ContactDetailViewController: MultilineLabelCellDelegate {
}))
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
present(alert, animated: true, completion: nil)
} else if url.isDeltaChatInvitation,
let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let appCoordinator = appDelegate.appCoordinator {
appCoordinator.handleDeltaChatInvitation(url: url, from: self)
} else {
UIApplication.shared.open(url)
}
Expand Down
174 changes: 2 additions & 172 deletions deltachat-ios/Controller/QrPageController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,6 @@ class QrPageController: UIPageViewController {
}
}

// MARK: - coordinator
private func showChats() {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.appCoordinator.showTab(index: appDelegate.appCoordinator.chatsTab)
}
}

private func showChat(chatId: Int) {
if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
appDelegate.appCoordinator.showChat(chatId: chatId, animated: false, clearViewControllerStack: true)
}
}
}

// MARK: - UIPageViewControllerDataSource, UIPageViewControllerDelegate
Expand Down Expand Up @@ -222,165 +210,7 @@ extension QrPageController: UIPageViewControllerDataSource, UIPageViewController
extension QrPageController: QrCodeReaderDelegate {

func handleQrCode(_ code: String) {
self.showChats()
let qrParsed: DcLot = self.dcContext.checkQR(qrCode: code)
let state = Int32(qrParsed.state)
switch state {
case DC_QR_ASK_VERIFYCONTACT:
let nameAndAddress = dcContext.getContact(id: qrParsed.id).nameNAddr
joinSecureJoin(alertMessage: String.localizedStringWithFormat(String.localized("ask_start_chat_with"), nameAndAddress), code: code)

case DC_QR_ASK_VERIFYGROUP:
let groupName = qrParsed.text1 ?? "ErrGroupName"
joinSecureJoin(alertMessage: String.localizedStringWithFormat(String.localized("qrscan_ask_join_group"), groupName), code: code)

case DC_QR_FPR_WITHOUT_ADDR:
let msg = String.localized("qrscan_no_addr_found") + "\n\n" +
String.localized("qrscan_fingerprint_label") + ":\n" + (qrParsed.text1 ?? "")
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
present(alert, animated: true, completion: nil)

case DC_QR_FPR_MISMATCH:
let nameAndAddress = dcContext.getContact(id: qrParsed.id).nameNAddr
let msg = String.localizedStringWithFormat(String.localized("qrscan_fingerprint_mismatch"), nameAndAddress)
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
present(alert, animated: true, completion: nil)

case DC_QR_ADDR, DC_QR_FPR_OK:
let nameAndAddress = dcContext.getContact(id: qrParsed.id).nameNAddr
let msg = String.localizedStringWithFormat(String.localized(state==DC_QR_ADDR ? "ask_start_chat_with" : "qrshow_x_verified"), nameAndAddress)
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
let chatId = self.dcContext.createChatByContactId(contactId: qrParsed.id)
self.showChat(chatId: chatId)
}))
present(alert, animated: true, completion: nil)

case DC_QR_TEXT:
let msg = String.localizedStringWithFormat(String.localized("qrscan_contains_text"), qrParsed.text1 ?? "")
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
present(alert, animated: true, completion: nil)

case DC_QR_URL:
let url = qrParsed.text1 ?? ""
let msg = String.localizedStringWithFormat(String.localized("qrscan_contains_url"), url)
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
alert.addAction(UIAlertAction(title: String.localized("open"), style: .default, handler: { _ in
if let url = URL(string: url) {
UIApplication.shared.open(url)
}
}))
present(alert, animated: true, completion: nil)

case DC_QR_ACCOUNT, DC_QR_LOGIN:
let msg = String.localizedStringWithFormat(String.localized(state == DC_QR_ACCOUNT ? "qraccount_ask_create_and_login_another" : "qrlogin_ask_login_another"), qrParsed.text1 ?? "")
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
_ = self.dcAccounts.add()
appDelegate.reloadDcContext(accountCode: code)
}))
present(alert, animated: true, completion: nil)

case DC_QR_BACKUP, DC_QR_BACKUP2:
// alert is shown in WelcomeViewController
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
_ = dcAccounts.add()
appDelegate.reloadDcContext(accountCode: code)

case DC_QR_WEBRTC_INSTANCE:
guard let domain = qrParsed.text1 else { return }
let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("videochat_instance_from_qr"), domain),
message: nil,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { [weak self] _ in
guard let self else { return }
let success = self.dcContext.setConfigFromQR(qrCode: code)
if !success {
logger.warning("Could not set webrtc instance from QR code.")
// TODO: alert?!
}
}))
present(alert, animated: true)

case DC_QR_WITHDRAW_VERIFYCONTACT:
let alert = UIAlertController(title: String.localized("withdraw_verifycontact_explain"),
message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("withdraw_qr_code"), style: .destructive, handler: { [weak self] _ in
_ = self?.dcContext.setConfigFromQR(qrCode: code)
}))
present(alert, animated: true)

case DC_QR_REVIVE_VERIFYCONTACT:
let alert = UIAlertController(title: String.localized("revive_verifycontact_explain"),
message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("revive_qr_code"), style: .default, handler: { [weak self] _ in
_ = self?.dcContext.setConfigFromQR(qrCode: code)
}))
present(alert, animated: true)

case DC_QR_WITHDRAW_VERIFYGROUP:
guard let groupName = qrParsed.text1 else { return }
let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("withdraw_verifygroup_explain"), groupName),
message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("withdraw_qr_code"), style: .destructive, handler: { [weak self] _ in
_ = self?.dcContext.setConfigFromQR(qrCode: code)
}))
present(alert, animated: true)

case DC_QR_REVIVE_VERIFYGROUP:
guard let groupName = qrParsed.text1 else { return }
let alert = UIAlertController(title: String.localizedStringWithFormat(String.localized("revive_verifygroup_explain"), groupName),
message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default))
alert.addAction(UIAlertAction(title: String.localized("revive_qr_code"), style: .default, handler: { [weak self] _ in
_ = self?.dcContext.setConfigFromQR(qrCode: code)
}))
present(alert, animated: true)

default:
var msg = String.localizedStringWithFormat(String.localized("qrscan_contains_text"), code)
if state == DC_QR_ERROR {
if let errorMsg = qrParsed.text1 {
msg = errorMsg + "\n\n" + msg
}
}
let alert = UIAlertController(title: msg, message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: nil))
present(alert, animated: true, completion: nil)
}
}

private func joinSecureJoin(alertMessage: String, code: String) {
let alert = UIAlertController(title: alertMessage,
message: nil,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .default, handler: nil))
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
let chatId = self.dcContext.joinSecurejoin(qrCode: code)
if chatId != 0 {
self.showChat(chatId: chatId)
} else {
self.showErrorAlert(error: self.dcContext.lastErrorString)
}
}))
present(alert, animated: true, completion: nil)
}

private func showErrorAlert(error: String) {
let alert = UIAlertController(title: String.localized("error"), message: error, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: String.localized("ok"), style: .default, handler: { _ in
alert.dismiss(animated: true, completion: nil)
}))
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else { return }
appDelegate.appCoordinator.coordinate(qrCode: code, from: self)
}
}
Loading
Loading