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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
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<String>) {
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<String> = []
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
}
}