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

add new webxdc APIs #2412

Merged
merged 9 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
10 changes: 10 additions & 0 deletions DcCore/DcCore/DC/events.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public enum Event {
public static let incomingMessage = Notification.Name(rawValue: "incomingMessage")
public static let incomingMessageOnAnyAccount = Notification.Name(rawValue: "incomingMessageOnAnyAccount")
public static let incomingReaction = Notification.Name(rawValue: "incomingReaction")
public static let incomingWebxdcNotify = Notification.Name(rawValue: "incomingWebxdcNotify")
public static let messagesNoticed = Notification.Name(rawValue: "messagesNoticed")

// Chats
Expand Down Expand Up @@ -155,6 +156,15 @@ public class DcEventHandler {
"reaction": event.data2String
])

case DC_EVENT_INCOMING_WEBXDC_NOTIFY:
logger.info("📡[\(accountId)] incoming webxdc notify")
NotificationCenter.default.post(name: Event.incomingWebxdcNotify, object: nil, userInfo: [
"account_id": Int(accountId),
"contact_id": Int(data1),
"msg_id": Int(data2),
"text": event.data2String
])

case DC_EVENT_CONTACTS_CHANGED:
if accountId != dcAccounts.getSelected().id {
return
Expand Down
16 changes: 16 additions & 0 deletions DcNotificationService/NotificationService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ class NotificationService: UNNotificationServiceExtension {
reactionCount += 1
}
}
} else if event.id == DC_EVENT_INCOMING_WEBXDC_NOTIFY {
let dcContext = dcAccounts.get(id: event.accountId)
if !dcContext.isMuted() {
let msg = dcContext.getMessage(id: event.data2Int)
let chat = dcContext.getChat(chatId: msg.chatId)
if !chat.isMuted {
bestAttemptContent.title = chat.name
bestAttemptContent.body = msg.getWebxdcAppName() + ": " + event.data2String
bestAttemptContent.userInfo["account_id"] = dcContext.id
bestAttemptContent.userInfo["chat_id"] = chat.id
bestAttemptContent.userInfo["message_id"] = msg.id

uniqueChats["\(dcContext.id)-\(chat.id)"] = bestAttemptContent.title
messageCount += 1
}
}
}
}

