Update Matrix Room API and allow media swipe on pinned event only.

This commit is contained in:
Benoit Marty 2025-02-17 16:45:05 +01:00
parent 728a2c1a32
commit 2e9a158fb0
30 changed files with 268 additions and 181 deletions

View file

@ -109,21 +109,17 @@ interface MatrixRoom : Closeable {
val liveTimeline: Timeline
/**
* Create a new timeline, focused on the provided Event.
* Should not be used directly, see `TimelineController` to manage the various timelines.
* Create a new timeline.
* @param focusedOnEventId The event to focus on, if any. Note: if not null, and for regular timeline,
* this method should not be used directly, see `TimelineController` to manage the various timelines.
* @param onlyPinnedEvents True to get the timeline for pinned events only.
* @param onlyMedia True to get the timeline for media events only.
*/
suspend fun timelineFocusedOnEvent(eventId: EventId): Result<Timeline>
/**
* Create a new timeline for the pinned events of the room.
*/
suspend fun pinnedEventsTimeline(): Result<Timeline>
/**
* Create a new timeline for the media events of the room.
* @param eventId The event to focus on, if any.
*/
suspend fun mediaTimeline(eventId: EventId?): Result<Timeline>
suspend fun createTimeline(
focusedOnEventId: EventId? = null,
onlyPinnedEvents: Boolean = false,
onlyMedia: Boolean = false,
): Result<Timeline>
fun destroy()

View file

@ -214,80 +214,72 @@ class RustMatrixRoom(
override suspend fun subscribeToSync() = roomSyncSubscriber.subscribe(roomId)
override suspend fun timelineFocusedOnEvent(eventId: EventId): Result<Timeline> = withContext(roomDispatcher) {
runCatching {
innerRoom.timelineWithConfiguration(
configuration = TimelineConfiguration(
focus = TimelineFocus.Event(
eventId = eventId.value,
numContextEvents = 50u,
),
allowedMessageTypes = AllowedMessageTypes.All,
internalIdPrefix = "focus_$eventId",
dateDividerMode = DateDividerMode.DAILY,
)
).let { inner ->
createTimeline(inner, mode = Timeline.Mode.FOCUSED_ON_EVENT)
}
}.mapFailure {
it.toFocusEventException()
}.onFailure {
if (it is CancellationException) {
throw it
}
}
}
override suspend fun pinnedEventsTimeline(): Result<Timeline> = withContext(roomDispatcher) {
runCatching {
innerRoom.timelineWithConfiguration(
configuration = TimelineConfiguration(
focus = TimelineFocus.PinnedEvents(
maxEventsToLoad = 100u,
maxConcurrentRequests = 10u,
),
allowedMessageTypes = AllowedMessageTypes.All,
internalIdPrefix = "pinned_events",
dateDividerMode = DateDividerMode.DAILY,
)
).let { inner ->
createTimeline(inner, mode = Timeline.Mode.PINNED_EVENTS)
}
}.onFailure {
if (it is CancellationException) {
throw it
}
}
}
override suspend fun mediaTimeline(
eventId: EventId?,
override suspend fun createTimeline(
focusedOnEventId: EventId?,
onlyPinnedEvents: Boolean,
onlyMedia: Boolean,
): Result<Timeline> = withContext(roomDispatcher) {
val focus = if (eventId != null) {
val focus = if (onlyPinnedEvents) {
TimelineFocus.PinnedEvents(
maxEventsToLoad = 100u,
maxConcurrentRequests = 10u,
)
} else if (focusedOnEventId != null) {
TimelineFocus.Event(
eventId = eventId.value,
eventId = focusedOnEventId.value,
numContextEvents = 50u,
)
} else {
TimelineFocus.Live
}
val allowedMessageTypes = if (onlyMedia) {
AllowedMessageTypes.Only(
types = listOf(
RoomMessageEventMessageType.FILE,
RoomMessageEventMessageType.IMAGE,
RoomMessageEventMessageType.VIDEO,
RoomMessageEventMessageType.AUDIO,
)
)
} else {
AllowedMessageTypes.All
}
val internalIdPrefix = if (onlyPinnedEvents) {
"pinned_events"
} else if (focusedOnEventId != null) {
"focus_$focusedOnEventId"
} else if (onlyMedia) {
"MediaGallery_"
} else {
"live"
}
val dateDividerMode = if (onlyMedia) {
DateDividerMode.MONTHLY
} else {
DateDividerMode.DAILY
}
val mode = when {
onlyPinnedEvents -> Timeline.Mode.PINNED_EVENTS
focusedOnEventId != null -> Timeline.Mode.FOCUSED_ON_EVENT
onlyMedia -> Timeline.Mode.MEDIA
else -> Timeline.Mode.LIVE
}
runCatching {
innerRoom.timelineWithConfiguration(
configuration = TimelineConfiguration(
focus = focus,
allowedMessageTypes = AllowedMessageTypes.Only(
types = listOf(
RoomMessageEventMessageType.FILE,
RoomMessageEventMessageType.IMAGE,
RoomMessageEventMessageType.VIDEO,
RoomMessageEventMessageType.AUDIO,
)
),
internalIdPrefix = "MediaGallery_",
dateDividerMode = DateDividerMode.MONTHLY,
allowedMessageTypes = allowedMessageTypes,
internalIdPrefix = internalIdPrefix,
dateDividerMode = dateDividerMode,
)
).let { inner ->
createTimeline(inner, mode = if (eventId != null) Timeline.Mode.FOCUSED_ON_EVENT else Timeline.Mode.MEDIA)
createTimeline(inner, mode = mode)
}
}.mapFailure {
if (focusedOnEventId != null) {
it.toFocusEventException()
} else {
it
}
}.onFailure {
if (it is CancellationException) {

View file

@ -138,9 +138,7 @@ class FakeMatrixRoom(
private val leaveRoomLambda: () -> Result<Unit> = { lambdaError() },
private val updateMembersResult: () -> Unit = { lambdaError() },
private val getMembersResult: (Int) -> Result<List<RoomMember>> = { lambdaError() },
private val timelineFocusedOnEventResult: (EventId) -> Result<Timeline> = { lambdaError() },
private val pinnedEventsTimelineResult: () -> Result<Timeline> = { lambdaError() },
private val mediaTimelineResult: (EventId?) -> Result<Timeline> = { lambdaError() },
private val createTimelineResult: (EventId?, Boolean, Boolean) -> Result<Timeline> = { _, _, _ -> lambdaError() },
private val setSendQueueEnabledLambda: (Boolean) -> Unit = { _: Boolean -> },
private val saveComposerDraftLambda: (ComposerDraft) -> Result<Unit> = { _: ComposerDraft -> Result.success(Unit) },
private val loadComposerDraftLambda: () -> Result<ComposerDraft?> = { Result.success<ComposerDraft?>(null) },
@ -220,16 +218,12 @@ class FakeMatrixRoom(
_syncUpdateFlow.tryEmit(_syncUpdateFlow.value + 1)
}
override suspend fun timelineFocusedOnEvent(eventId: EventId): Result<Timeline> = simulateLongTask {
timelineFocusedOnEventResult(eventId)
}
override suspend fun pinnedEventsTimeline(): Result<Timeline> = simulateLongTask {
pinnedEventsTimelineResult()
}
override suspend fun mediaTimeline(eventId: EventId?): Result<Timeline> = simulateLongTask {
mediaTimelineResult(eventId)
override suspend fun createTimeline(
focusedOnEventId: EventId?,
onlyPinnedEvents: Boolean,
onlyMedia: Boolean,
): Result<Timeline> = simulateLongTask {
createTimelineResult(focusedOnEventId, onlyPinnedEvents, onlyMedia)
}
override suspend fun subscribeToSync() {