@@ -9,7 +9,6 @@ package io.element.android.libraries.matrix.impl.room
9
9
10
10
import io.element.android.appconfig.TimelineConfig
11
11
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
12
- import io.element.android.libraries.core.data.tryOrNull
13
12
import io.element.android.libraries.featureflag.api.FeatureFlagService
14
13
import io.element.android.libraries.matrix.api.core.DeviceId
15
14
import io.element.android.libraries.matrix.api.core.RoomId
@@ -21,6 +20,7 @@ import io.element.android.libraries.matrix.api.room.RoomMembershipObserver
21
20
import io.element.android.libraries.matrix.api.roomlist.RoomListService
22
21
import io.element.android.libraries.matrix.api.roomlist.awaitLoaded
23
22
import io.element.android.libraries.matrix.impl.room.preview.RoomPreviewInfoMapper
23
+ import io.element.android.libraries.matrix.impl.roomlist.fullRoomWithTimeline
24
24
import io.element.android.libraries.matrix.impl.roomlist.roomOrNull
25
25
import io.element.android.services.toolbox.api.systemclock.SystemClock
26
26
import kotlinx.coroutines.CoroutineScope
@@ -30,10 +30,10 @@ import kotlinx.coroutines.sync.withLock
30
30
import kotlinx.coroutines.withContext
31
31
import org.matrix.rustcomponents.sdk.Client
32
32
import org.matrix.rustcomponents.sdk.Membership
33
+ import org.matrix.rustcomponents.sdk.Room
33
34
import org.matrix.rustcomponents.sdk.RoomListItem
34
35
import timber.log.Timber
35
36
import java.util.concurrent.atomic.AtomicBoolean
36
- import org.matrix.rustcomponents.sdk.Room as SdkRoom
37
37
import org.matrix.rustcomponents.sdk.RoomListService as InnerRoomListService
38
38
39
39
class RustRoomFactory (
@@ -56,11 +56,6 @@ class RustRoomFactory(
56
56
private val mutex = Mutex ()
57
57
private val isDestroyed: AtomicBoolean = AtomicBoolean (false )
58
58
59
- private data class RustRoomReferences (
60
- val roomListItem : RoomListItem ,
61
- val room : SdkRoom ,
62
- )
63
-
64
59
private val roomInfoMapper = RoomInfoMapper ()
65
60
66
61
private val eventFilters = TimelineConfig .excludedEvents
@@ -84,17 +79,22 @@ class RustRoomFactory(
84
79
Timber .d(" Room factory is destroyed, returning null for $roomId " )
85
80
return @withContext null
86
81
}
87
- val roomReferences = awaitRoomReferences (roomId) ? : return @withContext null
88
- getBaseRoom(roomReferences )
82
+ val roomListItem = awaitRoomListItem (roomId) ? : return @withContext null
83
+ getBaseRoom(roomListItem )
89
84
}
90
85
}
91
86
92
- private suspend fun getBaseRoom (roomReferences : RustRoomReferences ): RustBaseRoom ? {
93
- val initialRoomInfo = roomReferences.room.roomInfo()
87
+ private suspend fun getBaseRoom (roomListItem : RoomListItem ): RustBaseRoom ? {
88
+ val sdkRoom = innerClient.getRoom(roomListItem.id()) ? : return null
89
+ return getBaseRoom(sdkRoom)
90
+ }
91
+
92
+ private suspend fun getBaseRoom (sdkRoom : Room ): RustBaseRoom {
93
+ val initialRoomInfo = sdkRoom.roomInfo()
94
94
return RustBaseRoom (
95
95
sessionId = sessionId,
96
96
deviceId = deviceId,
97
- innerRoom = roomReferences.room ,
97
+ innerRoom = sdkRoom ,
98
98
coroutineDispatchers = dispatchers,
99
99
roomSyncSubscriber = roomSyncSubscriber,
100
100
roomMembershipObserver = roomMembershipObserver,
@@ -110,30 +110,26 @@ class RustRoomFactory(
110
110
Timber .d(" Room factory is destroyed, returning null for $roomId " )
111
111
return @withContext null
112
112
}
113
- val roomReferences = awaitRoomReferences(roomId) ? : return @withContext null
114
-
115
- if (roomReferences.room.membership() == Membership .JOINED ) {
116
- val baseRoom = getBaseRoom(roomReferences) ? : return @withContext null
113
+ val roomListItem = awaitRoomListItem(roomId) ? : return @withContext null
117
114
115
+ if (roomListItem.membership() == Membership .JOINED ) {
118
116
// Init the live timeline in the SDK from the RoomListItem
119
- if (! roomReferences.roomListItem.isTimelineInitialized()) {
120
- roomReferences.roomListItem.initTimeline(eventFilters, " LIVE" )
121
- }
117
+ val sdkRoom = roomListItem.fullRoomWithTimeline(eventFilters)
122
118
123
119
GetRoomResult .Joined (
124
120
JoinedRustRoom (
125
- baseRoom = baseRoom ,
121
+ baseRoom = getBaseRoom(sdkRoom) ,
126
122
notificationSettingsService = notificationSettingsService,
127
123
roomContentForwarder = roomContentForwarder,
128
- liveInnerTimeline = roomReferences.room .timeline(),
124
+ liveInnerTimeline = sdkRoom .timeline(),
129
125
coroutineDispatchers = dispatchers,
130
126
systemClock = systemClock,
131
127
featureFlagService = featureFlagService,
132
128
)
133
129
)
134
130
} else {
135
131
val preview = try {
136
- roomReferences. roomListItem.previewRoom(via = emptyList())
132
+ roomListItem.previewRoom(via = emptyList())
137
133
} catch (e: Exception ) {
138
134
Timber .e(e, " Failed to get room preview for $roomId " )
139
135
return @withContext null
@@ -142,44 +138,31 @@ class RustRoomFactory(
142
138
GetRoomResult .NotJoined (
143
139
NotJoinedRustRoom (
144
140
sessionId = sessionId,
145
- localRoom = getBaseRoom(roomReferences ),
141
+ localRoom = getBaseRoom(roomListItem ),
146
142
previewInfo = RoomPreviewInfoMapper .map(preview.info()),
147
143
)
148
144
)
149
145
}
150
146
}
151
147
}
152
148
153
- private fun getRoomReferences (roomId : RoomId ): RustRoomReferences ? {
154
- val roomListItem = innerRoomListService.roomOrNull(roomId.value)
155
- if (roomListItem == null ) {
156
- Timber .d(" Room not found for $roomId " )
157
- return null
158
- }
159
- val room = tryOrNull {
160
- innerClient.getRoom(roomId.value)
161
- } ? : error(" Failed to get room for room id: $roomId " )
162
-
163
- Timber .d(" Got room for $roomId " )
164
- return RustRoomReferences (
165
- roomListItem = roomListItem,
166
- room = room,
167
- )
168
- }
169
-
170
149
/* *
171
- * Get the Rust room references for a room, retrying after the room list is loaded if necessary.
150
+ * Get the Rust room list item for a room, retrying after the room list is loaded if necessary.
172
151
*/
173
- private suspend fun awaitRoomReferences (roomId : RoomId ): RustRoomReferences ? {
174
- var roomReferences = getRoomReferences(roomId)
175
-
176
- if (roomReferences == null ) {
152
+ private suspend fun awaitRoomListItem (roomId : RoomId ): RoomListItem ? {
153
+ var roomListItem = innerRoomListService.roomOrNull(roomId.value)
154
+ if (roomListItem == null ) {
177
155
// ... otherwise, lets wait for the SS to load all rooms and check again.
178
156
roomListService.allRooms.awaitLoaded()
179
- roomReferences = getRoomReferences(roomId)
157
+ roomListItem = innerRoomListService.roomOrNull(roomId.value)
158
+ }
159
+
160
+ if (roomListItem == null ) {
161
+ Timber .d(" Room not found for $roomId " )
162
+ return null
180
163
}
181
164
182
- return roomReferences
165
+ return roomListItem
183
166
}
184
167
}
185
168
0 commit comments