Expand Down
6 changes: 3 additions & 3 deletions deltachat-ios/Chat/ChatViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -833,7 +833,7 @@ class ChatViewController: UITableViewController, UITableViewDropDelegate {
showWebxdcViewFor(message: message)
case (_, DC_INFO_WEBXDC_INFO_MESSAGE):
if let parent = message.parent {
scrollToMessage(msgId: parent.id)
showWebxdcViewFor(message: parent, href: message.getWebxdcHref())
}
case (_, DC_INFO_PROTECTION_ENABLED):
showProtectionEnabledDialog()
Expand Down Expand Up @@ -2106,8 +2106,8 @@ extension ChatViewController: MCEmojiPickerDelegate {

extension ChatViewController {

func showWebxdcViewFor(message: DcMsg) {
let webxdcViewController = WebxdcViewController(dcContext: dcContext, messageId: message.id)
func showWebxdcViewFor(message: DcMsg, href: String? = nil) {
let webxdcViewController = WebxdcViewController(dcContext: dcContext, messageId: message.id, href: href)
navigationController?.pushViewController(webxdcViewController, animated: true)
}

Expand Down
22 changes: 18 additions & 4 deletions deltachat-ios/Controller/WebxdcViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ class WebxdcViewController: WebViewViewController {
let INTERNALSCHEMA = "webxdc"

var messageId: Int
var href: String?
var webxdcName: String = ""
var sourceCodeUrl: String?
var selfAddr: String = ""
private var allowInternet: Bool = false

private var shortcutManager: ShortcutManager?
Expand Down Expand Up @@ -57,7 +59,7 @@ class WebxdcViewController: WebViewViewController {
"""

lazy var webxdcbridge: String = {
let addr = dcContext.addr?
let addr = selfAddr
.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
let displayname = (dcContext.displayname ?? dcContext.addr)?
.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)
Expand Down Expand Up @@ -272,8 +274,9 @@ class WebxdcViewController: WebViewViewController {
}


init(dcContext: DcContext, messageId: Int) {
init(dcContext: DcContext, messageId: Int, href: String? = nil) {
self.messageId = messageId
self.href = href
self.shortcutManager = ShortcutManager(dcContext: dcContext, messageId: messageId)
super.init(dcContext: dcContext)

Expand All @@ -284,6 +287,7 @@ class WebxdcViewController: WebViewViewController {
object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(WebxdcViewController.handleWebxdcStatusUpdate(_:)), name: Event.webxdcStatusUpdate, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(WebxdcViewController.handleWebxdcRealtimeDataReceived(_:)), name: Event.webxdcRealtimeDataReceived, object: nil)
refreshWebxdcInfo()
}

required init?(coder: NSCoder) {
Expand All @@ -298,7 +302,6 @@ class WebxdcViewController: WebViewViewController {
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.rightBarButtonItem = moreButton
refreshWebxdcInfo()
}

override func willMove(toParent parent: UIViewController?) {
Expand All @@ -321,6 +324,10 @@ class WebxdcViewController: WebViewViewController {
!sourceCode.isEmpty {
sourceCodeUrl = sourceCode
}

if let addr = dict["self_addr"] as? String {
selfAddr = addr
}
}

// MARK: - Notifications
Expand Down Expand Up @@ -417,7 +424,14 @@ class WebxdcViewController: WebViewViewController {
private func loadHtml() {
DispatchQueue.global(qos: .userInitiated).async { [weak self] in
guard let self else { return }
let url = URL(string: "\(self.INTERNALSCHEMA)://acc\(self.dcContext.id)-msg\(self.messageId).localhost/index.html")

let file = if let href {
href
} else {
"index.html"
}
r10s marked this conversation as resolved.
Show resolved Hide resolved

let url = URL(string: "\(self.INTERNALSCHEMA)://acc\(self.dcContext.id)-msg\(self.messageId).localhost/" + file)
let urlRequest = URLRequest(url: url!)
DispatchQueue.main.async {
self.webView.load(urlRequest)
Expand Down
9 changes: 0 additions & 9 deletions deltachat-ios/DC/DcMsg+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,6 @@ extension DcMsg {
return "\(size) \(units[digitGroups])"
}

public func getWebxdcInfoDict() -> [String: AnyObject] {
let jsonString = self.getWebxdcInfoJson()
if let data: Data = jsonString.data(using: .utf8),
let infoDict = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: AnyObject] {
return infoDict
}
return [:]
}

public func getWebxdcPreviewImage() -> UIImage? {
let dict = self.getWebxdcInfoDict()
if let iconfilePath = dict["icon"] as? String {
Expand Down
27 changes: 27 additions & 0 deletions deltachat-ios/DC/DcMsg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,33 @@ public class DcMsg {
return swiftString
}

public func getWebxdcInfoDict() -> [String: AnyObject] {
let jsonString = self.getWebxdcInfoJson()
if let data: Data = jsonString.data(using: .utf8),
let infoDict = (try? JSONSerialization.jsonObject(with: data, options: [])) as? [String: AnyObject] {
return infoDict
}
return [:]
}

// returns webxdc app name for an webxdc-info-messages or webxdc-instances
public func getWebxdcAppName() -> String {
let msg = if self.isInfo, let parent = self.parent {
parent
} else {
self
}
let dict = msg.getWebxdcInfoDict()
return dict["name"] as? String ?? "ErrName"
}

public func getWebxdcHref() -> String? {
guard let cString = dc_msg_get_webxdc_href(messagePointer) else { return nil }
let swiftString = String(cString: cString)
dc_str_unref(cString)
return swiftString
}

public var messageHeight: CGFloat {
return CGFloat(dc_msg_get_height(messagePointer))
}
Expand Down
29 changes: 29 additions & 0 deletions deltachat-ios/Helper/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public class NotificationManager {

NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleIncomingMessageOnAnyAccount(_:)), name: Event.incomingMessageOnAnyAccount, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleIncomingReaction(_:)), name: Event.incomingReaction, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleIncomingWebxdcNotify(_:)), name: Event.incomingWebxdcNotify, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(NotificationManager.handleMessagesNoticed(_:)), name: Event.messagesNoticed, object: nil)
}

Expand Down Expand Up @@ -155,4 +156,32 @@ public class NotificationManager {
UIApplication.shared.endBackgroundTask(backgroundTask) // this line must be reached to balance call to `beginBackgroundTask` above
}
}

@objc private func handleIncomingWebxdcNotify(_ notification: Notification) {
let backgroundTask = UIApplication.shared.beginBackgroundTask {
logger.info("incoming-webxdc-notify-task will end soon")
}

DispatchQueue.global().async { [weak self] in
guard let self, let ui = notification.userInfo else { return }
let eventContext = dcAccounts.get(id: ui["account_id"] as? Int ?? 0)
if !eventContext.isMuted() {
let msg = eventContext.getMessage(id: ui["msg_id"] as? Int ?? 0)
let chat = eventContext.getChat(chatId: msg.chatId)
if !chat.isMuted {
let content = UNMutableNotificationContent()
content.title = chat.name
content.body = msg.getWebxdcAppName() + ": " + (ui["text"] as? String ?? "")
content.userInfo["account_id"] = eventContext.id
content.userInfo["chat_id"] = chat.id
content.userInfo["message_id"] = msg.id

let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: nil)
UNUserNotificationCenter.current().add(request, withCompletionHandler: nil)
}
}

UIApplication.shared.endBackgroundTask(backgroundTask) // this line must be reached to balance call to `beginBackgroundTask` above
}
}
}
Loading