mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-27 16:25:22 +00:00
Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ef010dff37 | ||
|
|
8fbfa359ff | ||
|
|
eeb4d55476 | ||
|
|
4882b99752 | ||
|
|
9b8498c78e | ||
|
|
1cf8449e22 | ||
|
|
7ca1ff0a7e | ||
|
|
7316a4a46d | ||
|
|
e992d96939 | ||
|
|
e427e1c62f | ||
|
|
fae76dc515 | ||
|
|
76f976438f | ||
|
|
369c335088 | ||
|
|
a84b8c05f1 | ||
|
|
9d738cff45 | ||
|
|
9879cb2580 | ||
|
|
7040d9ecb0 | ||
|
|
88bafb41cc |
@@ -1,10 +1,10 @@
|
||||
import Foundation
|
||||
import ImageIO
|
||||
import UniformTypeIdentifiers
|
||||
#if os(iOS)
|
||||
import UIKit
|
||||
#else
|
||||
import AppKit
|
||||
import ImageIO
|
||||
import UniformTypeIdentifiers
|
||||
#endif
|
||||
|
||||
enum ImageUtilsError: Error {
|
||||
@@ -40,19 +40,30 @@ enum ImageUtils {
|
||||
#if os(iOS)
|
||||
static func processImage(_ image: UIImage, maxDimension: CGFloat = 512) throws -> URL {
|
||||
return try autoreleasepool {
|
||||
// Scale the image first
|
||||
let scaled = scaledImage(image, maxDimension: maxDimension)
|
||||
var quality = compressionQuality
|
||||
guard var jpegData = scaled.jpegData(compressionQuality: quality) else {
|
||||
|
||||
// Get CGImage from UIImage - this is the key to stripping metadata
|
||||
guard let cgImage = scaled.cgImage else {
|
||||
throw ImageUtilsError.encodingFailed
|
||||
}
|
||||
|
||||
// Use CGImageDestination to encode without metadata (same as macOS)
|
||||
var quality = compressionQuality
|
||||
guard var jpegData = encodeJPEG(from: cgImage, quality: quality) else {
|
||||
throw ImageUtilsError.encodingFailed
|
||||
}
|
||||
|
||||
// Compress to target size
|
||||
while jpegData.count > targetImageBytes && quality > 0.3 {
|
||||
quality -= 0.1
|
||||
autoreleasepool {
|
||||
if let next = scaled.jpegData(compressionQuality: quality) {
|
||||
if let next = encodeJPEG(from: cgImage, quality: quality) {
|
||||
jpegData = next
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let outputURL = try makeOutputURL()
|
||||
try jpegData.write(to: outputURL, options: .atomic)
|
||||
return outputURL
|
||||
@@ -65,12 +76,35 @@ enum ImageUtils {
|
||||
guard maxSide > maxDimension else { return image }
|
||||
let scale = maxDimension / maxSide
|
||||
let newSize = CGSize(width: size.width * scale, height: size.height * scale)
|
||||
|
||||
// Draw into a new context to get a clean CGImage without metadata
|
||||
UIGraphicsBeginImageContextWithOptions(newSize, true, 1.0)
|
||||
image.draw(in: CGRect(origin: .zero, size: newSize))
|
||||
let rendered = UIGraphicsGetImageFromCurrentImageContext()
|
||||
UIGraphicsEndImageContext()
|
||||
return rendered ?? image
|
||||
}
|
||||
|
||||
// Shared EXIF-stripping JPEG encoder for both iOS and macOS
|
||||
private static func encodeJPEG(from cgImage: CGImage, quality: CGFloat) -> Data? {
|
||||
guard let data = CFDataCreateMutable(nil, 0) else {
|
||||
return nil
|
||||
}
|
||||
guard let destination = CGImageDestinationCreateWithData(data, UTType.jpeg.identifier as CFString, 1, nil) else {
|
||||
return nil
|
||||
}
|
||||
// Security: Strip ALL metadata (EXIF, GPS, TIFF, IPTC, XMP)
|
||||
// By only specifying compression quality and no metadata keys,
|
||||
// we ensure a clean JPEG with no privacy-leaking information
|
||||
let options: [CFString: Any] = [
|
||||
kCGImageDestinationLossyCompressionQuality: quality
|
||||
]
|
||||
CGImageDestinationAddImage(destination, cgImage, options as CFDictionary)
|
||||
guard CGImageDestinationFinalize(destination) else {
|
||||
return nil
|
||||
}
|
||||
return data as Data
|
||||
}
|
||||
#else
|
||||
static func processImage(_ image: NSImage, maxDimension: CGFloat = 512) throws -> URL {
|
||||
return try autoreleasepool {
|
||||
@@ -130,6 +164,7 @@ enum ImageUtils {
|
||||
return scaledImage
|
||||
}
|
||||
|
||||
// Shared EXIF-stripping JPEG encoder for both iOS and macOS
|
||||
private static func encodeJPEG(from cgImage: CGImage, quality: CGFloat) -> Data? {
|
||||
guard let data = CFDataCreateMutable(nil, 0) else {
|
||||
return nil
|
||||
@@ -137,8 +172,9 @@ enum ImageUtils {
|
||||
guard let destination = CGImageDestinationCreateWithData(data, UTType.jpeg.identifier as CFString, 1, nil) else {
|
||||
return nil
|
||||
}
|
||||
// Security H2: Strip ALL metadata (EXIF, GPS, TIFF, IPTC, XMP)
|
||||
// Don't add any metadata dictionary keys - fresh CGContext ensures clean image
|
||||
// Security: Strip ALL metadata (EXIF, GPS, TIFF, IPTC, XMP)
|
||||
// By only specifying compression quality and no metadata keys,
|
||||
// we ensure a clean JPEG with no privacy-leaking information
|
||||
let options: [CFString: Any] = [
|
||||
kCGImageDestinationLossyCompressionQuality: quality
|
||||
]
|
||||
|
||||
+23774
-23986
File diff suppressed because it is too large
Load Diff
@@ -906,6 +906,16 @@ struct NostrFilter: Encodable {
|
||||
filter.limit = limit
|
||||
return filter
|
||||
}
|
||||
|
||||
// For location notes with neighbors: subscribe to multiple geohashes (center + neighbors)
|
||||
static func geohashNotes(_ geohashes: [String], since: Date? = nil, limit: Int = 200) -> NostrFilter {
|
||||
var filter = NostrFilter()
|
||||
filter.kinds = [1]
|
||||
filter.since = since?.timeIntervalSince1970.toInt()
|
||||
filter.tagFilters = ["g": geohashes]
|
||||
filter.limit = limit
|
||||
return filter
|
||||
}
|
||||
}
|
||||
|
||||
// Dynamic coding key for tag filters
|
||||
|
||||
@@ -333,7 +333,8 @@ struct BinaryProtocol {
|
||||
originalSize = Int(rawSize)
|
||||
}
|
||||
// Guard to keep decompression bounded to sane BLE payload limits
|
||||
guard originalSize >= 0 && originalSize <= FileTransferLimits.maxPayloadBytes else { return nil }
|
||||
// Use maxFramedFileBytes to account for TLV overhead in file transfer payloads
|
||||
guard originalSize >= 0 && originalSize <= FileTransferLimits.maxFramedFileBytes else { return nil }
|
||||
let compressedSize = payloadLength - lengthFieldBytes
|
||||
guard compressedSize >= 0, let compressed = readData(compressedSize) else { return nil }
|
||||
|
||||
|
||||
@@ -119,4 +119,57 @@ enum Geohash {
|
||||
}
|
||||
return (latInterval.0, latInterval.1, lonInterval.0, lonInterval.1)
|
||||
}
|
||||
|
||||
/// Returns all 8 neighboring geohash cells at the same precision.
|
||||
/// - Parameter geohash: Base32 geohash string.
|
||||
/// - Returns: Array of 8 neighboring geohashes (N, NE, E, SE, S, SW, W, NW order).
|
||||
static func neighbors(of geohash: String) -> [String] {
|
||||
guard !geohash.isEmpty else { return [] }
|
||||
|
||||
let precision = geohash.count
|
||||
let bounds = decodeBounds(geohash)
|
||||
let center = decodeCenter(geohash)
|
||||
|
||||
// Calculate cell dimensions
|
||||
let latHeight = bounds.latMax - bounds.latMin
|
||||
let lonWidth = bounds.lonMax - bounds.lonMin
|
||||
|
||||
// Helper to wrap longitude around ±180
|
||||
func wrapLongitude(_ lon: Double) -> Double {
|
||||
var wrapped = lon
|
||||
while wrapped > 180.0 { wrapped -= 360.0 }
|
||||
while wrapped < -180.0 { wrapped += 360.0 }
|
||||
return wrapped
|
||||
}
|
||||
|
||||
// Helper to clamp latitude to ±90
|
||||
func clampLatitude(_ lat: Double) -> Double {
|
||||
return max(-90.0, min(90.0, lat))
|
||||
}
|
||||
|
||||
// Calculate 8 neighbor centers
|
||||
let neighbors: [(lat: Double, lon: Double)] = [
|
||||
(center.lat + latHeight, center.lon), // N
|
||||
(center.lat + latHeight, center.lon + lonWidth), // NE
|
||||
(center.lat, center.lon + lonWidth), // E
|
||||
(center.lat - latHeight, center.lon + lonWidth), // SE
|
||||
(center.lat - latHeight, center.lon), // S
|
||||
(center.lat - latHeight, center.lon - lonWidth), // SW
|
||||
(center.lat, center.lon - lonWidth), // W
|
||||
(center.lat + latHeight, center.lon - lonWidth) // NW
|
||||
]
|
||||
|
||||
// Encode each neighbor, handling boundary conditions
|
||||
return neighbors.compactMap { neighbor in
|
||||
let lat = clampLatitude(neighbor.lat)
|
||||
let lon = wrapLongitude(neighbor.lon)
|
||||
|
||||
// Skip if we've crossed a pole (latitude clamped to boundary)
|
||||
if (neighbor.lat > 90.0 || neighbor.lat < -90.0) {
|
||||
return nil
|
||||
}
|
||||
|
||||
return encode(latitude: lat, longitude: lon, precision: precision)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3414,44 +3414,84 @@ extension BLEService {
|
||||
let originalType = packet.payload[12]
|
||||
let fragmentData = packet.payload.suffix(from: 13)
|
||||
|
||||
// Sanity checks
|
||||
guard total > 0 && index >= 0 && index < total else { return }
|
||||
// Sanity checks - add reasonable upper bound on total to prevent DoS
|
||||
guard total > 0 && total <= 10000 && index >= 0 && index < total else { return }
|
||||
|
||||
// Store fragment
|
||||
// Compute fragment key for this assembly
|
||||
let key = FragmentKey(sender: senderU64, id: fragU64)
|
||||
if incomingFragments[key] == nil {
|
||||
// Cap in-flight assemblies to prevent memory/battery blowups
|
||||
if incomingFragments.count >= maxInFlightAssemblies {
|
||||
// Evict the oldest assembly by timestamp
|
||||
if let oldest = fragmentMetadata.min(by: { $0.value.timestamp < $1.value.timestamp })?.key {
|
||||
incomingFragments.removeValue(forKey: oldest)
|
||||
fragmentMetadata.removeValue(forKey: oldest)
|
||||
}
|
||||
}
|
||||
incomingFragments[key] = [:]
|
||||
fragmentMetadata[key] = (originalType, total, Date())
|
||||
}
|
||||
incomingFragments[key]?[index] = Data(fragmentData)
|
||||
|
||||
// Check if complete
|
||||
if let fragments = incomingFragments[key],
|
||||
fragments.count == total {
|
||||
// Reassemble
|
||||
var reassembled = Data()
|
||||
for i in 0..<total {
|
||||
if let fragment = fragments[i] {
|
||||
reassembled.append(fragment)
|
||||
// Critical section: Store fragment and check completion status
|
||||
var shouldReassemble: Bool = false
|
||||
var fragmentsToReassemble: [Int: Data]? = nil
|
||||
|
||||
collectionsQueue.sync(flags: .barrier) {
|
||||
if incomingFragments[key] == nil {
|
||||
// Cap in-flight assemblies to prevent memory/battery blowups
|
||||
if incomingFragments.count >= maxInFlightAssemblies {
|
||||
// Evict the oldest assembly by timestamp
|
||||
if let oldest = fragmentMetadata.min(by: { $0.value.timestamp < $1.value.timestamp })?.key {
|
||||
incomingFragments.removeValue(forKey: oldest)
|
||||
fragmentMetadata.removeValue(forKey: oldest)
|
||||
}
|
||||
}
|
||||
incomingFragments[key] = [:]
|
||||
fragmentMetadata[key] = (originalType, total, Date())
|
||||
}
|
||||
|
||||
// Decode the original packet bytes we reassembled, so flags/compression are preserved
|
||||
if let originalPacket = BinaryProtocol.decode(reassembled) {
|
||||
handleReceivedPacket(originalPacket, from: peerID)
|
||||
// Check cumulative size before storing this fragment
|
||||
let currentSize = incomingFragments[key]?.values.reduce(0) { $0 + $1.count } ?? 0
|
||||
let assemblyLimit: Int = {
|
||||
if originalType == MessageType.fileTransfer.rawValue {
|
||||
// Allow headroom for TLV metadata and binary framing overhead.
|
||||
return FileTransferLimits.maxFramedFileBytes
|
||||
}
|
||||
return FileTransferLimits.maxPayloadBytes
|
||||
}()
|
||||
let projectedSize = currentSize + fragmentData.count
|
||||
guard projectedSize <= assemblyLimit else {
|
||||
// Exceeds size limit - evict this assembly
|
||||
SecureLogger.warning(
|
||||
"🚫 Fragment assembly exceeds size limit (\(projectedSize) bytes > \(assemblyLimit)), evicting. Type=\(originalType) Index=\(index)/\(total)",
|
||||
category: .security
|
||||
)
|
||||
incomingFragments.removeValue(forKey: key)
|
||||
fragmentMetadata.removeValue(forKey: key)
|
||||
shouldReassemble = false
|
||||
fragmentsToReassemble = nil
|
||||
return
|
||||
}
|
||||
|
||||
incomingFragments[key]?[index] = Data(fragmentData)
|
||||
|
||||
// Check if complete
|
||||
if let fragments = incomingFragments[key], fragments.count == total {
|
||||
shouldReassemble = true
|
||||
fragmentsToReassemble = fragments
|
||||
} else {
|
||||
SecureLogger.error("❌ Failed to decode reassembled packet (type=\(originalType), total=\(total))", category: .session)
|
||||
shouldReassemble = false
|
||||
fragmentsToReassemble = nil
|
||||
}
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
// Heavy work outside lock: reassemble and decode
|
||||
guard shouldReassemble, let fragments = fragmentsToReassemble else { return }
|
||||
|
||||
var reassembled = Data()
|
||||
for i in 0..<total {
|
||||
if let fragment = fragments[i] {
|
||||
reassembled.append(fragment)
|
||||
}
|
||||
}
|
||||
|
||||
// Decode the original packet bytes we reassembled, so flags/compression are preserved
|
||||
if let originalPacket = BinaryProtocol.decode(reassembled) {
|
||||
handleReceivedPacket(originalPacket, from: peerID)
|
||||
} else {
|
||||
SecureLogger.error("❌ Failed to decode reassembled packet (type=\(originalType), total=\(total))", category: .session)
|
||||
}
|
||||
|
||||
// Critical section: Cleanup completed assembly
|
||||
collectionsQueue.sync(flags: .barrier) {
|
||||
incomingFragments.removeValue(forKey: key)
|
||||
fragmentMetadata.removeValue(forKey: key)
|
||||
}
|
||||
|
||||
@@ -1,104 +0,0 @@
|
||||
import BitLogger
|
||||
import Foundation
|
||||
|
||||
struct LocationNotesCounterDependencies {
|
||||
typealias RelayLookup = @MainActor (_ geohash: String, _ count: Int) -> [String]
|
||||
typealias Subscribe = @MainActor (_ filter: NostrFilter, _ id: String, _ relays: [String], _ handler: @escaping (NostrEvent) -> Void, _ onEOSE: (() -> Void)?) -> Void
|
||||
typealias Unsubscribe = @MainActor (_ id: String) -> Void
|
||||
|
||||
var relayLookup: RelayLookup
|
||||
var subscribe: Subscribe
|
||||
var unsubscribe: Unsubscribe
|
||||
|
||||
static let live = LocationNotesCounterDependencies(
|
||||
relayLookup: { geohash, count in
|
||||
GeoRelayDirectory.shared.closestRelays(toGeohash: geohash, count: count)
|
||||
},
|
||||
subscribe: { filter, id, relays, handler, onEOSE in
|
||||
NostrRelayManager.shared.subscribe(
|
||||
filter: filter,
|
||||
id: id,
|
||||
relayUrls: relays,
|
||||
handler: handler,
|
||||
onEOSE: onEOSE
|
||||
)
|
||||
},
|
||||
unsubscribe: { id in
|
||||
NostrRelayManager.shared.unsubscribe(id: id)
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
/// Lightweight background counter for location notes (kind 1) at building-level geohash (8 chars).
|
||||
@MainActor
|
||||
final class LocationNotesCounter: ObservableObject {
|
||||
static let shared = LocationNotesCounter()
|
||||
|
||||
@Published private(set) var geohash: String? = nil
|
||||
@Published private(set) var count: Int? = 0
|
||||
@Published private(set) var initialLoadComplete: Bool = false
|
||||
@Published private(set) var relayAvailable: Bool = true
|
||||
|
||||
private var subscriptionID: String? = nil
|
||||
private var noteIDs = Set<String>()
|
||||
private let dependencies: LocationNotesCounterDependencies
|
||||
|
||||
private init(dependencies: LocationNotesCounterDependencies = .live) {
|
||||
self.dependencies = dependencies
|
||||
}
|
||||
|
||||
init(testDependencies: LocationNotesCounterDependencies) {
|
||||
self.dependencies = testDependencies
|
||||
}
|
||||
|
||||
func subscribe(geohash gh: String) {
|
||||
let norm = gh.lowercased()
|
||||
if geohash == norm, subscriptionID != nil { return }
|
||||
// Validate geohash (building-level precision: 8 chars)
|
||||
guard Geohash.isValidBuildingGeohash(norm) else {
|
||||
SecureLogger.warning("LocationNotesCounter: rejecting invalid geohash '\(norm)' (expected 8 valid base32 chars)", category: .session)
|
||||
return
|
||||
}
|
||||
// Unsubscribe previous without clearing count to avoid flicker
|
||||
if let sub = subscriptionID { dependencies.unsubscribe(sub) }
|
||||
subscriptionID = nil
|
||||
geohash = norm
|
||||
noteIDs.removeAll()
|
||||
initialLoadComplete = false
|
||||
relayAvailable = true
|
||||
|
||||
// Subscribe only to the building geohash (precision 8)
|
||||
let subID = "locnotes-count-\(norm)-\(UUID().uuidString.prefix(6))"
|
||||
let relays = dependencies.relayLookup(norm, TransportConfig.nostrGeoRelayCount)
|
||||
guard !relays.isEmpty else {
|
||||
relayAvailable = false
|
||||
initialLoadComplete = true
|
||||
count = 0
|
||||
SecureLogger.warning("LocationNotesCounter: no geo relays for geohash=\(norm)", category: .session)
|
||||
return
|
||||
}
|
||||
|
||||
subscriptionID = subID
|
||||
let filter = NostrFilter.geohashNotes(norm, since: nil, limit: 200)
|
||||
dependencies.subscribe(filter, subID, relays, { [weak self] event in
|
||||
guard let self = self else { return }
|
||||
guard event.kind == NostrProtocol.EventKind.textNote.rawValue else { return }
|
||||
guard event.tags.contains(where: { $0.count >= 2 && $0[0].lowercased() == "g" && $0[1].lowercased() == norm }) else { return }
|
||||
if !self.noteIDs.contains(event.id) {
|
||||
self.noteIDs.insert(event.id)
|
||||
self.count = self.noteIDs.count
|
||||
}
|
||||
}, { [weak self] in
|
||||
self?.initialLoadComplete = true
|
||||
})
|
||||
}
|
||||
|
||||
func cancel() {
|
||||
if let sub = subscriptionID { dependencies.unsubscribe(sub) }
|
||||
subscriptionID = nil
|
||||
geohash = nil
|
||||
count = 0
|
||||
noteIDs.removeAll()
|
||||
relayAvailable = true
|
||||
}
|
||||
}
|
||||
@@ -163,14 +163,22 @@ final class LocationNotesManager: ObservableObject {
|
||||
|
||||
subscriptionID = subID
|
||||
initialLoadComplete = false
|
||||
// For persistent notes, allow relays to return recent history without an aggressive time cutoff
|
||||
let filter = NostrFilter.geohashNotes(geohash, since: nil, limit: 200)
|
||||
|
||||
// Subscribe to center + 8 neighbors (± 1 grid)
|
||||
let neighbors = Geohash.neighbors(of: geohash)
|
||||
let allGeohashes = [geohash] + neighbors
|
||||
let filter = NostrFilter.geohashNotes(allGeohashes, since: nil, limit: 200)
|
||||
|
||||
// Build a set of valid geohashes for tag matching (includes all 9 cells)
|
||||
let validGeohashes = Set(allGeohashes.map { $0.lowercased() })
|
||||
|
||||
dependencies.subscribe(filter, subID, relays, { [weak self] event in
|
||||
guard let self = self else { return }
|
||||
guard event.kind == NostrProtocol.EventKind.textNote.rawValue else { return }
|
||||
// Ensure matching tag
|
||||
guard event.tags.contains(where: { $0.count >= 2 && $0[0].lowercased() == "g" && $0[1].lowercased() == self.geohash }) else { return }
|
||||
// Ensure matching tag - accept any of our 9 geohashes
|
||||
guard event.tags.contains(where: { tag in
|
||||
tag.count >= 2 && tag[0].lowercased() == "g" && validGeohashes.contains(tag[1].lowercased())
|
||||
}) else { return }
|
||||
guard !self.noteIDs.contains(event.id) else { return }
|
||||
self.noteIDs.insert(event.id)
|
||||
let nick = event.tags.first(where: { $0.first?.lowercased() == "n" && $0.count >= 2 })?.dropFirst().first
|
||||
|
||||
@@ -2503,65 +2503,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func sendFileAttachment(from sourceURL: URL) {
|
||||
let targetPeer = selectedPrivateChatPeer
|
||||
|
||||
Task.detached(priority: .userInitiated) { [weak self] in
|
||||
guard let self = self else { return }
|
||||
defer { try? FileManager.default.removeItem(at: sourceURL) }
|
||||
var destinationURL: URL?
|
||||
do {
|
||||
// Security H1: Check file size BEFORE reading into memory
|
||||
let attrs = try FileManager.default.attributesOfItem(atPath: sourceURL.path)
|
||||
guard let fileSize = attrs[.size] as? Int else {
|
||||
throw MediaSendError.encodingFailed
|
||||
}
|
||||
guard FileTransferLimits.isValidPayload(fileSize) else {
|
||||
throw MediaSendError.tooLarge
|
||||
}
|
||||
|
||||
let data = try Data(contentsOf: sourceURL)
|
||||
|
||||
let destination = try self.prepareOutgoingFileCopy(from: sourceURL, data: data)
|
||||
destinationURL = destination
|
||||
let packet = BitchatFilePacket(
|
||||
fileName: destination.lastPathComponent,
|
||||
fileSize: UInt64(data.count),
|
||||
mimeType: self.mimeType(for: destination),
|
||||
content: data
|
||||
)
|
||||
guard packet.encode() != nil else {
|
||||
try? FileManager.default.removeItem(at: destination)
|
||||
throw MediaSendError.encodingFailed
|
||||
}
|
||||
|
||||
await MainActor.run {
|
||||
let message = self.enqueueMediaMessage(content: "[file] \(destination.lastPathComponent)", targetPeer: targetPeer?.id)
|
||||
let messageID = message.id
|
||||
let transferId = self.makeTransferID(messageID: messageID)
|
||||
self.registerTransfer(transferId: transferId, messageID: messageID)
|
||||
if let peerID = targetPeer {
|
||||
self.meshService.sendFilePrivate(packet, to: peerID, transferId: transferId)
|
||||
} else {
|
||||
self.meshService.sendFileBroadcast(packet, transferId: transferId)
|
||||
}
|
||||
}
|
||||
} catch MediaSendError.tooLarge {
|
||||
await MainActor.run {
|
||||
self.addSystemMessage("File is too large to send.")
|
||||
}
|
||||
} catch {
|
||||
if let destination = destinationURL {
|
||||
try? FileManager.default.removeItem(at: destination)
|
||||
}
|
||||
SecureLogger.error("File attachment send failed: \(error)", category: .session)
|
||||
await MainActor.run {
|
||||
self.addSystemMessage("Failed to send file.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
func cancelMediaSend(messageID: String) {
|
||||
@@ -2748,55 +2689,6 @@ final class ChatViewModel: ObservableObject, BitchatDelegate {
|
||||
}
|
||||
}
|
||||
|
||||
private func prepareOutgoingFileCopy(from sourceURL: URL, data: Data) throws -> URL {
|
||||
let base = try applicationFilesDirectory().appendingPathComponent("files/outgoing", isDirectory: true)
|
||||
try FileManager.default.createDirectory(at: base, withIntermediateDirectories: true, attributes: nil)
|
||||
|
||||
let rawName = sourceURL.lastPathComponent
|
||||
let sanitized = sanitizeOutgoingFileName(rawName.isEmpty ? "file" : rawName)
|
||||
var destination = base.appendingPathComponent(sanitized)
|
||||
var counter = 1
|
||||
while FileManager.default.fileExists(atPath: destination.path) {
|
||||
let baseName = (sanitized as NSString).deletingPathExtension
|
||||
let ext = (sanitized as NSString).pathExtension
|
||||
let newName: String
|
||||
if ext.isEmpty {
|
||||
newName = "\(baseName) (\(counter))"
|
||||
} else {
|
||||
newName = "\(baseName) (\(counter)).\(ext)"
|
||||
}
|
||||
destination = base.appendingPathComponent(newName)
|
||||
counter += 1
|
||||
}
|
||||
|
||||
try data.write(to: destination, options: .atomic)
|
||||
return destination
|
||||
}
|
||||
|
||||
private func sanitizeOutgoingFileName(_ name: String) -> String {
|
||||
var candidate = name
|
||||
candidate = candidate.replacingOccurrences(of: "\\", with: "/")
|
||||
candidate = candidate.components(separatedBy: "/").last ?? name
|
||||
candidate = candidate.trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
if candidate.isEmpty { candidate = "file" }
|
||||
let invalid = CharacterSet(charactersIn: "<>:\"|?*")
|
||||
candidate = candidate.components(separatedBy: invalid).joined(separator: "_")
|
||||
if candidate.isEmpty { candidate = "file" }
|
||||
if (candidate as NSString).pathExtension.isEmpty {
|
||||
candidate += ".bin"
|
||||
}
|
||||
return candidate
|
||||
}
|
||||
|
||||
private func mimeType(for url: URL) -> String {
|
||||
let ext = url.pathExtension.lowercased()
|
||||
if !ext.isEmpty,
|
||||
let type = UTType(filenameExtension: ext),
|
||||
let mime = type.preferredMIMEType {
|
||||
return mime
|
||||
}
|
||||
return "application/octet-stream"
|
||||
}
|
||||
|
||||
private func applicationFilesDirectory() throws -> URL {
|
||||
let base = try FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
|
||||
|
||||
+151
-119
@@ -15,9 +15,6 @@ import AppKit
|
||||
#endif
|
||||
import UniformTypeIdentifiers
|
||||
import BitLogger
|
||||
#if canImport(PhotosUI)
|
||||
import PhotosUI
|
||||
#endif
|
||||
|
||||
// MARK: - Supporting Types
|
||||
|
||||
@@ -38,7 +35,6 @@ struct ContentView: View {
|
||||
@EnvironmentObject var viewModel: ChatViewModel
|
||||
@ObservedObject private var locationManager = LocationChannelManager.shared
|
||||
@ObservedObject private var bookmarks = GeohashBookmarksStore.shared
|
||||
@ObservedObject private var notesCounter = LocationNotesCounter.shared
|
||||
@State private var messageText = ""
|
||||
@FocusState private var isTextFieldFocused: Bool
|
||||
@Environment(\.colorScheme) var colorScheme
|
||||
@@ -72,12 +68,12 @@ struct ContentView: View {
|
||||
@State private var recordingDuration: TimeInterval = 0
|
||||
@State private var recordingTimer: Timer?
|
||||
@State private var recordingStartDate: Date?
|
||||
@State private var showFileImporter = false
|
||||
#if os(iOS)
|
||||
@State private var showPhotoPicker = false
|
||||
@State private var selectedPhotoPickerItem: PhotosPickerItem?
|
||||
@State private var showImagePicker = false
|
||||
@State private var imagePickerSourceType: UIImagePickerController.SourceType = .camera
|
||||
#else
|
||||
@State private var showMacImagePicker = false
|
||||
#endif
|
||||
@State private var showAttachmentActions = false
|
||||
@ScaledMetric(relativeTo: .body) private var headerHeight: CGFloat = 44
|
||||
@ScaledMetric(relativeTo: .subheadline) private var headerPeerIconSize: CGFloat = 11
|
||||
@ScaledMetric(relativeTo: .subheadline) private var headerPeerCountFontSize: CGFloat = 12
|
||||
@@ -211,15 +207,46 @@ struct ContentView: View {
|
||||
}
|
||||
}
|
||||
#if os(iOS)
|
||||
.photosPicker(isPresented: $showPhotoPicker, selection: $selectedPhotoPickerItem, matching: .images)
|
||||
.onChange(of: selectedPhotoPickerItem) { newItem in
|
||||
guard let item = newItem else { return }
|
||||
Task { await handlePhotoSelection(item) }
|
||||
.sheet(isPresented: $showImagePicker) {
|
||||
ImagePickerView(sourceType: imagePickerSourceType) { image in
|
||||
showImagePicker = false
|
||||
if let image = image {
|
||||
Task {
|
||||
do {
|
||||
let processedURL = try ImageUtils.processImage(image)
|
||||
await MainActor.run {
|
||||
viewModel.sendImage(from: processedURL)
|
||||
}
|
||||
} catch {
|
||||
SecureLogger.error("Image processing failed: \(error)", category: .session)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.presentationDetents([.large])
|
||||
.presentationDragIndicator(.hidden)
|
||||
.ignoresSafeArea()
|
||||
}
|
||||
#endif
|
||||
.fileImporter(isPresented: $showFileImporter, allowedContentTypes: [.data], allowsMultipleSelection: false) { result in
|
||||
handleImportResult(result, handler: handleImportedFile)
|
||||
#if os(macOS)
|
||||
.sheet(isPresented: $showMacImagePicker) {
|
||||
MacImagePickerView { url in
|
||||
showMacImagePicker = false
|
||||
if let url = url {
|
||||
Task {
|
||||
do {
|
||||
let processedURL = try ImageUtils.processImage(at: url)
|
||||
await MainActor.run {
|
||||
viewModel.sendImage(from: processedURL)
|
||||
}
|
||||
} catch {
|
||||
SecureLogger.error("Image processing failed: \(error)", category: .session)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
.sheet(isPresented: Binding(
|
||||
get: { imagePreviewURL != nil },
|
||||
set: { presenting in if !presenting { imagePreviewURL = nil } }
|
||||
@@ -228,19 +255,6 @@ struct ContentView: View {
|
||||
ImagePreviewView(url: url)
|
||||
}
|
||||
}
|
||||
.confirmationDialog("Attach", isPresented: $showAttachmentActions, titleVisibility: .visible) {
|
||||
#if os(iOS)
|
||||
Button("Image") {
|
||||
showAttachmentActions = false
|
||||
DispatchQueue.main.async { showPhotoPicker = true }
|
||||
}
|
||||
#endif
|
||||
Button("File") {
|
||||
showAttachmentActions = false
|
||||
DispatchQueue.main.async { showFileImporter = true }
|
||||
}
|
||||
Button("Cancel", role: .cancel) {}
|
||||
}
|
||||
.alert("Recording Error", isPresented: $showRecordingAlert, actions: {
|
||||
Button("OK", role: .cancel) {}
|
||||
}, message: {
|
||||
@@ -1349,10 +1363,9 @@ struct ContentView: View {
|
||||
showLocationNotes = true
|
||||
}) {
|
||||
HStack(alignment: .center, spacing: 4) {
|
||||
let hasNotes = (notesCounter.count ?? 0) > 0
|
||||
Image(systemName: "long.text.page.and.pencil")
|
||||
Image(systemName: "note.text")
|
||||
.font(.bitchatSystem(size: 12))
|
||||
.foregroundColor(hasNotes ? textColor : Color.gray)
|
||||
.foregroundColor(Color.orange.opacity(0.8))
|
||||
.padding(.top, 1)
|
||||
}
|
||||
.fixedSize(horizontal: true, vertical: false)
|
||||
@@ -1454,7 +1467,7 @@ struct ContentView: View {
|
||||
}) {
|
||||
Group {
|
||||
if let gh = notesGeohash ?? LocationChannelManager.shared.availableChannels.first(where: { $0.level == .building })?.geohash {
|
||||
LocationNotesView(geohash: gh, onNotesCountChanged: { cnt in sheetNotesCount = cnt })
|
||||
LocationNotesView(geohash: gh)
|
||||
.environmentObject(viewModel)
|
||||
} else {
|
||||
VStack(spacing: 12) {
|
||||
@@ -1511,7 +1524,6 @@ struct ContentView: View {
|
||||
}
|
||||
}
|
||||
.onAppear {
|
||||
updateNotesCounterSubscription()
|
||||
if case .mesh = locationManager.selectedChannel,
|
||||
locationManager.permissionState == .authorized,
|
||||
LocationChannelManager.shared.availableChannels.isEmpty {
|
||||
@@ -1519,16 +1531,13 @@ struct ContentView: View {
|
||||
}
|
||||
}
|
||||
.onChange(of: locationManager.selectedChannel) { _ in
|
||||
updateNotesCounterSubscription()
|
||||
if case .mesh = locationManager.selectedChannel,
|
||||
locationManager.permissionState == .authorized,
|
||||
LocationChannelManager.shared.availableChannels.isEmpty {
|
||||
LocationChannelManager.shared.refreshChannels()
|
||||
}
|
||||
}
|
||||
.onChange(of: locationManager.availableChannels) { _ in updateNotesCounterSubscription() }
|
||||
.onChange(of: locationManager.permissionState) { _ in
|
||||
updateNotesCounterSubscription()
|
||||
if case .mesh = locationManager.selectedChannel,
|
||||
locationManager.permissionState == .authorized,
|
||||
LocationChannelManager.shared.availableChannels.isEmpty {
|
||||
@@ -1545,34 +1554,6 @@ struct ContentView: View {
|
||||
|
||||
}
|
||||
|
||||
// MARK: - Notes Counter Subscription Helper
|
||||
extension ContentView {
|
||||
private func updateNotesCounterSubscription() {
|
||||
switch locationManager.selectedChannel {
|
||||
case .mesh:
|
||||
// Ensure we have a fresh one-shot location fix so building geohash is current
|
||||
if locationManager.permissionState == .authorized {
|
||||
LocationChannelManager.shared.refreshChannels()
|
||||
}
|
||||
if locationManager.permissionState == .authorized {
|
||||
if let building = LocationChannelManager.shared.availableChannels.first(where: { $0.level == .building })?.geohash {
|
||||
LocationNotesCounter.shared.subscribe(geohash: building)
|
||||
} else {
|
||||
// Keep existing subscription if we had one to avoid flicker
|
||||
// Only cancel if we have no known geohash
|
||||
if LocationNotesCounter.shared.geohash == nil {
|
||||
LocationNotesCounter.shared.cancel()
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LocationNotesCounter.shared.cancel()
|
||||
}
|
||||
case .location:
|
||||
LocationNotesCounter.shared.cancel()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Helper Views
|
||||
|
||||
// Rounded payment chip button
|
||||
@@ -1581,11 +1562,10 @@ extension ContentView {
|
||||
private enum MessageMedia {
|
||||
case voice(URL)
|
||||
case image(URL)
|
||||
case file(URL)
|
||||
|
||||
var url: URL {
|
||||
switch self {
|
||||
case .voice(let url), .image(let url), .file(let url):
|
||||
case .voice(let url), .image(let url):
|
||||
return url
|
||||
}
|
||||
}
|
||||
@@ -1623,13 +1603,6 @@ private extension ContentView {
|
||||
let url = baseDirectory.appendingPathComponent(subdir, isDirectory: true).appendingPathComponent(filename)
|
||||
return .image(url)
|
||||
}
|
||||
if message.content.hasPrefix("[file] ") {
|
||||
let filename = String(message.content.dropFirst("[file] ".count)).trimmingCharacters(in: .whitespacesAndNewlines)
|
||||
guard !filename.isEmpty else { return nil }
|
||||
let subdir = message.sender == viewModel.nickname ? "files/outgoing" : "files/incoming"
|
||||
let url = baseDirectory.appendingPathComponent(subdir, isDirectory: true).appendingPathComponent(filename)
|
||||
return .file(url)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1721,13 +1694,6 @@ private extension ContentView {
|
||||
} : nil
|
||||
)
|
||||
.frame(maxWidth: 280)
|
||||
case .file(let url):
|
||||
FileAttachmentView(
|
||||
url: url,
|
||||
isSending: state.isSending,
|
||||
progress: state.progress,
|
||||
onCancel: cancelAction
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1862,13 +1828,33 @@ private extension ContentView {
|
||||
}
|
||||
|
||||
var attachmentButton: some View {
|
||||
Button(action: { showAttachmentActions = true }) {
|
||||
Image(systemName: "paperclip.circle.fill")
|
||||
#if os(iOS)
|
||||
Image(systemName: "camera.circle.fill")
|
||||
.font(.bitchatSystem(size: 24))
|
||||
.foregroundColor(composerAccentColor)
|
||||
.frame(width: 36, height: 36)
|
||||
.contentShape(Circle())
|
||||
.onTapGesture {
|
||||
// Tap = Photo Library
|
||||
imagePickerSourceType = .photoLibrary
|
||||
showImagePicker = true
|
||||
}
|
||||
.onLongPressGesture(minimumDuration: 0.3) {
|
||||
// Long press = Camera
|
||||
imagePickerSourceType = .camera
|
||||
showImagePicker = true
|
||||
}
|
||||
.accessibilityLabel("Tap for library, long press for camera")
|
||||
#else
|
||||
Button(action: { showMacImagePicker = true }) {
|
||||
Image(systemName: "photo.circle.fill")
|
||||
.font(.bitchatSystem(size: 24))
|
||||
.foregroundColor(composerAccentColor)
|
||||
.frame(width: 36, height: 36)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
.accessibilityLabel("Choose photo")
|
||||
#endif
|
||||
}
|
||||
|
||||
var sendOrMicButton: some View {
|
||||
@@ -2037,44 +2023,6 @@ private extension ContentView {
|
||||
}
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
func handlePhotoSelection(_ item: PhotosPickerItem) async {
|
||||
defer { Task { @MainActor in selectedPhotoPickerItem = nil } }
|
||||
do {
|
||||
if let data = try await item.loadTransferable(type: Data.self) {
|
||||
let tempURL = FileManager.default.temporaryDirectory
|
||||
.appendingPathComponent(UUID().uuidString)
|
||||
.appendingPathExtension("jpg")
|
||||
try data.write(to: tempURL, options: Data.WritingOptions.atomic)
|
||||
await MainActor.run {
|
||||
viewModel.sendImage(from: tempURL) {
|
||||
try? FileManager.default.removeItem(at: tempURL)
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
SecureLogger.error("Photo picker load failed: \(error)", category: .session)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
func handleImportedFile(url: URL) async {
|
||||
do {
|
||||
let fileManager = FileManager.default
|
||||
let tempDir = fileManager.temporaryDirectory
|
||||
let fileName = url.lastPathComponent.isEmpty ? "attachment" : url.lastPathComponent
|
||||
let destination = tempDir.appendingPathComponent(UUID().uuidString + "_" + fileName)
|
||||
if fileManager.fileExists(atPath: destination.path) {
|
||||
try fileManager.removeItem(at: destination)
|
||||
}
|
||||
try fileManager.copyItem(at: url, to: destination)
|
||||
await MainActor.run {
|
||||
viewModel.sendFileAttachment(from: destination)
|
||||
}
|
||||
} catch {
|
||||
SecureLogger.error("File copy failed before send: \(error)", category: .session)
|
||||
}
|
||||
}
|
||||
|
||||
func applicationFilesDirectory() -> URL? {
|
||||
// Cache the directory lookup to avoid repeated FileManager calls during view rendering
|
||||
@@ -2218,3 +2166,87 @@ struct ImagePreviewView: View {
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
// MARK: - Image Picker (Camera or Photo Library)
|
||||
struct ImagePickerView: UIViewControllerRepresentable {
|
||||
let sourceType: UIImagePickerController.SourceType
|
||||
let completion: (UIImage?) -> Void
|
||||
|
||||
func makeUIViewController(context: Context) -> UIImagePickerController {
|
||||
let picker = UIImagePickerController()
|
||||
picker.sourceType = sourceType
|
||||
picker.delegate = context.coordinator
|
||||
picker.allowsEditing = false
|
||||
|
||||
// Use standard full screen - iOS handles safe areas automatically
|
||||
picker.modalPresentationStyle = .fullScreen
|
||||
|
||||
// Force dark mode to make safe area bars black instead of white
|
||||
picker.overrideUserInterfaceStyle = .dark
|
||||
|
||||
return picker
|
||||
}
|
||||
|
||||
func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context) {}
|
||||
|
||||
func makeCoordinator() -> Coordinator {
|
||||
Coordinator(completion: completion)
|
||||
}
|
||||
|
||||
class Coordinator: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
|
||||
let completion: (UIImage?) -> Void
|
||||
|
||||
init(completion: @escaping (UIImage?) -> Void) {
|
||||
self.completion = completion
|
||||
}
|
||||
|
||||
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
|
||||
let image = info[.originalImage] as? UIImage
|
||||
completion(image)
|
||||
}
|
||||
|
||||
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
|
||||
completion(nil)
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if os(macOS)
|
||||
// MARK: - macOS Image Picker
|
||||
struct MacImagePickerView: View {
|
||||
let completion: (URL?) -> Void
|
||||
@Environment(\.dismiss) private var dismiss
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 16) {
|
||||
Text("Choose an image")
|
||||
.font(.headline)
|
||||
|
||||
Button("Select Image") {
|
||||
let panel = NSOpenPanel()
|
||||
panel.allowsMultipleSelection = false
|
||||
panel.canChooseDirectories = false
|
||||
panel.canChooseFiles = true
|
||||
panel.allowedContentTypes = [.image, .png, .jpeg, .heic]
|
||||
panel.message = "Choose an image to send"
|
||||
|
||||
if panel.runModal() == .OK {
|
||||
completion(panel.url)
|
||||
} else {
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
.buttonStyle(.borderedProminent)
|
||||
|
||||
Button("Cancel") {
|
||||
completion(nil)
|
||||
}
|
||||
.buttonStyle(.bordered)
|
||||
}
|
||||
.padding(40)
|
||||
.frame(minWidth: 300, minHeight: 150)
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -141,7 +141,7 @@ struct LocationNotesView: View {
|
||||
String(
|
||||
format: String(localized: "location_notes.header", comment: "Header displaying the geohash and localized note count"),
|
||||
locale: .current,
|
||||
geohash, count
|
||||
"\(geohash) ± 1", count
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
import SwiftUI
|
||||
|
||||
struct FileAttachmentView: View {
|
||||
private let url: URL
|
||||
private let isSending: Bool
|
||||
private let progress: Double?
|
||||
private let onCancel: (() -> Void)?
|
||||
|
||||
@Environment(\.colorScheme) private var colorScheme
|
||||
#if os(iOS)
|
||||
@State private var showExporter = false
|
||||
#endif
|
||||
|
||||
init(url: URL, isSending: Bool, progress: Double?, onCancel: (() -> Void)?) {
|
||||
self.url = url
|
||||
self.isSending = isSending
|
||||
self.progress = progress
|
||||
self.onCancel = onCancel
|
||||
}
|
||||
|
||||
private var fileName: String {
|
||||
url.lastPathComponent
|
||||
}
|
||||
|
||||
private var normalizedProgress: Double? {
|
||||
guard let progress = progress else { return nil }
|
||||
return max(0, min(1, progress))
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
HStack(alignment: .center, spacing: 12) {
|
||||
Image(systemName: "doc.fill")
|
||||
.foregroundColor(Color.blue)
|
||||
.font(.bitchatSystem(size: 24))
|
||||
|
||||
VStack(alignment: .leading, spacing: 4) {
|
||||
Text(fileName)
|
||||
.font(.bitchatSystem(size: 14, weight: .medium))
|
||||
.foregroundColor(.primary)
|
||||
.lineLimit(2)
|
||||
Text(url.lastPathComponent)
|
||||
.font(.bitchatSystem(size: 11, design: .monospaced))
|
||||
.foregroundColor(.secondary)
|
||||
.lineLimit(1)
|
||||
if let progress = normalizedProgress {
|
||||
ProgressView(value: progress)
|
||||
.progressViewStyle(.linear)
|
||||
.tint(Color.blue)
|
||||
}
|
||||
}
|
||||
|
||||
Spacer()
|
||||
|
||||
Button(action: openFile) {
|
||||
Text("open", comment: "Button to open attached file")
|
||||
.font(.bitchatSystem(size: 13, weight: .semibold))
|
||||
.padding(.horizontal, 12)
|
||||
.padding(.vertical, 6)
|
||||
.background(
|
||||
Capsule().fill(Color.blue.opacity(0.15))
|
||||
)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
|
||||
if let onCancel = onCancel, isSending {
|
||||
Button(action: onCancel) {
|
||||
Image(systemName: "xmark")
|
||||
.font(.bitchatSystem(size: 11, weight: .bold))
|
||||
.frame(width: 26, height: 26)
|
||||
.background(Circle().fill(Color.red.opacity(0.9)))
|
||||
.foregroundColor(.white)
|
||||
}
|
||||
.buttonStyle(.plain)
|
||||
}
|
||||
}
|
||||
.padding(12)
|
||||
.background(
|
||||
RoundedRectangle(cornerRadius: 14)
|
||||
.fill(colorScheme == .dark ? Color.black.opacity(0.6) : Color.white)
|
||||
)
|
||||
.overlay(
|
||||
RoundedRectangle(cornerRadius: 14)
|
||||
.stroke(Color.gray.opacity(0.2), lineWidth: 1)
|
||||
)
|
||||
#if os(iOS)
|
||||
.sheet(isPresented: $showExporter) {
|
||||
FileExportController(url: url)
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
private func openFile() {
|
||||
#if os(iOS)
|
||||
showExporter = true
|
||||
#else
|
||||
NSWorkspace.shared.open(url)
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
import UniformTypeIdentifiers
|
||||
import UIKit
|
||||
|
||||
private struct FileExportController: UIViewControllerRepresentable {
|
||||
let url: URL
|
||||
|
||||
func makeUIViewController(context: Context) -> UIDocumentPickerViewController {
|
||||
let controller = UIDocumentPickerViewController(forExporting: [url])
|
||||
controller.shouldShowFileExtensions = true
|
||||
return controller
|
||||
}
|
||||
|
||||
func updateUIViewController(_ uiViewController: UIDocumentPickerViewController, context: Context) {}
|
||||
}
|
||||
#else
|
||||
import AppKit
|
||||
#endif
|
||||
@@ -91,61 +91,3 @@ struct LocationNotesManagerTests {
|
||||
case shouldNotDerive
|
||||
}
|
||||
}
|
||||
|
||||
@MainActor
|
||||
struct LocationNotesCounterTests {
|
||||
@Test func subscribeWithoutRelaysMarksUnavailable() {
|
||||
var subscribeCalled = false
|
||||
let deps = LocationNotesCounterDependencies(
|
||||
relayLookup: { _, _ in [] },
|
||||
subscribe: { _, _, _, _, _ in subscribeCalled = true },
|
||||
unsubscribe: { _ in }
|
||||
)
|
||||
|
||||
let counter = LocationNotesCounter(testDependencies: deps)
|
||||
counter.subscribe(geohash: "u4pruydq")
|
||||
|
||||
#expect(!subscribeCalled)
|
||||
#expect(!counter.relayAvailable)
|
||||
#expect(counter.initialLoadComplete)
|
||||
#expect(counter.count == 0)
|
||||
}
|
||||
|
||||
@Test func subscribeCountsUniqueNotes() {
|
||||
var storedHandler: ((NostrEvent) -> Void)?
|
||||
var storedEOSE: (() -> Void)?
|
||||
let deps = LocationNotesCounterDependencies(
|
||||
relayLookup: { _, _ in ["wss://relay.geo"] },
|
||||
subscribe: { filter, id, relays, handler, eose in
|
||||
#expect(relays == ["wss://relay.geo"])
|
||||
#expect(filter.kinds == [1])
|
||||
#expect(!id.isEmpty)
|
||||
storedHandler = handler
|
||||
storedEOSE = eose
|
||||
},
|
||||
unsubscribe: { _ in }
|
||||
)
|
||||
|
||||
let counter = LocationNotesCounter(testDependencies: deps)
|
||||
counter.subscribe(geohash: "u4pruydq")
|
||||
|
||||
var first = NostrEvent(
|
||||
pubkey: "pub",
|
||||
createdAt: Date(),
|
||||
kind: .textNote,
|
||||
tags: [["g", "u4pruydq"]],
|
||||
content: "a"
|
||||
)
|
||||
first.id = "eventA"
|
||||
storedHandler?(first)
|
||||
|
||||
let duplicate = first
|
||||
storedHandler?(duplicate)
|
||||
|
||||
storedEOSE?()
|
||||
|
||||
#expect(counter.relayAvailable)
|
||||
#expect(counter.count == 1)
|
||||
#expect(counter.initialLoadComplete)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user