session-ios/SessionMessagingKit/Jobs/Types/RetrieveDefaultOpenGroupRoomsJob.swift
Morgan Pretty c9fdee9f24 Work on the PromiseKit refactor
# Conflicts:
#	Session.xcodeproj/project.pbxproj
#	Session/Conversations/ConversationVC+Interaction.swift
#	Session/Home/Message Requests/MessageRequestsViewModel.swift
#	Session/Notifications/AppNotifications.swift
#	Session/Notifications/PushRegistrationManager.swift
#	Session/Notifications/SyncPushTokensJob.swift
#	Session/Notifications/UserNotificationsAdaptee.swift
#	Session/Settings/BlockedContactsViewModel.swift
#	Session/Settings/NukeDataModal.swift
#	Session/Settings/SettingsViewModel.swift
#	Session/Utilities/BackgroundPoller.swift
#	SessionMessagingKit/Database/Models/ClosedGroup.swift
#	SessionMessagingKit/File Server/FileServerAPI.swift
#	SessionMessagingKit/Open Groups/OpenGroupAPI.swift
#	SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+ClosedGroups.swift
#	SessionMessagingKit/Sending & Receiving/Message Handling/MessageReceiver+UnsendRequests.swift
#	SessionMessagingKit/Sending & Receiving/Message Handling/MessageSender+ClosedGroups.swift
#	SessionMessagingKit/Sending & Receiving/MessageSender+Convenience.swift
#	SessionMessagingKit/Sending & Receiving/MessageSender.swift
#	SessionMessagingKit/Sending & Receiving/Notifications/PushNotificationAPI.swift
#	SessionMessagingKit/Sending & Receiving/Pollers/ClosedGroupPoller.swift
#	SessionMessagingKit/Sending & Receiving/Pollers/CurrentUserPoller.swift
#	SessionMessagingKit/Sending & Receiving/Pollers/Poller.swift
#	SessionMessagingKit/Utilities/ProfileManager.swift
#	SessionSnodeKit/Networking/SnodeAPI.swift
#	SessionSnodeKit/OnionRequestAPI.swift
#	SessionUtilitiesKit/Networking/HTTP.swift
2022-12-07 15:50:58 +11:00

57 lines
2 KiB
Swift

// Copyright © 2022 Rangeproof Pty Ltd. All rights reserved.
import Foundation
import GRDB
import SessionUtilitiesKit
public enum RetrieveDefaultOpenGroupRoomsJob: JobExecutor {
public static let maxFailureCount: Int = -1
public static let requiresThreadId: Bool = false
public static let requiresInteractionId: Bool = false
public static func run(
_ job: Job,
queue: DispatchQueue,
success: @escaping (Job, Bool) -> (),
failure: @escaping (Job, Error?, Bool) -> (),
deferred: @escaping (Job) -> ()
) {
// Don't run when inactive or not in main app
guard (UserDefaults.sharedLokiProject?[.isMainAppActive]).defaulting(to: false) else {
deferred(job) // Don't need to do anything if it's not the main app
return
}
// The OpenGroupAPI won't make any API calls if there is no entry for an OpenGroup
// in the database so we need to create a dummy one to retrieve the default room data
let defaultGroupId: String = OpenGroup.idFor(roomToken: "", server: OpenGroupAPI.defaultServer)
Storage.shared.write { db in
guard try OpenGroup.exists(db, id: defaultGroupId) == false else { return }
_ = try OpenGroup(
server: OpenGroupAPI.defaultServer,
roomToken: "",
publicKey: OpenGroupAPI.defaultServerPublicKey,
isActive: false,
name: "",
userCount: 0,
infoUpdates: 0
)
.saved(db)
}
OpenGroupManager.getDefaultRoomsIfNeeded()
.subscribe(on: queue)
.receive(on: queue)
.sinkUntilComplete(
receiveCompletion: { result in
switch result {
case .finished: success(job, false)
case .failure(let error): failure(job, error, false)
}
}
)
}
}