summaryrefslogtreecommitdiff
path: root/Hutch/Models/Todo.swift
blob: 129d4bf51f50dd75b290e5daf4392c2efe676de3 (plain) (blame)
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
import Foundation

// MARK: - Enums

/// Status of a ticket.
enum TicketStatus: String, Codable, Sendable, CaseIterable {
    case reported = "REPORTED"
    case confirmed = "CONFIRMED"
    case inProgress = "IN_PROGRESS"
    case pending = "PENDING"
    case resolved = "RESOLVED"

    /// Whether this status represents an open (unresolved) ticket.
    var isOpen: Bool {
        switch self {
        case .reported, .confirmed, .inProgress, .pending: true
        case .resolved: false
        }
    }

    var displayName: String {
        switch self {
        case .reported:   "Reported"
        case .confirmed:  "Confirmed"
        case .inProgress: "In Progress"
        case .pending:    "Pending"
        case .resolved:   "Resolved"
        }
    }
}

/// Resolution of a ticket.
enum TicketResolution: String, Codable, Sendable {
    case unresolved = "UNRESOLVED"
    case fixed = "FIXED"
    case implemented = "IMPLEMENTED"
    case wontFix = "WONT_FIX"
    case byDesign = "BY_DESIGN"
    case invalid = "INVALID"
    case duplicate = "DUPLICATE"
    case notOurBug = "NOT_OUR_BUG"
    case closed = "CLOSED"
    case notApplicable = "NOT_APPLICABLE"

    init(from decoder: Decoder) throws {
        let value = try decoder.singleValueContainer().decode(String.self)
        self = TicketResolution(rawValue: value) ?? .unresolved
    }

    var displayName: String {
        switch self {
        case .unresolved:     "Unresolved"
        case .fixed:          "Fixed"
        case .implemented:    "Implemented"
        case .wontFix:        "Won't Fix"
        case .byDesign:       "By Design"
        case .invalid:        "Invalid"
        case .duplicate:      "Duplicate"
        case .notOurBug:      "Not Our Bug"
        case .closed:         "Closed"
        case .notApplicable:  "Not Applicable"
        }
    }
}

/// Authenticity of a ticket or comment.
enum Authenticity: String, Codable, Sendable {
    case authentic = "AUTHENTIC"
    case tampered = "TAMPERED"
    case unauthenticated = "UNAUTHENTICATED"
}

// MARK: - Label

/// A label that can be applied to tickets.
/// Named `TicketLabel` to avoid collision with `SwiftUI.Label`.
struct TicketLabel: Codable, Sendable, Identifiable, Hashable {
    let id: Int
    let name: String
    let backgroundColor: String
    let foregroundColor: String
}

// MARK: - Tracker ACL

struct TrackerACLPermissions: Codable, Sendable, Hashable {
    let browse: Bool
    let submit: Bool
    let comment: Bool
    let edit: Bool
    let triage: Bool
}

struct TrackerACL: Codable, Sendable, Identifiable, Hashable {
    let id: Int
    let created: Date
    let entity: Entity
    let browse: Bool
    let submit: Bool
    let comment: Bool
    let edit: Bool
    let triage: Bool

    var permissions: TrackerACLPermissions {
        TrackerACLPermissions(
            browse: browse,
            submit: submit,
            comment: comment,
            edit: edit,
            triage: triage
        )
    }
}

struct DefaultTrackerACL: Codable, Sendable, Hashable {
    let browse: Bool
    let submit: Bool
    let comment: Bool
    let edit: Bool
    let triage: Bool

    var permissions: TrackerACLPermissions {
        TrackerACLPermissions(
            browse: browse,
            submit: submit,
            comment: comment,
            edit: edit,
            triage: triage
        )
    }
}

// MARK: - Tracker

/// A bug tracker from todo.sr.ht.
struct Tracker: Codable, Sendable, Identifiable, Hashable {
    let id: Int
    let created: Date
    let updated: Date
    let name: String
    let description: String?
    let visibility: Visibility
    let owner: Entity

