refactor default values in signEvent variables

This commit is contained in:
greenart7c3
2023-09-01 10:18:49 -03:00
parent f76ab01c11
commit 46effa572c
12 changed files with 123 additions and 55 deletions
@@ -264,7 +264,7 @@ class Account(
return note.hasReacted(userProfile(), reaction)
}
fun reactTo(note: Note, reaction: String, signEvent: Boolean = true): ReactionEvent? {
fun reactTo(note: Note, reaction: String, signEvent: Boolean): ReactionEvent? {
if (!isWriteable() && signEvent) return null
if (hasReacted(note, reaction)) {
@@ -465,11 +465,11 @@ class Account(
LocalCache.consume(event, null)
}
fun delete(note: Note, signEvent: Boolean = true): DeletionEvent? {
fun delete(note: Note, signEvent: Boolean): DeletionEvent? {
return delete(listOf(note), signEvent)
}
fun delete(notes: List<Note>, signEvent: Boolean = true): DeletionEvent? {
fun delete(notes: List<Note>, signEvent: Boolean): DeletionEvent? {
if (!isWriteable() && signEvent) return null
val myNotes = notes.filter { it.author == userProfile() }.map { it.idHex }
@@ -491,7 +491,7 @@ class Account(
return HTTPAuthorizationEvent.create(url, method, body, keyPair.privKey!!)
}
fun boost(note: Note, signEvent: Boolean = true): Event? {
fun boost(note: Note, signEvent: Boolean): Event? {
if (!isWriteable() && signEvent) return null
if (note.hasBoostedInTheLast5Minutes(userProfile())) {
@@ -558,7 +558,7 @@ class Account(
return returningContactList
}
fun follow(user: User, signEvent: Boolean = true): ContactListEvent? {
fun follow(user: User, signEvent: Boolean): ContactListEvent? {
if (!isWriteable() && signEvent) return null
val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList)
@@ -643,7 +643,7 @@ class Account(
return null
}
fun followHashtag(tag: String, signEvent: Boolean = true): ContactListEvent? {
fun followHashtag(tag: String, signEvent: Boolean): ContactListEvent? {
if (!isWriteable() && signEvent) return null
val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList)
@@ -707,7 +707,7 @@ class Account(
return null
}
fun unfollow(user: User, signEvent: Boolean = true): ContactListEvent? {
fun unfollow(user: User, signEvent: Boolean): ContactListEvent? {
if (!isWriteable() && signEvent) return null
val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList)
@@ -730,7 +730,7 @@ class Account(
return null
}
fun unfollowHashtag(tag: String, signEvent: Boolean = true): ContactListEvent? {
fun unfollowHashtag(tag: String, signEvent: Boolean): ContactListEvent? {
if (!isWriteable() && signEvent) return null
val contactList = migrateCommunitiesAndChannelsIfNeeded(userProfile().latestContactList)
@@ -936,7 +936,7 @@ class Account(
directMentions: Set<HexKey>,
relayList: List<Relay>? = null,
geohash: String? = null,
signEvent: Boolean = true
signEvent: Boolean
): TextNoteEvent? {
if (!isWriteable() && signEvent) return null
@@ -1000,7 +1000,7 @@ class Account(
zapRaiserAmount: Long? = null,
relayList: List<Relay>? = null,
geohash: String? = null,
signEvent: Boolean = true
signEvent: Boolean
): PollNoteEvent? {
if (!isWriteable() && signEvent) return null
@@ -1056,7 +1056,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
signEvent: Boolean = true
signEvent: Boolean
): ChannelMessageEvent? {
if (!isWriteable() && signEvent) return null
@@ -1095,7 +1095,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
signEvent: Boolean = true
signEvent: Boolean
): LiveActivitiesChatMessageEvent? {
if (!isWriteable() && signEvent) return null
@@ -1126,7 +1126,7 @@ class Account(
}
fun sendPrivateMessage(message: String, toUser: User, replyingTo: Note? = null, mentions: List<User>?, zapReceiver: String? = null, wantsToMarkAsSensitive: Boolean, zapRaiserAmount: Long? = null, geohash: String? = null): PrivateDmEvent? {
return sendPrivateMessage(message, toUser.pubkeyHex, replyingTo, mentions, zapReceiver, wantsToMarkAsSensitive, zapRaiserAmount, geohash)
return sendPrivateMessage(message, toUser.pubkeyHex, replyingTo, mentions, zapReceiver, wantsToMarkAsSensitive, zapRaiserAmount, geohash, true)
}
fun sendPrivateMessage(
@@ -1138,7 +1138,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
signEvent: Boolean = true
signEvent: Boolean
): PrivateDmEvent? {
if (!isWriteable() && signEvent) return null
@@ -1179,7 +1179,7 @@ class Account(
wantsToMarkAsSensitive: Boolean,
zapRaiserAmount: Long? = null,
geohash: String? = null,
signEvent: Boolean = true
signEvent: Boolean
): List<GiftWrapEvent>? {
if (!isWriteable() && signEvent) return null
@@ -43,7 +43,7 @@ object AmberUtils {
""
)
while (isActivityRunning) {
Thread.sleep(250)
// do nothing
}
}
@@ -57,7 +57,7 @@ object AmberUtils {
pubKey
)
while (isActivityRunning) {
Thread.sleep(250)
// do nothing
}
}
}
@@ -72,7 +72,7 @@ object AmberUtils {
pubKey
)
while (isActivityRunning) {
Thread.sleep(250)
// do nothing
}
}
}
@@ -158,7 +158,7 @@ open class NewPostViewModel() : ViewModel() {
this.account = account
}
fun sendPost(relayList: List<Relay>? = null, signEvent: Boolean = true): Event? {
fun sendPost(relayList: List<Relay>? = null, signEvent: Boolean): Event? {
try {
val tagger = NewMessageTagger(message.text, mentions, replyTos, originalNote?.channelHex())
tagger.run()
@@ -207,7 +207,8 @@ open class NewPostViewModel() : ViewModel() {
wantsToMarkAsSensitive = wantsToMarkAsSensitive,
zapReceiver = zapReceiver,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash
geohash = geoHash,
signEvent = signEvent
)
return null
} else if (!dmUsers.isNullOrEmpty()) {
@@ -221,7 +222,8 @@ open class NewPostViewModel() : ViewModel() {
wantsToMarkAsSensitive = wantsToMarkAsSensitive,
zapReceiver = zapReceiver,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash
geohash = geoHash,
signEvent = signEvent
)
return null
} else {
@@ -233,7 +235,8 @@ open class NewPostViewModel() : ViewModel() {
wantsToMarkAsSensitive = wantsToMarkAsSensitive,
zapReceiver = zapReceiver,
zapRaiserAmount = localZapRaiserAmount,
geohash = geoHash
geohash = geoHash,
signEvent = signEvent
)
return null
}
@@ -17,6 +17,8 @@ import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
@@ -31,11 +33,18 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.service.relays.Client
import com.vitorpamplona.amethyst.ui.actions.CloseButton
import com.vitorpamplona.amethyst.ui.actions.PostButton
import com.vitorpamplona.amethyst.ui.actions.SignerDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.events.Event
import com.vitorpamplona.quartz.events.TextNoteEvent
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
class AddBountyAmountViewModel : ViewModel() {
private var account: Account? = null
@@ -48,11 +57,11 @@ class AddBountyAmountViewModel : ViewModel() {
this.bounty = bounty
}
fun sendPost() {
fun sendPost(signEvent: Boolean): TextNoteEvent? {
val newValue = nextAmount.text.trim().toLongOrNull()
if (newValue != null) {
account?.sendPost(
val event = account?.sendPost(
message = newValue.toString(),
replyTo = listOfNotNull(bounty),
mentions = listOfNotNull(bounty?.author),
@@ -60,11 +69,14 @@ class AddBountyAmountViewModel : ViewModel() {
wantsToMarkAsSensitive = false,
replyingTo = null,
root = null,
directMentions = setOf()
directMentions = setOf(),
signEvent = signEvent
)
nextAmount = TextFieldValue("")
return event
}
return null
}
fun cancel() {
@@ -80,6 +92,26 @@ class AddBountyAmountViewModel : ViewModel() {
fun AddBountyAmountDialog(bounty: Note, accountViewModel: AccountViewModel, onClose: () -> Unit) {
val postViewModel: AddBountyAmountViewModel = viewModel()
postViewModel.load(accountViewModel.account, bounty)
val scope = rememberCoroutineScope()
var event by remember { mutableStateOf<Event?>(null) }
if (event != null) {
SignerDialog(
onClose = {
event = null
},
onPost = {
scope.launch(Dispatchers.IO) {
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
onClose()
}
},
data = event!!.toJson()
)
}
Dialog(
onDismissRequest = { onClose() },
@@ -89,7 +121,11 @@ fun AddBountyAmountDialog(bounty: Note, accountViewModel: AccountViewModel, onCl
)
) {
Surface() {
Column(modifier = Modifier.padding(10.dp).width(IntrinsicSize.Min)) {
Column(
modifier = Modifier
.padding(10.dp)
.width(IntrinsicSize.Min)
) {
Row(
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically,
@@ -102,8 +138,10 @@ fun AddBountyAmountDialog(bounty: Note, accountViewModel: AccountViewModel, onCl
PostButton(
onPost = {
postViewModel.sendPost()
onClose()
event = postViewModel.sendPost(!accountViewModel.loggedInWithAmber())
if (!accountViewModel.loggedInWithAmber()) {
onClose()
}
},
isActive = postViewModel.hasChanged()
)
@@ -315,8 +315,10 @@ private fun RenderMainPopup(
stringResource(R.string.quick_action_unfollow)
) {
scope.launch(Dispatchers.IO) {
accountViewModel.unfollow(note.author!!)
onDismiss()
event = accountViewModel.unfollow(note.author!!, !accountViewModel.loggedInWithAmber())
if (!accountViewModel.loggedInWithAmber()) {
onDismiss()
}
}
}
} else {
@@ -674,7 +674,7 @@ fun BoostReaction(
if (accountViewModel.isWriteable()) {
if (accountViewModel.hasBoosted(baseNote)) {
scope.launch(Dispatchers.IO) {
accountViewModel.deleteBoostsTo(baseNote)
accountViewModel.deleteBoostsTo(baseNote, true)
}
} else {
wantsToBoost = true
@@ -961,9 +961,9 @@ private fun likeClick(
scope.launch(Dispatchers.IO) {
val reaction = accountViewModel.account.reactionChoices.first()
if (accountViewModel.hasReactedTo(baseNote, reaction)) {
accountViewModel.deleteReactionTo(baseNote, reaction)
accountViewModel.deleteReactionTo(baseNote, reaction, true)
} else {
accountViewModel.reactTo(baseNote, reaction)
accountViewModel.reactTo(baseNote, reaction, true)
}
}
} else if (accountViewModel.account.reactionChoices.size > 1) {
@@ -1273,7 +1273,7 @@ private fun BoostTypeChoicePopup(baseNote: Note, iconSize: Dp, accountViewModel:
onClick = {
if (accountViewModel.isWriteable()) {
scope.launch(Dispatchers.IO) {
accountViewModel.boost(baseNote)
accountViewModel.boost(baseNote, true)
onDismiss()
}
} else {
@@ -344,7 +344,7 @@ fun ShowFollowingOrUnfollowingButton(
UnfollowButton {
if (!accountViewModel.isWriteable()) {
if (accountViewModel.loggedInWithAmber()) {
event = accountViewModel.account.unfollow(baseAuthor, false)
event = accountViewModel.unfollow(baseAuthor, false)
} else {
scope.launch {
Toast
@@ -358,7 +358,7 @@ fun ShowFollowingOrUnfollowingButton(
}
} else {
scope.launch(Dispatchers.IO) {
accountViewModel.unfollow(baseAuthor)
accountViewModel.unfollow(baseAuthor, true)
}
}
}
@@ -112,11 +112,11 @@ class AccountViewModel(val account: Account) : ViewModel() {
return account.userProfile()
}
fun reactTo(note: Note, reaction: String, signEvent: Boolean = true): ReactionEvent? {
fun reactTo(note: Note, reaction: String, signEvent: Boolean): ReactionEvent? {
return account.reactTo(note, reaction, signEvent)
}
fun reactToOrDelete(note: Note, reaction: String, signEvent: Boolean = true): Event? {
fun reactToOrDelete(note: Note, reaction: String, signEvent: Boolean): Event? {
val currentReactions = account.reactionTo(note, reaction)
if (currentReactions.isNotEmpty()) {
return account.delete(currentReactions, signEvent)
@@ -134,7 +134,7 @@ class AccountViewModel(val account: Account) : ViewModel() {
return account.hasReacted(baseNote, reaction)
}
fun deleteReactionTo(note: Note, reaction: String, signEvent: Boolean = true): DeletionEvent? {
fun deleteReactionTo(note: Note, reaction: String, signEvent: Boolean): DeletionEvent? {
return account.delete(account.reactionTo(note, reaction), signEvent)
}
@@ -142,7 +142,7 @@ class AccountViewModel(val account: Account) : ViewModel() {
return account.hasBoosted(baseNote)
}
fun deleteBoostsTo(note: Note, signEvent: Boolean = true): DeletionEvent? {
fun deleteBoostsTo(note: Note, signEvent: Boolean): DeletionEvent? {
return account.delete(account.boostsTo(note), signEvent)
}
@@ -302,7 +302,7 @@ class AccountViewModel(val account: Account) : ViewModel() {
}
}
fun boost(note: Note, signEvent: Boolean = true): Event? {
fun boost(note: Note, signEvent: Boolean): Event? {
return account.boost(note, signEvent)
}
@@ -386,8 +386,8 @@ class AccountViewModel(val account: Account) : ViewModel() {
return account.follow(user, signEvent)
}
fun unfollow(user: User) {
account.unfollow(user)
fun unfollow(user: User, signEvent: Boolean): ContactListEvent? {
return account.unfollow(user, signEvent)
}
fun isLoggedUser(user: User?): Boolean {
@@ -263,6 +263,24 @@ fun ChannelScreen(
val scope = rememberCoroutineScope()
var event by remember { mutableStateOf<Event?>(null) }
if (event != null) {
SignerDialog(
onClose = {
event = null
},
onPost = {
scope.launch(Dispatchers.IO) {
val signedEvent = Event.fromJson(it)
Client.send(signedEvent)
LocalCache.verifyAndConsume(signedEvent, null)
event = null
}
},
data = event!!.toJson()
)
}
// LAST ROW
EditFieldRow(newPostModel, isPrivate = false, accountViewModel = accountViewModel) {
scope.launch(Dispatchers.IO) {
@@ -274,20 +292,22 @@ fun ChannelScreen(
)
tagger.run()
if (channel is PublicChatChannel) {
accountViewModel.account.sendChannelMessage(
event = accountViewModel.account.sendChannelMessage(
message = tagger.message,
toChannel = channel.idHex,
replyTo = tagger.replyTos,
mentions = tagger.mentions,
wantsToMarkAsSensitive = false
wantsToMarkAsSensitive = false,
signEvent = !accountViewModel.loggedInWithAmber()
)
} else if (channel is LiveActivitiesChannel) {
accountViewModel.account.sendLiveMessage(
event = accountViewModel.account.sendLiveMessage(
message = tagger.message,
toChannel = channel.address,
replyTo = tagger.replyTos,
mentions = tagger.mentions,
wantsToMarkAsSensitive = false
wantsToMarkAsSensitive = false,
signEvent = !accountViewModel.loggedInWithAmber()
)
}
newPostModel.message = TextFieldValue("")
@@ -344,12 +344,14 @@ fun ChatroomScreen(
PrivateMessageEditFieldRow(newPostModel, isPrivate = true, accountViewModel) {
scope.launch(Dispatchers.IO) {
if (newPostModel.nip24 || room.users.size > 1 || replyTo.value?.event is ChatMessageEvent) {
// TODO: add support for amber
accountViewModel.account.sendNIP24PrivateMessage(
message = newPostModel.message.text,
toUsers = room.users.toList(),
replyingTo = replyTo.value,
mentions = null,
wantsToMarkAsSensitive = false
wantsToMarkAsSensitive = false,
signEvent = true
)
} else {
if (!accountViewModel.isWriteable() && accountViewModel.loggedInWithAmber()) {
@@ -360,7 +362,8 @@ fun ChatroomScreen(
toUser = room.users.first(),
replyingTo = replyTo.value,
mentions = null,
wantsToMarkAsSensitive = false
wantsToMarkAsSensitive = false,
signEvent = true
)
}
}
@@ -690,13 +693,15 @@ fun NewSubjectView(onClose: () -> Unit, accountViewModel: AccountViewModel, room
PostButton(
onPost = {
scope.launch(Dispatchers.IO) {
// TODO: add support for amber
accountViewModel.account.sendNIP24PrivateMessage(
message = message.value,
toUsers = room.users.toList(),
subject = groupName.value.ifBlank { null },
replyingTo = null,
mentions = null,
wantsToMarkAsSensitive = false
wantsToMarkAsSensitive = false,
signEvent = true
)
}
@@ -186,7 +186,7 @@ fun HashtagActionOptions(
}
} else {
scope.launch(Dispatchers.IO) {
accountViewModel.account.unfollowHashtag(tag)
accountViewModel.account.unfollowHashtag(tag, true)
}
}
}
@@ -208,7 +208,7 @@ fun HashtagActionOptions(
}
} else {
scope.launch(Dispatchers.IO) {
accountViewModel.account.followHashtag(tag)
accountViewModel.account.followHashtag(tag, true)
}
}
}
@@ -895,7 +895,7 @@ private fun DisplayFollowUnfollowButton(
}
} else {
scope.launch(Dispatchers.IO) {
accountViewModel.account.unfollow(baseUser)
accountViewModel.account.unfollow(baseUser, true)
}
}
}
@@ -918,7 +918,7 @@ private fun DisplayFollowUnfollowButton(
}
} else {
scope.launch(Dispatchers.IO) {
accountViewModel.account.follow(baseUser)
accountViewModel.account.follow(baseUser, true)
}
}
}
@@ -940,7 +940,7 @@ private fun DisplayFollowUnfollowButton(
}
} else {
scope.launch(Dispatchers.IO) {
accountViewModel.account.follow(baseUser)
accountViewModel.account.follow(baseUser, true)
}
}
}