mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-09-27 10:57:04 +02:00
add fields to pollNoteEvent,
add clearState fun to pollViewModel, add refined PollNoteEvent to Event,
This commit is contained in:
@@ -178,6 +178,7 @@ open class Event(
|
|||||||
LnZapRequestEvent.kind -> LnZapRequestEvent(id, pubKey, createdAt, tags, content, sig)
|
LnZapRequestEvent.kind -> LnZapRequestEvent(id, pubKey, createdAt, tags, content, sig)
|
||||||
LongTextNoteEvent.kind -> LongTextNoteEvent(id, pubKey, createdAt, tags, content, sig)
|
LongTextNoteEvent.kind -> LongTextNoteEvent(id, pubKey, createdAt, tags, content, sig)
|
||||||
MetadataEvent.kind -> MetadataEvent(id, pubKey, createdAt, tags, content, sig)
|
MetadataEvent.kind -> MetadataEvent(id, pubKey, createdAt, tags, content, sig)
|
||||||
|
PollNoteEvent.kind -> PollNoteEvent(id, pubKey, createdAt, tags, content, sig)
|
||||||
PrivateDmEvent.kind -> PrivateDmEvent(id, pubKey, createdAt, tags, content, sig)
|
PrivateDmEvent.kind -> PrivateDmEvent(id, pubKey, createdAt, tags, content, sig)
|
||||||
ReactionEvent.kind -> ReactionEvent(id, pubKey, createdAt, tags, content, sig)
|
ReactionEvent.kind -> ReactionEvent(id, pubKey, createdAt, tags, content, sig)
|
||||||
RecommendRelayEvent.kind -> RecommendRelayEvent(id, pubKey, createdAt, tags, content, sig, lenient)
|
RecommendRelayEvent.kind -> RecommendRelayEvent(id, pubKey, createdAt, tags, content, sig, lenient)
|
||||||
|
@@ -5,11 +5,18 @@ import com.vitorpamplona.amethyst.model.toHexKey
|
|||||||
import nostr.postr.Utils
|
import nostr.postr.Utils
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
|
const val POLL_OPTIONS = "poll_options"
|
||||||
|
const val VALUE_MAXIMUM = "value_maximum"
|
||||||
|
const val VALUE_MINIMUM = "value_minimum"
|
||||||
|
const val CONSENSUS_THRESHOLD = "consensus_threshold"
|
||||||
|
const val CLOSED_AT = "closed_at"
|
||||||
|
|
||||||
class PollNoteEvent(
|
class PollNoteEvent(
|
||||||
id: HexKey,
|
id: HexKey,
|
||||||
pubKey: HexKey,
|
pubKey: HexKey,
|
||||||
createdAt: Long,
|
createdAt: Long,
|
||||||
tags: List<List<String>>,
|
tags: List<List<String>>,
|
||||||
|
// ots: , TODO implement OTS: https://github.com/opentimestamps/java-opentimestamps
|
||||||
content: String,
|
content: String,
|
||||||
sig: HexKey
|
sig: HexKey
|
||||||
) : Event(id, pubKey, createdAt, kind, tags, content, sig) {
|
) : Event(id, pubKey, createdAt, kind, tags, content, sig) {
|
||||||
@@ -23,15 +30,28 @@ class PollNoteEvent(
|
|||||||
|
|
||||||
fun replyTos() = tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) }
|
fun replyTos() = tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) }
|
||||||
|
|
||||||
|
fun pollOptions() = tags.filter { it.firstOrNull() == POLL_OPTIONS }.mapNotNull { it.getOrNull(1) }
|
||||||
|
fun valueMaximum() = tags.filter { it.firstOrNull() == VALUE_MAXIMUM }.mapNotNull { it.getOrNull(1) }
|
||||||
|
fun valueMinimum() = tags.filter { it.firstOrNull() == VALUE_MINIMUM }.mapNotNull { it.getOrNull(1) }
|
||||||
|
fun consensusThreshold() = tags.filter { it.firstOrNull() == CONSENSUS_THRESHOLD }.mapNotNull { it.getOrNull(1) }
|
||||||
|
fun closedAt() = tags.filter { it.firstOrNull() == CLOSED_AT }.mapNotNull { it.getOrNull(1) }
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val kind = 6969
|
const val kind = 6969
|
||||||
|
|
||||||
fun create(msg: String,
|
fun create(
|
||||||
|
msg: String,
|
||||||
replyTos: List<String>?,
|
replyTos: List<String>?,
|
||||||
mentions: List<String>?,
|
mentions: List<String>?,
|
||||||
addresses: List<ATag>?,
|
addresses: List<ATag>?,
|
||||||
privateKey: ByteArray,
|
privateKey: ByteArray,
|
||||||
createdAt: Long = Date().time / 1000): PollNoteEvent {
|
createdAt: Long = Date().time / 1000,
|
||||||
|
pollOptions: List<Map<Int, String>>,
|
||||||
|
valueMaximum: Int?,
|
||||||
|
valueMinimum: Int?,
|
||||||
|
consensusThreshold: Int?,
|
||||||
|
closedAt: Int?
|
||||||
|
): PollNoteEvent {
|
||||||
val pubKey = Utils.pubkeyCreate(privateKey).toHexKey()
|
val pubKey = Utils.pubkeyCreate(privateKey).toHexKey()
|
||||||
val tags = mutableListOf<List<String>>()
|
val tags = mutableListOf<List<String>>()
|
||||||
replyTos?.forEach {
|
replyTos?.forEach {
|
||||||
@@ -43,6 +63,13 @@ class PollNoteEvent(
|
|||||||
addresses?.forEach {
|
addresses?.forEach {
|
||||||
tags.add(listOf("a", it.toTag()))
|
tags.add(listOf("a", it.toTag()))
|
||||||
}
|
}
|
||||||
|
pollOptions.forEach {
|
||||||
|
tags.add(listOf(POLL_OPTIONS, it.toString()))
|
||||||
|
}
|
||||||
|
tags.add(listOf(VALUE_MAXIMUM, valueMaximum.toString()))
|
||||||
|
tags.add(listOf(VALUE_MINIMUM, valueMinimum.toString()))
|
||||||
|
tags.add(listOf(CONSENSUS_THRESHOLD, consensusThreshold.toString()))
|
||||||
|
tags.add(listOf(CLOSED_AT, closedAt.toString()))
|
||||||
val id = generateId(pubKey, createdAt, kind, tags, msg)
|
val id = generateId(pubKey, createdAt, kind, tags, msg)
|
||||||
val sig = Utils.sign(id, privateKey)
|
val sig = Utils.sign(id, privateKey)
|
||||||
return PollNoteEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey())
|
return PollNoteEvent(id.toHexKey(), pubKey, createdAt, tags, msg, sig.toHexKey())
|
||||||
|
@@ -39,11 +39,6 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
|
|||||||
|
|
||||||
val scrollState = rememberScrollState()
|
val scrollState = rememberScrollState()
|
||||||
|
|
||||||
// if no recipients, add user's pubkey
|
|
||||||
if (pollViewModel.zapRecipients.isEmpty()) {
|
|
||||||
pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex)
|
|
||||||
}
|
|
||||||
|
|
||||||
LaunchedEffect(Unit) {
|
LaunchedEffect(Unit) {
|
||||||
pollViewModel.load(account, baseReplyTo, quote)
|
pollViewModel.load(account, baseReplyTo, quote)
|
||||||
delay(100)
|
delay(100)
|
||||||
@@ -114,7 +109,7 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
|
|||||||
}
|
}
|
||||||
|
|
||||||
Text(stringResource(R.string.poll_heading_required))
|
Text(stringResource(R.string.poll_heading_required))
|
||||||
PollRecipientsField(pollViewModel)
|
PollRecipientsField(pollViewModel, account)
|
||||||
PollPrimaryDescription(pollViewModel)
|
PollPrimaryDescription(pollViewModel)
|
||||||
pollViewModel.pollOptions.forEachIndexed { index, element ->
|
pollViewModel.pollOptions.forEachIndexed { index, element ->
|
||||||
PollOption(pollViewModel, index)
|
PollOption(pollViewModel, index)
|
||||||
|
@@ -39,14 +39,13 @@ class NewPollViewModel : NewPostViewModel() {
|
|||||||
override fun sendPost() {
|
override fun sendPost() {
|
||||||
super.sendPost()
|
super.sendPost()
|
||||||
|
|
||||||
// delete existing pollOptions
|
clearStates()
|
||||||
pollOptions = mutableStateListOf("", "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cancel() {
|
override fun cancel() {
|
||||||
super.cancel()
|
super.cancel()
|
||||||
|
|
||||||
pollOptions = mutableStateListOf("", "")
|
clearStates()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun findUrlInMessage(): String? {
|
override fun findUrlInMessage(): String? {
|
||||||
@@ -64,4 +63,14 @@ class NewPollViewModel : NewPostViewModel() {
|
|||||||
override fun autocompleteWithUser(item: User) {
|
override fun autocompleteWithUser(item: User) {
|
||||||
super.autocompleteWithUser(item)
|
super.autocompleteWithUser(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun clearStates() {
|
||||||
|
// clear states
|
||||||
|
zapRecipients = mutableStateListOf<HexKey>()
|
||||||
|
pollOptions = mutableStateListOf("", "")
|
||||||
|
zapMax = null
|
||||||
|
zapMin = null
|
||||||
|
consensus = null
|
||||||
|
closedAfter = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -8,10 +8,15 @@ import androidx.compose.runtime.Composable
|
|||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.res.stringResource
|
import androidx.compose.ui.res.stringResource
|
||||||
import com.vitorpamplona.amethyst.R
|
import com.vitorpamplona.amethyst.R
|
||||||
|
import com.vitorpamplona.amethyst.model.Account
|
||||||
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
|
import com.vitorpamplona.amethyst.ui.actions.NewPollViewModel
|
||||||
|
|
||||||
@Composable
|
@Composable
|
||||||
fun PollRecipientsField(pollViewModel: NewPollViewModel) {
|
fun PollRecipientsField(pollViewModel: NewPollViewModel, account: Account) {
|
||||||
|
// if no recipients, add user's pubkey
|
||||||
|
if (pollViewModel.zapRecipients.isEmpty()) {
|
||||||
|
pollViewModel.zapRecipients.add(account.userProfile().pubkeyHex)
|
||||||
|
}
|
||||||
|
|
||||||
OutlinedTextField(
|
OutlinedTextField(
|
||||||
modifier = Modifier
|
modifier = Modifier
|
||||||
|
Reference in New Issue
Block a user