Files
Telegram-iOS/submodules/TelegramUI/Sources/ClearNotificationsManager.swift
T
isaacisaacClaude Opus 4.7
86d1456552 Postbox -> TelegramEngine waves 107-137 (squashed)
31 waves of consumer-side migration from `import Postbox` to TelegramEngine
typealiases. Net: 173 import drops + 39 BUILD-dep drops + 1 new typealias
(`EngineStoryId = StoryId`, wave 113).

Wave shapes used:
- Orphan-import sweeps (107, 108, 128): drop `import Postbox` from files
  whose only Postbox-symbol reference was the import line itself, then
  resolve build failures. Methodology requires token-level (`grep -oE`)
  filtering, not line-level, to avoid masking real Postbox usage on lines
  that also contain `Namespaces.X` references.
- Identifier-swap mini-waves (109-127, 129-134, 136-137): rename
  Postbox-typealiased identifiers to engine equivalents
  (PeerId -> EnginePeer.Id, MessageId -> EngineMessage.Id,
  MediaId -> EngineMedia.Id, MessageIndex -> EngineMessage.Index,
  StoryId -> EngineStoryId, ItemCollectionId -> EngineItemCollectionId,
  PreferencesEntry -> EnginePreferencesEntry,
  FetchResourceSourceType/Error -> EngineFetchResourceSourceType/Error,
  MemoryBuffer -> EngineMemoryBuffer, MessageTags -> EngineMessage.Tags,
  MessageAttribute -> EngineMessage.Attribute,
  TempBox -> EngineTempBox).
- Asset-string FP-only orphans (124).
- Typealias addition + drain (113): added `EngineStoryId` typealias to
  TelegramCore, then drained 3+11 consumer sites.

