import Foundation
import DevPlaceSwiftSDK
extension DevPlaceApi where Self == ProdDevPlaceApi {
static var prod: Self {
.shared
}
}
final class ProdDevPlaceApi: DevPlaceApi {
static let shared = ProdDevPlaceApi()
let logger = DevPlaceRequestLogger()
let request: DevPlaceRequest
init() {
request = DevPlaceRequest(requestLogger: logger)
}
func logIn(email: String, password: String) async throws {
let token = try await request.getAuthToken(email: email, password: password)
AppState.shared.token = token
AppState.shared.currentUser = try? await request.getProfile(username: nil, token: token).user
}
func signUp(username: String, email: String, password: String, confirmPassword: String, birthDate: Date, acceptTerms: Bool) async throws {
try await request.signUp(username: username, email: email, password: password, confirmPassword: confirmPassword, birthDate: birthDate, acceptTerms: acceptTerms)
}
func deleteAccount(password: String) async throws {
guard let token = AppState.shared.token, let username = AppState.shared.currentUser?.username else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.deleteAccount(username: username, password: password, token: token)
}
func feed(before: Date?) async throws -> Feed {
try await refreshTokenIfNeeded()
return try await request.getFeed(before: before, token: AppState.shared.token)
}
func postDetail(slug: String) async throws -> PostDetail {
try await refreshTokenIfNeeded()
return try await request.getPost(slug: slug, token: AppState.shared.token)
}
func createPost(title: String?, topic: PostTopic?, content: String, attachments: [UploadResponse], pollQuestion: String?, pollOptions: [String], projectLink: String?) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.createPost(title: title, topic: topic, content: content, attachments: attachments, pollQuestion: pollQuestion, pollOptions: pollOptions, projectLink: projectLink, token: token)
}
func editPost(slug: String, title: String, topic: PostTopic, content: String, projectLink: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.editPost(slug: slug, title: title, topic: topic, content: content, projectLink: projectLink, token: token)
}
func deletePost(slug: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.deletePost(slug: slug, token: token)
}
func deleteComment(uid: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.deleteComment(uid: uid, token: token)
}
func editComment(uid: String, content: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.editComment(uid: uid, content: content, token: token)
}
func createComment(targetType: TargetType, targetId: String, content: String, parentId: String?, attachments: [UploadResponse]) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.createComment(targetType: targetType, targetId: targetId, content: content, parentId: parentId, attachments: attachments, token: token)
}
func vote(targetType: TargetType, targetId: String, vote: Vote) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.vote(targetType: targetType, targetId: targetId, vote: vote, token: token)
}
func submitPollChoice(pollId: String, optionId: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.submitPollChoice(pollId: pollId, optionId: optionId, token: token)
}
func react(targetType: TargetType, targetId: String, emoji: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.react(targetType: targetType, targetId: targetId, emoji: emoji, token: token)
}
func reportReasons() async throws -> [ReportReason] {
try await request.getReportReasons()
}
func report(targetType: TargetType, targetId: String, reason: String, detail: String?) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.report(targetType: targetType, targetId: targetId, reason: reason, detail: detail, token: token)
}
func uploadFile(data: Data, filename: String, mimeType: String) async throws -> UploadResponse {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
return try await request.uploadFile(data: data, filename: filename, mimeType: mimeType, token: token)
}
func uploadFromUrl(url: String, filename: String?) async throws -> UploadResponse {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
return try await request.uploadFromUrl(url: url, filename: filename, token: token)
}
func deleteAttachment(uid: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.deleteAttachment(uid: uid, token: token)
}
func followUser(username: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.followUser(username: username, token: token)
}
func unfollowUser(username: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.unfollowUser(username: username, token: token)
}
func blockUser(username: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.blockUser(username: username, token: token)
}
func unblockUser(username: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.unblockUser(username: username, token: token)
}
func muteUser(username: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.muteUser(username: username, token: token)
}
func unmuteUser(username: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.unmuteUser(username: username, token: token)
}
func messages(withUid: String?) async throws -> MessagesInbox {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
return try await request.getMessages(withUid: withUid, token: token)
}
func sendMessage(receiverId: String, content: String, attachments: [UploadResponse]) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.sendMessage(receiverId: receiverId, content: content, attachments: attachments, token: token)
}
func makeMessagesSocket(otherUserId: String) -> any MessagesSocket {
LiveMessagesSocket(receiverUid: otherUserId) {
try await self.messagesWebSocketURL()
}
}
private func messagesWebSocketURL() async throws -> URL {
guard AppState.shared.token != nil else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
let ticket = try await request.getMessagesWebSocketTicket(token: token)
guard let url = request.messagesWebSocketURL(ticket: ticket.ticket) else {
throw DevPlaceError.invalidUrl(ticket.ticket)
}
return url
}
func usersForMentioning(matching query: String) async throws -> UserSearch {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
return try await request.searchMessageRecipients(q: query, token: token)
}
func notifications(before: String?) async throws -> Notifications {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
return try await request.getNotifications(before: before, token: token)
}
func openNotification(uid: String) async throws -> NotificationOpen {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
return try await request.openNotification(uid: uid, token: token)
}
func notificationCounts() async throws -> NotificationCounts {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
return try await request.getNotificationCounts(token: token)
}
func markNotificationRead(uid: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.markNotificationRead(uid: uid, token: token)
}
func markAllNotificationsRead() async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.markAllNotificationsRead(token: token)
}
func notificationSettings() async throws -> NotificationSettings {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
return try await request.getNotificationSettings(token: token)
}
func setNotificationGrouping(username: String, enabled: Bool) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.setNotificationGrouping(username: username, enabled: enabled, token: token)
}
func setNotificationThrottling(username: String, enabled: Bool) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.setNotificationThrottling(username: username, enabled: enabled, token: token)
}
func setNotificationPreference(username: String, notificationType: String, channel: NotificationChannel, enabled: Bool) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.setNotificationPreference(
username: username,
notificationType: notificationType,
channel: channel,
enabled: enabled,
token: token,
)
}
func resetNotificationPreferences(username: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.resetNotificationPreferences(username: username, token: token)
}
func registerPushDevice(deviceToken: String) async throws {
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
try await request.registerPushDevice(provider: .apns, deviceToken: deviceToken, token: token)
}
func unregisterPushDevice(deviceToken: String) async throws {
guard AppState.shared.token != nil else {
throw DevPlaceError.notLoggedIn
}
try await refreshTokenIfNeeded()
// TODO: Call the SDK's unregister-push-device request here once the backend API and SDK support are ready.
// The backend has no APNs unsubscribe endpoint yet; the server stops delivering once APNs
// reports the device token as invalid (e.g. app uninstalled). Pass `deviceToken` through when available.
}
func profile(username: String?) async throws -> Profile {
try await refreshTokenIfNeeded()
if let username {
// Fetching user profiles of other users should be fine without a token.
return try await request.getProfile(username: username, token: AppState.shared.token)
} else {
// Fetching own profile requires a token.
guard let token = AppState.shared.token else {
throw DevPlaceError.notLoggedIn
}
return try await request.getProfile(username: username, token: token)
}
}
}