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