import Foundation import SwiftUI extension AppModel { func replyContext(for post: Post) -> [Post] { guard post.replyRootURI != nil || post.replyParentURI != nil else { return [] } if let state = postViewStates[post.id], cachedPostsByID[post.id] != nil, state.post.replyRootURI == post.replyRootURI, state.post.replyParentURI == post.replyParentURI { return state.replyContext } _ = postLookupRevision return resolvedReplyContext(for: post) } func resolvedReplyContext(for post: Post) -> [Post] { guard post.replyRootURI != nil || post.replyParentURI != nil else { return [] } var context: [Post] = [] for id in [post.replyRootURI, post.replyParentURI].compactMap({ $0 }) where id != post.id { let feedPost = postsByID[id].map { self.post(id: id) ?? $0 } let padPost = padStates.values.lazy.compactMap { $0.replyContexts[post.id]?.first(where: { $0.id == id }) } .first if let item = feedPost ?? replyContexts[post.id]?.first(where: { $0.id == id }) ?? padPost, !isHidden(item.author.id), !context.contains(where: { $0.id == item.id }) { context.append(item) } } return context } func hasMissingReplyContext(between earlier: Post, and later: Post) -> Bool { guard let parentID = later.replyParentURI else { return false } return parentID != earlier.id } func hasEarlierPosts(for post: Post) -> Bool { post.replyParentURI != nil || (post.replyRootURI != nil && post.replyRootURI != post.id) } func isVisibleInContentList(_ post: Post) -> Bool { guard !isHidden(post.author.id), let decision = contentDecision(for: post) else { return false } return !decision.ui(.contentList).filter && decision.action != .hide } func threadAncestors(for selectedPost: Post) -> [Post] { guard let thread = threadContexts[selectedPost.id] else { return [] } guard let selectedIndex = thread.firstIndex(where: { $0.id == selectedPost.id }) else { return [] } return thread.prefix(selectedIndex).filter { isVisibleInContentList($0) } } func threadContinuation(after selectedPost: Post) -> [Post] { guard let positions = threadPositions[selectedPost.id], let position = positions[selectedPost.id], let thread = threadContexts[selectedPost.id], isVisibleInContentList(selectedPost) else { return [] } let partsByIndex = Dictionary( thread.compactMap { post -> (Int, String)? in guard let part = positions[post.id], part.count == position.count, part.index > position.index else { return nil } return (part.index, post.id) }, uniquingKeysWith: { first, _ in first }) var continuation: [Post] = [] var parent = selectedPost var index = position.index while index < position.count, let id = partsByIndex[index + 1], let next = post(id: id), next.replyParentURI == parent.id, next.author.id == selectedPost.author.id, isVisibleInContentList(next) { continuation.append(next) parent = next index += 1 } return continuation } func directReplies(to post: Post, inThread threadID: String? = nil) -> [Post] { let currentPost = self.post(id: post.id) ?? post var candidateIDs: [String] = [] let preferredThreadID = threadID ?? currentPost.id if let thread = threadContexts[preferredThreadID] { candidateIDs.append(contentsOf: thread.map(\.id)) } if currentPost.id != preferredThreadID, let thread = threadContexts[currentPost.id] { candidateIDs.append(contentsOf: thread.map(\.id)) } candidateIDs.append(contentsOf: posts.map(\.id)) for threadID in threadContexts.keys.sorted() where threadID != preferredThreadID && threadID != currentPost.id { candidateIDs.append(contentsOf: threadContexts[threadID]?.map(\.id) ?? []) } var seen: Set = [] return candidateIDs.compactMap { id in guard id != currentPost.id, seen.insert(id).inserted, let candidate = self.post(id: id), isVisibleInContentList(candidate) else { return nil } if let explicitParentID = candidate.replyParentURI { return explicitParentID == currentPost.id ? candidate : nil } return currentPost.replyIDs.contains(candidate.id) ? candidate : nil } } func replyCount(for post: Post) -> Int { let currentPost = self.post(id: post.id) ?? post return max(max(0, currentPost.replyCount), directReplies(to: currentPost).count) } func conversationReplies(to selectedPost: Post) -> [Post] { guard let thread = threadContexts[selectedPost.id], let selectedIndex = thread.firstIndex(where: { $0.id == selectedPost.id }) else { return directReplies(to: selectedPost) } return thread.dropFirst(selectedIndex + 1).compactMap { post(id: $0.id) }.filter { isVisibleInContentList($0) } } }