-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
User.swift
319 lines (293 loc) · 11.3 KB
/
User.swift
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
public struct User {
public var avatar: Avatar
public var erroredBackingsCount: Int?
public var facebookConnected: Bool?
public var id: Int
public var isAdmin: Bool?
public var isEmailVerified: Bool?
public var isFriend: Bool?
public var isBlocked: Bool
public var location: Location?
public var name: String
public var needsFreshFacebookToken: Bool?
public var needsPassword: Bool?
public var newsletters: NewsletterSubscriptions
public var notifications: Notifications
public var optedOutOfRecommendations: Bool?
public var showPublicProfile: Bool?
public var social: Bool?
public var stats: Stats
public var unseenActivityCount: Int?
public struct Avatar {
public var large: String?
public var medium: String
public var small: String
}
public struct NewsletterSubscriptions {
public var arts: Bool?
public var games: Bool?
public var happening: Bool?
public var invent: Bool?
public var promo: Bool?
public var weekly: Bool?
public var films: Bool?
public var publishing: Bool?
public var alumni: Bool?
public var music: Bool?
public static func all(on: Bool) -> NewsletterSubscriptions {
return NewsletterSubscriptions(
arts: on,
games: on,
happening: on,
invent: on,
promo: on,
weekly: on,
films: on,
publishing: on,
alumni: on,
music: on
)
}
}
public struct Notifications {
public var backings: Bool?
public var commentReplies: Bool?
public var comments: Bool?
public var creatorDigest: Bool?
public var creatorTips: Bool?
public var follower: Bool?
public var friendActivity: Bool?
public var messages: Bool?
public var mobileBackings: Bool?
public var mobileComments: Bool?
public var mobileFollower: Bool?
public var mobileFriendActivity: Bool?
public var mobileMarketingUpdate: Bool?
public var mobileMessages: Bool?
public var mobilePostLikes: Bool?
public var mobileUpdates: Bool?
public var postLikes: Bool?
public var updates: Bool?
}
public struct Stats {
public var backedProjectsCount: Int?
public var createdProjectsCount: Int?
public var draftProjectsCount: Int?
public var memberProjectsCount: Int?
public var starredProjectsCount: Int?
public var unansweredSurveysCount: Int?
public var unreadMessagesCount: Int?
}
public var isCreator: Bool {
return (self.stats.createdProjectsCount ?? 0) > 0
}
public var isRepeatCreator: Bool? {
guard let createdProjectsCount = self.stats.createdProjectsCount else {
return nil
}
return createdProjectsCount > 1
}
}
extension User: Equatable {}
public func == (lhs: User, rhs: User) -> Bool {
return lhs.id == rhs.id
}
extension User: CustomDebugStringConvertible {
public var debugDescription: String {
return "User(id: \(self.id), name: \"\(self.name)\")"
}
}
extension User: Decodable {
public init(from decoder: Decoder) throws {
let values = try decoder.container(keyedBy: CodingKeys.self)
self.avatar = try values.decode(Avatar.self, forKey: .avatar)
self.erroredBackingsCount = try values.decodeIfPresent(Int.self, forKey: .erroredBackingsCount)
self.facebookConnected = try values.decodeIfPresent(Bool.self, forKey: .facebookConnected)
self.id = try values.decode(Int.self, forKey: .id)
self.isAdmin = try values.decodeIfPresent(Bool.self, forKey: .isAdmin)
self.isEmailVerified = try values.decodeIfPresent(Bool.self, forKey: .isEmailVerified)
self.isFriend = try values.decodeIfPresent(Bool.self, forKey: .isFriend)
self.isBlocked = try values.decodeIfPresent(Bool.self, forKey: .isBlocked) ?? false
self.location = try? values.decodeIfPresent(Location.self, forKey: .location)
self.name = try values.decode(String.self, forKey: .name)
self.needsFreshFacebookToken = try values.decodeIfPresent(Bool.self, forKey: .needsFreshFacebookToken)
self.needsPassword = try values.decodeIfPresent(Bool.self, forKey: .needsPassword)
self.newsletters = try User.NewsletterSubscriptions(from: decoder)
self.notifications = try User.Notifications(from: decoder)
self.optedOutOfRecommendations = try values.decodeIfPresent(Bool.self, forKey: .optedOutOfRecommendations)
self.showPublicProfile = try values.decodeIfPresent(Bool.self, forKey: .showPublicProfile)
self.social = try values.decodeIfPresent(Bool.self, forKey: .social)
self.stats = try User.Stats(from: decoder)
self.unseenActivityCount = try values.decodeIfPresent(Int.self, forKey: .unseenActivityCount)
}
enum CodingKeys: String, CodingKey {
case avatar
case erroredBackingsCount = "errored_backings_count"
case facebookConnected = "facebook_connected"
case id
case isAdmin = "is_admin"
case isEmailVerified = "is_email_verified"
case isFriend = "is_friend"
case isBlocked
case location
case name
case needsFreshFacebookToken = "needs_fresh_facebook_token"
case needsPassword = "needs_password"
case optedOutOfRecommendations = "opted_out_of_recommendations"
case showPublicProfile = "show_public_profile"
case social
case unseenActivityCount = "unseen_activity_count"
}
}
extension User: EncodableType {
public func encode() -> [String: Any] {
var result: [String: Any] = [:]
result["avatar"] = self.avatar.encode()
result["facebook_connected"] = self.facebookConnected ?? false
result["id"] = self.id
result["is_admin"] = self.isAdmin ?? false
result["is_email_verified"] = self.isEmailVerified ?? false
result["is_friend"] = self.isFriend ?? false
result["isBlocked"] = self.isBlocked ?? false
result["location"] = self.location?.encode()
result["name"] = self.name
result["needs_password"] = self.needsPassword ?? false
result["opted_out_of_recommendations"] = self.optedOutOfRecommendations ?? false
result["social"] = self.social ?? false
result["show_public_profile"] = self.showPublicProfile ?? false
result = result.withAllValuesFrom(self.newsletters.encode())
result = result.withAllValuesFrom(self.notifications.encode())
result = result.withAllValuesFrom(self.stats.encode())
return result
}
}
extension User.Avatar: Decodable {
enum CodingKeys: String, CodingKey {
case large
case medium
case small
}
}
extension User.Avatar: EncodableType {
public func encode() -> [String: Any] {
var ret: [String: Any] = [
"medium": self.medium,
"small": self.small
]
ret["large"] = self.large
return ret
}
}
extension User.NewsletterSubscriptions: Decodable {
enum CodingKeys: String, CodingKey {
case arts = "arts_culture_newsletter"
case games = "games_newsletter"
case happening = "happening_newsletter"
case invent = "invent_newsletter"
case promo = "promo_newsletter"
case weekly = "weekly_newsletter"
case films = "film_newsletter"
case publishing = "publishing_newsletter"
case alumni = "alumni_newsletter"
case music = "music_newsletter"
}
}
extension User.NewsletterSubscriptions: EncodableType {
public func encode() -> [String: Any] {
var result: [String: Any] = [:]
result["arts_culture_newsletter"] = self.arts
result["games_newsletter"] = self.games
result["happening_newsletter"] = self.happening
result["invent_newsletter"] = self.invent
result["promo_newsletter"] = self.promo
result["weekly_newsletter"] = self.weekly
result["film_newsletter"] = self.films
result["publishing_newsletter"] = self.publishing
result["alumni_newsletter"] = self.alumni
result["music_newsletter"] = self.music
return result
}
}
extension User.NewsletterSubscriptions: Equatable {}
public func == (lhs: User.NewsletterSubscriptions, rhs: User.NewsletterSubscriptions) -> Bool {
return lhs.arts == rhs.arts &&
lhs.games == rhs.games &&
lhs.happening == rhs.happening &&
lhs.invent == rhs.invent &&
lhs.promo == rhs.promo &&
lhs.weekly == rhs.weekly &&
lhs.films == rhs.films &&
lhs.publishing == rhs.publishing &&
lhs.alumni == rhs.alumni
}
extension User.Notifications: Decodable {
enum CodingKeys: String, CodingKey {
case backings = "notify_of_backings"
case commentReplies = "notify_of_comment_replies"
case comments = "notify_of_comments"
case creatorDigest = "notify_of_creator_digest"
case creatorTips = "notify_of_creator_edu"
case follower = "notify_of_follower"
case friendActivity = "notify_of_friend_activity"
case messages = "notify_of_messages"
case mobileBackings = "notify_mobile_of_backings"
case mobileComments = "notify_mobile_of_comments"
case mobileFriendActivity = "notify_mobile_of_friend_activity"
case mobileMarketingUpdate = "notify_mobile_of_marketing_update"
case mobileMessages = "notify_mobile_of_messages"
case mobileFollower = "notify_mobile_of_follower"
case mobilePostLikes = "notify_mobile_of_post_likes"
case mobileUpdates = "notify_mobile_of_updates"
case postLikes = "notify_of_post_likes"
case updates = "notify_of_updates"
}
}
extension User.Notifications: EncodableType {
public func encode() -> [String: Any] {
var result: [String: Any] = [:]
result["notify_of_backings"] = self.backings
result["notify_of_comment_replies"] = self.commentReplies
result["notify_of_comments"] = self.comments
result["notify_of_creator_digest"] = self.creatorDigest
result["notify_of_creator_edu"] = self.creatorTips
result["notify_of_follower"] = self.follower
result["notify_of_friend_activity"] = self.friendActivity
result["notify_of_messages"] = self.messages
result["notify_mobile_of_comments"] = self.mobileComments
result["notify_mobile_of_follower"] = self.mobileFollower
result["notify_mobile_of_friend_activity"] = self.mobileFriendActivity
result["notify_mobile_of_marketing_update"] = self.mobileMarketingUpdate
result["notify_mobile_of_messages"] = self.mobileMessages
result["notify_mobile_of_post_likes"] = self.mobilePostLikes
result["notify_mobile_of_updates"] = self.mobileUpdates
result["notify_of_post_likes"] = self.postLikes
result["notify_of_updates"] = self.updates
result["notify_mobile_of_backings"] = self.mobileBackings
return result
}
}
extension User.Notifications: Equatable {}
extension User.Stats: Decodable {
enum CodingKeys: String, CodingKey {
case backedProjectsCount = "backed_projects_count"
case createdProjectsCount = "created_projects_count"
case draftProjectsCount = "draft_projects_count"
case memberProjectsCount = "member_projects_count"
case starredProjectsCount = "starred_projects_count"
case unansweredSurveysCount = "unanswered_surveys_count"
case unreadMessagesCount = "unread_messages_count"
}
}
extension User.Stats: EncodableType {
public func encode() -> [String: Any] {
var result: [String: Any] = [:]
result["backed_projects_count"] = self.backedProjectsCount
result["created_projects_count"] = self.createdProjectsCount
result["draft_projects_count"] = self.draftProjectsCount
result["member_projects_count"] = self.memberProjectsCount
result["starred_projects_count"] = self.starredProjectsCount
result["unanswered_surveys_count"] = self.unansweredSurveysCount
result["unread_messages_count"] = self.unreadMessagesCount
return result
}
}