|
| 1 | +/* |
| 2 | + Infomaniak kDrive - Desktop |
| 3 | + Copyright (C) 2023-2025 Infomaniak Network SA |
| 4 | + |
| 5 | + This program is free software: you can redistribute it and/or modify |
| 6 | + it under the terms of the GNU General Public License as published by |
| 7 | + the Free Software Foundation, either version 3 of the License, or |
| 8 | + (at your option) any later version. |
| 9 | + |
| 10 | + This program is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + GNU General Public License for more details. |
| 14 | + |
| 15 | + You should have received a copy of the GNU General Public License |
| 16 | + along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +import Foundation |
| 20 | +import InfomaniakConcurrency |
| 21 | +import InfomaniakDI |
| 22 | + |
| 23 | +public struct DriveJobs: Sendable { |
| 24 | + @LazyInjectService private var coherentCache: CoherentCacheProtocol |
| 25 | + @LazyInjectService private var queryFetcher: XPCQueryFetcherProtocol |
| 26 | + |
| 27 | + public enum DriveJobsError: Error { |
| 28 | + case responseListNotFound |
| 29 | + case noReplyData |
| 30 | + } |
| 31 | + |
| 32 | + public init() {} |
| 33 | + |
| 34 | + public func availableDrives(userDbId: Int32) async throws -> [DriveResponse] { |
| 35 | + IKLogger.data.log("Query for availableDrives list") |
| 36 | + let query = DriveListQuery(userDbId: userDbId) |
| 37 | + let request = await RequestMessage<DriveListQuery>(num: RequestNum.USER_AVAILABLEDRIVES, body: query) |
| 38 | + |
| 39 | + do { |
| 40 | + let decodedMessage = try await queryFetcher.query(request, responseType: CallbackMessage<DriveListResponse>.self) |
| 41 | + |
| 42 | + guard let driveList = decodedMessage?.body.driveAvailableInfoList else { |
| 43 | + throw DriveJobsError.responseListNotFound |
| 44 | + } |
| 45 | + |
| 46 | + await driveList.asyncForEach { await coherentCache.updateDrive(drive: $0.asDrive) } |
| 47 | + |
| 48 | + return driveList |
| 49 | + } catch XPCQueryFetcher.QueryError.noReplyData { |
| 50 | + throw DriveJobsError.noReplyData |
| 51 | + } |
| 52 | + } |
| 53 | +} |
0 commit comments