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
import Foundation
import SwiftUI
extension AppModel {
func canDelete(_ post: Post) -> Bool {
account?.id == post.author.id && account != nil && !isSigningOut
}
func deletePost(id: String) async {
guard let original = post(id: id), canDelete(original), activePostActions.insert(id).inserted else { return }
let generation = sessionGeneration
defer { if generation == sessionGeneration { activePostActions.remove(id) } }
do {
try await service.deletePost(original)
guard generation == sessionGeneration else { return }
contentGeneration = UUID()
deletedPostIDs.insert(id)
removePost(id: id)
} catch {
guard generation == sessionGeneration else { return }
if error is ActionNotSentError
|| (error as? ServiceError).map({
if case .noSession = $0 { return true }
return false
}) == true {
if !NetworkFailure.isCancellation(error) { errorMessage = "Could not delete the post. Try again." }
} else {
errorMessage =
"Could not confirm that the post was deleted. Refresh the feed and try again if it is still there."
}
}
}
func toggleLike(postID: String) async {
guard account != nil, !isSigningOut, !uncertainPostIDs.contains(postID), let original = post(id: postID),
activePostActions.insert(postID).inserted
else { return }
let generation = sessionGeneration
defer { if generation == sessionGeneration { activePostActions.remove(postID) } }
do {
let uri = try await service.toggleLike(post: original, liked: !original.isLiked)
guard generation == sessionGeneration, var updated = post(id: postID) else { return }
let liked = !original.isLiked
if updated.isLiked != liked {
updated.likeCount = max(0, updated.likeCount + (liked ? 1 : -1))
}
updated.likeURI = uri
updated.isLiked = liked
updatePost(updated)
} catch {
guard generation == sessionGeneration 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 {
uncertainPostIDs.insert(postID)
errorMessage =
"\(error.localizedDescription)\n\nBluesky may have received this change. "
+ "This post's actions are unavailable for this session."
}
}
}
func toggleRepost(postID: String) async {
guard account != nil, !isSigningOut, !uncertainPostIDs.contains(postID), let original = post(id: postID),
activePostActions.insert(postID).inserted
else { return }
let generation = sessionGeneration
defer { if generation == sessionGeneration { activePostActions.remove(postID) } }
do {
let uri = try await service.toggleRepost(post: original, reposted: !original.isReposted)
guard generation == sessionGeneration, var updated = post(id: postID) else { return }
let reposted = !original.isReposted
if updated.isReposted != reposted {
updated.repostCount = max(0, updated.repostCount + (reposted ? 1 : -1))
}
updated.repostURI = uri
updated.isReposted = reposted
updatePost(updated)
} catch {
guard generation == sessionGeneration 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 {
uncertainPostIDs.insert(postID)
errorMessage =
"\(error.localizedDescription)\n\nBluesky may have received this change. "
+ "This post's actions are unavailable for this session."
}
}
}
}