Add command to send poll

This commit is contained in:
Valere 2020-01-05 12:30:44 +01:00 committed by Benoit Marty
parent 6001ac60ab
commit c4ea2507f8
9 changed files with 61 additions and 4 deletions

View File

@ -30,7 +30,7 @@ enum class OptionsType(val value: String) {
*/
@JsonClass(generateAdapter = true)
data class MessageOptionsContent(
@Json(name = "msgtype") override val type: String,
@Json(name = "msgtype") override val type: String = MessageType.MSGTYPE_OPTIONS,
@Json(name = "type") val optionType: String? = null,
@Json(name = "body") override val body: String,
@Json(name = "label") val label: String?,

View File

@ -62,12 +62,19 @@ interface SendService {
fun sendMedias(attachments: List<ContentAttachmentData>): Cancelable
/**
* Method to send a list of media asynchronously.
* @param attachments the list of media to send
* 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)
* @return a [Cancelable]
*/
fun sendPollReply(pollEventId: String, optionIndex: Int, optionValue: String): Cancelable
/**
* @param options list of (label, value)
*/
fun sendPoll(question: String, options: List<Pair<String, String>>)
/**
* Redacts (delete) the given event.
* @param event The event to redact

View File

@ -91,6 +91,13 @@ internal class DefaultSendService @AssistedInject constructor(
return sendEvent(event)
}
override fun sendPoll(question: String, options: List<Pair<String, String>>) {
localEchoEventFactory.createPollEvent(roomId, question, options).also {
saveLocalEcho(it)
sendEvent(it)
}
}
private fun sendEvent(event: Event): Cancelable {
// Encrypted room handling
return if (cryptoService.isRoomEncrypted(roomId)) {

View File

@ -36,11 +36,14 @@ import im.vector.matrix.android.api.session.room.model.message.MessageContent
import im.vector.matrix.android.api.session.room.model.message.MessageFileContent
import im.vector.matrix.android.api.session.room.model.message.MessageFormat
import im.vector.matrix.android.api.session.room.model.message.MessageImageContent
import im.vector.matrix.android.api.session.room.model.message.MessageOptionsContent
import im.vector.matrix.android.api.session.room.model.message.MessagePollResponseContent
import im.vector.matrix.android.api.session.room.model.message.MessageTextContent
import im.vector.matrix.android.api.session.room.model.message.MessageType
import im.vector.matrix.android.api.session.room.model.message.MessageVerificationRequestContent
import im.vector.matrix.android.api.session.room.model.message.MessageVideoContent
import im.vector.matrix.android.api.session.room.model.message.OptionItems
import im.vector.matrix.android.api.session.room.model.message.OptionsType
import im.vector.matrix.android.api.session.room.model.message.ThumbnailInfo
import im.vector.matrix.android.api.session.room.model.message.VideoInfo
import im.vector.matrix.android.api.session.room.model.message.isReply
@ -148,6 +151,30 @@ internal class LocalEchoEventFactory @Inject constructor(
))
}
fun createPollEvent(roomId: String,
question: String,
options: List<Pair<String, String>>): Event {
val compatLabel = buildString {
append(question)
append("\n")
options.forEach {
append("\n")
append(it.second)
}
}
return createEvent(
roomId,
MessageOptionsContent(
body = compatLabel,
label = question,
optionType = OptionsType.POLL.value,
options = options.map {
OptionItems(it.first, it.second)
}
)
)
}
fun createReplaceTextOfReply(roomId: String,
eventReplaced: TimelineEvent,
originalEvent: TimelineEvent,

View File

@ -42,6 +42,7 @@ enum class Command(val command: String, val parameters: String, @StringRes val d
CLEAR_SCALAR_TOKEN("/clear_scalar_token", "", R.string.command_description_clear_scalar_token),
SPOILER("/spoiler", "<message>", R.string.command_description_spoiler),
SHRUG("/shrug", "<message>", R.string.command_description_shrug),
SPOILER("/spoiler", "<message>", R.string.command_description_spoiler),
// TODO temporary command
VERIFY_USER("/verify", "<user-id>", R.string.command_description_verify);

View File

@ -251,7 +251,6 @@ object CommandParser {
}
Command.SPOILER.command -> {
val message = textMessage.substring(Command.SPOILER.command.length).trim()
ParsedCommand.SendSpoiler(message)
}
Command.SHRUG.command -> {
@ -265,6 +264,15 @@ object CommandParser {
ParsedCommand.VerifyUser(message)
}
Command.POLL.command -> {
val rawCommand = textMessage.substring(Command.POLL.command.length).trim()
val splited = rawCommand.split("|").map { it.trim() }
if (splited.size > 2) {
ParsedCommand.SendPoll(splited[0], splited.subList(1, splited.size))
} else {
ParsedCommand.ErrorUnknownSlashCommand(slashCommand)
}
}
else -> {
// Unknown command
ParsedCommand.ErrorUnknownSlashCommand(slashCommand)

View File

@ -50,4 +50,5 @@ sealed class ParsedCommand {
class SendSpoiler(val message: String) : ParsedCommand()
class SendShrug(val message: CharSequence) : ParsedCommand()
class VerifyUser(val userId: String) : ParsedCommand()
class SendPoll(val question: String, val options: List<String>) : ParsedCommand()
}

View File

@ -423,6 +423,11 @@ class RoomDetailViewModel @AssistedInject constructor(@Assisted initialState: Ro
_viewEvents.post(RoomDetailViewEvents.SlashCommandHandled())
popDraft()
}
is ParsedCommand.SendPoll -> {
room.sendPoll(slashCommandResult.question, slashCommandResult.options.mapIndexed { index, s -> s to "$index. $s" })
_sendMessageResultLiveData.postLiveEvent(SendMessageResult.SlashCommandHandled())
popDraft()
}
is ParsedCommand.ChangeTopic -> {
handleChangeTopicSlashCommand(slashCommandResult)
popDraft()

View File

@ -15,6 +15,7 @@
<item quantity="other">%d votes - Final results</item>
</plurals>
<string name="poll_item_selected_aria">Selected Option</string>
<string name="command_description_poll">Creates a simple poll</string>
<!-- END Strings added by Valere -->