Fix for the sender icon not being rendered in notifications sometimes (#862)

* fix for the sender icon not being rendered sometimes in notifications

* removing a line

* removing the environment every time we run a zsh command

* removed unused parameters that we already have stored in the notification

* changelog
This commit is contained in:
Mauro
2023-05-08 17:05:55 +02:00
committed by GitHub
parent 8cfb415410
commit 542cefd703
4 changed files with 39 additions and 23 deletions
@@ -18,6 +18,27 @@ import Foundation
import Intents import Intents
import UserNotifications 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 { extension UNNotificationContent {
@objc var receiverID: String? { @objc var receiverID: String? {
userInfo[NotificationConstants.UserInfoKey.receiverIdentifier] as? String userInfo[NotificationConstants.UserInfoKey.receiverIdentifier] as? String
@@ -98,16 +119,12 @@ extension UNMutableNotificationContent {
return self return self
} }
// swiftlint:disable:next function_parameter_count
func addSenderIcon(using mediaProvider: MediaProviderProtocol?, func addSenderIcon(using mediaProvider: MediaProviderProtocol?,
senderId: String, senderID: String,
receiverId: String,
senderName: String, senderName: String,
groupName: String?, iconType: NotificationIconType) async throws -> UNMutableNotificationContent {
mediaSource: MediaSourceProxy?,
roomId: String) async throws -> UNMutableNotificationContent {
var image: INImage? var image: INImage?
if let mediaSource { if let mediaSource = iconType.mediaSource {
switch await mediaProvider?.loadFileFromSource(mediaSource) { switch await mediaProvider?.loadFileFromSource(mediaSource) {
case .success(let mediaFile): case .success(let mediaFile):
image = try INImage(imageData: Data(contentsOf: mediaFile.url)) 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, let sender = INPerson(personHandle: senderHandle,
nameComponents: nil, nameComponents: nil,
displayName: senderName, displayName: senderName,
image: image, image: iconType.isSenderIcon ? image : nil,
contactIdentifier: nil, contactIdentifier: nil,
customIdentifier: nil) customIdentifier: nil)
// These are required to show the group name as subtitle // These are required to show the group name as subtitle
var speakableGroupName: INSpeakableString? var speakableGroupName: INSpeakableString?
var recipients: [INPerson]? var recipients: [INPerson]?
if let groupName { if case let .group(_, groupName) = iconType {
let meHandle = INPersonHandle(value: receiverId, type: .unknown) let meHandle = INPersonHandle(value: receiverID, type: .unknown)
let me = INPerson(personHandle: meHandle, nameComponents: nil, displayName: nil, image: nil, contactIdentifier: nil, customIdentifier: nil, isMe: true) let me = INPerson(personHandle: meHandle, nameComponents: nil, displayName: nil, image: nil, contactIdentifier: nil, customIdentifier: nil, isMe: true)
speakableGroupName = INSpeakableString(spokenPhrase: groupName) speakableGroupName = INSpeakableString(spokenPhrase: groupName)
recipients = [sender, me] recipients = [sender, me]
@@ -140,11 +157,13 @@ extension UNMutableNotificationContent {
outgoingMessageType: .outgoingMessageText, outgoingMessageType: .outgoingMessageText,
content: nil, content: nil,
speakableGroupName: speakableGroupName, speakableGroupName: speakableGroupName,
conversationIdentifier: roomId, conversationIdentifier: roomID,
serviceName: nil, serviceName: nil,
sender: sender, sender: sender,
attachments: nil) attachments: nil)
if speakableGroupName != nil, let image {
intent.setImage(image, forParameterNamed: \.speakableGroupName) intent.setImage(image, forParameterNamed: \.speakableGroupName)
}
// Use the intent to initialize the interaction. // Use the intent to initialize the interaction.
let interaction = INInteraction(intent: intent, response: nil) let interaction = INInteraction(intent: intent, response: nil)
@@ -232,22 +232,17 @@ extension NotificationItemProxyProtocol {
notification.sound = isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil notification.sound = isNoisy ? UNNotificationSound(named: UNNotificationSoundName(rawValue: "message.caf")) : nil
let senderName = senderDisplayName ?? roomDisplayName let senderName = senderDisplayName ?? roomDisplayName
var groupName: String? let iconType: NotificationIconType
var mediaSource: MediaSourceProxy?
if !isDirect { if !isDirect {
groupName = senderName != roomDisplayName ? roomDisplayName : nil iconType = .group(mediaSource: roomAvatarMediaSource, groupName: roomDisplayName)
mediaSource = roomAvatarMediaSource
} else { } else {
mediaSource = senderAvatarMediaSource iconType = .sender(mediaSource: senderAvatarMediaSource)
} }
notification = try await notification.addSenderIcon(using: mediaProvider, notification = try await notification.addSenderIcon(using: mediaProvider,
senderId: event.senderID, senderID: event.senderID,
receiverId: receiverID,
senderName: senderName, senderName: senderName,
groupName: groupName, iconType: iconType)
mediaSource: mediaSource,
roomId: roomID)
return notification return notification
} }
+1
View File
@@ -19,6 +19,7 @@ enum Utilities {
@discardableResult @discardableResult
static func zsh(_ command: String, workingDirectoryURL: URL = projectDirectoryURL) throws -> String? { static func zsh(_ command: String, workingDirectoryURL: URL = projectDirectoryURL) throws -> String? {
let process = Process() let process = Process()
process.environment = [:]
process.executableURL = URL(fileURLWithPath: "/bin/zsh") process.executableURL = URL(fileURLWithPath: "/bin/zsh")
process.arguments = ["-cu", command] process.arguments = ["-cu", command]
process.currentDirectoryURL = workingDirectoryURL process.currentDirectoryURL = workingDirectoryURL
+1
View File
@@ -0,0 +1 @@
Fixed a bug that did not render the sender icon of a dm sometimes.