Parse markdown and html from push notifications (#1688)

* Extract notification content building outside of the NotificationItemProxy. First step in a bigger refactoring of the NSE

* Import the attributed string buider into the NSE, sort `sources` imports

* Extract message event string building into the RoomMessageEventStringBuilder and reuse it building content for push notifications

* Cleanup

* Add changelog
This commit is contained in:
Stefan Ceriu
2023-09-13 12:30:41 +03:00
committed by GitHub
parent add148b77d
commit 97aa930852
15 changed files with 416 additions and 393 deletions
@@ -209,3 +209,9 @@ class NotificationSettingsScreenViewModel: NotificationSettingsScreenViewModelTy
state.applyingChange = false
}
}
extension UNUserNotificationCenter {
func authorizationStatus() async -> UNAuthorizationStatus {
await notificationSettings().authorizationStatus
}
}
@@ -398,8 +398,10 @@ class ClientProxy: ClientProxyProtocol {
.withCrossProcessLock(appIdentifier: "MainApp")
.finish()
let roomListService = syncService.roomListService()
let eventStringBuilder = RoomEventStringBuilder(stateEventStringBuilder: RoomStateEventStringBuilder(userID: userID))
let roomMessageEventStringBuilder = RoomMessageEventStringBuilder(attributedStringBuilder: AttributedStringBuilder(permalinkBaseURL: .homeDirectory))
let eventStringBuilder = RoomEventStringBuilder(stateEventStringBuilder: RoomStateEventStringBuilder(userID: userID),
messageEventStringBuilder: roomMessageEventStringBuilder)
roomSummaryProvider = RoomSummaryProvider(roomListService: roomListService,
eventStringBuilder: eventStringBuilder,
name: "AllRooms",
@@ -1,24 +0,0 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import UserNotifications
extension UNUserNotificationCenter {
func authorizationStatus() async -> UNAuthorizationStatus {
await notificationSettings().authorizationStatus
}
}
@@ -14,64 +14,9 @@
// limitations under the License.
//
import CryptoKit
import Foundation
import UserNotifications
import MatrixRustSDK
protocol NotificationItemProxyProtocol {
var event: NotificationEvent? { get }
var eventID: String { get }
var senderID: String { get }
var roomID: String { get }
var receiverID: String { get }
var senderDisplayName: String? { get }
var senderAvatarMediaSource: MediaSourceProxy? { get }
var roomDisplayName: String { get }
var roomCanonicalAlias: String? { get }
var roomAvatarMediaSource: MediaSourceProxy? { get }
var roomJoinedMembers: Int { get }
var isRoomDirect: Bool { get }
var isNoisy: Bool { get }
}
extension NotificationItemProxyProtocol {
var isEncrypted: Bool {
switch event {
case .none, .invite:
return false
case .timeline(let event):
switch try? event.eventType() {
case .messageLike(let content):
switch content {
case .roomEncrypted:
return true
default:
return false
}
default:
return false
}
}
}
var isDM: Bool {
isRoomDirect && roomJoinedMembers <= 2
}
}
import UserNotifications
struct NotificationItemProxy: NotificationItemProxyProtocol {
let notificationItem: NotificationItem
@@ -170,218 +115,3 @@ struct EmptyNotificationItemProxy: NotificationItemProxyProtocol {
var roomJoinedMembers: Int { 0 }
}
extension NotificationItemProxyProtocol {
var baseMutableContent: UNMutableNotificationContent {
let notification = UNMutableNotificationContent()
notification.receiverID = receiverID
notification.roomID = roomID
notification.eventID = eventID
notification.sound = isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil
// So that the UI groups notification that are received for the same room but also for the same user
// Removing the @ fixes an iOS bug where the notification crashes if the mute button is tapped
notification.threadIdentifier = "\(receiverID)\(roomID)".replacingOccurrences(of: "@", with: "")
return notification
}
var hasMedia: Bool {
if (isDM && senderAvatarMediaSource != nil) ||
(!isDM && roomAvatarMediaSource != nil) {
return true
}
switch event {
case .invite, .none:
return false
case .timeline(let event):
switch try? event.eventType() {
case .state, .none:
return false
case let .messageLike(content):
switch content {
case let .roomMessage(messageType):
switch messageType {
case .image, .video, .audio:
return true
default:
return false
}
default:
return false
}
}
}
}
var icon: NotificationIcon {
if isDM {
return NotificationIcon(mediaSource: senderAvatarMediaSource, groupInfo: nil)
} else {
return NotificationIcon(mediaSource: roomAvatarMediaSource,
groupInfo: .init(name: roomDisplayName, id: roomID))
}
}
/// Process the receiver item proxy
/// - Parameters:
/// - receiverId: identifier of the user that has received the notification
/// - roomId: Room identifier
/// - mediaProvider: Media provider to process also media. May be passed nil to ignore media operations.
/// - Returns: A notification content object if the notification should be displayed. Otherwise nil.
func process(mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
switch event {
case .none:
return processEmpty()
case .invite:
return try await processInvited(mediaProvider: mediaProvider)
case .timeline(let event):
switch try? event.eventType() {
case let .messageLike(content):
switch content {
case .roomMessage(messageType: let messageType):
return try await processRoomMessage(messageType: messageType, mediaProvider: mediaProvider)
default:
return processEmpty()
}
default:
return processEmpty()
}
}
}
// MARK: - Private
private func processInvited(mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
var notification = baseMutableContent
notification.categoryIdentifier = NotificationConstants.Category.invite
let body: String
if !isDM {
body = L10n.notificationRoomInviteBody
} else {
body = L10n.notificationInviteBody
}
notification = try await notification.addSenderIcon(using: mediaProvider,
senderID: senderID,
senderName: senderDisplayName ?? roomDisplayName,
icon: icon)
notification.body = body
return notification
}
private func processRoomMessage(messageType: MessageType, mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
switch messageType {
case .emote(content: let content):
return try await processEmote(content: content, mediaProvider: mediaProvider)
case .image(content: let content):
return try await processImage(content: content, mediaProvider: mediaProvider)
case .audio(content: let content):
return try await processAudio(content: content, mediaProvider: mediaProvider)
case .video(content: let content):
return try await processVideo(content: content, mediaProvider: mediaProvider)
case .file(content: let content):
return try await processFile(content: content, mediaProvider: mediaProvider)
case .notice(content: let content):
return try await processNotice(content: content, mediaProvider: mediaProvider)
case .text(content: let content):
return try await processText(content: content, mediaProvider: mediaProvider)
case .location:
return processEmpty()
}
}
private func processEmpty() -> UNMutableNotificationContent {
let notification = baseMutableContent
notification.title = InfoPlistReader(bundle: .app).bundleDisplayName
notification.body = L10n.notification
notification.categoryIdentifier = NotificationConstants.Category.message
return notification
}
private func processCommon(mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
var notification = baseMutableContent
notification.title = senderDisplayName ?? roomDisplayName
if notification.title != roomDisplayName {
notification.subtitle = roomDisplayName
}
notification.categoryIdentifier = NotificationConstants.Category.message
notification = try await notification.addSenderIcon(using: mediaProvider,
senderID: senderID,
senderName: senderDisplayName ?? roomDisplayName,
icon: icon)
return notification
}
// MARK: Message Types
private func processText(content: TextMessageContent,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(mediaProvider: mediaProvider)
notification.body = content.body
return notification
}
private func processImage(content: ImageMessageContent,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
var notification = try await processCommon(mediaProvider: mediaProvider)
notification.body = "📷 " + content.body
notification = await notification.addMediaAttachment(using: mediaProvider,
mediaSource: .init(source: content.source,
mimeType: content.info?.mimetype))
return notification
}
private func processVideo(content: VideoMessageContent,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
var notification = try await processCommon(mediaProvider: mediaProvider)
notification.body = "📹 " + content.body
notification = await notification.addMediaAttachment(using: mediaProvider,
mediaSource: .init(source: content.source,
mimeType: content.info?.mimetype))
return notification
}
private func processFile(content: FileMessageContent,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(mediaProvider: mediaProvider)
notification.body = "📄 " + content.body
return notification
}
private func processNotice(content: NoticeMessageContent,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(mediaProvider: mediaProvider)
notification.body = "❕ " + content.body
return notification
}
private func processEmote(content: EmoteMessageContent,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
let notification = try await processCommon(mediaProvider: mediaProvider)
notification.body = L10n.commonEmote(senderDisplayName ?? roomDisplayName, content.body)
return notification
}
private func processAudio(content: AudioMessageContent,
mediaProvider: MediaProviderProtocol?) async throws -> UNMutableNotificationContent {
var notification = try await processCommon(mediaProvider: mediaProvider)
notification.body = "🔊 " + content.body
notification = await notification.addMediaAttachment(using: mediaProvider,
mediaSource: .init(source: content.source,
mimeType: content.info?.mimetype))
return notification
}
}
@@ -0,0 +1,81 @@
//
// Copyright 2022 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import MatrixRustSDK
import UserNotifications
protocol NotificationItemProxyProtocol {
var event: NotificationEvent? { get }
var eventID: String { get }
var senderID: String { get }
var roomID: String { get }
var receiverID: String { get }
var senderDisplayName: String? { get }
var senderAvatarMediaSource: MediaSourceProxy? { get }
var roomDisplayName: String { get }
var roomCanonicalAlias: String? { get }
var roomAvatarMediaSource: MediaSourceProxy? { get }
var roomJoinedMembers: Int { get }
var isRoomDirect: Bool { get }
var isNoisy: Bool { get }
}
extension NotificationItemProxyProtocol {
var isDM: Bool {
isRoomDirect && roomJoinedMembers <= 2
}
var hasMedia: Bool {
if (isDM && senderAvatarMediaSource != nil) ||
(!isDM && roomAvatarMediaSource != nil) {
return true
}
switch event {
case .invite, .none:
return false
case .timeline(let event):
switch try? event.eventType() {
case .state, .none:
return false
case let .messageLike(content):
switch content {
case let .roomMessage(messageType):
switch messageType {
case .image, .video, .audio:
return true
default:
return false
}
default:
return false
}
}
}
}
}
@@ -19,70 +19,38 @@ import MatrixRustSDK
struct RoomEventStringBuilder {
private let stateEventStringBuilder: RoomStateEventStringBuilder
private let messageEventStringBuilder: RoomMessageEventStringBuilder
init(stateEventStringBuilder: RoomStateEventStringBuilder) {
init(stateEventStringBuilder: RoomStateEventStringBuilder, messageEventStringBuilder: RoomMessageEventStringBuilder) {
self.stateEventStringBuilder = stateEventStringBuilder
self.messageEventStringBuilder = messageEventStringBuilder
}
func buildAttributedString(for eventItemProxy: EventTimelineItemProxy) -> AttributedString? {
let sender = eventItemProxy.sender
let isOutgoing = eventItemProxy.isOwn
let senderDisplayName = sender.displayName ?? sender.id
switch eventItemProxy.content.kind() {
case .unableToDecrypt:
return prefix(L10n.commonDecryptionError, with: sender)
return prefix(L10n.commonDecryptionError, with: senderDisplayName)
case .redactedMessage:
return prefix(L10n.commonMessageRemoved, with: sender)
return prefix(L10n.commonMessageRemoved, with: senderDisplayName)
case .sticker:
return prefix(L10n.commonSticker, with: sender)
return prefix(L10n.commonSticker, with: senderDisplayName)
case .failedToParseMessageLike, .failedToParseState:
return prefix(L10n.commonUnsupportedEvent, with: sender)
return prefix(L10n.commonUnsupportedEvent, with: senderDisplayName)
case .message:
guard let messageContent = eventItemProxy.content.asMessage() else {
fatalError("Invalid message timeline item: \(eventItemProxy)")
}
guard let messageType = messageContent.msgtype() else {
return prefix(messageContent.body(), with: sender)
return prefix(messageContent.body(), with: senderDisplayName)
}
let message: String
switch messageType {
// Message types that don't need a prefix.
case .emote(content: let content):
let senderDisplayName = sender.displayName ?? sender.id
if let attributedMessage = attributedMessageFrom(formattedBody: content.formatted) {
return AttributedString(L10n.commonEmote(senderDisplayName, String(attributedMessage.characters)))
} else {
return AttributedString(L10n.commonEmote(senderDisplayName, content.body))
}
// Message types that should be prefixed with the sender's name.
case .audio:
message = L10n.commonAudio
case .image:
message = L10n.commonImage
case .video:
message = L10n.commonVideo
case .file:
message = L10n.commonFile
case .location:
message = L10n.commonSharedLocation
case .notice(content: let content):
if let attributedMessage = attributedMessageFrom(formattedBody: content.formatted) {
message = String(attributedMessage.characters)
} else {
message = content.body
}
case .text(content: let content):
if let attributedMessage = attributedMessageFrom(formattedBody: content.formatted) {
message = String(attributedMessage.characters)
} else {
message = content.body
}
}
return prefix(message, with: sender)
return messageEventStringBuilder.buildAttributedString(for: messageType, senderDisplayName: senderDisplayName, prefixWithSenderName: true)
case .state(let stateKey, let state):
return stateEventStringBuilder
.buildString(for: state, stateKey: stateKey, sender: sender, isOutgoing: isOutgoing)
@@ -106,21 +74,13 @@ struct RoomEventStringBuilder {
}
}
private func prefix(_ eventSummary: String, with sender: TimelineItemSender) -> AttributedString {
private func prefix(_ eventSummary: String, with senderDisplayName: String) -> AttributedString {
let attributedEventSummary = AttributedString(eventSummary.trimmingCharacters(in: .whitespacesAndNewlines))
if let senderDisplayName = sender.displayName {
var attributedSenderDisplayName = AttributedString(senderDisplayName)
attributedSenderDisplayName.bold()
// Don't include the message body in the markdown otherwise it makes tappable links.
return attributedSenderDisplayName + ": " + attributedEventSummary
} else {
return attributedEventSummary
}
}
private func attributedMessageFrom(formattedBody: FormattedBody?) -> AttributedString? {
formattedBody.flatMap { AttributedStringBuilder(permalinkBaseURL: .homeDirectory).fromHTML($0.body) }
var attributedSenderDisplayName = AttributedString(senderDisplayName)
attributedSenderDisplayName.bold()
// Don't include the message body in the markdown otherwise it makes tappable links.
return attributedSenderDisplayName + ": " + attributedEventSummary
}
}
@@ -0,0 +1,78 @@
//
// Copyright 2023 New Vector Ltd
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
import MatrixRustSDK
struct RoomMessageEventStringBuilder {
let attributedStringBuilder: AttributedStringBuilderProtocol
func buildAttributedString(for messageType: MessageType, senderDisplayName: String, prefixWithSenderName: Bool) -> AttributedString {
let message: String
switch messageType {
// Message types that don't need a prefix.
case .emote(content: let content):
if let attributedMessage = attributedMessageFrom(formattedBody: content.formatted) {
return AttributedString(L10n.commonEmote(senderDisplayName, String(attributedMessage.characters)))
} else {
return AttributedString(L10n.commonEmote(senderDisplayName, content.body))
}
// Message types that should be prefixed with the sender's name.
case .audio:
message = L10n.commonAudio
case .image:
message = L10n.commonImage
case .video:
message = L10n.commonVideo
case .file:
message = L10n.commonFile
case .location:
message = L10n.commonSharedLocation
case .notice(content: let content):
if let attributedMessage = attributedMessageFrom(formattedBody: content.formatted) {
message = String(attributedMessage.characters)
} else {
message = content.body
}
case .text(content: let content):
if let attributedMessage = attributedMessageFrom(formattedBody: content.formatted) {
message = String(attributedMessage.characters)
} else {
message = content.body
}
}
if prefixWithSenderName {
return prefix(message, with: senderDisplayName)
} else {
return AttributedString(message)
}
}
private func prefix(_ eventSummary: String, with senderDisplayName: String) -> AttributedString {
let attributedEventSummary = AttributedString(eventSummary.trimmingCharacters(in: .whitespacesAndNewlines))
var attributedSenderDisplayName = AttributedString(senderDisplayName)
attributedSenderDisplayName.bold()
// Don't include the message body in the markdown otherwise it makes tappable links.
return attributedSenderDisplayName + ": " + attributedEventSummary
}
private func attributedMessageFrom(formattedBody: FormattedBody?) -> AttributedString? {
formattedBody.flatMap { attributedStringBuilder.fromHTML($0.body) }
}
}