iOS/Sources/Shared/Notifications/NotificationAttachments/NotificationAttachmentParserCamera.swift
Zac West 85b5ec4579
Support attachments which aren't downloaded in advance (#1610)
## Summary
Allows attachments to be loaded on-the-fly in the content extension if they are not present.

## Link to pull request in Documentation repository
Documentation: home-assistant/companion.home-assistant#510

## Any other notes
- Adds `lazy` as an option to the `attachment` dictionary. When true, this prevents the attachment from being downloaded except when displayed in the content extension.  
  
This is similar, but not identical, to hide-thumbnail. Hiding the thumbnail still uses the internal storage of the notifications which are limited to e.g. 50mb. However, we'd still want to pre-load those attachments even if the thumbnail is hidden.
- Retries non-lazy attachments which failed due to a network error when the content extension opens.
2021-05-01 12:58:30 -07:00

38 lines
1.2 KiB
Swift

import CoreServices
import Foundation
import PromiseKit
import UserNotifications
final class NotificationAttachmentParserCamera: NotificationAttachmentParser {
enum CameraError: LocalizedError {
case invalidEntity
var errorDescription: String? {
switch self {
case .invalidEntity: return L10n.NotificationService.Parser.Camera.invalidEntity
}
}
}
func attachmentInfo(from content: UNNotificationContent) -> Guarantee<NotificationAttachmentParserResult> {
guard let entityId = content.userInfo["entity_id"] as? String, entityId.hasPrefix("camera.") else {
return .value(.missing)
}
guard let proxyURL = URL(string: "/api/camera_proxy/\(entityId)") else {
return .value(.rejected(CameraError.invalidEntity))
}
let hideThumbnail = (content.userInfo["attachment"] as? [String: Any])?["hide-thumbnail"] as? Bool
let lazy = (content.userInfo["attachment"] as? [String: Any])?["lazy"] as? Bool == true
return .value(.fulfilled(.init(
url: proxyURL,
needsAuth: true,
typeHint: kUTTypeJPEG,
hideThumbnail: hideThumbnail,
lazy: lazy
)))
}
}