Possibility to remove mentions from replies.

This commit is contained in:
Vitor Pamplona 2023-01-17 11:16:50 -05:00
parent 0789752112
commit d0b6e96c2f
4 changed files with 46 additions and 27 deletions

View File

@ -90,13 +90,17 @@ class Account(val loggedIn: Persona, val followingChannels: MutableSet<String> =
}
}
fun sendPost(message: String, replyingTo: Note?) {
fun sendPost(message: String, originalNote: Note?, modifiedMentions: List<User>?) {
if (!isWriteable()) return
val replyToEvent = replyingTo?.event
val replyToEvent = originalNote?.event
if (replyToEvent is TextNoteEvent) {
val modifiedMentionsHex = modifiedMentions?.map { it.pubkeyHex }?.toSet() ?: emptySet()
val repliesTo = replyToEvent.replyTos.plus(replyToEvent.id.toHex())
val mentions = replyToEvent.mentions.plus(replyToEvent.pubKey.toHex())
val mentions = replyToEvent.mentions.plus(replyToEvent.pubKey.toHex()).filter {
it in modifiedMentionsHex
}
val signedEvent = TextNoteEvent.create(
msg = message,

View File

@ -41,17 +41,17 @@ import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.ui.components.UrlPreview
import com.vitorpamplona.amethyst.ui.components.imageExtension
import com.vitorpamplona.amethyst.ui.navigation.UploadFromGallery
import com.vitorpamplona.amethyst.ui.note.ReplyInformation
import kotlinx.coroutines.delay
import nostr.postr.events.TextNoteEvent
@OptIn(ExperimentalComposeUiApi::class)
@Composable
fun NewPostView(onClose: () -> Unit, replyingTo: Note? = null, account: Account) {
val postViewModel: NewPostViewModel = viewModel<NewPostViewModel>().apply {
this.replyingTo = replyingTo
this.account = account
}
fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, account: Account) {
val postViewModel: NewPostViewModel = viewModel()
postViewModel.load(account, baseReplyTo)
val context = LocalContext.current
@ -103,18 +103,9 @@ fun NewPostView(onClose: () -> Unit, replyingTo: Note? = null, account: Account)
)
}
if (replyingTo != null && replyingTo.event is TextNoteEvent) {
Row(
verticalAlignment = Alignment.CenterVertically,
) {
val replyList = replyingTo.replyTo!!.plus(replyingTo).joinToString(", ", "", "", 2) { it.idDisplayHex }
val withList = replyingTo.mentions!!.plus(replyingTo.author!!).joinToString(", ", "", "", 2) { it.toBestDisplayName() }
Text(
"in reply to ${replyList} with ${withList}",
fontSize = 13.sp,
color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
)
if (postViewModel.replyTos != null && baseReplyTo?.event is TextNoteEvent) {
ReplyInformation(postViewModel.replyTos, postViewModel.mentions, "") {
postViewModel.removeFromReplyList(it)
}
}

View File

@ -11,18 +11,33 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.ui.components.isValidURL
import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator
class NewPostViewModel: ViewModel() {
var account: Account? = null
var replyingTo: Note? = null
private var account: Account? = null
private var originalNote: Note? = null
var mentions by mutableStateOf<List<User>?>(null)
var replyTos by mutableStateOf<MutableList<Note>?>(null)
var message by mutableStateOf("")
var urlPreview by mutableStateOf<String?>(null)
fun load(account: Account, replyingTo: Note?) {
originalNote = replyingTo
replyingTo?.let { replyNote ->
this.replyTos = (replyNote.replyTo ?: mutableListOf()).plus(replyNote).toMutableList()
replyNote.author?.let { replyUser ->
this.mentions = (replyNote.mentions ?: emptyList()).plus(replyUser)
}
}
this.account = account
}
fun sendPost() {
account?.sendPost(message, replyingTo)
account?.sendPost(message, originalNote, mentions)
message = ""
urlPreview = null
}
@ -54,4 +69,8 @@ class NewPostViewModel: ViewModel() {
}
}
}
fun removeFromReplyList(it: User) {
mentions = mentions?.minus(it)
}
}

View File

@ -24,6 +24,13 @@ import com.vitorpamplona.amethyst.model.User
@Composable
fun ReplyInformation(replyTo: MutableList<Note>?, mentions: List<User>?, navController: NavController) {
ReplyInformation(replyTo, mentions) {
navController.navigate("User/${it.pubkeyHex}")
}
}
@Composable
fun ReplyInformation(replyTo: MutableList<Note>?, mentions: List<User>?, prefix: String = "", onUserTagClick: (User) -> Unit) {
FlowRow() {
if (mentions != null && mentions.isNotEmpty()) {
if (replyTo != null && replyTo.isNotEmpty()) {
@ -39,11 +46,9 @@ fun ReplyInformation(replyTo: MutableList<Note>?, mentions: List<User>?, navCont
innerUser?.let { myUser ->
ClickableText(
AnnotatedString("@${myUser.toBestDisplayName()}"),
AnnotatedString("${prefix}@${myUser.toBestDisplayName()}"),
style = LocalTextStyle.current.copy(color = MaterialTheme.colors.primary.copy(alpha = 0.52f), fontSize = 13.sp),
onClick = {
navController.navigate("User/${myUser.pubkeyHex}")
}
onClick = { onUserTagClick(myUser) }
)
if (idx < mentions.size - 2) {