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
import Foundation
import SwiftUI
extension AppModel {
func loadModeration(actorID: String) async -> Bool {
guard account != nil, !isSigningOut, moderationBusy.insert(actorID).inserted else { return false }
let session = moderationSession
defer { if session == moderationSession { moderationBusy.remove(actorID) } }
do {
let state = try await service.moderationState(actorID: actorID)
guard session == moderationSession else { return false }
var merged = state
if let previous = moderationStates[actorID] {
if uncertainModerationIDs[actorID]?.contains(.mute) == true { merged.isMuted = previous.isMuted }
if uncertainModerationIDs[actorID]?.contains(.block) == true { merged.blockURI = previous.blockURI }
}
moderationStates[actorID] = merged
return true
} catch {
if session == moderationSession, !Task.isCancelled, !NetworkFailure.isCancellation(error) {
moderationError = "Could not load account controls. \(NetworkFailure.message(for: error))"
}
return false
}
}
func hideAccountFromTimeline(actorID: String, block: Bool) async {
let action: ModerationAction = block ? .block : .mute
guard account != nil, !isSigningOut, uncertainModerationIDs[actorID]?.contains(action) != true,
moderationBusy.insert(actorID).inserted
else { return }
let session = moderationSession
defer { if session == moderationSession { moderationBusy.remove(actorID) } }
do {
var state = moderationStates[actorID] ?? ModerationState()
if block {
guard !state.isBlocked else { return }
state.blockURI = try await service.setBlocked(true, actorID: actorID, blockURI: nil)
} else {
guard !state.isMuted else { return }
try await service.setMuted(true, actorID: actorID)
state.isMuted = true
}
guard session == moderationSession else { return }
moderationStates[actorID] = state
} catch {
if let message = moderationWriteFailureMessage(error, actorID: actorID, action: action, session: session) {
errorMessage = message
}
}
}
func setModeration(actorID: String, block: Bool) async {
let action: ModerationAction = block ? .block : .mute
guard account != nil, !isSigningOut, uncertainModerationIDs[actorID]?.contains(action) != true,
var state = moderationStates[actorID], moderationBusy.insert(actorID).inserted
else { return }
let session = moderationSession
let wasHidden = state.isMuted || state.isBlocked
defer { if session == moderationSession { moderationBusy.remove(actorID) } }
do {
if block {
state.blockURI = try await service.setBlocked(
!state.isBlocked, actorID: actorID, blockURI: state.blockURI)
} else {
try await service.setMuted(!state.isMuted, actorID: actorID)
state.isMuted.toggle()
}
guard session == moderationSession else { return }
moderationStates[actorID] = state
if wasHidden && !state.isMuted && !state.isBlocked,
authorPosts[actorID] != nil || loadingProfileIDs.contains(actorID) {
profileRequests[actorID] = nil
moreProfileRequests[actorID] = nil
loadingProfileIDs.remove(actorID)
loadingMoreProfileIDs.remove(actorID)
authorPosts[actorID] = nil
profileCursors[actorID] = nil
profileRepostedBy[actorID] = nil
await loadProfile(actorID: actorID)
}
} catch {
if let message = moderationWriteFailureMessage(error, actorID: actorID, action: action, session: session) {
moderationError = message
}
}
}
func submitReport(_ subject: ReportSubject, reason: String) async -> Bool {
let key = "report"
guard account != nil, !isSigningOut, !uncertainReports.contains(subject.retryKey),
moderationBusy.insert(key).inserted
else { return false }
let session = moderationSession
defer { if session == moderationSession { moderationBusy.remove(key) } }
do {
try await service.report(subject, reason: reason)
return session == moderationSession
} catch let error as ActionNotSentError {
if session == moderationSession, !NetworkFailure.isCancellation(error) {
moderationError = "The report was not sent. \(NetworkFailure.message(for: error))"
}
return false
} catch ModerationWriteError.rejected(let message) {
if session == moderationSession { moderationError = "The report was not sent. \(message)" }
return false
} catch {
if session == moderationSession {
uncertainReports.insert(subject.retryKey)
moderationError = "The report result is unknown. Sending it again can create a duplicate report."
}
return false
}
}
func toggleFollow(actorID: String) async {
guard account != nil, !isSigningOut, !uncertainFollowIDs.contains(actorID),
activeFollowIDs.insert(actorID).inserted
else { return }
let generation = sessionGeneration
let session = moderationSession
let accountID = account?.id
defer { if generation == sessionGeneration, session == moderationSession { activeFollowIDs.remove(actorID) } }
let newValue = !(followResults[actorID] ?? followingActorIDs.contains(actorID))
do {
_ = try await service.setFollowing(newValue, actorID: actorID)
guard generation == sessionGeneration, session == moderationSession, !isSigningOut, account?.id == accountID
else { return }
withContentUpdates {
followResults[actorID] = newValue
if newValue {
followingActorIDs.insert(actorID)
} else {
followingActorIDs.remove(actorID)
}
if var profile = profiles[actorID] {
profile.isFollowing = newValue
profiles[actorID] = profile
}
}
} catch {
guard generation == sessionGeneration, session == moderationSession, !isSigningOut, account?.id == accountID
else { return }
if error is ActionNotSentError
|| (error as? ServiceError).map({
if case .noSession = $0 { return true }
return false
}) == true {
if !NetworkFailure.isCancellation(error) { errorMessage = NetworkFailure.message(for: error) }
} else {
uncertainFollowIDs.insert(actorID)
errorMessage =
"\(error.localizedDescription)\n\nBluesky may have received this "
+ "change. Follow actions for this account are unavailable for this session."
}
}
}
private func moderationWriteFailureMessage(
_ error: Error, actorID: String, action: ModerationAction, session: UUID
) -> String? {
guard session == moderationSession else { return nil }
if let error = error as? ActionNotSentError {
guard !NetworkFailure.isCancellation(error) else { return nil }
return "The action was not sent. \(NetworkFailure.message(for: error))"
}
if let error = error as? ModerationWriteError, case .rejected(let message) = error {
return "The action was not sent. \(message)"
}
uncertainModerationIDs[actorID, default: []].insert(action)
return "The action may have succeeded. Check this account in Bluesky before changing it again."
}
}