    static func == (lhs: Tracker, rhs: Tracker) -> Bool {
        lhs.id == rhs.id
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}

// MARK: - Tracker Summary (for list view)

/// Lightweight tracker model matching the fields returned by the trackers list query.
struct TrackerSummary: Codable, Sendable, Identifiable, Hashable {
    let id: Int
    /// GraphQL resource identifier used by `tracker(id:)` queries.
    let rid: String
    let name: String
    let description: String?
    let visibility: Visibility
    let updated: Date
    let owner: Entity
}

// MARK: - Ticket Summary (for list view)

/// Lightweight ticket model for the list query.
struct TicketSummary: Codable, Sendable, Identifiable, Hashable {
    let id: Int
    let title: String
    let status: TicketStatus
    let resolution: TicketResolution?
    let created: Date
    let submitter: Entity
    let labels: [TicketLabel]
    let assignees: [Entity]

    static func == (lhs: TicketSummary, rhs: TicketSummary) -> Bool {
        lhs.id == rhs.id
    }

    func hash(into hasher: inout Hasher) {
        hasher.combine(id)
    }
}

// MARK: - Ticket Detail

/// Full ticket model for the detail view.
struct TicketDetail: Codable, Sendable {
    let id: Int
    let created: Date
    let updated: Date
    let title: String
    let description: String?
    let status: TicketStatus
    let resolution: TicketResolution?
    let authenticity: Authenticity
    let submitter: Entity
    let assignees: [Entity]
    let labels: [TicketLabel]
}

// MARK: - Event

/// A timeline event on a ticket. Each event contains one or more changes.
struct TicketEvent: Codable, Sendable, Identifiable {
    let id: Int
    let created: Date
    var changes: [EventChange]
}

/// A single change within an event, decoded from the polymorphic EventDetail
/// interface using inline fragments.
struct EventChange: Codable, Sendable, Identifiable {
    let id: UUID

    let eventType: String

    // Comment fields
    let author: Entity?
    var text: String?
    let authenticity: Authenticity?

    // StatusChange fields
    let oldStatus: TicketStatus?
    let newStatus: TicketStatus?

    // LabelUpdate fields
    let labeler: Entity?
    let label: EventLabel?

    // Assignment fields
    let assigner: Entity?
    let assignee: Entity?

    // Mention fields
    // TicketMention: mentioned is a Ticket with { id }
    // UserMention: mentioned is an Entity with { canonicalName }
    let mentioned: MentionTarget?

    // Created fields (author is shared with Comment)

    private enum CodingKeys: String, CodingKey {
        case eventType
        case author, text, authenticity
        case oldStatus, newStatus
        case labeler, label
        case assigner, assignee
        case mentioned
    }

    init(from decoder: any Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        self.id = UUID()
        self.eventType = try container.decode(String.self, forKey: .eventType)
        self.author = try container.decodeIfPresent(Entity.self, forKey: .author)
        self.text = try container.decodeIfPresent(String.self, forKey: .text)
        self.authenticity = try container.decodeIfPresent(Authenticity.self, forKey: .authenticity)
        self.oldStatus = try container.decodeIfPresent(TicketStatus.self, forKey: .oldStatus)
        self.newStatus = try container.decodeIfPresent(TicketStatus.self, forKey: .newStatus)
        self.labeler = try container.decodeIfPresent(Entity.self, forKey: .labeler)
        self.label = try container.decodeIfPresent(EventLabel.self, forKey: .label)
        self.assigner = try container.decodeIfPresent(Entity.self, forKey: .assigner)
        self.assignee = try container.decodeIfPresent(Entity.self, forKey: .assignee)
        self.mentioned = try container.decodeIfPresent(MentionTarget.self, forKey: .mentioned)
    }
}

/// Decoded from the `mentioned` field which may be a Ticket (with `id`) or
/// an Entity (with `canonicalName`) depending on the event type.
struct MentionTarget: Codable, Sendable {
    let id: Int?
    let canonicalName: String?
}

/// Label info as returned within a LabelUpdate event change.
struct EventLabel: Codable, Sendable {
    let name: String
}