Refactor editing polls.

This commit is contained in:
Onuray Sahin 2022-05-12 12:22:27 +03:00
parent e65d3ee993
commit 704e86d843
4 changed files with 130 additions and 16 deletions

View File

@ -90,6 +90,8 @@ import org.matrix.android.sdk.internal.session.permalinks.DefaultPermalinkServic
import org.matrix.android.sdk.internal.session.room.EventRelationsAggregationProcessor
import org.matrix.android.sdk.internal.session.room.aggregation.livelocation.DefaultLiveLocationAggregationProcessor
import org.matrix.android.sdk.internal.session.room.aggregation.livelocation.LiveLocationAggregationProcessor
import org.matrix.android.sdk.internal.session.room.aggregation.poll.DefaultPollAggregationProcessor
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollAggregationProcessor
import org.matrix.android.sdk.internal.session.room.create.RoomCreateEventProcessor
import org.matrix.android.sdk.internal.session.room.prune.RedactionEventProcessor
import org.matrix.android.sdk.internal.session.room.send.queue.EventSenderProcessor
@ -395,4 +397,7 @@ internal abstract class SessionModule {
@Binds
abstract fun bindLiveLocationAggregationProcessor(processor: DefaultLiveLocationAggregationProcessor): LiveLocationAggregationProcessor
@Binds
abstract fun bindPollAggregationProcessor(processor: DefaultPollAggregationProcessor): PollAggregationProcessor
}

View File

@ -68,6 +68,7 @@ import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.UserId
import org.matrix.android.sdk.internal.session.EventInsertLiveProcessor
import org.matrix.android.sdk.internal.session.room.aggregation.livelocation.LiveLocationAggregationProcessor
import org.matrix.android.sdk.internal.session.room.aggregation.poll.PollAggregationProcessor
import org.matrix.android.sdk.internal.session.room.state.StateEventDataSource
import org.matrix.android.sdk.internal.util.time.Clock
import timber.log.Timber
@ -79,6 +80,7 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
@SessionId private val sessionId: String,
private val sessionManager: SessionManager,
private val liveLocationAggregationProcessor: LiveLocationAggregationProcessor,
private val pollAggregationProcessor: PollAggregationProcessor,
private val clock: Clock,
) : EventInsertLiveProcessor {
@ -317,22 +319,6 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
return
}
ContentMapper
.map(eventAnnotationsSummaryEntity.pollResponseSummary?.aggregatedContent)
?.toModel<PollSummaryContent>()
?.let { existingPollSummaryContent ->
eventAnnotationsSummaryEntity.pollResponseSummary?.aggregatedContent = ContentMapper.map(
PollSummaryContent(
myVote = existingPollSummaryContent.myVote,
votes = emptyList(),
votesSummary = emptyMap(),
totalVotes = 0,
winnerVoteCount = 0,
)
.toContent()
)
}
val txId = event.unsignedData?.transactionId
// is it a remote echo?
if (!isLocalEcho && existingSummary.editions.any { it.eventId == txId }) {
@ -362,6 +348,10 @@ internal class EventRelationsAggregationProcessor @Inject constructor(
}
}
if (event.getClearType() in EventType.POLL_START) {
pollAggregationProcessor.handlePollStartEvent(realm, event)
}
if (!isLocalEcho) {
val replaceEvent = TimelineEventEntity
.where(realm, roomId, eventId)

View File

@ -0,0 +1,69 @@
/*
* Copyright 2022 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.
*/
package org.matrix.android.sdk.internal.session.room.aggregation.poll
import io.realm.Realm
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.RelationType
import org.matrix.android.sdk.api.session.events.model.toContent
import org.matrix.android.sdk.api.session.events.model.toModel
import org.matrix.android.sdk.api.session.room.model.PollSummaryContent
import org.matrix.android.sdk.api.session.room.model.message.MessagePollContent
import org.matrix.android.sdk.internal.database.mapper.ContentMapper
import org.matrix.android.sdk.internal.database.model.EventAnnotationsSummaryEntity
import org.matrix.android.sdk.internal.database.query.getOrCreate
import javax.inject.Inject
class DefaultPollAggregationProcessor @Inject constructor() : PollAggregationProcessor {
override fun handlePollStartEvent(realm: Realm, event: Event): Boolean {
val content = event.getClearContent()?.toModel<MessagePollContent>()
if (content?.relatesTo?.type != RelationType.REPLACE) {
return false
}
val roomId = event.roomId ?: return false
val targetEventId = content.relatesTo.eventId ?: return false
EventAnnotationsSummaryEntity.getOrCreate(realm, roomId, targetEventId).let { eventAnnotationsSummaryEntity ->
ContentMapper
.map(eventAnnotationsSummaryEntity.pollResponseSummary?.aggregatedContent)
?.toModel<PollSummaryContent>()
?.let { existingPollSummaryContent ->
eventAnnotationsSummaryEntity.pollResponseSummary?.aggregatedContent = ContentMapper.map(
PollSummaryContent(
myVote = existingPollSummaryContent.myVote,
votes = emptyList(),
votesSummary = emptyMap(),
totalVotes = 0,
winnerVoteCount = 0,
)
.toContent()
)
}
}
return true
}
override fun handlePollResponseEvent(realm: Realm, event: Event): Boolean {
TODO("Not yet implemented")
}
override fun handlePollEndEvent(realm: Realm, event: Event): Boolean {
TODO("Not yet implemented")
}
}

View File

@ -0,0 +1,50 @@
/*
* Copyright 2022 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.
*/
package org.matrix.android.sdk.internal.session.room.aggregation.poll
import io.realm.Realm
import org.matrix.android.sdk.api.session.events.model.Event
interface PollAggregationProcessor {
/**
* Poll start events don't need to be processed by the aggregator.
* This function will only handle if the poll is edited and will update the poll summary entity.
* Returns true if the event is aggregated.
*/
fun handlePollStartEvent(
realm: Realm,
event: Event
): Boolean
/**
* Aggregates poll response event after many conditional checks like if the poll is ended, if the user is changing his/her vote etc.
* Returns true if the event is aggregated.
*/
fun handlePollResponseEvent(
realm: Realm,
event: Event
): Boolean
/**
* Updates poll summary entity and mark it is ended after many conditional checks like if the poll is already ended etc.
* Returns true if the event is aggregated.
*/
fun handlePollEndEvent(
realm: Realm,
event: Event
): Boolean
}