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:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
1
changelog.d/863.bugfix
Normal file
1
changelog.d/863.bugfix
Normal file
@@ -0,0 +1 @@
|
||||
Fixed a bug that did not render the sender icon of a dm sometimes.
|
||||
Reference in New Issue
Block a user