88 lines
2.6 KiB
Swift
88 lines
2.6 KiB
Swift
//
|
||
// MessageCenterAPI.swift
|
||
// suixinkan
|
||
//
|
||
|
||
import Foundation
|
||
|
||
/// 消息中心服务协议,抽象列表、已读和删除接口。
|
||
@MainActor
|
||
protocol MessageCenterServing {
|
||
/// 拉取消息列表。
|
||
func list(lastId: Int, limit: Int, unread: Int) async throws -> MessageListResponse
|
||
|
||
/// 获取当前账号未读消息总数。
|
||
func unreadCount() async throws -> MessageUnreadCountResponse
|
||
|
||
/// 标记消息已读并返回最新未读数量。
|
||
func markAsRead(messageId: Int) async throws -> MessageUnreadCountResponse
|
||
|
||
/// 将当前账号全部消息标记已读。
|
||
func markAllAsRead() async throws -> MessageReadAllResponse
|
||
|
||
/// 删除消息。
|
||
func delete(messageId: Int) async throws
|
||
}
|
||
|
||
/// 消息中心 API,对齐 Android `NetworkApi` 的 msg 区域。
|
||
@MainActor
|
||
final class MessageCenterAPI: MessageCenterServing {
|
||
private let client: APIClient
|
||
|
||
/// 初始化消息中心 API。
|
||
init(client: APIClient) {
|
||
self.client = client
|
||
}
|
||
|
||
/// GET /api/app/msg/list
|
||
func list(lastId: Int = 0, limit: Int = 20, unread: Int = 0) async throws -> MessageListResponse {
|
||
try await client.send(
|
||
APIRequest(
|
||
method: .get,
|
||
path: "/api/app/msg/list",
|
||
queryItems: [
|
||
URLQueryItem(name: "last_id", value: String(max(lastId, 0))),
|
||
URLQueryItem(name: "limit", value: String(max(limit, 1))),
|
||
URLQueryItem(name: "unread", value: String(max(unread, 0))),
|
||
]
|
||
)
|
||
)
|
||
}
|
||
|
||
/// GET /api/app/msg/unread-count
|
||
func unreadCount() async throws -> MessageUnreadCountResponse {
|
||
try await client.send(
|
||
APIRequest(method: .get, path: "/api/app/msg/unread-count")
|
||
)
|
||
}
|
||
|
||
/// POST /api/app/msg/read
|
||
func markAsRead(messageId: Int) async throws -> MessageUnreadCountResponse {
|
||
try await client.send(
|
||
APIRequest(
|
||
method: .post,
|
||
path: "/api/app/msg/read",
|
||
queryItems: [URLQueryItem(name: "id", value: String(messageId))]
|
||
)
|
||
)
|
||
}
|
||
|
||
/// POST /api/app/msg/read-all
|
||
func markAllAsRead() async throws -> MessageReadAllResponse {
|
||
try await client.send(
|
||
APIRequest(method: .post, path: "/api/app/msg/read-all")
|
||
)
|
||
}
|
||
|
||
/// POST /api/app/msg/delete
|
||
func delete(messageId: Int) async throws {
|
||
let _: EmptyPayload = try await client.send(
|
||
APIRequest(
|
||
method: .post,
|
||
path: "/api/app/msg/delete",
|
||
body: MessageDeleteRequest(id: messageId)
|
||
)
|
||
)
|
||
}
|
||
}
|