-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathMXSpaceService.swift
899 lines (758 loc) · 39.9 KB
/
MXSpaceService.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
//
// Copyright 2021 The Matrix.org Foundation C.I.C
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
import Foundation
/// MXSpaceService error
public enum MXSpaceServiceError: Int, Error {
case spaceNotFound
case unknown
}
// MARK: - MXSpaceService errors
extension MXSpaceServiceError: CustomNSError {
public static let errorDomain = "org.matrix.sdk.spaceService"
public var errorCode: Int {
return Int(rawValue)
}
public var errorUserInfo: [String: Any] {
return [:]
}
}
// MARK: - MXSpaceService notification constants
extension MXSpaceService {
/// Posted once the first graph as been built or loaded
public static let didInitialise = Notification.Name("MXSpaceServiceDidInitialise")
/// Posted once the graph of rooms is up and running
public static let didBuildSpaceGraph = Notification.Name("MXSpaceServiceDidBuildSpaceGraph")
}
/// MXSpaceService enables to handle spaces.
@objcMembers
public class MXSpaceService: NSObject {
// MARK: - Properties
private let spacesPerIdReadWriteQueue: DispatchQueue
private unowned let session: MXSession
private lazy var stateEventBuilder: MXRoomInitialStateEventBuilder = {
return MXRoomInitialStateEventBuilder()
}()
private let roomTypeMapper: MXRoomTypeMapper
private let processingQueue: DispatchQueue
private let sdkProcessingQueue: DispatchQueue
private let completionQueue: DispatchQueue
private var spacesPerId: [String: MXSpace] = [:]
private var isGraphBuilding = false;
private var isClosed = false;
private var sessionStateDidChangeObserver: Any?
private var graph: MXSpaceGraphData = MXSpaceGraphData() {
didSet {
var spacesPerId: [String:MXSpace] = [:]
self.graph.spaceRoomIds.forEach { spaceId in
if let space = self.getSpace(withId: spaceId) {
spacesPerId[spaceId] = space
}
}
spacesPerIdReadWriteQueue.sync(flags: .barrier) {
self.spacesPerId = spacesPerId
}
}
}
// MARK: Public
/// The instance of `MXSpaceNotificationCounter` that computes the number of unread messages for each space
public let notificationCounter: MXSpaceNotificationCounter
/// List of `MXSpace` instances of the high level spaces.
public var rootSpaces: [MXSpace] {
return self.graph.rootSpaceIds.compactMap { spaceId in
self.getSpace(withId: spaceId)
}
}
/// List of `MXRoomSummary` of the high level spaces.
public var rootSpaceSummaries: [MXRoomSummary] {
return self.graph.rootSpaceIds.compactMap { spaceId in
self.session.roomSummary(withRoomId: spaceId)
}
}
/// List of `MXRoomSummary` of all spaces known by the user.
public var spaceSummaries: [MXRoomSummary] {
return self.graph.spaceRoomIds.compactMap { spaceId in
self.session.roomSummary(withRoomId: spaceId)
}
}
/// `true` if the `MXSpaceService` instance needs to be updated (e.g. the instance was busy while `handleSync` was called). `false` otherwise
public private(set) var needsUpdate: Bool = true
/// Set it to `false` if you want to temporarily disable graph update. This will be set automatically to `true` after next sync of the `MXSession`.
public var graphUpdateEnabled = true
/// List of ID of all the ancestors (direct parent spaces and parent spaces of the direct parent spaces) by room ID.
public var ancestorsPerRoomId: [String:Set<String>] {
return graph.ancestorsPerRoomId
}
/// The `MXSpaceService` instance is initialised if a previously saved graph has been restored or after the first sync.
public private(set) var isInitialised = false {
didSet {
if !oldValue && isInitialised {
self.completionQueue.async {
NotificationCenter.default.post(name: MXSpaceService.didInitialise, object: self)
}
}
}
}
// MARK: - Setup
public init(session: MXSession) {
self.session = session
self.notificationCounter = MXSpaceNotificationCounter(session: session)
self.roomTypeMapper = MXRoomTypeMapper(defaultRoomType: .room)
self.processingQueue = DispatchQueue(label: "org.matrix.sdk.MXSpaceService.processingQueue", attributes: .concurrent)
self.completionQueue = DispatchQueue.main
self.sdkProcessingQueue = DispatchQueue.main
self.spacesPerIdReadWriteQueue = DispatchQueue(
label: "org.matrix.sdk.MXSpaceService.spacesPerIdReadWriteQueue",
attributes: .concurrent
)
super.init()
self.registerNotificationObservers()
}
deinit {
unregisterNotificationObservers()
}
// MARK: - Public
/// close the service and free all data
public func close() {
self.isClosed = true
self.graph = MXSpaceGraphData()
self.notificationCounter.close()
self.isInitialised = false
self.completionQueue.async {
NotificationCenter.default.post(name: MXSpaceService.didBuildSpaceGraph, object: self)
}
}
/// Loads graph from the given store
public func loadData() {
self.processingQueue.async {
var _myUserId: String?
var _myDeviceId: String?
self.sdkProcessingQueue.sync {
_myUserId = self.session.myUserId
_myDeviceId = self.session.myDeviceId
}
guard let myUserId = _myUserId, let myDeviceId = _myDeviceId else {
MXLog.error("[MXSpaceService] loadData: Unexpectedly found nil for myUserId and/or myDeviceId")
return
}
let store = MXSpaceFileStore(userId: myUserId, deviceId: myDeviceId)
if let loadedGraph = store.loadSpaceGraphData() {
self.graph = loadedGraph
self.completionQueue.async {
self.isInitialised = true
self.notificationCounter.computeNotificationCount()
NotificationCenter.default.post(name: MXSpaceService.didBuildSpaceGraph, object: self)
}
}
}
}
/// Returns the set of direct parent IDs of the given room
/// - Parameters:
/// - roomId: ID of the room
/// - Returns: set of direct parent IDs of the given room. Empty set if the room has no parent.
public func directParentIds(ofRoomWithId roomId: String) -> Set<String> {
return graph.parentIdsPerRoomId[roomId] ?? Set()
}
/// Returns the set of direct parent IDs of the given room for which the room is suggested or not according to the request.
/// - Parameters:
/// - roomId: ID of the room
/// - suggested: If `true` the method will return the parent IDs where the room is suggested. If `false` the method will return the parent IDs where the room is NOT suggested
/// - Returns: set of direct parent IDs of the given room. Empty set if the room has no parent.
public func directParentIds(ofRoomWithId roomId: String, whereRoomIsSuggested suggested: Bool) -> Set<String> {
return directParentIds(ofRoomWithId: roomId).filter { spaceId in
guard let space = spacesPerId[spaceId] else {
return false
}
return (suggested && space.suggestedRoomIds.contains(roomId)) || (!suggested && !space.suggestedRoomIds.contains(roomId))
}
}
/// Allows to know if a given room is a descendant of a given space
/// - Parameters:
/// - roomId: ID of the room
/// - spaceId: ID of the space
/// - Returns: `true` if the room with the given ID is an ancestor of the space with the given ID .`false` otherwise
public func isRoom(withId roomId: String, descendantOf spaceId: String) -> Bool {
return self.graph.descendantsPerRoomId[spaceId]?.contains(roomId) ?? false
}
/// Allows to know if the room is oprhnaed (e.g. has no ancestor)
/// - Parameters:
/// - roomId: ID of the room
/// - Returns: `true` if the room with the given ID is orphaned .`false` otherwise
public func isOrphanedRoom(withId roomId: String) -> Bool {
return self.graph.orphanedRoomIds.contains(roomId) || self.graph.orphanedDirectRoomIds.contains(roomId)
}
/// Returns the first ancestor which is a root space
/// - Parameters:
/// - roomId: ID of the room
/// - Returns: Instance of the ancestor if found. `nil` otherwise
public func firstRootAncestorForRoom(withId roomId: String) -> MXSpace? {
if let ancestorIds = ancestorsPerRoomId[roomId] {
for ancestorId in ancestorIds where ancestorsPerRoomId[ancestorId] == nil {
return spacesPerId[ancestorId]
}
}
return nil
}
/// Handle a sync response
/// - Parameters:
/// - syncResponse: The sync response object
public func handleSyncResponse(_ syncResponse: MXSyncResponse) {
guard self.needsUpdate || !(syncResponse.rooms?.join?.isEmpty ?? true) || !(syncResponse.rooms?.invite?.isEmpty ?? true) || !(syncResponse.rooms?.leave?.isEmpty ?? true) || !(syncResponse.toDevice?.events.isEmpty ?? true) else {
return
}
self.buildGraph()
}
/// Create a space.
/// - Parameters:
/// - parameters: The parameters for space creation.
/// - completion: A closure called when the operation completes.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
public func createSpace(with parameters: MXSpaceCreationParameters, completion: @escaping (MXResponse<MXSpace>) -> Void) -> MXHTTPOperation {
return self.session.createRoom(parameters: parameters) { (response) in
switch response {
case .success(let room):
let space: MXSpace = MXSpace(roomId: room.roomId, session:self.session)
self.completionQueue.async {
completion(.success(space))
}
case .failure(let error):
self.completionQueue.async {
completion(.failure(error))
}
}
}
}
/// Create a space shortcut.
/// - Parameters:
/// - name: The space name.
/// - topic: The space topic.
/// - isPublic: true to indicate to use public chat presets and join the space without invite or false to use private chat presets and join the space on invite.
/// - aliasLocalPart: local part of the alias
/// (e.g. for the alias "#my_alias:example.org", the local part is "my_alias")
/// - inviteArray: list of invited user IDs
/// - completion: A closure called when the operation completes.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
public func createSpace(withName name: String?, topic: String?, isPublic: Bool, aliasLocalPart: String? = nil, inviteArray: [String]? = nil, completion: @escaping (MXResponse<MXSpace>) -> Void) -> MXHTTPOperation {
let parameters = MXSpaceCreationParameters()
parameters.name = name
parameters.topic = topic
parameters.preset = isPublic ? kMXRoomPresetPublicChat : kMXRoomPresetPrivateChat
parameters.visibility = isPublic ? kMXRoomDirectoryVisibilityPublic : kMXRoomDirectoryVisibilityPrivate
parameters.inviteArray = inviteArray
if isPublic {
parameters.roomAlias = aliasLocalPart
let guestAccessStateEvent = self.stateEventBuilder.buildGuestAccessEvent(withAccess: .canJoin)
parameters.addOrUpdateInitialStateEvent(guestAccessStateEvent)
let historyVisibilityStateEvent = self.stateEventBuilder.buildHistoryVisibilityEvent(withVisibility: .worldReadable)
parameters.addOrUpdateInitialStateEvent(historyVisibilityStateEvent)
parameters.powerLevelContentOverride?.invite = 0 // default
} else {
parameters.powerLevelContentOverride?.invite = 50 // moderator
}
return self.createSpace(with: parameters, completion: completion)
}
/// Get a space from a roomId.
/// - Parameter spaceId: The id of the space.
/// - Returns: A MXSpace with the associated roomId or null if room doesn't exists or the room type is not space.
public func getSpace(withId spaceId: String) -> MXSpace? {
var space: MXSpace?
spacesPerIdReadWriteQueue.sync {
space = self.spacesPerId[spaceId]
}
if space == nil, let newSpace = self.session.room(withRoomId: spaceId)?.toSpace() {
space = newSpace
spacesPerIdReadWriteQueue.sync(flags: .barrier) {
self.spacesPerId[spaceId] = newSpace
}
}
return space
}
/// Get the space children informations of a given space from the server.
/// - Parameters:
/// - spaceId: The room id of the queried space.
/// - suggestedOnly: If `true`, return only child events and rooms where the `m.space.child` event has `suggested: true`.
/// - limit: Optional. A limit to the maximum number of children to return per space. `-1` for no limit
/// - maxDepth: Optional. The maximum depth in the tree (from the root room) to return. `-1` for no limit
/// - paginationToken: Optional. Pagination token given to retrieve the next set of rooms.
/// - completion: A closure called when the operation completes.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
public func getSpaceChildrenForSpace(withId spaceId: String,
suggestedOnly: Bool,
limit: Int?,
maxDepth: Int?,
paginationToken: String?,
completion: @escaping (MXResponse<MXSpaceChildrenSummary>) -> Void) -> MXHTTPOperation {
return self.session.matrixRestClient.getSpaceChildrenForSpace(withId: spaceId, suggestedOnly: suggestedOnly, limit: limit, maxDepth: maxDepth, paginationToken: paginationToken) { (response) in
switch response {
case .success(let spaceChildrenResponse):
self.processingQueue.async { [weak self] in
guard let self = self else {
return
}
guard let rooms = spaceChildrenResponse.rooms else {
// We should have at least one room for the requested space
self.completionQueue.async {
completion(.failure(MXSpaceServiceError.spaceNotFound))
}
return
}
// Build room hierarchy and events
var childrenIdsPerChildRoomId: [String: [String]] = [:]
var parentIdsPerChildRoomId: [String:Set<String>] = [:]
var spaceChildEventsPerChildRoomId: [String:[String:Any]] = [:]
for room in spaceChildrenResponse.rooms ?? [] {
for event in room.childrenState ?? [] where event.wireContent.count > 0 {
spaceChildEventsPerChildRoomId[event.stateKey] = event.wireContent
var parentIds = parentIdsPerChildRoomId[event.stateKey] ?? Set()
parentIds.insert(event.roomId)
parentIdsPerChildRoomId[event.stateKey] = parentIds
var childrenIds = childrenIdsPerChildRoomId[event.roomId] ?? []
childrenIds.append(event.stateKey)
childrenIdsPerChildRoomId[event.roomId] = childrenIds
}
}
var spaceInfo: MXSpaceChildInfo?
if let rootSpaceChildSummaryResponse = rooms.first(where: { spaceResponse -> Bool in spaceResponse.roomId == spaceId}) {
spaceInfo = self.createSpaceChildInfo(with: rootSpaceChildSummaryResponse, childrenIds: childrenIdsPerChildRoomId[spaceId], childEvents: spaceChildEventsPerChildRoomId[spaceId])
}
// Build the child summaries of the queried space
let childInfos = self.spaceChildInfos(from: spaceChildrenResponse, excludedSpaceId: spaceId, childrenIdsPerChildRoomId: childrenIdsPerChildRoomId, parentIdsPerChildRoomId: parentIdsPerChildRoomId, spaceChildEventsPerChildRoomId: spaceChildEventsPerChildRoomId)
let spaceChildrenSummary = MXSpaceChildrenSummary(spaceInfo: spaceInfo, childInfos: childInfos, nextBatch: spaceChildrenResponse.nextBatch)
self.completionQueue.async {
completion(.success(spaceChildrenSummary))
}
}
case .failure(let error):
self.completionQueue.async {
completion(.failure(error))
}
}
}
}
// MARK: - Space graph computation
private class PrepareDataResult {
private var _spaces: [MXSpace] = []
private var _spacesPerId: [String : MXSpace] = [:]
private var _directRoomIdsPerMemberId: [String: [String]] = [:]
private var computingSpaces: Set<String> = Set()
private var computingDirectRooms: Set<String> = Set()
var spaces: [MXSpace] {
var result: [MXSpace] = []
self.serialQueue.sync {
result = self._spaces
}
return result
}
var spacesPerId: [String : MXSpace] {
var result: [String : MXSpace] = [:]
self.serialQueue.sync {
result = self._spacesPerId
}
return result
}
var directRoomIdsPerMemberId: [String: [String]] {
var result: [String: [String]] = [:]
self.serialQueue.sync {
result = self._directRoomIdsPerMemberId
}
return result
}
var isPreparingData = true
var isComputing: Bool {
var isComputing = false
self.serialQueue.sync {
isComputing = !self.computingSpaces.isEmpty || !self.computingDirectRooms.isEmpty
}
return isComputing
}
private let serialQueue = DispatchQueue(label: "org.matrix.sdk.MXSpaceService.PrepareDataResult.serialQueue")
func add(space: MXSpace) {
self.serialQueue.sync {
self._spaces.append(space)
self._spacesPerId[space.spaceId] = space
}
}
func add(directRoom: MXRoom, toUserWithId userId: String) {
self.serialQueue.sync {
var rooms = self._directRoomIdsPerMemberId[userId] ?? []
rooms.append(directRoom.roomId)
self._directRoomIdsPerMemberId[userId] = rooms
}
}
func setComputing(_ isComputing: Bool, forSpace space: MXSpace) {
self.serialQueue.sync {
if isComputing {
computingSpaces.insert(space.spaceId)
} else {
computingSpaces.remove(space.spaceId)
}
}
}
func setComputing(_ isComputing: Bool, forDirectRoom room: MXRoom) {
self.serialQueue.sync {
if isComputing {
computingDirectRooms.insert(room.roomId)
} else {
computingDirectRooms.remove(room.roomId)
}
}
}
}
/// Build the graph of rooms
private func buildGraph() {
guard !self.isClosed && !self.isGraphBuilding && self.graphUpdateEnabled else {
MXLog.debug("[MXSpaceService] buildGraph: aborted: graph is building or disabled")
self.needsUpdate = true
return
}
self.isGraphBuilding = true
self.needsUpdate = false
let startDate = Date()
MXLog.debug("[MXSpaceService] buildGraph: started")
var directRoomIds = Set<String>()
let roomIds: [String] = self.session.rooms.compactMap { room in
if room.isDirect {
directRoomIds.insert(room.roomId)
}
return room.roomId
}
let output = PrepareDataResult()
MXLog.debug("[MXSpaceService] buildGraph: preparing data for \(roomIds.count) rooms")
self.prepareData(with: roomIds, index: 0, output: output) { result in
guard !self.isClosed else {
return
}
MXLog.debug("[MXSpaceService] buildGraph: data prepared in \(Date().timeIntervalSince(startDate))")
self.computSpaceGraph(with: result, roomIds: roomIds, directRoomIds: directRoomIds) { graph in
guard !self.isClosed else {
return
}
self.graph = graph
MXLog.debug("[MXSpaceService] buildGraph: ended after \(Date().timeIntervalSince(startDate))s")
self.isGraphBuilding = false
self.isInitialised = true
NotificationCenter.default.post(name: MXSpaceService.didBuildSpaceGraph, object: self)
self.processingQueue.async {
var _myUserId: String?
var _myDeviceId: String?
self.sdkProcessingQueue.sync {
_myUserId = self.session.myUserId
_myDeviceId = self.session.myDeviceId
}
guard let myUserId = _myUserId, let myDeviceId = _myDeviceId else {
MXLog.error("[MXSpaceService] buildGraph: Unexpectedly found nil for myUserId and/or myDeviceId")
return
}
let store = MXSpaceFileStore(userId: myUserId, deviceId: myDeviceId)
if !store.store(spaceGraphData: self.graph) {
MXLog.error("[MXSpaceService] buildGraph: failed to store space graph")
}
// TODO improve updateNotificationsCount and call the method to all spaces once subspaces will be supported
self.notificationCounter.computeNotificationCount()
}
}
}
}
private func prepareData(with roomIds:[String], index: Int, output: PrepareDataResult, completion: @escaping (_ result: PrepareDataResult) -> Void) {
guard !self.isClosed else {
// abort prepareData if the service is closed. No completion needed
return
}
self.processingQueue.async {
guard index < roomIds.count else {
self.completionQueue.async {
output.isPreparingData = false
if !output.isComputing {
completion(output)
}
}
return
}
self.sdkProcessingQueue.async {
guard let room = self.session.room(withRoomId: roomIds[index]) else {
self.prepareData(with: roomIds, index: index+1, output: output, completion: completion)
return
}
var space: MXSpace?
self.spacesPerIdReadWriteQueue.sync {
space = self.spacesPerId[room.roomId] ?? room.toSpace()
}
self.prepareData(with: roomIds, index: index, output: output, room: room, space: space, isRoomDirect: room.isDirect, directUserId: room.directUserId, completion: completion)
}
}
}
private func prepareData(with roomIds:[String], index: Int, output: PrepareDataResult, room: MXRoom, space _space: MXSpace?, isRoomDirect:Bool, directUserId _directUserId: String?, completion: @escaping (_ result: PrepareDataResult) -> Void) {
guard !self.isClosed else {
// abort prepareData if the service is closed. No completion needed
return
}
self.processingQueue.async {
if let space = _space {
output.setComputing(true, forSpace: space)
space.readChildRoomsAndMembers {
output.setComputing(false, forSpace: space)
if !output.isPreparingData && !output.isComputing {
guard !self.isClosed else {
// abort prepareData if the service is closed. No completion needed
return
}
completion(output)
}
}
output.add(space: space)
self.prepareData(with: roomIds, index: index+1, output: output, completion: completion)
} else if isRoomDirect {
if let directUserId = _directUserId {
output.add(directRoom: room, toUserWithId: directUserId)
self.prepareData(with: roomIds, index: index+1, output: output, completion: completion)
} else {
self.sdkProcessingQueue.async {
output.setComputing(true, forDirectRoom: room)
room.members { response in
guard !self.isClosed else {
// abort prepareData if the service is closed. No completion needed
return
}
guard let members = response.value as? MXRoomMembers else {
self.prepareData(with: roomIds, index: index+1, output: output, completion: completion)
return
}
let membersId = members.members?.compactMap({ roomMember in
return roomMember.userId != self.session.myUserId ? roomMember.userId : nil
}) ?? []
self.processingQueue.async {
membersId.forEach { memberId in
output.add(directRoom: room, toUserWithId: memberId)
}
output.setComputing(false, forDirectRoom: room)
if !output.isPreparingData && !output.isComputing {
completion(output)
}
}
}
self.prepareData(with: roomIds, index: index+1, output: output, completion: completion)
}
}
} else {
self.prepareData(with: roomIds, index: index+1, output: output, completion: completion)
}
}
}
private func computSpaceGraph(with result: PrepareDataResult, roomIds: [String], directRoomIds: Set<String>, completion: @escaping (_ graph: MXSpaceGraphData) -> Void) {
let startDate = Date()
MXLog.debug("[MXSpaceService] computSpaceGraph: started for \(roomIds.count) rooms, \(directRoomIds.count) direct rooms, \(result.spaces.count) spaces, \(result.spaces.reduce(0, { $0 + $1.childSpaces.count })) child spaces, \(result.spaces.reduce(0, { $0 + $1.childRoomIds.count })) child rooms, \(result.spaces.reduce(0, { $0 + $1.otherMembersId.count })) other members, \(result.directRoomIdsPerMemberId.count) members")
self.processingQueue.async {
var parentIdsPerRoomId: [String : Set<String>] = [:]
result.spaces.forEach { space in
space.updateChildSpaces(with: result.spacesPerId)
space.updateChildDirectRooms(with: result.directRoomIdsPerMemberId)
space.childRoomIds.forEach { roomId in
var parentIds = parentIdsPerRoomId[roomId] ?? Set<String>()
parentIds.insert(space.spaceId)
parentIdsPerRoomId[roomId] = parentIds
}
space.childSpaces.forEach { childSpace in
var parentIds = parentIdsPerRoomId[childSpace.spaceId] ?? Set<String>()
parentIds.insert(space.spaceId)
parentIdsPerRoomId[childSpace.spaceId] = parentIds
}
}
let rootSpaces = result.spaces.filter { space in
return parentIdsPerRoomId[space.spaceId] == nil
}.sorted { space1, space2 in
let _space1Order = space1.order
let _space2Order = space2.order
if let space1Order = _space1Order, let space2Order = _space2Order {
return space1Order <= space2Order
}
if _space1Order == nil && _space2Order == nil {
return space1.spaceId <= space2.spaceId
} else if _space1Order != nil && _space2Order == nil {
return true
} else {
return false
}
}
var ancestorsPerRoomId: [String: Set<String>] = [:]
var descendantsPerRoomId: [String: Set<String>] = [:]
rootSpaces.forEach { space in
self.buildRoomHierarchy(with: space, visitedSpaceIds: [], ancestorsPerRoomId: &ancestorsPerRoomId, descendantsPerRoomId: &descendantsPerRoomId)
}
var orphanedRoomIds: Set<String> = Set<String>()
var orphanedDirectRoomIds: Set<String> = Set<String>()
for roomId in roomIds {
let isRoomDirect = directRoomIds.contains(roomId)
if !isRoomDirect && parentIdsPerRoomId[roomId] == nil {
orphanedRoomIds.insert(roomId)
} else if isRoomDirect && parentIdsPerRoomId[roomId] == nil {
orphanedDirectRoomIds.insert(roomId)
}
}
let graph = MXSpaceGraphData(
spaceRoomIds: result.spaces.map({ space in
space.spaceId
}),
parentIdsPerRoomId: parentIdsPerRoomId,
ancestorsPerRoomId: ancestorsPerRoomId,
descendantsPerRoomId: descendantsPerRoomId,
rootSpaceIds: rootSpaces.map({ space in
space.spaceId
}),
orphanedRoomIds: orphanedRoomIds,
orphanedDirectRoomIds: orphanedDirectRoomIds)
MXLog.debug("[MXSpaceService] computSpaceGraph: space graph computed in \(Date().timeIntervalSince(startDate))s")
self.completionQueue.async {
completion(graph)
}
}
}
private func buildRoomHierarchy(with space: MXSpace, visitedSpaceIds: [String], ancestorsPerRoomId: inout [String: Set<String>], descendantsPerRoomId: inout [String: Set<String>]) {
var visitedSpaceIds = visitedSpaceIds
visitedSpaceIds.append(space.spaceId)
space.childRoomIds.forEach { roomId in
var parentIds = ancestorsPerRoomId[roomId] ?? Set<String>()
visitedSpaceIds.forEach { spaceId in
parentIds.insert(spaceId)
var descendantIds = descendantsPerRoomId[spaceId] ?? Set<String>()
descendantIds.insert(roomId)
descendantsPerRoomId[spaceId] = descendantIds
}
ancestorsPerRoomId[roomId] = parentIds
}
space.childSpaces.forEach { childSpace in
buildRoomHierarchy(with: childSpace, visitedSpaceIds: visitedSpaceIds, ancestorsPerRoomId: &ancestorsPerRoomId, descendantsPerRoomId: &descendantsPerRoomId)
}
}
// MARK: - Notification handling
private func registerNotificationObservers() {
self.sessionStateDidChangeObserver = NotificationCenter.default.addObserver(forName: NSNotification.Name.mxSessionStateDidChange, object: session, queue: nil) { [weak self] notification in
guard let session = self?.session, session.state == .storeDataReady else {
return
}
self?.loadData()
}
}
private func unregisterNotificationObservers() {
if let observer = self.sessionStateDidChangeObserver {
NotificationCenter.default.removeObserver(observer)
}
}
// MARK: - Private
private func createRoomSummary(with spaceChildSummaryResponse: MXSpaceChildSummaryResponse) -> MXRoomSummary {
let roomId = spaceChildSummaryResponse.roomId
let roomTypeString = spaceChildSummaryResponse.roomType
let roomSummary: MXRoomSummary = MXRoomSummary(roomId: roomId, andMatrixSession: nil)
roomSummary.roomTypeString = roomTypeString
roomSummary.roomType = self.roomTypeMapper.roomType(from: roomTypeString)
let joinedMembersCount = UInt(spaceChildSummaryResponse.numJoinedMembers)
let membersCount = MXRoomMembersCount()
membersCount.joined = joinedMembersCount
membersCount.members = joinedMembersCount
roomSummary.membersCount = membersCount
roomSummary.displayname = spaceChildSummaryResponse.name
roomSummary.topic = spaceChildSummaryResponse.topic
roomSummary.avatar = spaceChildSummaryResponse.avatarUrl
roomSummary.isEncrypted = false
return roomSummary
}
private func spaceChildInfos(from spaceChildrenResponse: MXSpaceChildrenResponse, excludedSpaceId: String, childrenIdsPerChildRoomId: [String: [String]], parentIdsPerChildRoomId: [String:Set<String>], spaceChildEventsPerChildRoomId: [String:[String:Any]]) -> [MXSpaceChildInfo] {
guard let spaceChildSummaries = spaceChildrenResponse.rooms else {
return []
}
let childInfos: [MXSpaceChildInfo] = spaceChildSummaries.compactMap { (spaceChildSummaryResponse) -> MXSpaceChildInfo? in
let spaceId = spaceChildSummaryResponse.roomId
guard spaceId != excludedSpaceId else {
return nil
}
return self.createSpaceChildInfo(with: spaceChildSummaryResponse, childrenIds: childrenIdsPerChildRoomId[spaceId], childEvents: spaceChildEventsPerChildRoomId[spaceId])
}
return childInfos
}
private func createSpaceChildInfo(with spaceChildSummaryResponse: MXSpaceChildSummaryResponse, childrenIds: [String]?, childEvents: [String:Any]?) -> MXSpaceChildInfo {
let roomTypeString = spaceChildSummaryResponse.roomType
let roomType = self.roomTypeMapper.roomType(from: roomTypeString)
return MXSpaceChildInfo(childRoomId: spaceChildSummaryResponse.roomId,
isKnown: true,
roomTypeString: roomTypeString,
roomType: roomType,
name: spaceChildSummaryResponse.name,
topic: spaceChildSummaryResponse.topic,
canonicalAlias: spaceChildSummaryResponse.canonicalAlias,
avatarUrl: spaceChildSummaryResponse.avatarUrl,
activeMemberCount: spaceChildSummaryResponse.numJoinedMembers,
autoJoin: childEvents?[kMXEventTypeStringAutoJoinKey] as? Bool ?? false,
suggested: childEvents?[kMXEventTypeStringSuggestedKey] as? Bool ?? false,
childrenIds: childrenIds ?? [])
}
}
// MARK: - Objective-C interface
extension MXSpaceService {
/// Create a space.
/// - Parameters:
/// - parameters: The parameters for space creation.
/// - success: A closure called when the operation is complete.
/// - failure: A closure called when the operation fails.
/// - Returns: a `MXHTTPOperation` instance.
@objc public func createSpace(with parameters: MXSpaceCreationParameters, success: @escaping (MXSpace) -> Void, failure: @escaping (Error) -> Void) -> MXHTTPOperation {
return self.createSpace(with: parameters) { (response) in
uncurryResponse(response, success: success, failure: failure)
}
}
/// Create a space shortcut.
/// - Parameters:
/// - name: The space name.
/// - topic: The space topic.
/// - isPublic: true to indicate to use public chat presets and join the space without invite or false to use private chat presets and join the space on invite.
/// - aliasLocalPart: local part of the alias
/// (e.g. for the alias "#my_alias:example.org", the local part is "my_alias")
/// - inviteArray: list of invited user IDs
/// - success: A closure called when the operation is complete.
/// - failure: A closure called when the operation fails.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
@objc public func createSpace(withName name: String, topic: String?, isPublic: Bool, aliasLocalPart: String?, inviteArray: [String]?, success: @escaping (MXSpace) -> Void, failure: @escaping (Error) -> Void) -> MXHTTPOperation {
return self.createSpace(withName: name, topic: topic, isPublic: isPublic, aliasLocalPart: aliasLocalPart, inviteArray: inviteArray) { (response) in
uncurryResponse(response, success: success, failure: failure)
}
}
/// Get the space children informations of a given space from the server.
/// - Parameters:
/// - spaceId: The room id of the queried space.
/// - suggestedOnly: If `true`, return only child events and rooms where the `m.space.child` event has `suggested: true`.
/// - limit: Optional. A limit to the maximum number of children to return per space. `-1` for no limit
/// - maxDepth: Optional. The maximum depth in the tree (from the root room) to return. `-1` for no limit
/// - paginationToken: Optional. Pagination token given to retrieve the next set of rooms.
/// - success: A closure called when the operation is complete.
/// - failure: A closure called when the operation fails.
/// - Returns: a `MXHTTPOperation` instance.
@discardableResult
@objc public func getSpaceChildrenForSpace(withId spaceId: String, suggestedOnly: Bool, limit: Int, maxDepth: Int, paginationToken: String?, success: @escaping (MXSpaceChildrenSummary) -> Void, failure: @escaping (Error) -> Void) -> MXHTTPOperation {
return self.getSpaceChildrenForSpace(withId: spaceId, suggestedOnly: suggestedOnly, limit: limit, maxDepth: maxDepth, paginationToken: paginationToken) { (response) in
uncurryResponse(response, success: success, failure: failure)
}
}
}
// MARK: - Internal room additions
extension MXRoom {
func toSpace() -> MXSpace? {
guard let summary = self.summary, summary.roomType == .space else {
return nil
}
return MXSpace(roomId: self.roomId, session: self.mxSession)
}
}