Voice Broadcast - Get voice messages events related to a given VB

This commit is contained in:
Florian Renaud 2022-10-17 23:14:42 +02:00
parent 215128c213
commit 16c27ba174
4 changed files with 29 additions and 2 deletions

View File

@ -55,4 +55,9 @@ interface TimelineService {
* Returns a snapshot list of TimelineEvent with EventType.MESSAGE and MessageType.MSGTYPE_IMAGE or MessageType.MSGTYPE_VIDEO.
*/
fun getAttachmentMessages(): List<TimelineEvent>
/**
* Returns a snapshot list of TimelineEvent with a content relation of the given type to the given eventId.
*/
fun getTimelineEventsRelatedTo(relationType: String, eventId: String): List<TimelineEvent>
}

View File

@ -96,4 +96,8 @@ internal class DefaultTimelineService @AssistedInject constructor(
override fun getAttachmentMessages(): List<TimelineEvent> {
return timelineEventDataSource.getAttachmentMessages(roomId)
}
override fun getTimelineEventsRelatedTo(relationType: String, eventId: String): List<TimelineEvent> {
return timelineEventDataSource.getTimelineEventsRelatedTo(roomId, relationType, eventId)
}
}

View File

@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.session.room.timeline
import androidx.lifecycle.LiveData
import com.zhuinden.monarchy.Monarchy
import io.realm.Sort
import org.matrix.android.sdk.api.session.events.model.getRelationContent
import org.matrix.android.sdk.api.session.events.model.isImageMessage
import org.matrix.android.sdk.api.session.events.model.isVideoMessage
import org.matrix.android.sdk.api.session.room.timeline.TimelineEvent
@ -63,4 +64,18 @@ internal class TimelineEventDataSource @Inject constructor(
.orEmpty()
}
}
fun getTimelineEventsRelatedTo(roomId: String, eventType: String, eventId: String): List<TimelineEvent> {
// TODO Remove this trick and call relations API
// see https://spec.matrix.org/latest/client-server-api/#get_matrixclientv1roomsroomidrelationseventidreltypeeventtype
return realmSessionProvider.withRealm { realm ->
TimelineEventEntity.whereRoomId(realm, roomId)
.sort(TimelineEventEntityFields.ROOT.ORIGIN_SERVER_TS, Sort.ASCENDING)
.distinct(TimelineEventEntityFields.EVENT_ID)
.findAll()
.mapNotNull {
timelineEventMapper.map(it).takeIf { it.root.getRelationContent()?.takeIf { it.type == eventType && it.eventId == eventId } != null }
}
}
}
}

View File

@ -26,11 +26,13 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.RelationType
import org.matrix.android.sdk.api.session.events.model.getRelationContent
import org.matrix.android.sdk.api.session.getRoom
import org.matrix.android.sdk.api.session.room.Room
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioContent
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioEvent
import org.matrix.android.sdk.api.session.room.model.message.asMessageAudioEvent
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton
@ -80,9 +82,10 @@ class VoiceBroadcastPlayer @Inject constructor(
currentPlayingIndex = -1
}
@Suppress("UNUSED_PARAMETER")
private fun updatePlaylist(room: Room, eventId: String) {
// TODO get the list of voice messages
val timelineEvents = room.timelineService().getTimelineEventsRelatedTo(RelationType.REFERENCE, eventId)
val audioEvents = timelineEvents.mapNotNull { it.root.asMessageAudioEvent() }
playlist = audioEvents.sortedBy { it.root.originServerTs }
}
private fun startPlayback() {