import Foundation import SwiftUI extension AppModel { func excludingDeletedQuote(_ post: Post) -> Post { guard let quote = post.quote, deletedPostIDs.contains(quote.postID) else { return post } var post = post post.quote = Post.Quote(postID: quote.postID) return post } func excludingDeletedPosts(_ page: FeedPage) -> FeedPage { FeedPage( posts: page.posts.filter { !deletedPostIDs.contains($0.id) }.map(excludingDeletedQuote), cursor: page.cursor, replyContexts: page.replyContexts.filter { !deletedPostIDs.contains($0.key) }.mapValues { context in context.filter { !deletedPostIDs.contains($0.id) }.map(excludingDeletedQuote) }, repostedBy: page.repostedBy.filter { !deletedPostIDs.contains($0.key) }, mutedPostIDs: page.mutedPostIDs ) } func excludingDeletedPosts(_ notifications: [FeedNotification]) -> [FeedNotification] { notifications.map { notification in var notification = notification if let id = notification.relatedPost?.id, deletedPostIDs.contains(id) { notification.relatedPost = nil } if let id = notification.parentPost?.id, deletedPostIDs.contains(id) { notification.parentPost = nil } notification.relatedPost = notification.relatedPost.map(excludingDeletedQuote) notification.parentPost = notification.parentPost.map(excludingDeletedQuote) return notification } } func applyMutedPostIDs(_ ids: Set) { guard !ids.isEmpty else { return } contentGeneration = UUID() for id in ids { removePost(id: id) } } // Prepare observable results in the same main-thread operation as their source data. func postViewState(for post: Post) -> PostViewState { if let state = postViewStates[post.id] { return state } let current = cachedPostsByID[post.id] ?? post let state = PostViewState(post: current) postViewStates[post.id] = state state.update( post: current, isBusy: activePostActions.contains(post.id), isUncertain: uncertainPostIDs.contains(post.id), decision: contentDecision(for: current), quotedPost: current.quote.flatMap { cachedPostsByID[$0.postID] }) state.updateReplyContext(resolvedReplyContext(for: current)) return state } func updatePostViewState(id: String) { guard let state = postViewStates[id] else { return } let post = cachedPostsByID[id] ?? state.post state.update( post: post, isBusy: activePostActions.contains(id), isUncertain: uncertainPostIDs.contains(id), decision: cachedPostsByID[id] == nil ? nil : contentDecision(for: post), quotedPost: post.quote.flatMap { cachedPostsByID[$0.postID] }) } func post(id: String) -> Post? { if preparedContentUpdateDepth > 0 { return unpreparedPost(id: id) } guard preparedDecisionInputs == nil else { return cachedPostsByID[id] } _ = postLookupRevision if let post = cachedPostsByID[id] { if let state = postViewStates[id] { return state.post } return post } return nil } func removePost(id: String) { withContentUpdates { removeCachedPost(id: id) } } func removeCachedPost(id: String) { searchPosts = searchPosts.filter { $0.id != id }.map(excludingDeletedQuote) searchReplyContexts.removeValue(forKey: id) searchReplyContexts = searchReplyContexts.mapValues { $0.filter { $0.id != id }.map(excludingDeletedQuote) } searchRepostedBy.removeValue(forKey: id) savedPosts = savedPosts.filter { $0.id != id }.map(excludingDeletedQuote) posts.removeAll { $0.id == id } for state in padStates.values { state.posts.removeAll { $0.id == id } state.posts = state.posts.map(excludingDeletedQuote) state.replyContexts.removeValue(forKey: id) state.replyContexts = state.replyContexts.mapValues { $0.filter { $0.id != id }.map(excludingDeletedQuote) } state.repostedBy.removeValue(forKey: id) } contentInputsChanged() timelinePostIDs.remove(id) replyContexts.removeValue(forKey: id) repostedBy.removeValue(forKey: id) threadContexts.removeValue(forKey: id) threadPositions.removeValue(forKey: id) for key in Array(replyContexts.keys) { replyContexts[key]?.removeAll { $0.id == id } } for key in Array(threadContexts.keys) { threadContexts[key]?.removeAll { $0.id == id } } for key in Array(authorPosts.keys) { authorPosts[key]?.removeAll { $0.id == id } profileRepostedBy[key]?.removeValue(forKey: id) } for index in notifications.indices { if notifications[index].relatedPost?.id == id { notifications[index].relatedPost = nil } if notifications[index].parentPost?.id == id { notifications[index].parentPost = nil } } posts = posts.map(excludingDeletedQuote) replyContexts = replyContexts.mapValues { $0.map(excludingDeletedQuote) } threadContexts = threadContexts.mapValues { $0.map(excludingDeletedQuote) } authorPosts = authorPosts.mapValues { $0.map(excludingDeletedQuote) } notifications = excludingDeletedPosts(notifications) } func postsForCacheUpdate(from page: FeedPage) -> [Post] { var seen: Set = [] let context = page.replyContexts.keys.sorted().flatMap { page.replyContexts[$0] ?? [] } return (page.posts + context).filter { seen.insert($0.id).inserted } } func updateFeedPostCaches(_ page: FeedPage) { let loaded = postsForCacheUpdate(from: page) let replacements = Dictionary(loaded.map { ($0.id, $0) }, uniquingKeysWith: { _, latest in latest }) func updated(_ cached: [Post]) -> [Post] { cached.map { replacements[$0.id] ?? $0 } } for state in padStates.values { state.posts = updated(state.posts) state.replyContexts = state.replyContexts.mapValues(updated) } searchPosts = updated(searchPosts) savedPosts = updated(savedPosts) searchReplyContexts = searchReplyContexts.mapValues(updated) let threads = threadContexts.mapValues(updated) if threads != threadContexts { threadContexts = threads } let profiles = authorPosts.mapValues(updated) if profiles != authorPosts { authorPosts = profiles } let activity = notifications.map { notification in var notification = notification if let post = notification.relatedPost { notification.relatedPost = replacements[post.id] ?? post } if let post = notification.parentPost { notification.parentPost = replacements[post.id] ?? post } return notification } if activity != notifications { notifications = activity } } func updatePost(_ post: Post) { withContentUpdates { updatePosts([post]) } } func updatePosts( _ loaded: [Post], appending appended: [Post] = [], mergingReplyContexts incomingReplies: [String: [Post]] = [:] ) { let replacements = Dictionary(loaded.map { ($0.id, $0) }, uniquingKeysWith: { _, latest in latest }) func updated(_ cached: [Post]) -> [Post] { cached.map { replacements[$0.id] ?? $0 } } for state in padStates.values { state.posts = updated(state.posts) state.replyContexts = state.replyContexts.mapValues(updated) } if !padStates.isEmpty { contentInputsChanged() } var updatedPosts = updated(posts) var seen = Set(updatedPosts.map(\.id)) updatedPosts.append( contentsOf: appended.filter { seen.insert($0.id).inserted }.map { replacements[$0.id] ?? $0 }) let replies = replyContexts.mapValues(updated).merging(incomingReplies) { _, new in new } searchPosts = updated(searchPosts) savedPosts = updated(savedPosts) searchReplyContexts = searchReplyContexts.mapValues(updated) let threads = threadContexts.mapValues(updated) let profiles = authorPosts.mapValues(updated) let activity = notifications.map { notification in var notification = notification if let post = notification.relatedPost { notification.relatedPost = replacements[post.id] ?? post } if let post = notification.parentPost { notification.parentPost = replacements[post.id] ?? post } return notification } if posts != updatedPosts { posts = updatedPosts } if replyContexts != replies { replyContexts = replies } if threadContexts != threads { threadContexts = threads } if authorPosts != profiles { authorPosts = profiles } if notifications != activity { notifications = activity } } private func unpreparedPost(id: String) -> Post? { if let post = postsByID[id] { return post } for collection in [replyContexts, threadContexts, authorPosts, searchReplyContexts] { for key in collection.keys.sorted() { if let post = collection[key]?.first(where: { $0.id == id }) { return post } } } if let post = unpreparedPadPost(id: id) { return post } if let post = savedPosts.first(where: { $0.id == id }) { return post } for notification in notifications { if let post = notification.relatedPost, post.id == id { return post } if let post = notification.parentPost, post.id == id { return post } } return nil } private func unpreparedPadPost(id: String) -> Post? { for state in padStates.values { if let post = state.posts.first(where: { $0.id == id }) { return post } for context in state.replyContexts.values { if let post = context.first(where: { $0.id == id }) { return post } } } return nil } }