Hard blockers identified during these waves (must restore `import Postbox`
when present): MediaResource[A-Za-z]* (any suffix -- the literal
`MediaResource` matches don't catch MediaResourceData/MediaResourceId/etc.),
Postbox/MediaBox/MediaResource raw types, PostboxCoding/PostboxEncoder/
PostboxDecoder, TempBoxFile, ValueBoxKey, PostboxView, combinedView,
HashFunctions, postboxLog, openPostbox, declareEncodable, PeerView,
MessageHistoryView, MessageHistoryThreadData, CachedPeerData, RenderedPeer,
SelectivePrivacyPeer, SimpleDictionary, ItemCollectionInfosView,
ItemCollectionItem, ItemCollectionItemIndex, ItemCollectionViewEntryIndex,
ChatListIndex, ChatListEntrySummaryComponents, CodableEntry,
MessageHistoryThread, MessageHistoryAnchorIndex,
MessageHistoryEntryLocation, PeerStoryStats, PeerNameIndex,
PeerSummaryCounterTags, ChatListTotalUnreadStateCategory/Stats,
arePeersEqual. Protocol-shape blockers: bare `Peer`/`Message`/`Media`
in function signatures, generic args, enum-case payloads, or dict value
types (e.g., `[PeerId: Peer]`, `case messages([Message])`,
`Signal<(Peer?, ...), NoError>`).

`replace_all PeerId -> EnginePeer.Id` is dangerous: mangles compound
names like `failedPeerId`, `ContactListPeerId`, `nextRemoteMediaId`,
`replyToMessageId`. Pre-flight grep `\b[a-z][a-zA-Z]*PeerId\b` and only
replace_all if 0 matches.

Also removes unneeded design/plan docs from a separate (link-highlighting)
feature branch:
- docs/superpowers/plans/2026-05-02-link-highlighting-modern-path-fixes.md
- docs/superpowers/specs/2026-05-02-link-highlighting-modern-path-fixes-design.md

Squashed commits: 6d82c2980d..e6de5d53a3 (59 commits, including
per-wave content commits and per-wave CLAUDE.md bumps).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-03 10:28:50 +02:00

152 lines
5.7 KiB
Swift

import Foundation
import SwiftSignalKit
import TelegramCore
private let messageNotificationKeyExpr = try? NSRegularExpression(pattern: "m([-\\d]+):([-\\d]+):([-\\d]+)_?", options: [])
public enum NotificationManagedNotificationRequestId: Hashable {
case messageId(EngineMessage.Id)
case globallyUniqueId(Int64, EnginePeer.Id?)
public init?(string: String) {
if string.hasPrefix("m") {
let matches = messageNotificationKeyExpr!.matches(in: string, options: [], range: NSRange(location: 0, length: string.count))
if let match = matches.first {
let nsString = string as NSString
let peerIdString = nsString.substring(with: match.range(at: 1))
let namespaceString = nsString.substring(with: match.range(at: 2))
let idString = nsString.substring(with: match.range(at: 3))
guard let peerId = Int64(peerIdString) else {
return nil
}
guard let namespace = Int32(namespaceString) else {
return nil
}
guard let id = Int32(idString) else {
return nil
}
self = .messageId(EngineMessage.Id(peerId: EnginePeer.Id(peerId), namespace: namespace, id: id))
return
}
}
return nil
}
}
public final class ClearNotificationIdsCompletion {
public let f: ([(String, NotificationManagedNotificationRequestId)]) -> Void
public init(f: @escaping ([(String, NotificationManagedNotificationRequestId)]) -> Void) {
self.f = f
}
}
public final class ClearNotificationsManager {
private let getNotificationIds: (ClearNotificationIdsCompletion) -> Void
private let getPendingNotificationIds: (ClearNotificationIdsCompletion) -> Void
private let removeNotificationIds: ([String]) -> Void
private let removePendingNotificationIds: ([String]) -> Void
private var ids: [EnginePeer.Id: EngineMessage.Id] = [:]
private var timer: SwiftSignalKit.Timer?
public init(getNotificationIds: @escaping (ClearNotificationIdsCompletion) -> Void, removeNotificationIds: @escaping ([String]) -> Void, getPendingNotificationIds: @escaping (ClearNotificationIdsCompletion) -> Void, removePendingNotificationIds: @escaping ([String]) -> Void) {
self.getNotificationIds = getNotificationIds
self.removeNotificationIds = removeNotificationIds
self.getPendingNotificationIds = getPendingNotificationIds
self.removePendingNotificationIds = removePendingNotificationIds
}
deinit {
self.timer?.invalidate()
}
public func clearAll() {
self.getNotificationIds(ClearNotificationIdsCompletion { [weak self] result in
Queue.mainQueue().async {
var removeKeys: [String] = []
for (identifier, _) in result {
removeKeys.append(identifier)
}
if let strongSelf = self, !removeKeys.isEmpty {
strongSelf.removeNotificationIds(removeKeys)
}
}
})
self.getPendingNotificationIds(ClearNotificationIdsCompletion { [weak self] result in
Queue.mainQueue().async {
var removeKeys: [String] = []
for (identifier, _) in result {
removeKeys.append(identifier)
}
if let strongSelf = self, !removeKeys.isEmpty {
strongSelf.removePendingNotificationIds(removeKeys)
}
}
})
}
public func append(_ id: EngineMessage.Id) {
if let current = self.ids[id.peerId] {
if current < id {
self.ids[id.peerId] = id
}
} else {
self.ids[id.peerId] = id
}
self.timer?.invalidate()
let timer = SwiftSignalKit.Timer(timeout: 2.0, repeat: false, completion: { [weak self] in
self?.commitNow()
}, queue: Queue.mainQueue())
self.timer = timer
timer.start()
}
public func commitNow() {
self.timer?.invalidate()
self.timer = nil
let ids = self.ids
self.ids.removeAll()
self.getNotificationIds(ClearNotificationIdsCompletion { [weak self] result in
Queue.mainQueue().async {
var removeKeys: [String] = []
for (identifier, requestId) in result {
if case let .messageId(messageId) = requestId {
if let maxId = ids[messageId.peerId], messageId <= maxId {
removeKeys.append(identifier)
}
}
}
if let strongSelf = self, !removeKeys.isEmpty {
strongSelf.removeNotificationIds(removeKeys)
}
}
})
self.getPendingNotificationIds(ClearNotificationIdsCompletion { [weak self] result in
Queue.mainQueue().async {
var removeKeys: [String] = []
for (identifier, requestId) in result {
if case let .messageId(messageId) = requestId {
if let maxId = ids[messageId.peerId], messageId <= maxId {
removeKeys.append(identifier)
}
}
}
if let strongSelf = self, !removeKeys.isEmpty {
strongSelf.removePendingNotificationIds(removeKeys)
}
}
})
}
}