mirror of
https://github.com/permissionlesstech/bitchat.git
synced 2026-07-25 04:05:20 +00:00
- Add testable ChatViewModel initializer accepting Transport dependency - Create MockTransport implementing full Transport protocol for testing - Add 21 unit tests covering: - Initialization (delegate, services, nickname) - Message sending (basic, empty, mentions, commands) - Message receiving (delegate calls, public messages) - Peer connections (connect, disconnect, isPeerConnected) - Deduplication service integration - Private chat routing - Bluetooth state handling - Panic clear functionality - Guard NotificationService methods against test environment crashes - Make deduplicationService internal for test access All 303 tests pass.
106 lines
3.5 KiB
Swift
106 lines
3.5 KiB
Swift
//
|
|
// NotificationService.swift
|
|
// bitchat
|
|
//
|
|
// This is free and unencumbered software released into the public domain.
|
|
// For more information, see <https://unlicense.org>
|
|
//
|
|
|
|
import Foundation
|
|
import UserNotifications
|
|
#if os(iOS)
|
|
import UIKit
|
|
#elseif os(macOS)
|
|
import AppKit
|
|
#endif
|
|
|
|
final class NotificationService {
|
|
static let shared = NotificationService()
|
|
|
|
/// Returns true if running in test environment (XCTest, Swift Testing, or SPM tests)
|
|
private var isRunningTests: Bool {
|
|
NSClassFromString("XCTestCase") != nil ||
|
|
ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] != nil ||
|
|
Bundle.main.bundleIdentifier == nil
|
|
}
|
|
|
|
private init() {}
|
|
|
|
func requestAuthorization() {
|
|
guard !isRunningTests else { return }
|
|
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
|
|
if granted {
|
|
// Permission granted
|
|
} else {
|
|
// Permission denied
|
|
}
|
|
}
|
|
}
|
|
|
|
func sendLocalNotification(
|
|
title: String,
|
|
body: String,
|
|
identifier: String,
|
|
userInfo: [String: Any]? = nil,
|
|
interruptionLevel: UNNotificationInterruptionLevel = .active
|
|
) {
|
|
guard !isRunningTests else { return }
|
|
let content = UNMutableNotificationContent()
|
|
content.title = title
|
|
content.body = body
|
|
content.sound = .default
|
|
content.interruptionLevel = interruptionLevel
|
|
|
|
if let userInfo = userInfo {
|
|
content.userInfo = userInfo
|
|
}
|
|
|
|
let request = UNNotificationRequest(
|
|
identifier: identifier,
|
|
content: content,
|
|
trigger: nil // Deliver immediately
|
|
)
|
|
|
|
UNUserNotificationCenter.current().add(request)
|
|
}
|
|
|
|
func sendMentionNotification(from sender: String, message: String) {
|
|
let title = "🫵 you were mentioned by \(sender)"
|
|
let body = message
|
|
let identifier = "mention-\(UUID().uuidString)"
|
|
|
|
sendLocalNotification(title: title, body: body, identifier: identifier)
|
|
}
|
|
|
|
func sendPrivateMessageNotification(from sender: String, message: String, peerID: PeerID) {
|
|
let title = "🔒 DM from \(sender)"
|
|
let body = message
|
|
let identifier = "private-\(UUID().uuidString)"
|
|
let userInfo = ["peerID": peerID.id, "senderName": sender]
|
|
|
|
sendLocalNotification(title: title, body: body, identifier: identifier, userInfo: userInfo)
|
|
}
|
|
|
|
// Geohash public chat notification with deep link to a specific geohash
|
|
func sendGeohashActivityNotification(geohash: String, titlePrefix: String = "#", bodyPreview: String) {
|
|
let title = "\(titlePrefix)\(geohash)"
|
|
let identifier = "geo-activity-\(geohash)-\(Date().timeIntervalSince1970)"
|
|
let deeplink = "bitchat://geohash/\(geohash)"
|
|
let userInfo: [String: Any] = ["deeplink": deeplink]
|
|
sendLocalNotification(title: title, body: bodyPreview, identifier: identifier, userInfo: userInfo)
|
|
}
|
|
|
|
func sendNetworkAvailableNotification(peerCount: Int) {
|
|
let title = "👥 bitchatters nearby!"
|
|
let body = peerCount == 1 ? "1 person around" : "\(peerCount) people around"
|
|
let identifier = "network-available-\(Date().timeIntervalSince1970)"
|
|
|
|
sendLocalNotification(
|
|
title: title,
|
|
body: body,
|
|
identifier: identifier,
|
|
interruptionLevel: .timeSensitive
|
|
)
|
|
}
|
|
}
|