mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-10-04 21:57:00 +02:00
create pollSend funs,
This commit is contained in:
@@ -3,20 +3,7 @@ package com.vitorpamplona.amethyst.model
|
|||||||
import android.content.res.Resources
|
import android.content.res.Resources
|
||||||
import androidx.core.os.ConfigurationCompat
|
import androidx.core.os.ConfigurationCompat
|
||||||
import androidx.lifecycle.LiveData
|
import androidx.lifecycle.LiveData
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelCreateEvent
|
import com.vitorpamplona.amethyst.service.model.*
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelMessageEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.Contact
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ContactListEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.DeletionEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.IdentityClaim
|
|
||||||
import com.vitorpamplona.amethyst.service.model.LnZapRequestEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.MetadataEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.PrivateDmEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ReactionEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.ReportEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.RepostEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.model.TextNoteEvent
|
|
||||||
import com.vitorpamplona.amethyst.service.relays.Client
|
import com.vitorpamplona.amethyst.service.relays.Client
|
||||||
import com.vitorpamplona.amethyst.service.relays.Constants
|
import com.vitorpamplona.amethyst.service.relays.Constants
|
||||||
import com.vitorpamplona.amethyst.service.relays.FeedType
|
import com.vitorpamplona.amethyst.service.relays.FeedType
|
||||||
@@ -300,6 +287,39 @@ class Account(
|
|||||||
LocalCache.consume(signedEvent)
|
LocalCache.consume(signedEvent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun sendPoll(
|
||||||
|
message: String,
|
||||||
|
replyTo: List<Note>?,
|
||||||
|
mentions: List<User>?,
|
||||||
|
pollOptions: List<Map<Int, String>>,
|
||||||
|
valueMaximum: Int?,
|
||||||
|
valueMinimum: Int?,
|
||||||
|
consensusThreshold: Int?,
|
||||||
|
closedAt: Int?
|
||||||
|
) {
|
||||||
|
if (!isWriteable()) return
|
||||||
|
|
||||||
|
val repliesToHex = replyTo?.map { it.idHex }
|
||||||
|
val mentionsHex = mentions?.map { it.pubkeyHex }
|
||||||
|
val addresses = replyTo?.mapNotNull { it.address() }
|
||||||
|
|
||||||
|
val signedEvent = PollNoteEvent.create(
|
||||||
|
msg = message,
|
||||||
|
replyTos = repliesToHex,
|
||||||
|
mentions = mentionsHex,
|
||||||
|
addresses = addresses,
|
||||||
|
privateKey = loggedIn.privKey!!,
|
||||||
|
pollOptions = pollOptions,
|
||||||
|
valueMaximum = valueMaximum,
|
||||||
|
valueMinimum = valueMinimum,
|
||||||
|
consensusThreshold = consensusThreshold,
|
||||||
|
closedAt = closedAt
|
||||||
|
)
|
||||||
|
println("PollNoteEvent: %s".format(signedEvent.toJson()))
|
||||||
|
// Client.send(signedEvent)
|
||||||
|
// LocalCache.consume(signedEvent)
|
||||||
|
}
|
||||||
|
|
||||||
fun sendChannelMessage(message: String, toChannel: String, replyingTo: Note? = null, mentions: List<User>?) {
|
fun sendChannelMessage(message: String, toChannel: String, replyingTo: Note? = null, mentions: List<User>?) {
|
||||||
if (!isWriteable()) return
|
if (!isWriteable()) return
|
||||||
|
|
||||||
|
@@ -16,7 +16,7 @@ class PollNoteEvent(
|
|||||||
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
|
// ots: String?, 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) {
|
||||||
@@ -63,9 +63,7 @@ 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, pollOptions.toString()))
|
||||||
tags.add(listOf(POLL_OPTIONS, it.toString()))
|
|
||||||
}
|
|
||||||
tags.add(listOf(VALUE_MAXIMUM, valueMaximum.toString()))
|
tags.add(listOf(VALUE_MAXIMUM, valueMaximum.toString()))
|
||||||
tags.add(listOf(VALUE_MINIMUM, valueMinimum.toString()))
|
tags.add(listOf(VALUE_MINIMUM, valueMinimum.toString()))
|
||||||
tags.add(listOf(CONSENSUS_THRESHOLD, consensusThreshold.toString()))
|
tags.add(listOf(CONSENSUS_THRESHOLD, consensusThreshold.toString()))
|
||||||
|
@@ -84,7 +84,7 @@ fun NewPollView(onClose: () -> Unit, baseReplyTo: Note? = null, quote: Note? = n
|
|||||||
|
|
||||||
PollButton(
|
PollButton(
|
||||||
onPost = {
|
onPost = {
|
||||||
pollViewModel.sendPost()
|
pollViewModel.sendPoll()
|
||||||
onClose()
|
onClose()
|
||||||
},
|
},
|
||||||
isActive = pollViewModel.message.text.isNotBlank() &&
|
isActive = pollViewModel.message.text.isNotBlank() &&
|
||||||
|
@@ -2,19 +2,17 @@ package com.vitorpamplona.amethyst.ui.actions
|
|||||||
|
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
import androidx.compose.ui.text.input.TextFieldValue
|
import androidx.compose.ui.text.input.TextFieldValue
|
||||||
import com.vitorpamplona.amethyst.model.Account
|
import com.vitorpamplona.amethyst.model.*
|
||||||
import com.vitorpamplona.amethyst.model.HexKey
|
import com.vitorpamplona.amethyst.service.nip19.Nip19
|
||||||
import com.vitorpamplona.amethyst.model.Note
|
|
||||||
import com.vitorpamplona.amethyst.model.User
|
|
||||||
|
|
||||||
class NewPollViewModel : NewPostViewModel() {
|
class NewPollViewModel : NewPostViewModel() {
|
||||||
|
|
||||||
var zapRecipients = mutableStateListOf<HexKey>()
|
var zapRecipients = mutableStateListOf<HexKey>()
|
||||||
var pollOptions = mutableStateListOf("", "")
|
var pollOptions = mutableStateListOf("", "")
|
||||||
var zapMax: Int? = null
|
var valueMaximum: Int? = null
|
||||||
var zapMin: Int? = null
|
var valueMinimum: Int? = null
|
||||||
var consensus: Int? = null
|
var consensusThreshold: Int? = null
|
||||||
var closedAfter: Int? = null
|
var closedAt: Int? = null
|
||||||
|
|
||||||
override fun load(account: Account, replyingTo: Note?, quote: Note?) {
|
override fun load(account: Account, replyingTo: Note?, quote: Note?) {
|
||||||
super.load(account, replyingTo, quote)
|
super.load(account, replyingTo, quote)
|
||||||
@@ -36,16 +34,65 @@ class NewPollViewModel : NewPostViewModel() {
|
|||||||
return super.tagIndex(note)
|
return super.tagIndex(note)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun sendPost() {
|
fun sendPoll() {
|
||||||
super.sendPost()
|
// adds all references to mentions and reply tos
|
||||||
|
message.text.split('\n').forEach { paragraph: String ->
|
||||||
|
paragraph.split(' ').forEach { word: String ->
|
||||||
|
val results = parseDirtyWordForKey(word)
|
||||||
|
|
||||||
clearStates()
|
if (results?.key?.type == Nip19.Type.USER) {
|
||||||
|
addUserToMentions(LocalCache.getOrCreateUser(results.key.hex))
|
||||||
|
} else if (results?.key?.type == Nip19.Type.NOTE) {
|
||||||
|
addNoteToReplyTos(LocalCache.getOrCreateNote(results.key.hex))
|
||||||
|
} else if (results?.key?.type == Nip19.Type.ADDRESS) {
|
||||||
|
val note = LocalCache.checkGetOrCreateAddressableNote(results.key.hex)
|
||||||
|
if (note != null) {
|
||||||
|
addNoteToReplyTos(note)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tags the text in the correct order.
|
||||||
|
val newMessage = message.text.split('\n').map { paragraph: String ->
|
||||||
|
paragraph.split(' ').map { word: String ->
|
||||||
|
val results = parseDirtyWordForKey(word)
|
||||||
|
if (results?.key?.type == Nip19.Type.USER) {
|
||||||
|
val user = LocalCache.getOrCreateUser(results.key.hex)
|
||||||
|
|
||||||
|
"#[${tagIndex(user)}]${results.restOfWord}"
|
||||||
|
} else if (results?.key?.type == Nip19.Type.NOTE) {
|
||||||
|
val note = LocalCache.getOrCreateNote(results.key.hex)
|
||||||
|
|
||||||
|
"#[${tagIndex(note)}]${results.restOfWord}"
|
||||||
|
} else if (results?.key?.type == Nip19.Type.ADDRESS) {
|
||||||
|
val note = LocalCache.checkGetOrCreateAddressableNote(results.key.hex)
|
||||||
|
if (note != null) {
|
||||||
|
"#[${tagIndex(note)}]${results.restOfWord}"
|
||||||
|
} else {
|
||||||
|
word
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
word
|
||||||
|
}
|
||||||
|
}.joinToString(" ")
|
||||||
|
}.joinToString("\n")
|
||||||
|
|
||||||
|
/* if (originalNote?.channel() != null) {
|
||||||
|
account?.sendChannelMessage(newMessage, originalNote!!.channel()!!.idHex, originalNote!!, mentions)
|
||||||
|
} else {
|
||||||
|
account?.sendPoll(newMessage, replyTos, mentions)
|
||||||
|
}*/
|
||||||
|
|
||||||
|
account?.sendPoll(newMessage, replyTos, mentions, getPollOptionsList(), valueMaximum, valueMinimum, consensusThreshold, closedAt)
|
||||||
|
|
||||||
|
clearPollStates()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun cancel() {
|
override fun cancel() {
|
||||||
super.cancel()
|
super.cancel()
|
||||||
|
|
||||||
clearStates()
|
clearPollStates()
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun findUrlInMessage(): String? {
|
override fun findUrlInMessage(): String? {
|
||||||
@@ -64,13 +111,26 @@ class NewPollViewModel : NewPostViewModel() {
|
|||||||
super.autocompleteWithUser(item)
|
super.autocompleteWithUser(item)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun clearStates() {
|
// clear all states
|
||||||
// clear states
|
private fun clearPollStates() {
|
||||||
|
message = TextFieldValue("")
|
||||||
|
urlPreview = null
|
||||||
|
isUploadingImage = false
|
||||||
|
mentions = null
|
||||||
|
|
||||||
zapRecipients = mutableStateListOf<HexKey>()
|
zapRecipients = mutableStateListOf<HexKey>()
|
||||||
pollOptions = mutableStateListOf("", "")
|
pollOptions = mutableStateListOf("", "")
|
||||||
zapMax = null
|
valueMaximum = null
|
||||||
zapMin = null
|
valueMinimum = null
|
||||||
consensus = null
|
consensusThreshold = null
|
||||||
closedAfter = null
|
closedAt = null
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun getPollOptionsList(): List<Map<Int, String>> {
|
||||||
|
val optionsList: MutableList<Map<Int, String>> = mutableListOf()
|
||||||
|
pollOptions.forEachIndexed { i, s ->
|
||||||
|
optionsList.add(mapOf(Pair(i, s)))
|
||||||
|
}
|
||||||
|
return optionsList
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -71,7 +71,7 @@ open class NewPostViewModel : ViewModel() {
|
|||||||
return (if (originalNote?.channel() != null) 1 else 0) + (replyTos?.indexOf(note) ?: 0)
|
return (if (originalNote?.channel() != null) 1 else 0) + (replyTos?.indexOf(note) ?: 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
open fun sendPost() {
|
fun sendPost() {
|
||||||
// adds all references to mentions and reply tos
|
// adds all references to mentions and reply tos
|
||||||
message.text.split('\n').forEach { paragraph: String ->
|
message.text.split('\n').forEach { paragraph: String ->
|
||||||
paragraph.split(' ').forEach { word: String ->
|
paragraph.split(' ').forEach { word: String ->
|
||||||
|
@@ -33,7 +33,7 @@ fun PollClosing(pollViewModel: NewPollViewModel) {
|
|||||||
val int = text.toInt()
|
val int = text.toInt()
|
||||||
if (int < 0) {
|
if (int < 0) {
|
||||||
isInputValid = false
|
isInputValid = false
|
||||||
} else { pollViewModel.closedAfter = int }
|
} else { pollViewModel.closedAt = int }
|
||||||
} catch (e: Exception) { isInputValid = false }
|
} catch (e: Exception) { isInputValid = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -33,7 +33,7 @@ fun PollConsensusThreshold(pollViewModel: NewPollViewModel) {
|
|||||||
val int = text.toInt()
|
val int = text.toInt()
|
||||||
if (int < 0 || int > 100) {
|
if (int < 0 || int > 100) {
|
||||||
isInputValid = false
|
isInputValid = false
|
||||||
} else { pollViewModel.consensus = int }
|
} else { pollViewModel.consensusThreshold = int }
|
||||||
} catch (e: Exception) { isInputValid = false }
|
} catch (e: Exception) { isInputValid = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -33,9 +33,9 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
|
|||||||
if (textMax.isNotEmpty()) {
|
if (textMax.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
val int = textMax.toInt()
|
val int = textMax.toInt()
|
||||||
if ( int < 1)
|
if (int < 1) {
|
||||||
isMaxValid = false
|
isMaxValid = false
|
||||||
else pollViewModel.zapMax = int
|
} else { pollViewModel.valueMaximum = int }
|
||||||
} catch (e: Exception) { isMaxValid = false }
|
} catch (e: Exception) { isMaxValid = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -44,9 +44,9 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
|
|||||||
if (textMin.isNotEmpty()) {
|
if (textMin.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
val int = textMin.toInt()
|
val int = textMin.toInt()
|
||||||
if ( int < 1)
|
if (int < 1) {
|
||||||
isMinValid = false
|
isMinValid = false
|
||||||
else pollViewModel.zapMin = int
|
} else { pollViewModel.valueMinimum = int }
|
||||||
} catch (e: Exception) { isMinValid = false }
|
} catch (e: Exception) { isMinValid = false }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +56,7 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
|
|||||||
val intMin = textMin.toInt()
|
val intMin = textMin.toInt()
|
||||||
val intMax = textMax.toInt()
|
val intMax = textMax.toInt()
|
||||||
|
|
||||||
if ( intMin > intMax) {
|
if (intMin > intMax) {
|
||||||
isMinValid = false
|
isMinValid = false
|
||||||
isMaxValid = false
|
isMaxValid = false
|
||||||
}
|
}
|
||||||
@@ -68,7 +68,8 @@ fun PollVoteValueRange(pollViewModel: NewPollViewModel) {
|
|||||||
|
|
||||||
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
|
val colorInValid = TextFieldDefaults.outlinedTextFieldColors(
|
||||||
focusedBorderColor = MaterialTheme.colors.error,
|
focusedBorderColor = MaterialTheme.colors.error,
|
||||||
unfocusedBorderColor = Color.Red)
|
unfocusedBorderColor = Color.Red
|
||||||
|
)
|
||||||
val colorValid = TextFieldDefaults.outlinedTextFieldColors(
|
val colorValid = TextFieldDefaults.outlinedTextFieldColors(
|
||||||
focusedBorderColor = MaterialTheme.colors.primary,
|
focusedBorderColor = MaterialTheme.colors.primary,
|
||||||
unfocusedBorderColor = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
unfocusedBorderColor = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
|
||||||
|
Reference in New Issue
Block a user