Skip to content

Commit a239f5f

Browse files
committed
misc : clean up after PR review
1 parent fe0b54e commit a239f5f

File tree

24 files changed

+81
-115
lines changed

24 files changed

+81
-115
lines changed

matrix-sdk-android-flow/src/main/java/org/matrix/android/sdk/flow/FlowRoom.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import org.matrix.android.sdk.api.session.room.model.ReadReceipt
3030
import org.matrix.android.sdk.api.session.room.model.RoomMemberSummary
3131
import org.matrix.android.sdk.api.session.room.model.RoomSummary
3232
import org.matrix.android.sdk.api.session.room.notification.RoomNotificationState
33+
import org.matrix.android.sdk.api.session.room.powerlevels.RoomPowerLevels
3334
import org.matrix.android.sdk.api.session.room.send.UserDraft
3435
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
3536
import org.matrix.android.sdk.api.util.Optional
@@ -95,6 +96,10 @@ class FlowRoom(private val room: Room) {
9596
}
9697
}
9798

99+
fun liveRoomPowerLevels(): Flow<RoomPowerLevels> {
100+
return room.stateService().getRoomPowerLevelsLive().asFlow()
101+
}
102+
98103
fun liveReadMarker(): Flow<Optional<String>> {
99104
return room.readService().getReadMarkerLive().asFlow()
100105
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/RoomExtensions.kt

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,7 @@
1717
package org.matrix.android.sdk.api.session.room
1818

1919
import org.matrix.android.sdk.api.query.QueryStateEventValue
20-
import org.matrix.android.sdk.api.query.QueryStringValue
2120
import org.matrix.android.sdk.api.session.events.model.Event
22-
import org.matrix.android.sdk.api.session.events.model.EventType
23-
import org.matrix.android.sdk.api.session.events.model.toModel
24-
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
25-
import org.matrix.android.sdk.api.session.room.model.create.getRoomCreateContentWithSender
2621
import org.matrix.android.sdk.api.session.room.powerlevels.RoomPowerLevels
2722
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
2823

@@ -45,10 +40,5 @@ fun Room.getStateEvent(eventType: String, stateKey: QueryStateEventValue): Event
4540
* Get the current RoomPowerLevels of the room.
4641
*/
4742
fun Room.getRoomPowerLevels(): RoomPowerLevels {
48-
val powerLevelsContent = getStateEvent(EventType.STATE_ROOM_POWER_LEVELS, QueryStringValue.IsEmpty)?.content?.toModel<PowerLevelsContent>()
49-
val roomCreateContent = getStateEvent(EventType.STATE_ROOM_CREATE, QueryStringValue.IsEmpty)?.getRoomCreateContentWithSender()
50-
return RoomPowerLevels(
51-
powerLevelsContent,
52-
roomCreateContent
53-
)
43+
return stateService().getRoomPowerLevels()
5444
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/state/StateService.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import org.matrix.android.sdk.api.session.room.model.GuestAccess
2424
import org.matrix.android.sdk.api.session.room.model.RoomHistoryVisibility
2525
import org.matrix.android.sdk.api.session.room.model.RoomJoinRules
2626
import org.matrix.android.sdk.api.session.room.model.RoomJoinRulesAllowEntry
27+
import org.matrix.android.sdk.api.session.room.powerlevels.RoomPowerLevels
2728
import org.matrix.android.sdk.api.util.JsonDict
2829
import org.matrix.android.sdk.api.util.Optional
2930

@@ -106,4 +107,6 @@ interface StateService {
106107
suspend fun setJoinRulePublic()
107108
suspend fun setJoinRuleInviteOnly()
108109
suspend fun setJoinRuleRestricted(allowList: List<String>)
110+
fun getRoomPowerLevels(): RoomPowerLevels
111+
fun getRoomPowerLevelsLive(): LiveData<RoomPowerLevels>
109112
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/pushers/DefaultConditionResolver.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,11 @@ import org.matrix.android.sdk.api.session.pushrules.SenderNotificationPermission
2424
import org.matrix.android.sdk.api.session.room.getRoomPowerLevels
2525
import org.matrix.android.sdk.internal.di.UserId
2626
import org.matrix.android.sdk.internal.session.room.RoomGetter
27-
import org.matrix.android.sdk.internal.session.room.state.StateEventDataSource
2827
import javax.inject.Inject
2928

3029
internal class DefaultConditionResolver @Inject constructor(
3130
private val roomGetter: RoomGetter,
3231
@UserId private val userId: String,
33-
private val stateEventDataSource: StateEventDataSource,
3432
) : ConditionResolver {
3533

3634
override fun resolveEventMatchCondition(

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/powerlevels/RoomPowerLevels.kt

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,53 @@
1717

1818
package org.matrix.android.sdk.internal.session.room.powerlevels
1919

20+
import androidx.lifecycle.LiveData
21+
import androidx.lifecycle.MediatorLiveData
2022
import org.matrix.android.sdk.api.query.QueryStringValue
23+
import org.matrix.android.sdk.api.session.events.model.Event
2124
import org.matrix.android.sdk.api.session.events.model.EventType
2225
import org.matrix.android.sdk.api.session.events.model.toModel
2326
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
2427
import org.matrix.android.sdk.api.session.room.model.create.getRoomCreateContentWithSender
2528
import org.matrix.android.sdk.api.session.room.powerlevels.RoomPowerLevels
29+
import org.matrix.android.sdk.api.util.Optional
2630
import org.matrix.android.sdk.internal.session.room.state.StateEventDataSource
2731

2832
internal fun StateEventDataSource.getRoomPowerLevels(roomId: String): RoomPowerLevels {
29-
val powerLevelsContent = getStateEvent(roomId, EventType.STATE_ROOM_POWER_LEVELS, QueryStringValue.IsEmpty)
30-
?.content?.toModel<PowerLevelsContent>()
31-
val roomCreateContent = getStateEvent(roomId, EventType.STATE_ROOM_CREATE, QueryStringValue.IsEmpty)?.getRoomCreateContentWithSender()
33+
val powerLevelsEvent = getStateEvent(roomId, EventType.STATE_ROOM_POWER_LEVELS, QueryStringValue.IsEmpty)
34+
val roomCreateEvent = getStateEvent(roomId, EventType.STATE_ROOM_CREATE, QueryStringValue.IsEmpty)
35+
return createRoomPowerLevels(powerLevelsEvent = powerLevelsEvent, roomCreateEvent = roomCreateEvent)
36+
}
37+
38+
internal fun StateEventDataSource.getRoomPowerLevelsLive(roomId: String): LiveData<RoomPowerLevels> {
39+
val powerLevelsEventLive = getStateEventLive(roomId, EventType.STATE_ROOM_POWER_LEVELS, QueryStringValue.IsEmpty)
40+
val roomCreateEventLive = getStateEventLive(roomId, EventType.STATE_ROOM_CREATE, QueryStringValue.IsEmpty)
41+
val resultLiveData = MediatorLiveData<RoomPowerLevels>()
42+
43+
fun emitIfReady(powerLevelEvent: Optional<Event>?, roomCreateEvent: Optional<Event>?) {
44+
if (powerLevelEvent != null && roomCreateEvent != null) {
45+
val roomPowerLevels = createRoomPowerLevels(powerLevelEvent.get(), roomCreateEvent.get())
46+
resultLiveData.postValue(roomPowerLevels)
47+
}
48+
}
49+
resultLiveData.apply {
50+
var powerLevelEvent: Optional<Event>? = null
51+
var roomCreateEvent: Optional<Event>? = null
52+
53+
addSource(powerLevelsEventLive) {
54+
powerLevelEvent = it
55+
emitIfReady(powerLevelEvent, roomCreateEvent)
56+
}
57+
addSource(roomCreateEventLive) {
58+
roomCreateEvent = it
59+
emitIfReady(powerLevelEvent, roomCreateEvent)
60+
}
61+
}
62+
return resultLiveData
63+
}
64+
65+
private fun createRoomPowerLevels(powerLevelsEvent: Event?, roomCreateEvent: Event?): RoomPowerLevels {
66+
val powerLevelsContent = powerLevelsEvent?.content?.toModel<PowerLevelsContent>()
67+
val roomCreateContent = roomCreateEvent?.getRoomCreateContentWithSender()
3268
return RoomPowerLevels(powerLevelsContent, roomCreateContent)
3369
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/state/DefaultStateService.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ import org.matrix.android.sdk.api.session.room.model.RoomHistoryVisibility
3131
import org.matrix.android.sdk.api.session.room.model.RoomJoinRules
3232
import org.matrix.android.sdk.api.session.room.model.RoomJoinRulesAllowEntry
3333
import org.matrix.android.sdk.api.session.room.model.RoomJoinRulesContent
34+
import org.matrix.android.sdk.api.session.room.powerlevels.RoomPowerLevels
3435
import org.matrix.android.sdk.api.session.room.state.StateService
3536
import org.matrix.android.sdk.api.util.JsonDict
3637
import org.matrix.android.sdk.api.util.MimeTypes
3738
import org.matrix.android.sdk.api.util.Optional
3839
import org.matrix.android.sdk.internal.session.content.FileUploader
40+
import org.matrix.android.sdk.internal.session.room.powerlevels.getRoomPowerLevels
41+
import org.matrix.android.sdk.internal.session.room.powerlevels.getRoomPowerLevelsLive
3942

4043
internal class DefaultStateService @AssistedInject constructor(
4144
@Assisted private val roomId: String,
@@ -65,6 +68,14 @@ internal class DefaultStateService @AssistedInject constructor(
6568
return stateEventDataSource.getStateEventsLive(roomId, eventTypes, stateKey)
6669
}
6770

71+
override fun getRoomPowerLevels(): RoomPowerLevels {
72+
return stateEventDataSource.getRoomPowerLevels(roomId)
73+
}
74+
75+
override fun getRoomPowerLevelsLive(): LiveData<RoomPowerLevels> {
76+
return stateEventDataSource.getRoomPowerLevelsLive(roomId)
77+
}
78+
6879
override suspend fun sendStateEvent(
6980
eventType: String,
7081
stateKey: String,

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/version/DefaultRoomVersionService.kt

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ import org.matrix.android.sdk.api.query.QueryStringValue
2323
import org.matrix.android.sdk.api.session.events.model.EventType
2424
import org.matrix.android.sdk.api.session.events.model.toModel
2525
import org.matrix.android.sdk.api.session.homeserver.RoomVersionStatus
26-
import org.matrix.android.sdk.api.session.room.model.PowerLevelsContent
2726
import org.matrix.android.sdk.api.session.room.model.create.RoomCreateContent
28-
import org.matrix.android.sdk.api.session.room.model.create.getRoomCreateContentWithSender
29-
import org.matrix.android.sdk.api.session.room.powerlevels.RoomPowerLevels
3027
import org.matrix.android.sdk.api.session.room.version.RoomVersionService
3128
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
29+
import org.matrix.android.sdk.internal.session.room.powerlevels.getRoomPowerLevels
3230
import org.matrix.android.sdk.internal.session.room.state.StateEventDataSource
3331

3432
internal class DefaultRoomVersionService @AssistedInject constructor(
@@ -72,16 +70,7 @@ internal class DefaultRoomVersionService @AssistedInject constructor(
7270
}
7371

7472
override fun userMayUpgradeRoom(userId: String): Boolean {
75-
val powerLevelsContent = stateEventDataSource.getStateEvent(roomId, EventType.STATE_ROOM_POWER_LEVELS, QueryStringValue.IsEmpty)
76-
?.content?.toModel<PowerLevelsContent>()
77-
78-
val roomCreateContent = stateEventDataSource.getStateEvent(roomId, EventType.STATE_ROOM_CREATE, QueryStringValue.IsEmpty)
79-
?.getRoomCreateContentWithSender()
80-
81-
val roomPowerLevels = RoomPowerLevels(
82-
powerLevelsContent = powerLevelsContent,
83-
roomCreateContent = roomCreateContent
84-
)
73+
val roomPowerLevels = stateEventDataSource.getRoomPowerLevels(roomId)
8574
return roomPowerLevels.isUserAllowedToSend(userId, true, EventType.STATE_ROOM_TOMBSTONE)
8675
}
8776

vector/src/main/java/im/vector/app/features/home/room/detail/TimelineViewModel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ import im.vector.app.features.home.room.typing.TypingHelper
5454
import im.vector.app.features.location.live.StopLiveLocationShareUseCase
5555
import im.vector.app.features.location.live.tracking.LocationSharingServiceConnection
5656
import im.vector.app.features.notifications.NotificationDrawerManager
57-
import im.vector.app.features.powerlevel.PowerLevelsFlowFactory
5857
import im.vector.app.features.raw.wellknown.CryptoConfig
5958
import im.vector.app.features.raw.wellknown.getOutboundSessionKeySharingStrategyOrDefault
6059
import im.vector.app.features.raw.wellknown.withElementWellKnown
@@ -302,7 +301,7 @@ class TimelineViewModel @AssistedInject constructor(
302301

303302
private fun observePowerLevel() {
304303
if (room == null) return
305-
PowerLevelsFlowFactory(room).createFlow()
304+
room.flow().liveRoomPowerLevels()
306305
.onEach { powerLevels ->
307306
val canInvite = powerLevels.isUserAbleToInvite(session.myUserId)
308307
val isAllowedToManageWidgets = session.widgetService().hasPermissionsToHandleWidgets(room.roomId)

vector/src/main/java/im/vector/app/features/home/room/detail/composer/MessageComposerViewModel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import im.vector.app.features.home.room.detail.ChatEffect
3030
import im.vector.app.features.home.room.detail.composer.rainbow.RainbowGenerator
3131
import im.vector.app.features.home.room.detail.composer.voice.VoiceMessageRecorderView
3232
import im.vector.app.features.home.room.detail.toMessageType
33-
import im.vector.app.features.powerlevel.PowerLevelsFlowFactory
3433
import im.vector.app.features.session.coroutineScope
3534
import im.vector.app.features.settings.VectorPreferences
3635
import im.vector.app.features.voice.VoiceFailure
@@ -179,7 +178,7 @@ class MessageComposerViewModel @AssistedInject constructor(
179178

180179
private fun observePowerLevelAndEncryption(room: Room) {
181180
combine(
182-
PowerLevelsFlowFactory(room).createFlow(),
181+
room.flow().liveRoomPowerLevels(),
183182
room.flow().liveRoomSummary().unwrap()
184183
) { pl, sum ->
185184
val canSendMessage = pl.isUserAllowedToSend(session.myUserId, false, EventType.MESSAGE)

vector/src/main/java/im/vector/app/features/home/room/detail/timeline/action/MessageActionsViewModel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import im.vector.app.features.home.room.detail.timeline.format.NoticeEventFormat
2323
import im.vector.app.features.html.EventHtmlRenderer
2424
import im.vector.app.features.html.PillsPostProcessor
2525
import im.vector.app.features.html.VectorHtmlCompressor
26-
import im.vector.app.features.powerlevel.PowerLevelsFlowFactory
2726
import im.vector.app.features.reactions.data.EmojiDataSource
2827
import im.vector.app.features.settings.VectorPreferences
2928
import im.vector.lib.strings.CommonStrings
@@ -115,7 +114,7 @@ class MessageActionsViewModel @AssistedInject constructor(
115114
if (room == null) {
116115
return
117116
}
118-
PowerLevelsFlowFactory(room).createFlow()
117+
room.flow().liveRoomPowerLevels()
119118
.onEach { roomPowerLevels ->
120119
val canReact = roomPowerLevels.isUserAllowedToSend(session.myUserId, false, EventType.REACTION)
121120
val canRedact = roomPowerLevels.isUserAbleToRedact(session.myUserId)

0 commit comments

Comments
 (0)