mirror of
https://codeberg.org/secana/ForgejoKit.git
synced 2026-06-16 05:13:53 -07:00
38 lines
1.4 KiB
Swift
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)
|
|
}
|
|
}
|