diff --git a/ElementX/Sources/Other/Extensions/UNNotificationContent.swift b/ElementX/Sources/Other/Extensions/UNNotificationContent.swift index ea84ee48f..a65139092 100644 --- a/ElementX/Sources/Other/Extensions/UNNotificationContent.swift +++ b/ElementX/Sources/Other/Extensions/UNNotificationContent.swift @@ -18,6 +18,27 @@ import Foundation import Intents import UserNotifications +enum NotificationIconType { + case sender(mediaSource: MediaSourceProxy?) + case group(mediaSource: MediaSourceProxy?, groupName: String) + + var mediaSource: MediaSourceProxy? { + switch self { + case .group(let mediaSource, _), .sender(let mediaSource): + return mediaSource + } + } + + var isSenderIcon: Bool { + switch self { + case .sender: + return true + case .group: + return false + } + } +} + extension UNNotificationContent { @objc var receiverID: String? { userInfo[NotificationConstants.UserInfoKey.receiverIdentifier] as? String @@ -98,16 +119,12 @@ extension UNMutableNotificationContent { return self } - // swiftlint:disable:next function_parameter_count func addSenderIcon(using mediaProvider: MediaProviderProtocol?, - senderId: String, - receiverId: String, + senderID: String, senderName: String, - groupName: String?, - mediaSource: MediaSourceProxy?, - roomId: String) async throws -> UNMutableNotificationContent { + iconType: NotificationIconType) async throws -> UNMutableNotificationContent { var image: INImage? - if let mediaSource { + if let mediaSource = iconType.mediaSource { switch await mediaProvider?.loadFileFromSource(mediaSource) { case .success(let mediaFile): image = try INImage(imageData: Data(contentsOf: mediaFile.url)) @@ -118,19 +135,19 @@ extension UNMutableNotificationContent { } } - let senderHandle = INPersonHandle(value: senderId, type: .unknown) + let senderHandle = INPersonHandle(value: senderID, type: .unknown) let sender = INPerson(personHandle: senderHandle, nameComponents: nil, displayName: senderName, - image: image, + image: iconType.isSenderIcon ? image : nil, contactIdentifier: nil, customIdentifier: nil) // These are required to show the group name as subtitle var speakableGroupName: INSpeakableString? var recipients: [INPerson]? - if let groupName { - let meHandle = INPersonHandle(value: receiverId, type: .unknown) + if case let .group(_, groupName) = iconType { + let meHandle = INPersonHandle(value: receiverID, type: .unknown) let me = INPerson(personHandle: meHandle, nameComponents: nil, displayName: nil, image: nil, contactIdentifier: nil, customIdentifier: nil, isMe: true) speakableGroupName = INSpeakableString(spokenPhrase: groupName) recipients = [sender, me] @@ -140,11 +157,13 @@ extension UNMutableNotificationContent { outgoingMessageType: .outgoingMessageText, content: nil, speakableGroupName: speakableGroupName, - conversationIdentifier: roomId, + conversationIdentifier: roomID, serviceName: nil, sender: sender, attachments: nil) - intent.setImage(image, forParameterNamed: \.speakableGroupName) + if speakableGroupName != nil, let image { + intent.setImage(image, forParameterNamed: \.speakableGroupName) + } // Use the intent to initialize the interaction. let interaction = INInteraction(intent: intent, response: nil) diff --git a/ElementX/Sources/Services/Notification/Proxy/NotificationItemProxy.swift b/ElementX/Sources/Services/Notification/Proxy/NotificationItemProxy.swift index c081b3faf..57bb20d31 100644 --- a/ElementX/Sources/Services/Notification/Proxy/NotificationItemProxy.swift +++ b/ElementX/Sources/Services/Notification/Proxy/NotificationItemProxy.swift @@ -232,22 +232,17 @@ extension NotificationItemProxyProtocol { notification.sound = isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil let senderName = senderDisplayName ?? roomDisplayName - var groupName: String? - var mediaSource: MediaSourceProxy? + let iconType: NotificationIconType if !isDirect { - groupName = senderName != roomDisplayName ? roomDisplayName : nil - mediaSource = roomAvatarMediaSource + iconType = .group(mediaSource: roomAvatarMediaSource, groupName: roomDisplayName) } else { - mediaSource = senderAvatarMediaSource + iconType = .sender(mediaSource: senderAvatarMediaSource) } notification = try await notification.addSenderIcon(using: mediaProvider, - senderId: event.senderID, - receiverId: receiverID, + senderID: event.senderID, senderName: senderName, - groupName: groupName, - mediaSource: mediaSource, - roomId: roomID) + iconType: iconType) return notification } diff --git a/Tools/Sources/Utilities.swift b/Tools/Sources/Utilities.swift index e0c0602b1..052b6a18a 100644 --- a/Tools/Sources/Utilities.swift +++ b/Tools/Sources/Utilities.swift @@ -19,6 +19,7 @@ enum Utilities { @discardableResult static func zsh(_ command: String, workingDirectoryURL: URL = projectDirectoryURL) throws -> String? { let process = Process() + process.environment = [:] process.executableURL = URL(fileURLWithPath: "/bin/zsh") process.arguments = ["-cu", command] process.currentDirectoryURL = workingDirectoryURL diff --git a/changelog.d/863.bugfix b/changelog.d/863.bugfix new file mode 100644 index 000000000..c4d806983 --- /dev/null +++ b/changelog.d/863.bugfix @@ -0,0 +1 @@ +Fixed a bug that did not render the sender icon of a dm sometimes. \ No newline at end of file