|
| 1 | +// |
| 2 | +// File.swift |
| 3 | +// |
| 4 | +// |
| 5 | +// Created by Finn Behrens on 26.04.22. |
| 6 | +// |
| 7 | + |
| 8 | +import Foundation |
| 9 | +import MatrixClient |
| 10 | + |
| 11 | +@available(macOS 12.0, iOS 15.0, watchOS 8.0, tvOS 15.0, *) |
| 12 | +public extension MatrixCore { |
| 13 | + /// Start a task to sync |
| 14 | + /// |
| 15 | + /// - Throws: ``MatrixCoreError`` if sync is already running |
| 16 | + func startSync() throws { |
| 17 | + guard syncTask == nil else { |
| 18 | + throw MatrixCoreError.syncAlreadyStarted |
| 19 | + } |
| 20 | + |
| 21 | + syncTask = buildSyncTask() |
| 22 | + } |
| 23 | + |
| 24 | + private nonisolated func buildSyncTask() -> Task<Void, Never> { |
| 25 | + Task(priority: .background) { [self] in |
| 26 | + var parameters = MatrixSyncRequest.Parameters(timeout: 45 * 1000) |
| 27 | + while true { |
| 28 | + do { |
| 29 | + parameters = try await self.runSync(parameters: parameters) |
| 30 | + |
| 31 | + parameters.presence = await self.presence |
| 32 | + |
| 33 | + try Task.checkCancellation() |
| 34 | + } catch is CancellationError { |
| 35 | + return |
| 36 | + } catch { |
| 37 | + MatrixCoreLogger.logger.fault("Sync task: \(error.localizedDescription)") |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + nonisolated func runSync(parameters: MatrixSyncRequest.Parameters) async throws -> MatrixSyncRequest.Parameters { |
| 44 | + var parameters = parameters |
| 45 | + let client = await self.client |
| 46 | + let sync = try await client.sync(parameters: parameters) |
| 47 | + |
| 48 | + try await parseSync(sync) |
| 49 | + |
| 50 | + parameters.since = sync.nextBatch |
| 51 | + return parameters |
| 52 | + } |
| 53 | + |
| 54 | + nonisolated func parseSync(_ sync: MatrixSync) async throws { |
| 55 | + guard let rooms = sync.rooms else { |
| 56 | + return |
| 57 | + } |
| 58 | + |
| 59 | + var acountRooms: [String] = [] |
| 60 | + |
| 61 | + guard let joinedRooms = rooms.joined else { |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + for (roomId, room) in joinedRooms { |
| 66 | + try await parseRoom(roomId: roomId, room: room) |
| 67 | + acountRooms.append(roomId) |
| 68 | + } |
| 69 | + |
| 70 | + // TODO: save account <-> roomId mapping |
| 71 | + } |
| 72 | + |
| 73 | + nonisolated func parseRoom(roomId: String, room: MatrixSync.JoinedRoom) async throws { |
| 74 | + MatrixCoreLogger.logger.trace("Parsing room \(roomId)") |
| 75 | + |
| 76 | + if let timeline = room.timeline { |
| 77 | + for event in timeline.events ?? [] { |
| 78 | + if let event = event as? MatrixStateEvent { |
| 79 | + try await store.addRoomState(state: .init(roomId: roomId, event: event)) |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + /// Stop the sync task |
| 86 | + /// |
| 87 | + /// - Throws: ``MatrixCoreError`` if the sync task is not running |
| 88 | + func stopSync() throws { |
| 89 | + guard let syncTask = self.syncTask, |
| 90 | + !syncTask.isCancelled |
| 91 | + else { |
| 92 | + throw MatrixCoreError.syncNotRunning |
| 93 | + } |
| 94 | + syncTask.cancel() |
| 95 | + self.syncTask = nil |
| 96 | + } |
| 97 | +} |
0 commit comments