Fix notifications

This commit is contained in:
Isaac 2024-11-21 09:23:12 +04:00
parent 9a038722d3
commit 11aa6eb76b

View File

@ -440,11 +440,11 @@ private func avatarImage(path: String?, peerId: PeerId, letters: [String], size:
}
}
private func storeTemporaryImage(path: String) -> String {
private func storeTemporaryImage(path: String, fileExtension: String) -> String {
let imagesPath = NSTemporaryDirectory() + "/aps-data"
let _ = try? FileManager.default.createDirectory(at: URL(fileURLWithPath: imagesPath), withIntermediateDirectories: true, attributes: nil)
let tempPath = imagesPath + "\(path.persistentHashValue)"
let tempPath = imagesPath + "\(path.persistentHashValue).\(fileExtension)"
if FileManager.default.fileExists(atPath: tempPath) {
return tempPath
}
@ -459,7 +459,7 @@ private func peerAvatar(mediaBox: MediaBox, accountPeerId: PeerId, peer: Peer, i
if let resource = smallestImageRepresentation(peer.profileImageRepresentations)?.resource, let path = mediaBox.completedResourcePath(resource) {
let cachedPath = mediaBox.cachedRepresentationPathForId(resource.id.stringRepresentation, representationId: "intents\(isStory ? "-story2" : "").png", keepDuration: .shortLived)
if let _ = fileSize(cachedPath), !"".isEmpty {
return INImage(url: URL(fileURLWithPath: storeTemporaryImage(path: cachedPath)))
return INImage(url: URL(fileURLWithPath: storeTemporaryImage(path: cachedPath, fileExtension: "jpg")))
} else {
let image = avatarImage(path: path, peerId: peer.id, letters: peer.displayLetters, size: CGSize(width: 50.0, height: 50.0), isStory: isStory)
if let data = image.pngData() {
@ -467,19 +467,19 @@ private func peerAvatar(mediaBox: MediaBox, accountPeerId: PeerId, peer: Peer, i
let _ = try? data.write(to: URL(fileURLWithPath: cachedPath), options: .atomic)
}
return INImage(url: URL(fileURLWithPath: storeTemporaryImage(path: cachedPath)))
return INImage(url: URL(fileURLWithPath: storeTemporaryImage(path: cachedPath, fileExtension: "jpg")))
}
}
let cachedPath = mediaBox.cachedRepresentationPathForId("lettersAvatar2-\(peer.displayLetters.joined(separator: ","))\(isStory ? "-story" : "")", representationId: "intents.png", keepDuration: .shortLived)
if let _ = fileSize(cachedPath) {
return INImage(url: URL(fileURLWithPath: storeTemporaryImage(path: cachedPath)))
return INImage(url: URL(fileURLWithPath: storeTemporaryImage(path: cachedPath, fileExtension: "jpg")))
} else {
let image = avatarImage(path: nil, peerId: peer.id, letters: peer.displayLetters, size: CGSize(width: 50.0, height: 50.0), isStory: isStory)
if let data = image.pngData() {
let _ = try? data.write(to: URL(fileURLWithPath: cachedPath), options: .atomic)
}
return INImage(url: URL(fileURLWithPath: storeTemporaryImage(path: cachedPath)))
return INImage(url: URL(fileURLWithPath: storeTemporaryImage(path: cachedPath, fileExtension: "jpg")))
}
}
@ -1247,6 +1247,8 @@ private final class NotificationServiceHandler {
case let .poll(peerId, initialContent, messageId):
Logger.shared.log("NotificationService \(episode)", "Will poll")
if let stateManager = strongSelf.stateManager {
let shouldKeepConnection = stateManager.network.shouldKeepConnection
let pollCompletion: (NotificationContent) -> Void = { content in
var content = content
@ -1272,6 +1274,9 @@ private final class NotificationServiceHandler {
} else if file.isVideo {
fetchResource = file.previewRepresentations.first?.resource as? TelegramMultipartFetchableResource
contentType = .video
} else if file.isVoice {
fetchResource = file.resource as? TelegramMultipartFetchableResource
contentType = .audio
} else {
contentType = .file
}
@ -1443,8 +1448,10 @@ private final class NotificationServiceHandler {
completed()
return
}
shouldKeepConnection.set(.single(false))
Logger.shared.log("NotificationService \(episode)", "Did fetch media \(mediaData == nil ? "Non-empty" : "Empty")")
Logger.shared.log("NotificationService \(episode)", "Did fetch media \(mediaData == nil ? "Empty" : "Non-empty")")
if let notificationSoundData = notificationSoundData {
Logger.shared.log("NotificationService \(episode)", "Did fetch notificationSoundData")
@ -1550,11 +1557,10 @@ private final class NotificationServiceHandler {
}
let pollSignal: Signal<Never, NoError>
if !shouldSynchronizeState {
pollSignal = .complete()
} else {
let shouldKeepConnection = stateManager.network.shouldKeepConnection
shouldKeepConnection.set(.single(true))
if peerId.namespace == Namespaces.Peer.CloudChannel {
Logger.shared.log("NotificationService \(episode)", "Will poll channel \(peerId)")
@ -1566,9 +1572,6 @@ private final class NotificationServiceHandler {
peerId: peerId,
stateManager: stateManager
)
|> afterDisposed { [weak shouldKeepConnection] in
shouldKeepConnection?.set(.single(false))
}
} else {
Logger.shared.log("NotificationService \(episode)", "Will perform non-specific getDifference")
enum ControlError {
@ -1584,9 +1587,6 @@ private final class NotificationServiceHandler {
}
}
|> restartIfError
|> afterDisposed { [weak shouldKeepConnection] in
shouldKeepConnection?.set(.single(false))
}
pollSignal = signal
}