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
import Foundation
struct NotificationPage: Equatable {
let notifications: [FeedNotification]
let cursor: String?
}
struct FeedNotification: Identifiable, Equatable {
enum Reason: String, CaseIterable, Hashable {
case like
case repost
case follow
case mention
case reply
case quote
case unknown
var filterLabel: String {
switch self {
case .like: "Likes"
case .repost: "Reposts"
case .follow: "Follows"
case .mention: "Mentions"
case .reply: "Replies"
case .quote: "Quotes"
case .unknown: "Other"
}
}
var message: String {
switch self {
case .like: "liked your post"
case .repost: "reposted your post"
case .follow: "followed you"
case .mention: "mentioned you"
case .reply: "replied to your post"
case .quote: "quoted your post"
case .unknown: "interacted with you"
}
}
var includesPost: Bool {
switch self {
case .like, .repost, .mention, .reply, .quote: true
case .follow, .unknown: false
}
}
var symbol: String {
switch self {
case .like: "heart.fill"
case .repost: "arrow.2.squarepath"
case .follow: "person.badge.plus"
case .mention, .reply: "bubble.left"
case .quote: "quote.bubble"
case .unknown: "bell"
}
}
}
let id: String
let author: Post.Author
let reason: Reason
let indexedAt: Date
var isRead: Bool
let postID: String?
var relatedPost: Post?
var parentPost: Post?
var isFollowedBy: Bool = false
func replyIsToAccount(_ accountID: String?) -> Bool {
guard let accountID, let parentPost else { return false }
return parentPost.author.id == accountID
}
}
struct NotificationFilter {
var mutualsOnly: Bool
var excludedTypes: Set<FeedNotification.Reason>
init(mutualsOnly: Bool, excludedTypesRaw: String) {
self.mutualsOnly = mutualsOnly
let excluded = Set(
excludedTypesRaw.split(separator: ",").compactMap { FeedNotification.Reason(rawValue: String($0)) })
self.excludedTypes = excluded.count == FeedNotification.Reason.allCases.count ? [] : excluded
}
var isActive: Bool { mutualsOnly || !excludedTypes.isEmpty }
var showsAllTypes: Bool { excludedTypes.isEmpty }
func typeIsSelected(_ reason: FeedNotification.Reason) -> Bool {
!showsAllTypes && !excludedTypes.contains(reason)
}
func settingType(_ reason: FeedNotification.Reason, enabled: Bool) -> String {
let all = Set(FeedNotification.Reason.allCases)
var excluded = showsAllTypes && enabled ? all : excludedTypes
if enabled { excluded.remove(reason) } else { excluded.insert(reason) }
guard excluded.count < all.count else { return excludedTypes.map(\.rawValue).sorted().joined(separator: ",") }
return excluded.map(\.rawValue).sorted().joined(separator: ",")
}
func includes(_ notification: FeedNotification, follows: Bool) -> Bool {
!excludedTypes.contains(notification.reason) && (!mutualsOnly || (follows && notification.isFollowedBy))
}
}