Allow sending vote and ending poll.

This commit is contained in:
Onuray Sahin 2021-12-03 11:21:21 +03:00
parent ebc131f492
commit 7c269309c2
3 changed files with 56 additions and 17 deletions

View File

@ -91,11 +91,17 @@ interface SendService {
/**
* Method to send a poll response.
* @param pollEventId the poll currently replied to
* @param optionIndex The reply index
* @param optionValue The option value (for compatibility)
* @param optionKey The option key
* @return a [Cancelable]
*/
fun sendOptionsReply(pollEventId: String, optionIndex: Int, optionValue: String): Cancelable
fun registerVoteToPoll(pollEventId: String, optionKey: String): Cancelable
/**
* End a poll in the room.
* @param pollEventId event id of the poll
* @return a [Cancelable]
*/
fun endPoll(pollEventId: String): Cancelable
/**
* Redact (delete) the given event.

View File

@ -103,8 +103,14 @@ internal class DefaultSendService @AssistedInject constructor(
.let { sendEvent(it) }
}
override fun sendOptionsReply(pollEventId: String, optionIndex: Int, optionValue: String): Cancelable {
return localEchoEventFactory.createOptionsReplyEvent(roomId, pollEventId, optionIndex, optionValue)
override fun registerVoteToPoll(pollEventId: String, optionKey: String): Cancelable {
return localEchoEventFactory.createPollReplyEvent(roomId, pollEventId, optionKey)
.also { createLocalEcho(it) }
.let { sendEvent(it) }
}
override fun endPoll(pollEventId: String): Cancelable {
return localEchoEventFactory.createEndPollEvent(roomId, pollEventId)
.also { createLocalEcho(it) }
.let { sendEvent(it) }
}

View File

@ -35,6 +35,7 @@ import org.matrix.android.sdk.api.session.room.model.message.ImageInfo
import org.matrix.android.sdk.api.session.room.model.message.MessageAudioContent
import org.matrix.android.sdk.api.session.room.model.message.MessageContent
import org.matrix.android.sdk.api.session.room.model.message.MessageContentWithFormattedBody
import org.matrix.android.sdk.api.session.room.model.message.MessageEndPollContent
import org.matrix.android.sdk.api.session.room.model.message.MessageFileContent
import org.matrix.android.sdk.api.session.room.model.message.MessageFormat
import org.matrix.android.sdk.api.session.room.model.message.MessageImageContent
@ -46,6 +47,7 @@ import org.matrix.android.sdk.api.session.room.model.message.MessageVideoContent
import org.matrix.android.sdk.api.session.room.model.message.PollAnswer
import org.matrix.android.sdk.api.session.room.model.message.PollCreationInfo
import org.matrix.android.sdk.api.session.room.model.message.PollQuestion
import org.matrix.android.sdk.api.session.room.model.message.PollResponse
import org.matrix.android.sdk.api.session.room.model.message.ThumbnailInfo
import org.matrix.android.sdk.api.session.room.model.message.VideoInfo
import org.matrix.android.sdk.api.session.room.model.relation.ReactionContent
@ -122,19 +124,28 @@ internal class LocalEchoEventFactory @Inject constructor(
))
}
fun createOptionsReplyEvent(roomId: String,
pollEventId: String,
optionIndex: Int,
optionLabel: String): Event {
return createMessageEvent(roomId,
MessagePollResponseContent(
body = optionLabel,
relatesTo = RelationDefaultContent(
type = RelationType.RESPONSE,
option = optionIndex,
eventId = pollEventId)
fun createPollReplyEvent(roomId: String,
pollEventId: String,
optionLabel: String): Event {
val content = MessagePollResponseContent(
body = optionLabel,
relatesTo = RelationDefaultContent(
type = RelationType.REFERENCE,
eventId = pollEventId),
response = PollResponse(
answers = listOf(optionLabel)
)
))
)
val localId = LocalEcho.createLocalEchoId()
return Event(
roomId = roomId,
originServerTs = dummyOriginServerTs(),
senderId = userId,
eventId = localId,
type = EventType.POLL_RESPONSE,
content = content.toContent(),
unsignedData = UnsignedData(age = null, transactionId = localId))
}
fun createPollEvent(roomId: String,
@ -164,6 +175,22 @@ internal class LocalEchoEventFactory @Inject constructor(
unsignedData = UnsignedData(age = null, transactionId = localId))
}
fun createEndPollEvent(roomId: String,
eventId: String): Event {
val content = MessageEndPollContent(
eventId = eventId
)
val localId = LocalEcho.createLocalEchoId()
return Event(
roomId = roomId,
originServerTs = dummyOriginServerTs(),
senderId = userId,
eventId = localId,
type = EventType.POLL_END,
content = content.toContent(),
unsignedData = UnsignedData(age = null, transactionId = localId))
}
fun createReplaceTextOfReply(roomId: String,
eventReplaced: TimelineEvent,
originalEvent: TimelineEvent,