-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Comment.swift
89 lines (79 loc) · 2.06 KB
/
Comment.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
import Foundation
public struct Comment {
public var author: Author
public var authorBadges: [AuthorBadge]
public var body: String
public let createdAt: TimeInterval
public var id: String
public var isDeleted: Bool
public var parentId: String?
public var replyCount: Int
/// return the first `authorBadges`, if nil return `.backer`
public var authorBadge: AuthorBadge {
return self.authorBadges.first ?? .backer
}
/// Use to determine if a comment is a reply to another comment
public var isReply: Bool {
return self.parentId != nil
}
/// Track and return the current status of the `Comment`
public var status: Status = .unknown
public struct Author: Decodable, Equatable {
public var id: String
public var imageUrl: String
/// TODO(MBL-1025): Get isBlocked status from the backend instead.
public var isBlocked: Bool = false
public var isCreator: Bool
public var name: String
}
public enum AuthorBadge: String, Decodable {
case collaborator
case creator
case backer
case superbacker
case you
}
public enum Status: String, Decodable {
case failed
case retrying
case retrySuccess
case success
case unknown // Before a status is set
}
}
extension Comment: Decodable {}
extension Comment {
public static func failableComment(
withId id: String,
date: Date,
project: Project,
parentId: String?,
user: User,
body: String
) -> Comment {
let author = Author(
id: "\(user.id)",
imageUrl: user.avatar.medium,
isBlocked: user.isBlocked ?? false,
isCreator: project.creator == user,
name: user.name
)
return Comment(
author: author,
authorBadges: [.you],
body: body,
createdAt: date.timeIntervalSince1970,
id: id,
isDeleted: false,
parentId: parentId,
replyCount: 0,
status: .success
)
}
public func updatingStatus(to status: Comment.Status) -> Comment {
var comment = self
comment.status = status
return comment
}
}
extension Comment: Equatable {}