feat: the group's posts, seen and proposed (group-nostr-profile, Phase 6)

A Posts row under the profile row counts the group's kind:1s; behind it,
the posts screen and the composer from Curare, newest first and under the
group's byline. The composer refuses a blank note through the same
predicate every member's device declines on (GroupStatementRefusal), and
says above the button that a signed post cannot be taken back. Proposing
is gated as for the profile (D4); a device holding no posts says so (D8).
The broadcast under each post comes with Phase 7.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-11-21 20:37:17 +02:00
1 parent add309f79a
commit ffde0553c2
16 files changed
+1383 -3

No files matched your search

@@ -613,4 +613,19 @@
<string name="what_the_group_says_about_itself_on_nostr">What the group says about itself on Nostr</string>
<string name="not_described_yet">Not described yet</string>
<string name="this_device_holds_no_profile_for_this_group">This device holds no profile for this group. A profile signed before you joined isn't passed on.</string>
<!-- The group's posts: docs/group-nostr-profile-and-posts.md -->
<string name="add_post">Add post</string>
<string name="a_post_needs_something_to_say">A post needs something to say.</string>
<string name="a_reply">A reply</string>
<string name="could_not_ask_the_group_to_sign_this_post">Couldn't ask the group to sign this post.</string>
<string name="posted_on">Posted %1$s</string>
<string name="posts">Posts</string>
<string name="propose_post">Propose post</string>
<string name="the_group_signs_a_post_so_it_takes_a_quorum">The group signs a post, so it takes a quorum. It goes out in the group's name, not yours, and can't be taken back once signed.</string>
<string name="this_device_holds_no_posts_for_this_group">This device holds no posts by this group. Posts signed before you joined aren't passed on.</string>
<string name="this_group_has_no_shared_key_to_sign_a_post">This group has no shared key, so it cannot sign a post. Run a shared key ceremony first.</string>
<string name="what_should_the_group_say">What should the group say?</string>
<string name="no_posts_yet">No posts yet</string>
<string name="one_post">1 post</string>
<string name="n_posts">%1$d posts</string>
</resources>
@@ -0,0 +1,313 @@
package mobi.giza.compose.ui.composable
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.text.input.TextFieldLineLimits
import androidx.compose.foundation.text.input.rememberTextFieldState
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Draw
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.BottomAppBarDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.OutlinedTextFieldDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.lifecycle.viewmodel.compose.viewModel
import mobi.giza.compose.database.model.ChatRoom
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.repository.ChatRepository
import mobi.giza.compose.repository.FrostSigningRepository
import mobi.giza.compose.ui.composable.navigation.routes.FrostSigningRoute
import mobi.giza.compose.ui.composable.navigation.routes.Route
import mobi.giza.compose.ui.composable.widgets.ErrorState
import mobi.giza.compose.ui.composable.widgets.LoadingDataIndicator
import mobi.giza.compose.ui.composable.widgets.LocalCanSign
import mobi.giza.compose.ui.composable.widgets.LocalSnackbarHostState
import mobi.giza.compose.ui.composable.widgets.NavigateBackButton
import mobi.giza.compose.ui.composable.widgets.ScreenStateTransition
import mobi.giza.compose.ui.composable.widgets.buttons.ExtendedFab
import mobi.giza.compose.ui.composable.widgets.profile.ProfileAvatar
import mobi.giza.compose.ui.composable.widgets.rememberNotifier
import mobi.giza.compose.ui.theme.ConformancePreviews
import mobi.giza.compose.ui.theme.GizaTheme
import mobi.giza.compose.ui.theme.readableContent
import mobi.giza.compose.ui.theme.spacing
import mobi.giza.compose.ui.view.model.AddGroupPostViewModel
import mobi.giza.compose.ui.view.state.AddGroupPostUIState
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import mobi.giza.compose.resources.Res
import mobi.giza.compose.resources.a_post_needs_something_to_say
import mobi.giza.compose.resources.add_post
import mobi.giza.compose.resources.could_not_ask_the_group_to_sign_this_post
import mobi.giza.compose.resources.propose_post
import mobi.giza.compose.resources.the_group_signs_a_post_so_it_takes_a_quorum
import mobi.giza.compose.resources.this_group_has_no_shared_key_to_sign_a_post
import mobi.giza.compose.resources.what_should_the_group_say
import org.jetbrains.compose.resources.stringResource
/**
* The draft of something the group would say, in its own name.
*
* Not a message in the room: this is signed by the group's key and read by anybody,
* which is why the byline over the draft is the group's rather than the writer's.
* Whoever types it is proposing words for the group to put its signature to, and
* the words become a post -- attributed to the group and to nobody in particular --
* only once a quorum has signed.
*
* The byline is the whole of the design here. Every other composer in this app puts
* the author's name over the draft because the author is the person typing; getting
* that wrong on this screen would have a member writing what looks like their own
* note and finding the group had said it.
*/
@OptIn(ExperimentalMaterial3ExpressiveApi::class, ExperimentalMaterial3Api::class)
@Composable
fun AddGroupPostScreen(
activeUserPublicKey: HexKey,
chatRoomId: String,
relayHint: String?,
initialAddGroupPostUIState: AddGroupPostUIState = AddGroupPostUIState.Loading,
chatRepository: ChatRepository,
frostSigningRepository: FrostSigningRepository,
onNavigateToRouteAndPopUpInclusive: (Route) -> Unit,
onNavigateBack: () -> Unit,
) {
val addGroupPostViewModel: AddGroupPostViewModel = viewModel(
factory = AddGroupPostViewModel.factory(
chatRoomId = chatRoomId,
relayHint = relayHint,
initialAddGroupPostUIState = initialAddGroupPostUIState,
activeUserPublicKey = activeUserPublicKey,
chatRepository = chatRepository,
frostSigningRepository = frostSigningRepository
)
)
// Read out here rather than in the click handler: both are composable and an
// onClick lambda is not. The scope is the caller's so a message survives this
// screen being replaced by the signing session.
val notify = rememberNotifier(rememberCoroutineScope())
val couldNotPropose = stringResource(Res.string.could_not_ask_the_group_to_sign_this_post)
val nothingToSay = stringResource(Res.string.a_post_needs_something_to_say)
ScreenStateTransition(addGroupPostViewModel.addGroupPostUIState) { uiState ->
when (val addGroupPostUIState = uiState) {
is AddGroupPostUIState.Error -> {
// Nothing to retry: the room came from a navigation argument, and
// reading it again with the same one fails the same way.
ErrorState(message = addGroupPostUIState.message, onRetry = null)
}
is AddGroupPostUIState.Loaded -> {
val noteFieldState = rememberTextFieldState()
// The room and this device's share, and the identity: an npub-only
// profile cannot send the proposal at all. docs/npub-sign-in.md.
val canSign = addGroupPostUIState.canSign && LocalCanSign.current
// imePadding: this screen is a text field the size of the screen,
// and without it the software keyboard covers what is being typed.
Scaffold(
snackbarHost = { SnackbarHost(LocalSnackbarHostState.current) },
modifier = Modifier.imePadding(),
topBar = {
TopAppBar(
title = { Text(text = stringResource(Res.string.add_post)) },
navigationIcon = { NavigateBackButton(onNavigateBack) },
)
},
bottomBar = {
BottomAppBar(
actions = {},
floatingActionButton = {
ExtendedFab(
label = stringResource(Res.string.propose_post),
icon = { Icon(Icons.Default.Draw, contentDescription = "Propose post") },
enabled = canSign,
onClick = {
addGroupPostViewModel.proposePost(
localChatRoom = addGroupPostUIState.localChatRoom,
noteField = noteFieldState,
onSuccess = { sessionId ->
// Onto the session rather than back
// to the group. Nothing has been
// posted yet -- the post appears
// when a quorum signs -- so landing
// on the list it is not in would
// read as a failure.
onNavigateToRouteAndPopUpInclusive.invoke(
FrostSigningRoute(
activeUserPublicKey = activeUserPublicKey,
chatRoomId = chatRoomId,
sessionId = sessionId
)
)
},
// Both stay on the draft. One is an
// empty note and one is a failure to
// propose, and neither is a reason to
// throw away what was written.
onEmpty = { notify(nothingToSay) },
onFailure = { notify(couldNotPropose) }
)
}
)
}
)
}
) { innerPadding ->
Column(
modifier = Modifier.padding(innerPadding).readableContent().fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap),
horizontalAlignment = Alignment.CenterHorizontally
) {
// Whose name this goes out in, said before a word is typed.
// It is the group's, and it is the one thing about this
// screen a member could reasonably get wrong.
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
ProfileAvatar(
size = MaterialTheme.spacing.space500,
publicKey = chatRoomId,
picture = addGroupPostUIState.groupNostrProfile?.picture(),
name = addGroupPostUIState.groupNostrProfile?.name()
)
Spacer(modifier = Modifier.width(MaterialTheme.spacing.itemGap))
Text(
text = addGroupPostUIState.groupNostrProfile?.name()
?: addGroupPostUIState.localChatRoom.chatRoom.subject
?: chatRoomId.take(16),
style = MaterialTheme.typography.titleSmall,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
}
Text(
text = stringResource(
Res.string.the_group_signs_a_post_so_it_takes_a_quorum
),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.onSurfaceVariant,
textAlign = TextAlign.Center
)
if (!addGroupPostUIState.canSign) {
Text(
text = stringResource(
Res.string.this_group_has_no_shared_key_to_sign_a_post
),
style = MaterialTheme.typography.bodySmall,
color = MaterialTheme.colorScheme.error,
textAlign = TextAlign.Center
)
}
OutlinedTextField(
modifier = Modifier.fillMaxWidth().weight(1f)
.background(BottomAppBarDefaults.containerColor),
state = noteFieldState,
// The draft grows into the screen rather than scrolling
// inside three lines: a post is the one thing here
// somebody writes at length, and a composer that hides
// what was written two sentences ago is a composer
// nobody proofreads in.
lineLimits = TextFieldLineLimits.MultiLine(),
colors = OutlinedTextFieldDefaults.colors(
focusedBorderColor = Color.Transparent,
unfocusedBorderColor = Color.Transparent,
disabledBorderColor = Color.Transparent
),
placeholder = {
Text(stringResource(Res.string.what_should_the_group_say))
}
)
}
}
}
AddGroupPostUIState.Loading -> {
Column(
modifier = Modifier.fillMaxWidth().padding(MaterialTheme.spacing.screenMargin),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.sectionGap)
) {
Spacer(modifier = Modifier.height(MaterialTheme.spacing.emphasisGap))
Text(
text = stringResource(Res.string.add_post),
style = MaterialTheme.typography.bodyLarge,
textAlign = TextAlign.Center
)
LoadingDataIndicator(fillScreen = false)
}
}
}
}
LaunchedEffect(true) {
if (initialAddGroupPostUIState == AddGroupPostUIState.Loading) {
addGroupPostViewModel.initiateAddGroupPost()
}
}
}
@ConformancePreviews
@Composable
private fun AddGroupPostScreenPreview() {
GizaTheme {
Surface(modifier = Modifier.fillMaxSize()) {
AddGroupPostScreen(
activeUserPublicKey = "",
chatRoomId = "publicKey",
relayHint = null,
initialAddGroupPostUIState = AddGroupPostUIState.Loaded(
localChatRoom = LocalChatRoom(
chatRoom = ChatRoom(
id = "publicKey",
userPublicKey = "",
subject = "Translation room",
description = "A group translating hard books.",
initialGiftWrapPayloadId = "sdfaer",
mlsGroupState = "state"
),
),
// A group that can sign, so the composer renders in the state a
// member actually meets it in rather than greyed out.
canSign = true
),
chatRepository = ChatRepository.NO_OP_CHAT_REPOSITORY,
frostSigningRepository = FrostSigningRepository.NO_OP_FROST_SIGNING_REPOSITORY,
onNavigateToRouteAndPopUpInclusive = {},
onNavigateBack = {},
)
}
}
}
@@ -1,5 +1,11 @@
package mobi.giza.compose.ui.composable
import mobi.giza.compose.resources.n_posts
import mobi.giza.compose.resources.one_post
import mobi.giza.compose.resources.no_posts_yet
import mobi.giza.compose.resources.posts
import mobi.giza.compose.ui.composable.navigation.routes.GroupPostsRoute
import androidx.compose.material.icons.filled.Campaign
import mobi.giza.compose.resources.not_described_yet
import mobi.giza.compose.resources.no_name_on_this_profile
import mobi.giza.compose.resources.nostr_profile
@@ -651,8 +657,9 @@ fun ChatRoomDetailScreen(
HorizontalDivider()
}
// What the group says about itself on nostr, as the group
// signed it (docs/group-nostr-profile-and-posts.md). A
// What the group says about itself on nostr and what it has
// posted, as the group signed them
// (docs/group-nostr-profile-and-posts.md). A
// Marmot room only: the room's id is the key it signs as,
// and a NIP-17 room has no key of its own to describe.
// Shown to every member -- the profile is worth reading
@@ -674,6 +681,22 @@ fun ChatRoomDetailScreen(
)
}
// What the group has said publicly, as itself.
item {
GroupPostsRow(
postCount = chatRoomDetailUIState.groupPosts.size,
onClick = {
onNavigateToRoute.invoke(
GroupPostsRoute(
activeUserPublicKey = activeUserPublicKey,
chatRoomId = chatRoomId,
relayHint = relayHint
)
)
}
)
}
item {
HorizontalDivider()
}
@@ -2004,3 +2027,40 @@ private fun GroupNostrProfileRow(
)
}
}
/**
* The row on the group's screen that leads to its posts, counting them. A count
* rather than the newest post: the posts screen is where they are read whole, and
* a quote here would be the words twice with the second copy cut short.
*/
@Composable
private fun GroupPostsRow(
postCount: Int,
onClick: () -> Unit,
) {
Card(
modifier = Modifier.fillMaxWidth(),
onClick = onClick
) {
ListItem(
leadingContent = {
Icon(Icons.Default.Campaign, contentDescription = Decorative)
},
trailingContent = {
Icon(Icons.Default.ChevronRight, contentDescription = Decorative)
},
headlineContent = {
Text(text = stringResource(Res.string.posts))
},
supportingContent = {
Text(
text = when (postCount) {
0 -> stringResource(Res.string.no_posts_yet)
1 -> stringResource(Res.string.one_post)
else -> stringResource(Res.string.n_posts, postCount)
}
)
}
)
}
}
@@ -0,0 +1,334 @@
package mobi.giza.compose.ui.composable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Campaign
import androidx.compose.material3.Card
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextOverflow
import androidx.lifecycle.viewmodel.compose.viewModel
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip10Notes.TextNoteEvent
import mobi.giza.compose.resources.Res
import mobi.giza.compose.resources.a_reply
import mobi.giza.compose.resources.add_post
import mobi.giza.compose.resources.posted_on
import mobi.giza.compose.resources.posts
import mobi.giza.compose.resources.this_device_holds_no_posts_for_this_group
import org.jetbrains.compose.resources.stringResource
import mobi.giza.compose.database.model.ChatRoom
import mobi.giza.compose.database.model.GroupSignedEvent
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.extensions.inComparableGroups
import mobi.giza.compose.extensions.toFormattedTimeAndDateString
import mobi.giza.compose.nostr.GroupNostrProfile
import mobi.giza.compose.nostr.GroupPost
import mobi.giza.compose.repository.ChatRepository
import mobi.giza.compose.ui.composable.navigation.routes.AddGroupPostRoute
import mobi.giza.compose.ui.composable.navigation.routes.Route
import mobi.giza.compose.ui.composable.widgets.Decorative
import mobi.giza.compose.ui.composable.widgets.EmptyState
import mobi.giza.compose.ui.composable.widgets.ErrorState
import mobi.giza.compose.ui.composable.widgets.LoadingDataIndicator
import mobi.giza.compose.ui.composable.widgets.LocalCanSign
import mobi.giza.compose.ui.composable.widgets.LocalSnackbarHostState
import mobi.giza.compose.ui.composable.widgets.NavigateBackButton
import mobi.giza.compose.ui.composable.widgets.ScreenStateTransition
import mobi.giza.compose.ui.composable.widgets.profile.ProfileAvatar
import mobi.giza.compose.ui.theme.ConformancePreviews
import mobi.giza.compose.ui.theme.GizaTheme
import mobi.giza.compose.ui.theme.readableContent
import mobi.giza.compose.ui.theme.spacing
import mobi.giza.compose.ui.view.model.GroupPostsViewModel
import mobi.giza.compose.ui.view.state.GroupPostsUIState
import kotlin.time.Instant
/**
* What the group has said publicly, as itself, newest first.
*
* Pushed from its row on the group's screen, which counts the posts and nothing
* more. This is where they are read: one card per post and -- for a member
* holding a share of the key -- the way into the composer. A device holding no
* posts says so, and offers the composer from the same place; a post signed
* before this member joined is not passed on (docs/group-nostr-profile-and-posts.md,
* D8), so "holds none" is all it can say.
*
* A post is the one thing the group signs that is meant to be *read* by people
* who were never in the room, so a member without a share reads it like
* anybody else and simply cannot propose the next one: proposing is a signature
* by the group, and `proposeSigningBatch` throws for a device holding no share.
*/
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun GroupPostsScreen(
activeUserPublicKey: HexKey,
chatRoomId: String,
relayHint: String?,
initialGroupPostsUIState: GroupPostsUIState = GroupPostsUIState.Loading,
chatRepository: ChatRepository,
onNavigateToRoute: (Route) -> Unit,
onNavigateBack: () -> Unit,
) {
val groupPostsViewModel: GroupPostsViewModel = viewModel(
factory = GroupPostsViewModel.factory(
chatRoomId = chatRoomId,
initialGroupPostsUIState = initialGroupPostsUIState,
chatRepository = chatRepository,
)
)
LaunchedEffect(true) {
if (initialGroupPostsUIState == GroupPostsUIState.Loading) {
groupPostsViewModel.initiate()
}
}
Scaffold(
snackbarHost = { SnackbarHost(LocalSnackbarHostState.current) },
topBar = {
TopAppBar(
title = {
Text(
text = stringResource(Res.string.posts),
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
},
navigationIcon = { NavigateBackButton(onNavigateBack) }
)
}
) { padding ->
ScreenStateTransition(groupPostsViewModel.groupPostsUIState) { uiState ->
when (val state = uiState) {
is GroupPostsUIState.Error -> {
// Nothing to retry: the room came from a navigation argument,
// and reading it again with the same one fails the same way.
ErrorState(
modifier = Modifier.padding(padding),
message = state.message,
onRetry = null
)
}
GroupPostsUIState.Loading -> {
Column(
modifier = Modifier.fillMaxWidth().padding(padding)
.padding(MaterialTheme.spacing.screenMargin),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.sectionGap)
) {
Spacer(modifier = Modifier.height(MaterialTheme.spacing.emphasisGap))
LoadingDataIndicator(fillScreen = false)
}
}
is GroupPostsUIState.Loaded -> {
// The room and this device's share, and the identity: an
// npub-only profile cannot send a proposal at all.
val canPropose = state.canSign && LocalCanSign.current
val add: () -> Unit = {
onNavigateToRoute.invoke(
AddGroupPostRoute(
activeUserPublicKey = activeUserPublicKey,
chatRoomId = chatRoomId,
relayHint = relayHint
)
)
}
LazyColumn(
modifier = Modifier.padding(padding).readableContent().fillMaxSize()
.padding(MaterialTheme.spacing.space250),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.space125)
) {
if (state.groupPosts.isEmpty()) {
item {
EmptyState(
message = stringResource(
Res.string.this_device_holds_no_posts_for_this_group
),
action = if (canPropose) {
{ AddPostButton(onClick = add) }
} else {
null
}
)
}
} else {
items(
items = state.groupPosts,
key = { post -> post.signedEvent.id }
) { post ->
GroupPostCard(
post = post,
publicKey = chatRoomId,
groupNostrProfile = state.groupNostrProfile
)
}
if (canPropose) {
item {
AddPostButton(onClick = add)
}
}
}
}
}
}
}
}
}
/**
* One thing the group said, as the group signed it.
*
* The whole note rather than a preview. A post is short by construction -- kind:1
* with no long-form arm behind it -- and truncating the one kind of content on this
* screen that exists to be *read* would mean opening something to see a paragraph.
*
* **The byline is the group's**, taken from its profile where it has one and from
* the room's own id where it has not. It is worth spelling out because a post is
* the one thing here a reader might otherwise attribute to whichever member
* proposed it -- and the point of signing it as the group is that they cannot.
*
* A reply is marked as one. Nothing in this app writes replies, so the mark is for
* an event that arrived some other way; drawing one as an announcement would put
* half a conversation on a group's page with no sign that it was half.
*/
@Composable
private fun GroupPostCard(
post: GroupPost,
publicKey: HexKey,
groupNostrProfile: GroupNostrProfile?
) {
Card(modifier = Modifier.fillMaxWidth()) {
Column(
modifier = Modifier.fillMaxWidth().padding(MaterialTheme.spacing.containerPadding),
verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap)
) {
Row(verticalAlignment = Alignment.CenterVertically) {
ProfileAvatar(
size = MaterialTheme.spacing.space400,
publicKey = publicKey,
picture = groupNostrProfile?.picture(),
name = groupNostrProfile?.name()
)
Spacer(modifier = Modifier.width(MaterialTheme.spacing.itemGap))
Column(modifier = Modifier.weight(1f)) {
Text(
text = groupNostrProfile?.name() ?: publicKey.take(16).inComparableGroups(),
style = MaterialTheme.typography.titleSmall,
maxLines = 1,
overflow = TextOverflow.Ellipsis
)
Text(
text = stringResource(
Res.string.posted_on,
post.postedAt.toFormattedTimeAndDateString()
),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
if (!post.isNewThread()) {
Text(
text = stringResource(Res.string.a_reply),
style = MaterialTheme.typography.labelSmall,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
Text(
text = post.content,
style = MaterialTheme.typography.bodyMedium
)
}
}
}
/** The way into `AddGroupPostScreen`. The icon is decorative because the label is beside it. */
@Composable
private fun AddPostButton(onClick: () -> Unit) {
TextButton(onClick = onClick) {
Icon(Icons.Default.Campaign, contentDescription = Decorative)
Spacer(modifier = Modifier.width(MaterialTheme.spacing.space125))
Text(stringResource(Res.string.add_post))
}
}
@ConformancePreviews
@Composable
private fun GroupPostsScreenPreview() {
val chatRoomId = "d".repeat(64)
GizaTheme {
Surface(modifier = Modifier.fillMaxSize()) {
GroupPostsScreen(
activeUserPublicKey = "a".repeat(64),
chatRoomId = chatRoomId,
relayHint = null,
initialGroupPostsUIState = GroupPostsUIState.Loaded(
localChatRoom = LocalChatRoom(
chatRoom = ChatRoom(
id = chatRoomId,
userPublicKey = "a".repeat(64),
subject = "Translation room",
description = null,
initialGiftWrapPayloadId = "sdfaer",
mlsGroupState = "state"
)
),
groupPosts = listOf(
GroupPost(
signedEvent = GroupSignedEvent(
id = "b".repeat(64),
chatRoomId = chatRoomId,
publicKey = chatRoomId,
kind = TextNoteEvent.KIND,
tags = emptyArray(),
content = "The first chapter is out. Read it, and tell us where it reads wrong.",
signature = "f".repeat(128),
createdAt = Instant.fromEpochSeconds(1_700_000_000)
)
)
),
canSign = true
),
chatRepository = ChatRepository.NO_OP_CHAT_REPOSITORY,
onNavigateToRoute = {},
onNavigateBack = {},
)
}
}
}
@@ -1,5 +1,9 @@
package mobi.giza.compose.ui.composable.navigation
import mobi.giza.compose.ui.composable.navigation.routes.AddGroupPostRoute
import mobi.giza.compose.ui.composable.navigation.routes.GroupPostsRoute
import mobi.giza.compose.ui.composable.AddGroupPostScreen
import mobi.giza.compose.ui.composable.GroupPostsScreen
import mobi.giza.compose.ui.composable.navigation.routes.EditGroupNostrProfileRoute
import mobi.giza.compose.ui.composable.navigation.routes.GroupNostrProfileRoute
import mobi.giza.compose.ui.composable.EditGroupNostrProfileScreen
@@ -1268,6 +1272,46 @@ fun GizaNavHost(
}
)
}
// The group's posts: docs/group-nostr-profile-and-posts.md.
composable<GroupPostsRoute> { backStackEntry ->
val route = backStackEntry.toRoute<GroupPostsRoute>()
GroupPostsScreen(
activeUserPublicKey = route.activeUserPublicKey,
chatRoomId = route.chatRoomId,
relayHint = route.relayHint,
chatRepository = databaseChatRepository,
onNavigateToRoute = { actionRoute ->
navController.navigate(route = actionRoute)
},
onNavigateBack = {
navController.popBackStack()
}
)
}
composable<AddGroupPostRoute> { backStackEntry ->
val route = backStackEntry.toRoute<AddGroupPostRoute>()
AddGroupPostScreen(
activeUserPublicKey = route.activeUserPublicKey,
chatRoomId = route.chatRoomId,
relayHint = route.relayHint,
chatRepository = databaseChatRepository,
frostSigningRepository = databaseFrostSigningRepository,
onNavigateToRouteAndPopUpInclusive = { signingRoute ->
// Replace the composer so back returns to the posts rather
// than to a draft whose proposal has already gone out.
navController.navigate(route = signingRoute) {
popUpTo<AddGroupPostRoute> {
inclusive = true
}
}
},
onNavigateBack = {
navController.popBackStack()
}
)
}
composable<ImplementationPendingRoute> { backStackEntry ->
val route: ImplementationPendingRoute = backStackEntry.toRoute()
@@ -0,0 +1,11 @@
package mobi.giza.compose.ui.composable.navigation.routes
import kotlinx.serialization.Serializable
/** The composer that proposes a post for the group's quorum to sign as itself. */
@Serializable
data class AddGroupPostRoute(
val activeUserPublicKey: String,
val chatRoomId: String, // TODO: have this as a publicKey
val relayHint: String?
): Route()
@@ -0,0 +1,15 @@
package mobi.giza.compose.ui.composable.navigation.routes
import kotlinx.serialization.Serializable
/**
* What the group has said publicly, as itself, newest first, with the way into
* the composer for a member holding a share of the key.
* docs/group-nostr-profile-and-posts.md.
*/
@Serializable
data class GroupPostsRoute(
val activeUserPublicKey: String,
val chatRoomId: String, // TODO: have this as a publicKey
val relayHint: String?
): Route()
@@ -0,0 +1,156 @@
package mobi.giza.compose.ui.view.model
import androidx.compose.foundation.text.input.TextFieldState
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import co.touchlab.kermit.Logger
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.managers.GroupStatementRefusal
import mobi.giza.compose.nostr.GroupPost
import mobi.giza.compose.repository.ChatRepository
import mobi.giza.compose.repository.FrostSigningRepository
import mobi.giza.compose.ui.view.state.AddGroupPostUIState
import com.vitorpamplona.quartz.nip01Core.core.HexKey
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.launch
/**
* Asks the group to say something publicly, in its own name.
*
* The post is not written here and does not exist yet. What goes out is a proposal
* to sign one, and the post appears -- on every member's device at once, authored by
* the room's own key rather than by whoever typed it -- when enough members have
* signed. That author is the room's id: signing runs at the path the room was
* derived at, so a post says which group made it simply by being signed.
*
* That is the difference from a message in the room. A message is a member speaking
* where the group can hear; this is the group speaking where anyone can, and it
* takes a quorum to say.
*/
class AddGroupPostViewModel(
val chatRoomId: String,
val activeUserPublicKey: HexKey,
val relayHint: String?,
initialAddGroupPostUIState: AddGroupPostUIState,
val chatRepository: ChatRepository,
val frostSigningRepository: FrostSigningRepository,
): ViewModel() {
var addGroupPostUIState: AddGroupPostUIState by mutableStateOf(initialAddGroupPostUIState)
private set
private val logger = Logger.withTag(TAG)
val isActionPending: MutableState<Boolean> = mutableStateOf(false)
fun initiateAddGroupPost() {
viewModelScope.launch(Dispatchers.IO) {
val localChatRoom = chatRepository.getChatRoomByIdentifier(chatRoomId)
addGroupPostUIState = if (localChatRoom == null) {
AddGroupPostUIState.Error("Couldn't find the chat room")
} else {
AddGroupPostUIState.Loaded(
localChatRoom = localChatRoom,
groupNostrProfile = chatRepository.groupNostrProfile(chatRoomId),
// The same condition the group detail screen gates the entry
// point on.
canSign = GroupStatementCapability.roomIsOpen(localChatRoom) &&
frostSigningRepository.canSign(chatRoomId),
)
}
}
}
/**
* Opens a session over one kind:1 authored by the group.
*
* Refuses a blank draft rather than proposing it: a quorum signing an empty note
* would put a post on the record that renders as nothing and cannot be told from
* a broken one -- and kind:1 is not replaceable, so there is no un-saying it.
* `GroupPost.newestFirstAmong` drops blanks on the way back out too, which is
* the second line of the same defence.
*
* The draft is not cleared on success. This screen is replaced by the signing
* session, and on the failure path the words are the thing worth keeping.
*/
fun proposePost(
localChatRoom: LocalChatRoom,
noteField: TextFieldState,
onSuccess: (sessionId: String) -> Unit,
onEmpty: () -> Unit,
onFailure: () -> Unit
) {
// Guard against double submits. Posts accumulate rather than replace, so a
// second session over the same words is a second post on the record forever.
if (isActionPending.value) return
val note = noteField.text.toString().trim()
// The line every member's device declines on too, so a proposal this
// composer sends is never one the group refuses. GroupStatementRefusal.
if (GroupStatementRefusal.isBlankPost(note)) {
onEmpty.invoke()
return
}
isActionPending.value = true
val template = GroupPost.template(note)
viewModelScope.launch(Dispatchers.IO) {
val session = runCatching {
frostSigningRepository.proposeSigning(
localChatRoom = localChatRoom,
userPublicKey = activeUserPublicKey,
kind = template.kind,
tags = template.tags,
content = template.content,
)
}.onFailure { error ->
logger.e("Failed to propose a post for $chatRoomId", error)
}.getOrNull()
viewModelScope.launch(Dispatchers.Main) {
if (session != null) {
onSuccess.invoke(session.id)
} else {
onFailure.invoke()
}
}
isActionPending.value = false
}
}
companion object {
private const val TAG = "AddGroupPostViewModel"
fun factory(
activeUserPublicKey: HexKey,
chatRoomId: String,
relayHint: String?,
initialAddGroupPostUIState: AddGroupPostUIState = AddGroupPostUIState.Loading,
chatRepository: ChatRepository,
frostSigningRepository: FrostSigningRepository
): ViewModelProvider.Factory = viewModelFactory {
initializer {
AddGroupPostViewModel(
activeUserPublicKey = activeUserPublicKey,
chatRoomId = chatRoomId,
relayHint = relayHint,
initialAddGroupPostUIState = initialAddGroupPostUIState,
chatRepository = chatRepository,
frostSigningRepository = frostSigningRepository
)
}
}
}
}
@@ -131,6 +131,7 @@ class ChatRoomDetailViewModel(
parentChatRoomId = chatRepository.parentOf(chatRoomId),
canAddSubgroup = canSign && !subgroupsNeedAWallet,
groupNostrProfile = chatRepository.groupNostrProfile(chatRoomId),
groupPosts = chatRepository.groupPosts(chatRoomId),
canProposeAsGroup = canSign && roomIsOpen,
subgroupsNeedAWallet = canSign && subgroupsNeedAWallet,
canGovern = canGovern,
@@ -0,0 +1,64 @@
package mobi.giza.compose.ui.view.model
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import kotlinx.coroutines.launch
import mobi.giza.compose.repository.ChatRepository
import mobi.giza.compose.ui.view.state.GroupPostsUIState
/**
* The group's posts, read once for the screen that shows them, with the
* profile that bylines them. One read rather than a watch, for the reason
* `GroupNostrProfileViewModel` gives.
*/
class GroupPostsViewModel(
val chatRoomId: String,
initialGroupPostsUIState: GroupPostsUIState,
val chatRepository: ChatRepository,
): ViewModel() {
var groupPostsUIState: GroupPostsUIState by mutableStateOf(initialGroupPostsUIState)
private set
fun initiate() {
viewModelScope.launch(Dispatchers.IO) {
val localChatRoom = chatRepository.getChatRoomByIdentifier(chatRoomId)
groupPostsUIState = if (localChatRoom == null) {
GroupPostsUIState.Error("Couldn't find the group")
} else {
GroupPostsUIState.Loaded(
localChatRoom = localChatRoom,
groupPosts = chatRepository.groupPosts(chatRoomId),
groupNostrProfile = chatRepository.groupNostrProfile(chatRoomId),
canSign = GroupStatementCapability.roomIsOpen(localChatRoom) &&
chatRepository.canSign(chatRoomId),
)
}
}
}
companion object {
fun factory(
chatRoomId: String,
initialGroupPostsUIState: GroupPostsUIState = GroupPostsUIState.Loading,
chatRepository: ChatRepository,
): ViewModelProvider.Factory = viewModelFactory {
initializer {
GroupPostsViewModel(
chatRoomId = chatRoomId,
initialGroupPostsUIState = initialGroupPostsUIState,
chatRepository = chatRepository,
)
}
}
}
}
@@ -0,0 +1,33 @@
package mobi.giza.compose.ui.view.state
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.nostr.GroupNostrProfile
sealed interface AddGroupPostUIState {
data class Loaded(
val localChatRoom: LocalChatRoom,
/**
* Who the group says it is, for the byline over the draft.
*
* A post is signed by the group and read as the group, so the composer
* shows whose voice is being written in -- which is not the member typing.
* Null in a group that has not described itself, and the byline falls back
* to the room.
*/
val groupNostrProfile: GroupNostrProfile? = null,
/**
* Whether this device holds a share of the group's key.
*
* False leaves the draft writable and the button inert, for the same reason
* the other two editors do: reading and composing cost nothing, and only a
* share-holder can open the session that would sign it.
*/
val canSign: Boolean = false,
): AddGroupPostUIState
data class Error(
val message: String
): AddGroupPostUIState
data object Loading: AddGroupPostUIState
}
@@ -6,6 +6,7 @@ import mobi.giza.compose.managers.SubgroupManager
import mobi.giza.compose.database.model.GroupSignedEvent
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.nostr.GroupNostrProfile
import mobi.giza.compose.nostr.GroupPost
sealed interface ChatRoomDetailUIState {
data class Loaded(
@@ -66,6 +67,11 @@ sealed interface ChatRoomDetailUIState {
* none. docs/group-nostr-profile-and-posts.md.
*/
val groupNostrProfile: GroupNostrProfile? = null,
/**
* The kind:1s the group signed as itself, newest first, as many as this
* device holds. docs/group-nostr-profile-and-posts.md.
*/
val groupPosts: List<GroupPost> = emptyList(),
/**
* Whether this device could put a profile or a post to the group's quorum:
* a Marmot room it has not left, not disbanded, where it holds a share of
@@ -0,0 +1,40 @@
package mobi.giza.compose.ui.view.state
import mobi.giza.compose.database.model.intermdiate.LocalChatRoom
import mobi.giza.compose.nostr.GroupNostrProfile
import mobi.giza.compose.nostr.GroupPost
sealed interface GroupPostsUIState {
data class Loaded(
val localChatRoom: LocalChatRoom,
/**
* What the group has said publicly, as itself, newest first. Empty in
* a group that has said nothing, which is every group until somebody
* proposes a post.
*/
val groupPosts: List<GroupPost> = emptyList(),
/**
* The group's profile, for the byline on every post.
*
* Read alongside the posts because a post is the one thing here a
* reader might otherwise attribute to whichever member proposed it,
* and the group's own name and picture are what say it was the group.
* Null where the group has not described itself, and the byline falls
* back to the key.
*/
val groupNostrProfile: GroupNostrProfile? = null,
/**
* Whether this device holds a share of the group's key.
*
* False leaves the posts readable and hides the way into the composer,
* for the reason `GroupNostrProfileUIState.Loaded.canSign` gives.
*/
val canSign: Boolean = false,
): GroupPostsUIState
data class Error(
val message: String
): GroupPostsUIState
data object Loading: GroupPostsUIState
}