krz/hutch

an ios client for sourcehut

clone: git clone https://gitbay.org/krz/hutch.git

v3.5.0: Hutch/Networking/APICacheKeys.swift · raw

  1import Foundation
  2
  3enum APICacheKeys {
  4    static func repositories(service: SRHTService, owner: String? = nil, cursor: String? = nil, filter: String? = nil) -> String {
  5        make([
  6            service.rawValue,
  7            "repositories",
  8            owner.map { "owner:\(normalize($0))" },
  9            cursor.map { "cursor:\($0)" },
 10            filter.map { "filter:\(normalize($0))" }
 11        ])
 12    }
 13
 14    static func repository(service: SRHTService, owner: String, name: String) -> String {
 15        make([service.rawValue, "repository", normalize(owner), normalize(name)])
 16    }
 17
 18    static func repositoryRID(service: SRHTService, rid: String) -> String {
 19        make([service.rawValue, "repository", "rid:\(rid)"])
 20    }
 21
 22    static func refs(service: SRHTService, rid: String, cursor: String? = nil) -> String {
 23        make([service.rawValue, "refs", "rid:\(rid)", cursor.map { "cursor:\($0)" }])
 24    }
 25
 26    static func readme(service: SRHTService, rid: String, path: String? = nil, ref: String = "HEAD") -> String {
 27        make([service.rawValue, "readme", "rid:\(rid)", "ref:\(ref)", path.map { "path:\($0)" }])
 28    }
 29
 30    static func treeRoot(service: SRHTService, rid: String, ref: String) -> String {
 31        make([service.rawValue, "tree", "rid:\(rid)", "ref:\(ref)", "root"])
 32    }
 33
 34    static func treeEntries(service: SRHTService, rid: String, treeId: String, cursor: String? = nil) -> String {
 35        make([service.rawValue, "tree", "rid:\(rid)", "tree:\(treeId)", cursor.map { "cursor:\($0)" }])
 36    }
 37
 38    static func blob(service: SRHTService, rid: String, blobId: String) -> String {
 39        make([service.rawValue, "blob", "rid:\(rid)", "blob:\(blobId)"])
 40    }
 41
 42    static func path(service: SRHTService, rid: String, ref: String, path: String) -> String {
 43        make([service.rawValue, "path", "rid:\(rid)", "ref:\(ref)", "path:\(path)"])
 44    }
 45
 46    static func ticketDetail(owner: String, trackerRid: String, ticketId: Int) -> String {
 47        make([SRHTService.todo.rawValue, "ticket", normalize(owner), "tracker:\(trackerRid)", "ticket:\(ticketId)"])
 48    }
 49
 50    static func trackerLabels(trackerRid: String) -> String {
 51        make([SRHTService.todo.rawValue, "tracker-labels", "tracker:\(trackerRid)"])
 52    }
 53
 54    static func trackers(cursor: String? = nil) -> String {
 55        make([SRHTService.todo.rawValue, "trackers", cursor.map { "cursor:\($0)" }])
 56    }
 57
 58    static func tickets(trackerRid: String, cursor: String? = nil) -> String {
 59        make([SRHTService.todo.rawValue, "tickets", "tracker:\(trackerRid)", cursor.map { "cursor:\($0)" }])
 60    }
 61
 62    static func builds(cursor: String? = nil, filter: String? = nil) -> String {
 63        make([SRHTService.builds.rawValue, "jobs", cursor.map { "cursor:\($0)" }, filter.map { "filter:\($0)" }])
 64    }
 65
 66    static func buildDetail(jobId: Int) -> String {
 67        make([SRHTService.builds.rawValue, "job", "id:\(jobId)"])
 68    }
 69
 70    static func buildLog(url: URL, jobId: Int? = nil, task: String? = nil) -> String {
 71        make([SRHTService.builds.rawValue, "log", jobId.map { "job:\($0)" }, task.map { "task:\($0)" }, url.absoluteString])
 72    }
 73
 74    static func userRepositories(owner: String, cursor: String? = nil) -> String {
 75        make([SRHTService.git.rawValue, "user-repositories", normalize(owner), cursor.map { "cursor:\($0)" }])
 76    }
 77
 78    static func userTrackers(owner: String, cursor: String? = nil) -> String {
 79        make([SRHTService.todo.rawValue, "user-trackers", normalize(owner), cursor.map { "cursor:\($0)" }])
 80    }
 81
 82    static func projects(cursor: String? = nil) -> String {
 83        make([SRHTService.hub.rawValue, "projects", cursor.map { "cursor:\($0)" }])
 84    }
 85
 86    static func projectDetail(rid: String, mailingListsCursor: String? = nil, sourcesCursor: String? = nil, trackersCursor: String? = nil) -> String {
 87        make([
 88            SRHTService.hub.rawValue,
 89            "project",
 90            "rid:\(rid)",
 91            mailingListsCursor.map { "ml:\($0)" },
 92            sourcesCursor.map { "src:\($0)" },
 93            trackersCursor.map { "trk:\($0)" }
 94        ])
 95    }
 96
 97    static func homeJobs(actor: String) -> String {
 98        make(["home", "jobs", normalize(actor)])
 99    }
100
101    static func homeTrackerTickets(owner: String, tracker: String) -> String {
102        make(["home", "tickets", normalize(owner), normalize(tracker)])
103    }
104
105    static func inboxSubscriptions(cursor: String? = nil) -> String {
106        make([SRHTService.lists.rawValue, "subscriptions", cursor.map { "cursor:\($0)" }])
107    }
108
109    static func inboxThreads(listRid: String, cursor: String? = nil) -> String {
110        make([SRHTService.lists.rawValue, "threads", "list:\(listRid)", cursor.map { "cursor:\($0)" }])
111    }
112
113    static func pasteList(cursor: String? = nil) -> String {
114        make([SRHTService.paste.rawValue, "pastes", cursor.map { "cursor:\($0)" }])
115    }
116
117    static func prefix(_ components: String...) -> String {
118        make(components)
119    }
120
121    private static func make(_ parts: [String?]) -> String {
122        parts.compactMap { $0?.replacingOccurrences(of: "|", with: "%7C") }
123            .joined(separator: "|")
124    }
125
126    private static func normalize(_ value: String) -> String {
127        value.trimmingCharacters(in: .whitespacesAndNewlines).lowercased()
128    }
129}
130
131enum APICacheTTLs {
132    // Active build data changes quickly; completed logs and content-addressed git data are effectively immutable.
133    static let activeBuild: TimeInterval = 15
134    static let completedBuildDetail: TimeInterval = 60 * 60
135    static let completedBuildLog: TimeInterval = 30 * 24 * 60 * 60
136    static let ticketDetail: TimeInterval = 5 * 60
137    static let ticketList: TimeInterval = 2 * 60
138    static let repositoryMetadata: TimeInterval = 30 * 60
139    static let repositoryList: TimeInterval = 5 * 60
140    static let immutableFileContent: TimeInterval = 14 * 24 * 60 * 60
141    static let movingRefFileContent: TimeInterval = 10 * 60
142    static let userProfile: TimeInterval = 30 * 60
143    static let status: TimeInterval = 5 * 60
144    static let homeDashboard: TimeInterval = 2 * 60
145    static let inboxSummary: TimeInterval = 2 * 60
146    static let projectList: TimeInterval = 10 * 60
147    static let projectDetail: TimeInterval = 10 * 60
148}