Skip to content

Commit

Permalink
add new webxdc-notify api
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Nov 23, 2024
1 parent dee7b66 commit 80f2ed0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
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:

Check failure on line 159 in DcCore/DcCore/DC/events.swift

View workflow job for this annotation

GitHub Actions / build

cannot find 'DC_EVENT_INCOMING_WEBXDC_NOTIFY' in scope
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
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
20 changes: 20 additions & 0 deletions deltachat-ios/DC/DcMsg.swift
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,26 @@ 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 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
}
}
}

0 comments on commit 80f2ed0

Please sign in to comment.