Skip to content

Commit

Permalink
- Migrates NIP-17 DM new message model to its own model and disable s…
Browse files Browse the repository at this point in the history
…ome of the generic behavior from new posts.

- Fixes a bug on edits showing a previous edit.
- Refactors user suggestions and lastword procedures
- Adds a DM inline reply from other parts of the app, like on notification.
- Hides Retweet button from DMs on Notifications
- Fixes colors for drafts in chats.
- Adds a nav function that computes the destination only after the user clicks on it and on a coroutine.
  • Loading branch information
vitorpamplona committed Mar 4, 2025
1 parent 1a001e7 commit 0580aef
Show file tree
Hide file tree
Showing 54 changed files with 1,858 additions and 698 deletions.
17 changes: 10 additions & 7 deletions amethyst/src/main/java/com/vitorpamplona/amethyst/model/Account.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2577,7 +2577,6 @@ class Account(
message: String,
toUser: User,
replyingTo: Note? = null,
mentions: List<User>?,
zapReceiver: List<ZapSplitSetup>? = null,
contentWarningReason: String? = null,
zapRaiserAmount: Long? = null,
Expand All @@ -2590,7 +2589,6 @@ class Account(
message,
toUser.toPTag(),
replyingTo,
mentions,
zapReceiver,
contentWarningReason,
zapRaiserAmount,
Expand All @@ -2605,7 +2603,6 @@ class Account(
message: String,
toUser: PTag,
replyingTo: Note? = null,
mentions: List<User>?,
zapReceiver: List<ZapSplitSetup>? = null,
contentWarningReason: String? = null,
zapRaiserAmount: Long? = null,
Expand Down Expand Up @@ -3183,10 +3180,16 @@ class Account(
fun cachedDecryptContent(event: Event?): String? {
if (event == null) return null

return if (event is PrivateDmEvent && isWriteable()) {
event.cachedContentFor(signer)
} else if (event is LnZapRequestEvent && event.isPrivateZap() && isWriteable()) {
event.cachedPrivateZap()?.content
return if (isWriteable()) {
if (event is PrivateDmEvent) {
event.cachedContentFor(signer)
} else if (event is LnZapRequestEvent && event.isPrivateZap()) {
event.cachedPrivateZap()?.content
} else if (event is DraftEvent) {
event.preCachedDraft(signer)?.content
} else {
event.content
}
} else {
event.content
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,16 +236,13 @@ class User(
}.flatten()

@Synchronized
private fun getOrCreatePrivateChatroomSync(key: ChatroomKey): Chatroom {
checkNotInMainThread()

return privateChatrooms[key]
private fun getOrCreatePrivateChatroomSync(key: ChatroomKey): Chatroom =
privateChatrooms[key]
?: run {
val privateChatroom = Chatroom()
privateChatrooms = privateChatrooms + Pair(key, privateChatroom)
privateChatroom
}
}

private fun getOrCreatePrivateChatroom(user: User): Chatroom {
val key = ChatroomKey(persistentSetOf(user.pubkeyHex))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ import com.vitorpamplona.amethyst.ui.note.SearchIcon
import com.vitorpamplona.amethyst.ui.note.UsernameDisplay
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.CloseButton
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.ChannelName
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.ChannelName
import com.vitorpamplona.amethyst.ui.screen.loggedIn.search.SearchBarViewModel
import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.DividerThickness
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ import com.vitorpamplona.quartz.nip10Notes.content.findURLs
import com.vitorpamplona.quartz.nip10Notes.tags.notify
import com.vitorpamplona.quartz.nip10Notes.tags.positionalMarkedTags
import com.vitorpamplona.quartz.nip14Subject.subject
import com.vitorpamplona.quartz.nip17Dm.base.BaseDMGroupEvent
import com.vitorpamplona.quartz.nip17Dm.base.NIP17Group
import com.vitorpamplona.quartz.nip17Dm.messages.ChatMessageEvent
import com.vitorpamplona.quartz.nip17Dm.messages.changeSubject
Expand Down Expand Up @@ -818,7 +819,6 @@ open class NewPostViewModel : ViewModel() {
message = tagger.message,
toUser = originalNote!!.author!!,
replyingTo = originalNote!!,
mentions = tagger.pTags,
zapReceiver = zapReceiver,
contentWarningReason = contentWarningReason,
zapRaiserAmount = localZapRaiserAmount,
Expand All @@ -827,7 +827,7 @@ open class NewPostViewModel : ViewModel() {
draftTag = localDraft,
)
} else if (originalNote?.event is NIP17Group) {
val replyHint = originalNote?.toEventHint<ChatMessageEvent>()
val replyHint = originalNote?.toEventHint<BaseDMGroupEvent>()

val template =
if (replyHint == null) {
Expand Down Expand Up @@ -868,7 +868,7 @@ open class NewPostViewModel : ViewModel() {
account?.sendNIP17PrivateMessage(template, localDraft)
} else if (!dmUsers.isNullOrEmpty()) {
if (nip17 || dmUsers.size > 1) {
val replyHint = originalNote?.toEventHint<ChatMessageEvent>()
val replyHint = originalNote?.toEventHint<BaseDMGroupEvent>()
val template =
if (replyHint == null) {
ChatMessageEvent.build(tagger.message, dmUsers.map { it.toPTag() }) {
Expand Down Expand Up @@ -910,7 +910,6 @@ open class NewPostViewModel : ViewModel() {
message = tagger.message,
toUser = dmUsers.first(),
replyingTo = originalNote,
mentions = tagger.pTags,
contentWarningReason = contentWarningReason,
zapReceiver = zapReceiver,
zapRaiserAmount = localZapRaiserAmount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ import com.vitorpamplona.amethyst.ui.note.UserCompose
import com.vitorpamplona.amethyst.ui.note.timeAgo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.CloseButton
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.LoadUser
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.LoadUser
import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.DoubleHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.DoubleVertSpacer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ import com.vitorpamplona.amethyst.ui.navigation.INav
import com.vitorpamplona.amethyst.ui.note.NoteCompose
import com.vitorpamplona.amethyst.ui.note.toShortenHex
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.LoadUser
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.LoadUser
import com.vitorpamplona.amethyst.ui.screen.loggedIn.mockAccountViewModel
import com.vitorpamplona.amethyst.ui.theme.HalfVertPadding
import com.vitorpamplona.amethyst.ui.theme.inlinePlaceholder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.ui.note.elements.TimeAgo
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.NewItemsBubble
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.NewItemsBubble
import com.vitorpamplona.amethyst.ui.stringRes
import com.vitorpamplona.amethyst.ui.theme.DividerThickness
import com.vitorpamplona.amethyst.ui.theme.DoubleHorzSpacer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.LoadRedirectScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.NewPostScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.bookmarks.BookmarkListScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.ChannelScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.ChatroomListScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.ChatroomScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.ChatroomScreenByAuthor
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.ChatroomListScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.private.ChatroomScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.private.ChatroomScreenByAuthor
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.public.ChannelScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.communities.CommunityScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.discover.DiscoverScreen
import com.vitorpamplona.amethyst.ui.screen.loggedIn.drafts.DraftListScreen
Expand Down Expand Up @@ -94,6 +94,16 @@ fun NavBackStackEntry.message(): String? =
URLDecoder.decode(it, "utf-8")
}

fun NavBackStackEntry.replyId(): String? =
arguments?.getString("replyId")?.let {
URLDecoder.decode(it, "utf-8")
}

fun NavBackStackEntry.draftId(): String? =
arguments?.getString("draftId")?.let {
URLDecoder.decode(it, "utf-8")
}

@Composable
fun AppNavigation(
accountViewModel: AccountViewModel,
Expand Down Expand Up @@ -219,6 +229,8 @@ fun AppNavigation(
ChatroomScreen(
roomId = it.id(),
draftMessage = it.message(),
replyToNote = it.replyId(),
editFromDraft = it.draftId(),
accountViewModel = accountViewModel,
nav = nav,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ interface INav {

fun nav(route: String)

fun nav(computeRoute: suspend () -> String)

fun newStack(route: String)

fun popBack()
Expand Down Expand Up @@ -91,6 +93,15 @@ class Nav(
}
}

override fun nav(computeRoute: suspend () -> String) {
scope.launch {
val route = computeRoute()
if (getRouteWithArguments(controller) != route) {
controller.navigate(route)
}
}
}

override fun newStack(route: String) {
scope.launch {
controller.navigate(route) {
Expand Down Expand Up @@ -135,6 +146,9 @@ object EmptyNav : INav {
override fun nav(route: String) {
}

override fun nav(computeRoute: suspend () -> String) {
}

override fun newStack(route: String) {
}

Expand All @@ -161,6 +175,11 @@ class ObservableNavigate(
nav.nav(route)
}

override fun nav(computeRoute: suspend () -> String) {
onNavigate()
nav.nav(computeRoute)
}

override fun newStack(route: String) {
onNavigate()
nav.newStack(route)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
package com.vitorpamplona.amethyst.ui.navigation

import com.vitorpamplona.amethyst.model.Channel
import com.vitorpamplona.amethyst.model.LocalCache.users
import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.NostrUserProfileDataSource.user
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.quartz.nip01Core.core.AddressableEvent
import com.vitorpamplona.quartz.nip01Core.core.Event
Expand All @@ -36,7 +38,6 @@ import com.vitorpamplona.quartz.nip53LiveActivities.chat.LiveActivitiesChatMessa
import com.vitorpamplona.quartz.nip53LiveActivities.streaming.LiveActivitiesEvent
import com.vitorpamplona.quartz.nip72ModCommunities.definition.CommunityDefinitionEvent
import com.vitorpamplona.quartz.nip89AppHandlers.definition.AppDefinitionEvent
import kotlinx.collections.immutable.persistentSetOf
import java.net.URLEncoder

fun routeFor(
Expand Down Expand Up @@ -110,23 +111,71 @@ fun routeFor(
fun routeToMessage(
user: HexKey,
draftMessage: String?,
replyId: HexKey? = null,
quoteId: HexKey? = null,
accountViewModel: AccountViewModel,
): String =
routeToMessage(
setOf(user),
draftMessage,
replyId,
quoteId,
accountViewModel,
)

fun routeToMessage(
users: Set<HexKey>,
draftMessage: String?,
replyId: HexKey? = null,
quoteId: HexKey? = null,
accountViewModel: AccountViewModel,
) = routeToMessage(
ChatroomKey(users),
draftMessage,
replyId,
quoteId,
accountViewModel,
)

fun routeToMessage(
room: ChatroomKey,
draftMessage: String?,
replyId: HexKey? = null,
quoteId: HexKey? = null,
accountViewModel: AccountViewModel,
): String {
val withKey = ChatroomKey(persistentSetOf(user))
accountViewModel.account.userProfile().createChatroom(withKey)
return if (draftMessage != null) {
val encodedMessage = URLEncoder.encode(draftMessage, "utf-8")
"Room/${withKey.hashCode()}?message=$encodedMessage"
} else {
"Room/${withKey.hashCode()}"
accountViewModel.account.userProfile().createChatroom(room)

val params =
listOfNotNull(
draftMessage?.let {
"message=${URLEncoder.encode(it, "utf-8")}"
},
replyId?.let {
"replyId=${URLEncoder.encode(it, "utf-8")}"
},
quoteId?.let {
"quoteId=${URLEncoder.encode(it, "utf-8")}"
},
)

return buildString {
append("Room/")
append(room.hashCode().toString())
if (params.isNotEmpty()) {
append("?")
append(params.joinToString("&"))
}
}
}

fun routeToMessage(
user: User,
draftMessage: String?,
replyId: HexKey? = null,
quoteId: HexKey? = null,
accountViewModel: AccountViewModel,
): String = routeToMessage(user.pubkeyHex, draftMessage, accountViewModel)
): String = routeToMessage(user.pubkeyHex, draftMessage, replyId, quoteId, accountViewModel)

fun routeFor(note: Channel): String = "Channel/${note.idHex}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ sealed class Route(

object Room :
Route(
route = "Room/{id}?message={message}",
route = "Room/{id}?message={message}&replyId={replyId}&draftId={draftId}",
icon = R.drawable.ic_moments,
arguments =
listOf(
Expand All @@ -179,6 +179,16 @@ sealed class Route(
nullable = true
defaultValue = null
},
navArgument("replyId") {
type = NavType.StringType
nullable = true
defaultValue = null
},
navArgument("draftId") {
type = NavType.StringType
nullable = true
defaultValue = null
},
).toImmutableList(),
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ import com.vitorpamplona.amethyst.ui.layouts.LeftPictureLayout
import com.vitorpamplona.amethyst.ui.navigation.INav
import com.vitorpamplona.amethyst.ui.note.elements.BannerImage
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.ChannelHeader
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.EndedFlag
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.LiveFlag
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.OfflineFlag
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatrooms.ScheduledFlag
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.public.ChannelHeader
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.public.EndedFlag
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.public.LiveFlag
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.public.OfflineFlag
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chatlist.public.ScheduledFlag
import com.vitorpamplona.amethyst.ui.screen.loggedIn.dvms.observeAppDefinition
import com.vitorpamplona.amethyst.ui.screen.loggedIn.home.CheckIfVideoIsOnline
import com.vitorpamplona.amethyst.ui.screen.loggedIn.notifications.equalImmutableLists
Expand Down
Loading

0 comments on commit 0580aef

Please sign in to comment.