Skip to content

Commit

Permalink
Merge pull request #254 from wakatime/main
Browse files Browse the repository at this point in the history
Release v5.14.2
  • Loading branch information
alanhamlett authored Mar 28, 2024
2 parents 9ab67a4 + c7ce837 commit 2ae5706
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
56 changes: 56 additions & 0 deletions WakaTime/Helpers/Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,62 @@ class Dependencies {
Bundle.main.version == "local-build"
}

public static func recentBrowserExtension() async -> String? {
guard
let apiKey = ConfigFile.getSetting(section: "settings", key: "api_key"),
!apiKey.isEmpty
else { return nil }
let url = "https://api.wakatime.com/api/v1/users/current/user_agents?api_key=\(apiKey)"
let request = URLRequest(url: URL(string: url)!, cachePolicy: .reloadIgnoringCacheData)
do {
let (data, response) = try await URLSession.shared.data(for: request)
guard
let httpResponse = response as? HTTPURLResponse,
httpResponse.statusCode == 200
else { return nil }

struct Resp: Decodable {
let data: [UserAgent]
}
struct UserAgent: Decodable {
let isBrowserExtension: Bool
let editor: String?
let lastSeenAt: String?
enum CodingKeys: String, CodingKey {
case isBrowserExtension = "is_browser_extension"
case editor
case lastSeenAt = "last_seen_at"
}
}

let release = try JSONDecoder().decode(Resp.self, from: data)
let now = Date()
for agent in release.data {
guard
agent.isBrowserExtension,
let editor = agent.editor,
!editor.isEmpty,
let lastSeenAt = agent.lastSeenAt
else { continue }

let isoDateFormatter = ISO8601DateFormatter()
isoDateFormatter.timeZone = TimeZone(secondsFromGMT: 0)
isoDateFormatter.formatOptions = [.withInternetDateTime]
if let lastSeen = isoDateFormatter.date(from: lastSeenAt) {
if now.timeIntervalSince(lastSeen) > 600 {
break
}
}

return agent.editor
}
} catch {
Logging.default.log("Request error checking for conflicting browser extension: \(error)")
return nil
}
return nil
}

private static func getLatestVersion() async throws -> String? {
struct Release: Decodable {
let tagName: String
Expand Down
10 changes: 10 additions & 0 deletions WakaTime/WakaTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ class WakaTime: HeartbeatEventHandler {
}
PropertiesManager.hasLaunchedBefore = true
}

if MonitoringManager.isMonitoringBrowsing {
Task {
if let browser = await Dependencies.recentBrowserExtension() {
delegate.toastNotification("Warning: WakaTime \(browser) extension detected. " +
"It’s recommended to only track browsing activity with the \(browser) " +
"extension or Mac Desktop app, but not both.")
}
}
}
}

private func configureFirebase() {
Expand Down

0 comments on commit 2ae5706

Please sign in to comment.