ForgejoKit/Sources/ForgejoKit/Services/NotificationService.swift
Stefan Hausotte 897a8ebedd feat: initial commit
Intitial commit for ForgejoKit, a native Swift library to interact with
the Frogejo API
2026-02-28 20:25:57 +01:00

38 lines
1.4 KiB
Swift

import Foundation
public final class NotificationService: Sendable {
private let client: ForgejoClient
public init(client: ForgejoClient) {
self.client = client
}
public func fetchNotifications(
statusTypes: [String] = ["unread"],
page: Int = 1, limit: Int = 20,
) async throws -> [NotificationThread] {
var queryItems = [
URLQueryItem(name: "page", value: "\(page)"),
URLQueryItem(name: "limit", value: "\(limit)"),
]
for status in statusTypes {
queryItems.append(URLQueryItem(name: "status-types", value: status))
}
let url = try client.makeURL(path: "/api/v1/notifications", queryItems: queryItems)
return try await client.performRequest(url: url, responseType: [NotificationThread].self)
}
public func fetchUnreadCount() async throws -> Int {
let url = try client.makeURL(path: "/api/v1/notifications/new")
let count = try await client.performRequest(url: url, responseType: NotificationCount.self)
return count.new
}
public func markAsRead(id: Int) async throws {
let url = try client.makeURL(
path: "/api/v1/notifications/threads/\(id)",
queryItems: [URLQueryItem(name: "to-status", value: "read")],
)
_ = try await client.performRequestNoContent(url: url, method: "PATCH", validateStatus: true)
}
}