1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
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<String> = []
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) }
}
}