From 8e502aa857b20b5982eaa265fc9a1c8a866c3a9b Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sun, 13 Sep 2026 20:25:37 +0200 Subject: [PATCH] feat(groups): a new curated schema starts from one of curare.to's templates "Add schema" opened the editor on the two fields every list has and left the member to build the rest. curare.to's /new page does it the other way round: a "Start a list" picker of four shapes -- links & text, discussion, catalogue, custom -- and an editor seeded from the one chosen. The button now pushes SelectCuratedSchemaTemplateScreen, that picker on a phone, and a card pushes the editor with the template's key, the same word the site takes as ?template=. Back from the editor is the picker and another shape; when a new list's proposal goes out the signing session replaces both. CuratedSchemaTemplate is lib/protocol/templates.ts field for field, with CuratedDeriveRule for the [rule:] mark the site records in the identifier field's hint -- its own convention, not the NIP's, and the thing that lets that site's form derive an entry's d for a list the group starts here. The hint is written byte for byte as the site writes it; the app still composes no suggestions and applies no rule. Null or an unknown key seeds Custom, which is the bare two fields the editor opened on before, so a screen handed no template behaves as it did. The picker's one departure from the site: its cards list a template's fields by label, the way the group's schema cards do, rather than counting them -- what an entry asks for is what the choice is made on. Co-Authored-By: Claude Opus 5 --- .../composeResources/values/strings.xml | 12 + .../nostr/curated/CuratedSchemaTemplate.kt | 262 ++++++++++++++++++ .../EditGroupCuratedSchemaScreen.kt | 19 +- .../composable/GroupCuratedSchemasScreen.kt | 36 ++- .../SelectCuratedSchemaTemplateScreen.kt | 248 +++++++++++++++++ .../ui/composable/navigation/CurareNavHost.kt | 35 ++- .../routes/EditGroupCuratedSchemaRoute.kt | 9 +- .../SelectCuratedSchemaTemplateRoute.kt | 19 ++ .../model/EditGroupCuratedSchemaViewModel.kt | 32 ++- .../curated/CuratedSchemaTemplateTest.kt | 155 +++++++++++ .../compose/ui/StringCatalogueJvmTest.kt | 10 + .../EditGroupCuratedSchemaScreenJvmTest.kt | 41 ++- .../GroupCuratedSchemasScreenJvmTest.kt | 20 +- ...electCuratedSchemaTemplateScreenJvmTest.kt | 115 ++++++++ .../EditGroupCuratedSchemaViewModelJvmTest.kt | 47 +++- 15 files changed, 1024 insertions(+), 36 deletions(-) create mode 100644 composeApp/src/commonMain/kotlin/to/curare/compose/nostr/curated/CuratedSchemaTemplate.kt create mode 100644 composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/SelectCuratedSchemaTemplateScreen.kt create mode 100644 composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/routes/SelectCuratedSchemaTemplateRoute.kt create mode 100644 composeApp/src/commonTest/kotlin/to/curare/compose/nostr/curated/CuratedSchemaTemplateTest.kt create mode 100644 composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/SelectCuratedSchemaTemplateScreenJvmTest.kt diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index adb1181e..6dc9f7ff 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -471,6 +471,18 @@ Edit schema Propose schema Fields: %1$s + + Start a list + A list is a schema: what an entry may contain, who may suggest one, and where it lives. The group signs it with its shared key, and only that key can put an entry on the list. + Pick a shape to start from. Everything in it can be changed before the group signs. + Links & text + Reddit’s default: a title with a link or some text, a picture if you like, and a flair. + Discussion + Text posts only: a title and a body, with a flair. + Catalogue + bitcoin.mov’s shape: things with a year, a type, an external id and links — one entry per thing however often it is suggested. + Custom + Just the two fields every list has — an identifier and a title — and whatever you add. Public Closed Private diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/nostr/curated/CuratedSchemaTemplate.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/nostr/curated/CuratedSchemaTemplate.kt new file mode 100644 index 00000000..3ed558c7 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/nostr/curated/CuratedSchemaTemplate.kt @@ -0,0 +1,262 @@ +package to.curare.compose.nostr.curated + +/** + * How a client works an entry's `d` out of the entry itself. + * + * The NIP marks the identifier field `derived` and leaves the derivation to the + * publishing client, which is fine for a site with one list and one rule -- + * bitcoin.mov's -- and useless for a list started from a template, whose + * entries will be composed by clients that have never seen its code. So + * curare.to records the rule *in the schema*: a machine-readable mark, + * `[rule:]`, at the end of a human sentence in the identifier field's + * hint. A client that knows the rule applies it and one that does not can at + * least read what it is; one that finds no mark falls back to [Title]. + * + * This app composes no entries and applies none of these. It writes the mark + * so that a list the group starts here is one that site's form can post to + * with the right identifier -- the same link one post whoever submits it, + * say -- rather than with whatever its fallback happens to be. + * + * The keys, the wording and the shape of the hint are `lib/protocol/derive.ts` + * in the curare.to repository, byte for byte, so that a template started here + * and one started there produce the same schema. + */ +enum class CuratedDeriveRule( + /** The word in the mark. */ + val key: String, + /** The rule as the hint says it, after "Derived by the client — ". */ + private val phrase: String, +) { + /** + * A post with a link is identified by the link, so the same link is one + * post whoever submits it; a text post by its title and its author. + */ + LinkOrTitle("link-or-title", "the link, else the title and author"), + + /** Two people's same-titled posts are two posts; one person's is an edit. */ + TitleAuthor("title-author", "the title and author"), + + /** + * A catalogue entry with an external id (`imdb:tt…`) is one entry however + * often it is suggested; otherwise the title and year. + */ + ExternalOrTitleYear("external-or-title-year", "the external id, else the title and year"), + + /** The same title is the same post, whoever posts it. */ + Title("title", "the title"), + ; + + /** The hint that records this rule on an identifier field. */ + fun hint(): String = "Derived by the client — $phrase. [rule:$key]" +} + +/** + * The shapes a list can start from: curare.to's templates, the ones its + * "Start a list" page offers, so that a list started in this app and one + * started on that site are the same kind of thing. + * + * Every template carries the two fields every list has -- the identifier, + * derived and with its [rule] recorded in its hint, and the title -- and + * declares no derived `t` field: the hashtag rule is bitcoin.mov's own, and a + * list is its own topic. Flair is the sub-topic. + * + * The field names, labels, placeholders and configs are signed into the + * schema and read by other clients, which is why they are plain strings here + * rather than resources: they are protocol data, not this app's copy. What + * the picker *calls* a template is UI text and lives in the catalogue. + * + * The fields are `lib/protocol/templates.ts` in the curare.to repository, + * field for field. A template added there belongs here too. + */ +enum class CuratedSchemaTemplate( + /** The stable key, the one curare.to's editor takes as `?template=`. */ + val key: String, + val rule: CuratedDeriveRule, + val fields: List, + val requireAny: List> = emptyList(), +) { + /** Reddit's default: a title with a link or some text, a picture, and a flair. */ + Links( + key = "links", + rule = CuratedDeriveRule.LinkOrTitle, + fields = listOf( + identifier(CuratedDeriveRule.LinkOrTitle), + title(placeholder = "What is it?"), + CuratedField( + name = "link", + type = CuratedFieldType.Url, + isRequired = false, + placeholder = "https://…", + label = "Link", + config = CuratedFieldConfig(tag = "r", marker = "link", max = 500), + ), + CuratedField( + name = "body", + type = CuratedFieldType.LongText, + isRequired = false, + placeholder = "Say more, or nothing.", + label = "Text", + config = CuratedFieldConfig(tag = CuratedFieldConfig.CONTENT_TAG, max = 10_000), + ), + CuratedField( + name = "image", + type = CuratedFieldType.Image, + isRequired = false, + placeholder = "https://…/picture.jpg", + label = "Picture", + config = CuratedFieldConfig(max = 500, hint = "An https link to an image."), + ), + flair("discussion", "news", "question"), + ), + requireAny = listOf(listOf("link", "body")), + ), + + /** Text posts only: a title and a body, with a flair. */ + Discussion( + key = "discussion", + rule = CuratedDeriveRule.TitleAuthor, + fields = listOf( + identifier(CuratedDeriveRule.TitleAuthor), + title(placeholder = "What do you want to talk about?"), + CuratedField( + name = "body", + type = CuratedFieldType.LongText, + isRequired = true, + label = "Text", + config = CuratedFieldConfig(tag = CuratedFieldConfig.CONTENT_TAG, max = 10_000), + ), + flair("question", "meta"), + ), + ), + + /** + * bitcoin.mov's shape, generalised: things with a year, a type, an + * external id and links, one entry per thing however often it is suggested. + */ + Catalogue( + key = "catalogue", + rule = CuratedDeriveRule.ExternalOrTitleYear, + fields = listOf( + identifier(CuratedDeriveRule.ExternalOrTitleYear), + title(placeholder = "The thing’s name"), + CuratedField( + name = "year", + type = CuratedFieldType.Year, + isRequired = false, + placeholder = "2014", + label = "Year", + config = CuratedFieldConfig(min = 1900, max = 2100), + ), + CuratedField( + name = "type", + type = CuratedFieldType.Enum, + isRequired = true, + label = "Type", + config = CuratedFieldConfig( + options = listOf("movie", "documentary", "short", "series", "book", "other"), + ), + ), + CuratedField( + name = "externalId", + type = CuratedFieldType.Token, + isRequired = false, + placeholder = "imdb:tt2821314", + label = "External id", + config = CuratedFieldConfig( + tag = "i", + max = 60, + hint = "Helps dedupe, e.g. imdb:tt2821314 or isbn:…", + ), + ), + CuratedField( + name = "watchUrl", + type = CuratedFieldType.Url, + isRequired = false, + placeholder = "https://…", + label = "Where to see it", + config = CuratedFieldConfig(tag = "r", marker = "watch", max = 500, repeat = true), + ), + CuratedField( + name = "referenceUrl", + type = CuratedFieldType.Url, + isRequired = false, + placeholder = "https://…", + label = "Reference", + config = CuratedFieldConfig(tag = "r", marker = "reference", max = 500), + ), + CuratedField( + name = "image", + type = CuratedFieldType.Image, + isRequired = false, + placeholder = "https://…/poster.jpg", + label = "Picture", + config = CuratedFieldConfig(max = 500), + ), + CuratedField( + name = "lang", + type = CuratedFieldType.Token, + isRequired = false, + placeholder = "en", + label = "Language", + config = CuratedFieldConfig(max = 60, pattern = "[A-Za-z]{2,8}([-_][A-Za-z0-9]{2,8})*"), + ), + CuratedField( + name = "description", + type = CuratedFieldType.LongText, + isRequired = false, + placeholder = "Why is it worth a look?", + label = "Description", + config = CuratedFieldConfig(tag = CuratedFieldConfig.CONTENT_TAG, max = 4000), + ), + ), + requireAny = listOf(listOf("watchUrl", "referenceUrl")), + ), + + /** Just the two fields every list has, and whatever the group adds. */ + Custom( + key = "custom", + rule = CuratedDeriveRule.Title, + fields = listOf(identifier(CuratedDeriveRule.Title), title(placeholder = "")), + ), + ; + + companion object { + /** + * The template with [key], or [Custom] for null and for a key this build + * does not know. + * + * Custom rather than the first, which is what curare.to falls back to, + * because Custom is the two fields every list has and nothing else: the + * one shape that cannot presume anything about a list it was not asked + * for. Null is what an edit carries, where the template is meaningless, + * and what a screen handed a loaded state without one gets. + */ + fun byKey(key: String?): CuratedSchemaTemplate = entries.firstOrNull { it.key == key } ?: Custom + } +} + +// The fields the templates share, as functions of the file rather than of the +// companion: an enum's entries are built before its companion is, so they +// cannot call into it. + +/** + * The identifier field every template starts with: [CuratedField.IDENTIFIER] + * with the rule recorded in its hint. + */ +private fun identifier(rule: CuratedDeriveRule): CuratedField = + CuratedField.IDENTIFIER.copy( + config = CuratedField.IDENTIFIER.config.copy(hint = rule.hint()), + ) + +/** The title field every template starts with: [CuratedField.TITLE] with an example. */ +private fun title(placeholder: String): CuratedField = + CuratedField.TITLE.copy(placeholder = placeholder) + +/** A sub-topic an entry may pick one of. */ +private fun flair(vararg options: String): CuratedField = CuratedField( + name = "flair", + type = CuratedFieldType.Enum, + isRequired = false, + label = "Flair", + config = CuratedFieldConfig(options = options.toList()), +) diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/EditGroupCuratedSchemaScreen.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/EditGroupCuratedSchemaScreen.kt index 84ba4dd9..836dedf2 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/EditGroupCuratedSchemaScreen.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/EditGroupCuratedSchemaScreen.kt @@ -92,6 +92,7 @@ import to.curare.compose.nostr.curated.CuratedField import to.curare.compose.nostr.curated.CuratedFieldType import to.curare.compose.nostr.curated.CuratedSchemaEvent import to.curare.compose.nostr.curated.CuratedSchemaProblem +import to.curare.compose.nostr.curated.CuratedSchemaTemplate import to.curare.compose.nostr.curated.CuratedVisibility import to.curare.compose.repository.ChatRepository import to.curare.compose.repository.FrostSigningRepository @@ -250,6 +251,12 @@ import org.jetbrains.compose.resources.stringResource * suggestion to the list replies to, so changing it would not edit the list but * start a second one and orphan the first's queue. The field is read-only on an * edit and says why; a new list is "Add schema" on the group's screen. + * + * **A new list starts from a template.** "Add schema" opens + * `SelectCuratedSchemaTemplateScreen` first, and the shape chosen there arrives + * as [template] -- the fields and rules the form is seeded with, in place of the + * bare two every list has. The form is the same either way: a template is a + * starting point, and everything in it can be changed before the group signs. */ @OptIn( ExperimentalMaterial3ExpressiveApi::class, @@ -262,6 +269,8 @@ fun EditGroupCuratedSchemaScreen( chatRoomId: String, relayHint: String?, identifier: String?, + /** The key of the `CuratedSchemaTemplate` a new schema starts from. Ignored on an edit. */ + template: String? = null, initialEditGroupCuratedSchemaUIState: EditGroupCuratedSchemaUIState = EditGroupCuratedSchemaUIState.Loading, chatRepository: ChatRepository, @@ -274,6 +283,7 @@ fun EditGroupCuratedSchemaScreen( chatRoomId = chatRoomId, relayHint = relayHint, identifier = identifier, + template = template, initialEditGroupCuratedSchemaUIState = initialEditGroupCuratedSchemaUIState, activeUserPublicKey = activeUserPublicKey, chatRepository = chatRepository, @@ -1472,6 +1482,9 @@ private fun EditGroupCuratedSchemaScreenPreview() { chatRoomId = "publicKey", relayHint = null, identifier = null, + // The shape a new list most often starts from, so the preview + // shows a templated form rather than the bare two fields. + template = CuratedSchemaTemplate.Links.key, initialEditGroupCuratedSchemaUIState = EditGroupCuratedSchemaUIState.Loaded( localChatRoom = LocalChatRoom( chatRoom = ChatRoom( @@ -1483,9 +1496,9 @@ private fun EditGroupCuratedSchemaScreenPreview() { mlsGroupState = "state" ), ), - // A new schema, so the editor seeds the two fields every list - // has and the preview shows what a group actually starts from - // rather than an empty form. + // A new schema, so the editor seeds the template's fields and + // the preview shows what a group actually starts from rather + // than an empty form. defaultRelays = listOf("wss://relay.example.com"), // A group that can sign, so the form renders in the state a // member actually meets it in rather than greyed out. diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/GroupCuratedSchemasScreen.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/GroupCuratedSchemasScreen.kt index 3eee137b..a5c8c24e 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/GroupCuratedSchemasScreen.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/GroupCuratedSchemasScreen.kt @@ -57,6 +57,7 @@ import to.curare.compose.ui.composable.navigation.routes.BroadcastGroupSignedEve import to.curare.compose.ui.composable.navigation.routes.CuratedSuggestionListRoute import to.curare.compose.ui.composable.navigation.routes.EditGroupCuratedSchemaRoute import to.curare.compose.ui.composable.navigation.routes.Route +import to.curare.compose.ui.composable.navigation.routes.SelectCuratedSchemaTemplateRoute import to.curare.compose.ui.composable.widgets.Decorative import to.curare.compose.ui.composable.widgets.EmptyState import to.curare.compose.ui.composable.widgets.ErrorState @@ -80,7 +81,9 @@ import kotlin.time.Instant * nothing more. This is where they are read: one card per list, most recently * signed first, each with its queue and its broadcast under it, and -- for a * member holding a share of the key -- the way into the editor, on the card - * for a revision and at the end for a new list. + * for a revision, and at the end the way to a new list, which starts with the + * template picker rather than with the editor: a new list has a shape to + * choose and a revision has one already. * * A schema exists to be read by people who were never in the room, so a member * without a share reads it like anybody else, and reads its queue, which the @@ -155,16 +158,28 @@ fun GroupCuratedSchemasScreen( } is GroupCuratedSchemasUIState.Loaded -> { - // The one way into the editor, for a revision and for a new - // list alike: the editor takes the identifier of the schema - // to revise, and null for one that does not exist yet. - val edit: (String?) -> Unit = { identifier -> + // A revision opens the editor on the schema to revise, which + // needs no template: it starts from the schema itself. + val edit: (String) -> Unit = { identifier -> onNavigateToRoute.invoke( EditGroupCuratedSchemaRoute( activeUserPublicKey = activeUserPublicKey, chatRoomId = chatRoomId, relayHint = relayHint, - identifier = identifier + identifier = identifier, + template = null + ) + ) + } + + // A new list opens the template picker, which opens the + // editor in turn on the shape chosen there. + val add: () -> Unit = { + onNavigateToRoute.invoke( + SelectCuratedSchemaTemplateRoute( + activeUserPublicKey = activeUserPublicKey, + chatRoomId = chatRoomId, + relayHint = relayHint ) ) } @@ -182,7 +197,7 @@ fun GroupCuratedSchemasScreen( Res.string.this_group_has_not_published_a_curated_schema_yet ), action = if (state.canSign) { - { AddSchemaButton(onClick = { edit(null) }) } + { AddSchemaButton(onClick = add) } } else { null } @@ -272,7 +287,7 @@ fun GroupCuratedSchemasScreen( if (state.canSign) { item { - AddSchemaButton(onClick = { edit(null) }) + AddSchemaButton(onClick = add) } } } @@ -370,8 +385,9 @@ private fun GroupCuratedSchemaCard( } /** - * The way into `EditGroupCuratedSchemaScreen` for a list that does not exist - * yet. The icon is decorative because the label is beside it. + * The way to a list that does not exist yet: `SelectCuratedSchemaTemplateScreen`, + * and through it the editor. The icon is decorative because the label is + * beside it. */ @Composable private fun AddSchemaButton(onClick: () -> Unit) { diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/SelectCuratedSchemaTemplateScreen.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/SelectCuratedSchemaTemplateScreen.kt new file mode 100644 index 00000000..1bc26fb9 --- /dev/null +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/SelectCuratedSchemaTemplateScreen.kt @@ -0,0 +1,248 @@ +package to.curare.compose.ui.composable + +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.rememberScrollState +import androidx.compose.foundation.verticalScroll +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.automirrored.filled.ArrowForward +import androidx.compose.material.icons.automirrored.filled.Chat +import androidx.compose.material.icons.filled.Category +import androidx.compose.material.icons.filled.Link +import androidx.compose.material.icons.filled.Tune +import androidx.compose.material3.Card +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Icon +import androidx.compose.material3.ListItem +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.TopAppBar +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.text.style.TextOverflow +import com.vitorpamplona.quartz.nip01Core.core.HexKey +import curare.composeapp.generated.resources.Res +import curare.composeapp.generated.resources.a_list_is_a_schema_what_an_entry_may_contain +import curare.composeapp.generated.resources.fields_colon +import curare.composeapp.generated.resources.pick_a_shape_to_start_from +import curare.composeapp.generated.resources.start_a_list +import curare.composeapp.generated.resources.template_catalogue +import curare.composeapp.generated.resources.template_catalogue_description +import curare.composeapp.generated.resources.template_custom +import curare.composeapp.generated.resources.template_custom_description +import curare.composeapp.generated.resources.template_discussion +import curare.composeapp.generated.resources.template_discussion_description +import curare.composeapp.generated.resources.template_links +import curare.composeapp.generated.resources.template_links_description +import org.jetbrains.compose.resources.StringResource +import org.jetbrains.compose.resources.stringResource +import to.curare.compose.nostr.curated.CuratedSchemaTemplate +import to.curare.compose.ui.composable.navigation.routes.EditGroupCuratedSchemaRoute +import to.curare.compose.ui.composable.navigation.routes.Route +import to.curare.compose.ui.composable.widgets.Decorative +import to.curare.compose.ui.composable.widgets.LocalSnackbarHostState +import to.curare.compose.ui.composable.widgets.NavigateBackButton +import to.curare.compose.ui.theme.ConformancePreviews +import to.curare.compose.ui.theme.CurareTheme +import to.curare.compose.ui.theme.readableContent +import to.curare.compose.ui.theme.spacing + +/** + * The first step of adding a curated schema: which shape the list starts from. + * + * curare.to's "Start a list" page, which is what its `/new` shows before a + * template is chosen: a line on what a list is, and one card per template + * naming it, saying what it is for and listing what an entry in it asks for. + * Tapping a card opens the editor seeded from that template, so that a list + * started here and one started on that site are the same kind of thing -- + * and a member who wants none of the shapes takes *Custom*, which is the two + * fields every list has and what "Add schema" used to open on directly. + * + * There is nothing to load and no state to keep: the templates are constants, + * and the choice leaves as an argument to `EditGroupCuratedSchemaRoute` rather + * than being remembered here. Back from the editor is this screen and another + * shape, the way the browser's back is on the site. + * + * Gated by the screen before it, not here. "Add schema" is offered only to a + * member holding a share of the key, and the editor says so again if a screen + * ever reaches it another way. + */ +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun SelectCuratedSchemaTemplateScreen( + activeUserPublicKey: HexKey, + chatRoomId: String, + relayHint: String?, + onNavigateToRoute: (Route) -> Unit, + onNavigateBack: () -> Unit, +) { + Scaffold( + snackbarHost = { SnackbarHost(LocalSnackbarHostState.current) }, + topBar = { + TopAppBar( + title = { + Text( + text = stringResource(Res.string.start_a_list), + maxLines = 1, + overflow = TextOverflow.Ellipsis + ) + }, + navigationIcon = { NavigateBackButton(onNavigateBack) } + ) + } + ) { innerPadding -> + // A scrolling column rather than a lazy list: four cards, all of them + // wanted at once, and nothing under them to page in. + Column( + modifier = Modifier + .padding(innerPadding) + .readableContent() + .fillMaxSize() + .verticalScroll(rememberScrollState()) + .padding( + horizontal = MaterialTheme.spacing.screenMargin, + vertical = MaterialTheme.spacing.itemGap + ), + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap) + ) { + // At the leading edge like the cards under it, whose icons and + // headlines it lines up with; the editor after this one centres + // its lead because nothing on it has an edge to keep. + Text( + text = stringResource(Res.string.a_list_is_a_schema_what_an_entry_may_contain), + style = MaterialTheme.typography.bodyMedium + ) + + Text( + text = stringResource(Res.string.pick_a_shape_to_start_from), + style = MaterialTheme.typography.bodySmall, + color = MaterialTheme.colorScheme.onSurfaceVariant + ) + + CuratedSchemaTemplate.entries.forEach { template -> + CuratedSchemaTemplateCard( + template = template, + onClick = { + onNavigateToRoute.invoke( + EditGroupCuratedSchemaRoute( + activeUserPublicKey = activeUserPublicKey, + chatRoomId = chatRoomId, + relayHint = relayHint, + identifier = null, + template = template.key + ) + ) + } + ) + } + } + } +} + +/** + * One shape a list can start from, as a card that opens the editor on it. + * + * The site's card is the name, the description and a count of fields. The + * count is the one thing changed: the fields are listed by label instead, the + * way the group's own schema cards list them, because what a member choosing + * a shape wants to know is what an entry in it will ask for, and "6 fields" + * answers a different question. The identifier is among them although a form + * never asks for it: the editor lists it as a row too, marked derived, and the + * card shows the rows the editor is about to. + */ +@Composable +private fun CuratedSchemaTemplateCard( + template: CuratedSchemaTemplate, + onClick: () -> Unit +) { + Card(modifier = Modifier.fillMaxWidth(), onClick = onClick) { + ListItem( + leadingContent = { + // Decorative: the name beside it is the label, and the card is + // the target. + Icon(template.icon(), contentDescription = Decorative) + }, + trailingContent = { + Icon(Icons.AutoMirrored.Filled.ArrowForward, contentDescription = Decorative) + }, + headlineContent = { + Text( + text = stringResource(template.label()), + maxLines = 1, + overflow = TextOverflow.Ellipsis + ) + }, + supportingContent = { + Column( + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.relatedGap) + ) { + Text(text = stringResource(template.description())) + + // Unclipped, unlike the group's schema cards: a template's + // fields are this build's own and ten at most, and the list + // is what the choice is made on. + Text( + text = stringResource( + Res.string.fields_colon, + template.fields.joinToString { it.labelOrName() } + ), + style = MaterialTheme.typography.bodySmall + ) + } + } + ) + } +} + +/** + * What the picker calls a template. + * + * A `when` rather than a field on each entry, for the reason `CuratedVisibility` + * has one: the enum stays the protocol's four shapes with no copy behind it, + * and a fifth is a compile error here rather than a card with no name. + */ +private fun CuratedSchemaTemplate.label(): StringResource = when (this) { + CuratedSchemaTemplate.Links -> Res.string.template_links + CuratedSchemaTemplate.Discussion -> Res.string.template_discussion + CuratedSchemaTemplate.Catalogue -> Res.string.template_catalogue + CuratedSchemaTemplate.Custom -> Res.string.template_custom +} + +/** What the template is for, in the site's words. */ +private fun CuratedSchemaTemplate.description(): StringResource = when (this) { + CuratedSchemaTemplate.Links -> Res.string.template_links_description + CuratedSchemaTemplate.Discussion -> Res.string.template_discussion_description + CuratedSchemaTemplate.Catalogue -> Res.string.template_catalogue_description + CuratedSchemaTemplate.Custom -> Res.string.template_custom_description +} + +/** The picture of the shape: what the entries mostly are, or the sliders for none in particular. */ +private fun CuratedSchemaTemplate.icon(): ImageVector = when (this) { + CuratedSchemaTemplate.Links -> Icons.Default.Link + CuratedSchemaTemplate.Discussion -> Icons.AutoMirrored.Filled.Chat + CuratedSchemaTemplate.Catalogue -> Icons.Default.Category + CuratedSchemaTemplate.Custom -> Icons.Default.Tune +} + +@ConformancePreviews +@Composable +private fun SelectCuratedSchemaTemplateScreenPreview() { + CurareTheme { + Surface(modifier = Modifier.fillMaxSize()) { + SelectCuratedSchemaTemplateScreen( + activeUserPublicKey = "a".repeat(64), + chatRoomId = "d".repeat(64), + relayHint = null, + onNavigateToRoute = {}, + onNavigateBack = {}, + ) + } + } +} diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/CurareNavHost.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/CurareNavHost.kt index 088baa99..7c11d5f1 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/CurareNavHost.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/CurareNavHost.kt @@ -45,6 +45,7 @@ import to.curare.compose.ui.composable.EditGroupNostrProfileScreen import to.curare.compose.ui.composable.AddGroupPostScreen import to.curare.compose.ui.composable.AcceptCuratedSuggestionScreen import to.curare.compose.ui.composable.EditGroupCuratedSchemaScreen +import to.curare.compose.ui.composable.SelectCuratedSchemaTemplateScreen import to.curare.compose.ui.composable.BroadcastGroupSignedEventScreen import to.curare.compose.ui.composable.CuratedSuggestionListScreen import to.curare.compose.ui.composable.ProposeGroupEventScreen @@ -152,6 +153,7 @@ import to.curare.compose.ui.composable.navigation.routes.EditGroupNostrProfileRo import to.curare.compose.ui.composable.navigation.routes.AddGroupPostRoute import to.curare.compose.ui.composable.navigation.routes.AcceptCuratedSuggestionRoute import to.curare.compose.ui.composable.navigation.routes.EditGroupCuratedSchemaRoute +import to.curare.compose.ui.composable.navigation.routes.SelectCuratedSchemaTemplateRoute import to.curare.compose.ui.composable.navigation.routes.BroadcastGroupSignedEventRoute import to.curare.compose.ui.composable.navigation.routes.CuratedSuggestionListRoute import to.curare.compose.ui.composable.navigation.routes.ProposeGroupEventRoute @@ -1328,6 +1330,23 @@ fun CurareNavHost( } ) } + composable { backStackEntry -> + val route = backStackEntry.toRoute() + + SelectCuratedSchemaTemplateScreen( + activeUserPublicKey = route.activeUserPublicKey, + chatRoomId = route.chatRoomId, + relayHint = route.relayHint, + // Pushed rather than replaced, so back from the editor is + // this screen and another shape. + onNavigateToRoute = { editorRoute -> + navController.navigate(route = editorRoute) + }, + onNavigateBack = { + navController.popBackStack() + } + ) + } composable { backStackEntry -> val route = backStackEntry.toRoute() @@ -1336,14 +1355,24 @@ fun CurareNavHost( chatRoomId = route.chatRoomId, relayHint = route.relayHint, identifier = route.identifier, + template = route.template, chatRepository = databaseChatRepository, frostSigningRepository = databaseFrostSigningRepository, onNavigateToRouteAndPopUpInclusive = { signingRoute -> // Replace the editor so back returns to the group rather - // than to a schema whose proposal has already gone out. + // than to a schema whose proposal has already gone out -- + // and, for a new list, the template picker under it, for + // the same reason: the shape has been chosen and signed + // for, and offering it again would offer a second list. navController.navigate(route = signingRoute) { - popUpTo { - inclusive = true + if (route.identifier == null) { + popUpTo { + inclusive = true + } + } else { + popUpTo { + inclusive = true + } } } }, diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/routes/EditGroupCuratedSchemaRoute.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/routes/EditGroupCuratedSchemaRoute.kt index 93d4f8bd..b3ed19bd 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/routes/EditGroupCuratedSchemaRoute.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/routes/EditGroupCuratedSchemaRoute.kt @@ -10,11 +10,18 @@ import kotlinx.serialization.Serializable * an edit replaces the event under the same `d`, and the editor's job is to * propose that replacement, so the coordinate is what names the thing being * edited and the event is only its current version. + * + * [template] is the key of the `CuratedSchemaTemplate` a new schema starts + * from -- what `SelectCuratedSchemaTemplateRoute` chose, and the same word + * curare.to's editor takes as `?template=`. It means nothing on an edit, which + * starts from the schema itself, and null on a new schema is the bare two + * fields every list has. */ @Serializable data class EditGroupCuratedSchemaRoute( val activeUserPublicKey: String, val chatRoomId: String, // TODO: have this as a publicKey val relayHint: String?, - val identifier: String? + val identifier: String?, + val template: String? ): Route() diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/routes/SelectCuratedSchemaTemplateRoute.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/routes/SelectCuratedSchemaTemplateRoute.kt new file mode 100644 index 00000000..5d86e8cb --- /dev/null +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/composable/navigation/routes/SelectCuratedSchemaTemplateRoute.kt @@ -0,0 +1,19 @@ +package to.curare.compose.ui.composable.navigation.routes + +import kotlinx.serialization.Serializable + +/** + * The first step of adding a curated schema: which shape the list starts from. + * + * Pushed from the group's schemas screen, and pushing the editor in turn with + * the chosen template's key -- so that back from the editor is this screen and + * a different shape, the way curare.to's `/new` page is under its editor. + * There is no template to carry: the choice is made here and leaves as an + * argument to `EditGroupCuratedSchemaRoute`. + */ +@Serializable +data class SelectCuratedSchemaTemplateRoute( + val activeUserPublicKey: String, + val chatRoomId: String, // TODO: have this as a publicKey + val relayHint: String? +): Route() diff --git a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/EditGroupCuratedSchemaViewModel.kt b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/EditGroupCuratedSchemaViewModel.kt index b490fb58..deebe2bc 100644 --- a/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/EditGroupCuratedSchemaViewModel.kt +++ b/composeApp/src/commonMain/kotlin/to/curare/compose/ui/view/model/EditGroupCuratedSchemaViewModel.kt @@ -20,6 +20,7 @@ import to.curare.compose.nostr.curated.CuratedFieldType import to.curare.compose.nostr.curated.CuratedSchema import to.curare.compose.nostr.curated.CuratedSchemaEvent import to.curare.compose.nostr.curated.CuratedSchemaProblem +import to.curare.compose.nostr.curated.CuratedSchemaTemplate import to.curare.compose.nostr.curated.CuratedVisibility import to.curare.compose.repository.ChatRepository import to.curare.compose.repository.FrostSigningRepository @@ -49,6 +50,16 @@ import kotlinx.coroutines.launch * identifier is a new list, with a queue of its own, and the group's screen * offers that as adding a schema rather than as editing this one. * + * ### Starting from a template + * + * A new schema is seeded from a [CuratedSchemaTemplate] -- curare.to's four + * shapes, chosen on the screen before this one and named by [template] -- so + * that the form opens on a list's worth of fields rather than on the two every + * list has, and so that the identifier field records how a client derives an + * entry's `d`, which is what lets that site's form post to a list started here. + * The template decides only what the working copy starts as: it is not kept, + * and nothing about the schema remembers which one it came from. + * * The comparison for "nothing changed" is against the schema as the group signed * it, normalized on both sides, because re-signing an identical schema would put * a second statement on the record with a newer timestamp: harmless, since the @@ -61,6 +72,12 @@ class EditGroupCuratedSchemaViewModel( val relayHint: String?, /** The `d` of the schema being edited, or null for a new one. */ val identifier: String?, + /** + * The key of the [CuratedSchemaTemplate] a new schema starts from; see + * [CuratedSchemaTemplate.byKey] for what null and an unknown key get. Not + * read on an edit, which starts from the schema itself. + */ + val template: String? = null, initialEditGroupCuratedSchemaUIState: EditGroupCuratedSchemaUIState, val chatRepository: ChatRepository, val frostSigningRepository: FrostSigningRepository, @@ -204,10 +221,11 @@ class EditGroupCuratedSchemaViewModel( /** * Fills the working copy from the schema the group signed, once. * - * An existing schema starts at itself. A new one starts at the two fields - * every list has -- the identifier and the title, which `CuratedSchema.normalized` - * would put back anyway, shown up front so that what the member sees is what - * the group will sign -- and at the group's own relays. + * An existing schema starts at itself. A new one starts at its [template]'s + * fields and rules -- every template carrying the identifier and the title, + * which `CuratedSchema.normalized` would put back anyway, shown up front so + * that what the member sees is what the group will sign -- and at the + * group's own relays. * * Called from the screen rather than from [initiateEditGroupCuratedSchema], * because the loader is not the only way a loaded state arrives: a preview @@ -226,8 +244,10 @@ class EditGroupCuratedSchemaViewModel( suggesters.addAll(schema.suggesters) relays.addAll(schema.relays) } else { + val shape = CuratedSchemaTemplate.byKey(template) visibility = CuratedVisibility.Public - fields.addAll(listOf(CuratedField.IDENTIFIER, CuratedField.TITLE)) + fields.addAll(shape.fields) + requireAny.addAll(shape.requireAny) relays.addAll(state.defaultRelays) } } @@ -523,6 +543,7 @@ class EditGroupCuratedSchemaViewModel( chatRoomId: String, relayHint: String?, identifier: String?, + template: String? = null, initialEditGroupCuratedSchemaUIState: EditGroupCuratedSchemaUIState = EditGroupCuratedSchemaUIState.Loading, chatRepository: ChatRepository, @@ -534,6 +555,7 @@ class EditGroupCuratedSchemaViewModel( chatRoomId = chatRoomId, relayHint = relayHint, identifier = identifier, + template = template, initialEditGroupCuratedSchemaUIState = initialEditGroupCuratedSchemaUIState, chatRepository = chatRepository, frostSigningRepository = frostSigningRepository diff --git a/composeApp/src/commonTest/kotlin/to/curare/compose/nostr/curated/CuratedSchemaTemplateTest.kt b/composeApp/src/commonTest/kotlin/to/curare/compose/nostr/curated/CuratedSchemaTemplateTest.kt new file mode 100644 index 00000000..a4ba9949 --- /dev/null +++ b/composeApp/src/commonTest/kotlin/to/curare/compose/nostr/curated/CuratedSchemaTemplateTest.kt @@ -0,0 +1,155 @@ +package to.curare.compose.nostr.curated + +import com.vitorpamplona.quartz.nip01Core.core.Event +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotNull +import kotlin.test.assertSame +import kotlin.test.assertTrue + +/** + * The shapes a list can start from, held to what curare.to's own test holds + * its templates to: each one publishes a schema the module verifies, with its + * rule recorded and no derived `t` field. + * + * The rest is what this app's editor adds on top. A template is seeded + * straight into the working copy, so every field has to be one the editor + * would have let a member commit, and every rule has to name fields that are + * there -- a template that broke either would put the editor in a state the + * editor itself refuses to reach. + */ +class CuratedSchemaTemplateTest { + private val curator = "7c965d8c2acdfd635562da3bcb82596b595be28b008d8b54ec702ed4c67d9d25" + + /** A schema from a template, as the editor would propose it. */ + private fun schemaFrom(template: CuratedSchemaTemplate) = CuratedSchema( + identifier = template.key, + title = "${template.key} suggestion", + name = template.key, + description = "A list started from the ${template.key} template.", + visibility = CuratedVisibility.Public, + fields = template.fields, + requireAny = template.requireAny, + relays = listOf("wss://relay.example"), + ) + + @Test + fun `every template publishes a schema a reader accepts, and reads back as itself`() { + for (template in CuratedSchemaTemplate.entries) { + val schema = schemaFrom(template) + assertEquals(emptyList(), schema.problems(), "${template.key} is not a usable schema") + + // The mandatory two are there and required, so the one repair a + // reader makes has nothing to do: what the member sees is what the + // group signs. + assertEquals(schema, schema.normalized(), "${template.key} is changed by normalizing") + + val built = CuratedSchemaEvent.template(schema, createdAt = 1_735_689_600) + val read = assertNotNull(CuratedSchemaEvent.parse(event(built.tags, built.content))) + assertEquals(schema, read, "${template.key} did not round trip") + } + } + + @Test + fun `every template records its rule on the identifier and declares no hashtag field`() { + for (template in CuratedSchemaTemplate.entries) { + val identifier = template.fields.single { it.tag() == "d" } + assertEquals(true, identifier.config.derived, "${template.key}'s identifier is prompted for") + assertTrue( + identifier.config.hint.orEmpty().endsWith("[rule:${template.rule.key}]"), + "${template.key}'s identifier does not record its rule: ${identifier.config.hint}" + ) + + // The hashtag rule is bitcoin.mov's own; a list is its own topic. + assertTrue(template.fields.none { it.tag() == "t" }, "${template.key} declares a t field") + } + } + + @Test + fun `the hint is the one curare_to writes`() { + // Byte for byte, because that site's `ruleOf` reads the mark back with a + // regular expression and a template started there and here should + // produce the same schema. + assertEquals( + "Derived by the client — the link, else the title and author. [rule:link-or-title]", + CuratedDeriveRule.LinkOrTitle.hint() + ) + assertEquals( + "Derived by the client — the title. [rule:title]", + CuratedDeriveRule.Title.hint() + ) + } + + @Test + fun `every template is one the editor would have let a member build`() { + for (template in CuratedSchemaTemplate.entries) { + val names = template.fields.map { it.name } + assertTrue( + names.all { it.isNotBlank() && it.none(Char::isWhitespace) }, + "${template.key} has a field name the editor refuses: $names" + ) + assertEquals(names.distinct(), names, "${template.key} names a field twice") + + for (group in template.requireAny) { + assertTrue(group.size > 1, "${template.key} has a rule that says nothing: $group") + assertTrue( + group.all { it in names }, + "${template.key} has a rule naming a field it does not have: $group" + ) + } + + template.fields.filter { it.type == CuratedFieldType.Enum }.forEach { field -> + assertTrue( + !field.config.options.isNullOrEmpty(), + "${template.key}'s ${field.name} is an enum with nothing to choose from" + ) + } + } + } + + @Test + fun `the templates are the site's four, in its order`() { + assertEquals( + listOf("links", "discussion", "catalogue", "custom"), + CuratedSchemaTemplate.entries.map { it.key } + ) + + // The reddit-shaped default, by name: what an entry asks for and what + // it needs one of. + val links = CuratedSchemaTemplate.Links + assertEquals( + listOf("identifier", "title", "link", "body", "image", "flair"), + links.fields.map { it.name } + ) + assertEquals(listOf(listOf("link", "body")), links.requireAny) + assertEquals("r", links.fields.first { it.name == "link" }.tag()) + assertEquals(CuratedFieldConfig.CONTENT_TAG, links.fields.first { it.name == "body" }.tag()) + + // Custom is the two fields every list has and nothing else. + assertEquals(listOf("d", "title"), CuratedSchemaTemplate.Custom.fields.map { it.tag() }) + assertEquals(emptyList(), CuratedSchemaTemplate.Custom.requireAny) + } + + @Test + fun `a key finds its template, and anything else is custom`() { + for (template in CuratedSchemaTemplate.entries) { + assertSame(template, CuratedSchemaTemplate.byKey(template.key)) + } + + // Custom rather than the first: the one shape that presumes nothing + // about a list it was not asked for. + assertSame(CuratedSchemaTemplate.Custom, CuratedSchemaTemplate.byKey(null)) + assertSame(CuratedSchemaTemplate.Custom, CuratedSchemaTemplate.byKey("gallery")) + assertSame(CuratedSchemaTemplate.Custom, CuratedSchemaTemplate.byKey("Links")) + } + + private fun event(tags: Array>, content: String): Event = Event( + id = "a".repeat(64), + pubKey = curator, + createdAt = 1_735_689_600, + kind = CuratedSchemaEvent.KIND, + tags = tags, + content = content, + sig = "b".repeat(128), + ) +} diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/StringCatalogueJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/StringCatalogueJvmTest.kt index 5ddc7b11..beb1baf2 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/StringCatalogueJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/StringCatalogueJvmTest.kt @@ -8,6 +8,7 @@ import curare.composeapp.generated.resources.something_went_wrong import curare.composeapp.generated.resources.add_chapter_to import curare.composeapp.generated.resources.chapter_words_characters import curare.composeapp.generated.resources.sent_a_private_message_to +import curare.composeapp.generated.resources.template_links import org.jetbrains.compose.resources.getString import kotlin.test.Test import kotlin.test.assertEquals @@ -65,6 +66,15 @@ class StringCatalogueJvmTest { ) } + @Test + fun `an ampersand entity comes back as an ampersand`() = runBlocking { + // `&` is the one character XML itself refuses in text, so it is the entity + // `&` in the catalogue -- an escape the parser handles rather than the + // resources library, and the first the app has depended on. The failure + // mode is a card offering "Links & text". + assertEquals("Links & text", getString(Res.string.template_links)) + } + @Test fun `a unicode escape was decoded into the catalogue`() = runBlocking { // `·` is Kotlin source syntax with no meaning in XML, so the extractor diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/EditGroupCuratedSchemaScreenJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/EditGroupCuratedSchemaScreenJvmTest.kt index 5126f678..c1e7211f 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/EditGroupCuratedSchemaScreenJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/EditGroupCuratedSchemaScreenJvmTest.kt @@ -8,7 +8,10 @@ import androidx.compose.ui.test.ExperimentalTestApi import androidx.compose.ui.test.assertIsDisplayed import androidx.compose.ui.test.assertIsNotEnabled import androidx.compose.ui.test.assertIsOn +import androidx.compose.ui.test.assertCountEquals +import androidx.compose.ui.test.assertIsNotSelected import androidx.compose.ui.test.assertIsSelected +import androidx.compose.ui.test.onAllNodesWithText import androidx.compose.ui.test.getBoundsInRoot import androidx.compose.ui.test.onNodeWithContentDescription import androidx.compose.ui.test.onNodeWithText @@ -24,6 +27,7 @@ import to.curare.compose.nostr.curated.CuratedFieldConfig import to.curare.compose.nostr.curated.CuratedFieldType import to.curare.compose.nostr.curated.CuratedSchema import to.curare.compose.nostr.curated.CuratedSchemaEvent +import to.curare.compose.nostr.curated.CuratedSchemaTemplate import to.curare.compose.nostr.curated.CuratedVisibility import to.curare.compose.repository.ChatRepository import to.curare.compose.repository.FrostSigningRepository @@ -42,7 +46,9 @@ import kotlin.time.Instant * own relays are on the form before anything is typed, because they are what the * group will sign whether or not the member adds to them -- and a form that * showed an empty list and then signed two fields would be lying about what it - * proposed. + * proposed. A new list from a template starts at the template: its fields as + * rows and its rules as chips, since the picker before this screen promised + * that shape and the form is where the promise is kept. * * **An existing list opens as an edit.** Its identifier is locked and the form * says why, its rules show which fields they name, and the suggesters a closed @@ -113,6 +119,37 @@ class EditGroupCuratedSchemaScreenJvmTest { .assertDoesNotExist() } + @Test + fun `a new list from a template starts at the template's fields and rules`() = render( + state(existing = null, defaultRelays = listOf("wss://relay.one.example/")), + template = CuratedSchemaTemplate.Links.key + ) { + // The template's fields, in its order, the mandatory two included -- + // and the identifier still marked derived, since the template records + // its rule in the hint rather than prompting for the value. + onNodeWithText("identifier · Token · Required · Derived").assertIsDisplayed() + val title = onNodeWithText("title · Text · Required").getBoundsInRoot().top + val link = onNodeWithText("link · Url · Optional").getBoundsInRoot().top + val body = onNodeWithText("body · Long text · Optional").getBoundsInRoot().top + val image = onNodeWithText("image · Image · Optional").getBoundsInRoot().top + val flair = onNodeWithText("flair · One of a list · Optional").getBoundsInRoot().top + assertTrue(title < link && link < body && body < image && image < flair, "the fields were drawn out of order") + + // The rule the template carries, as a row of chips with its two fields + // selected. The chip is the one node saying exactly "link": the row + // above is headed by the label, "Link", and its supporting line says more. + onNodeWithText("At least one of").assertIsDisplayed() + onAllNodesWithText("link").assertCountEquals(1) + onNodeWithText("link").assertIsSelected() + onNodeWithText("body").assertIsSelected() + onNodeWithText("flair").assertIsNotSelected() + + // Everything else is as a new list is. + onNodeWithText("wss://relay.one.example/").assertIsDisplayed() + onNodeWithText("Public").assertIsSelected() + onNodeWithText("Propose schema").assertIsDisplayed() + } + @Test fun `an existing list opens as an edit with its identifier locked`() = render( state(existing = existing(films)) @@ -210,6 +247,7 @@ class EditGroupCuratedSchemaScreenJvmTest { */ private fun render( uiState: EditGroupCuratedSchemaUIState, + template: String? = null, assertions: ComposeUiTest.() -> Unit ) = runDesktopComposeUiTest(width = 400, height = 3000) { setContent { @@ -222,6 +260,7 @@ class EditGroupCuratedSchemaScreenJvmTest { relayHint = null, identifier = (uiState as? EditGroupCuratedSchemaUIState.Loaded) ?.existing?.identifier, + template = template, initialEditGroupCuratedSchemaUIState = uiState, chatRepository = ChatRepository.NO_OP_CHAT_REPOSITORY, frostSigningRepository = diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/GroupCuratedSchemasScreenJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/GroupCuratedSchemasScreenJvmTest.kt index f0ec7b3a..a33cbee7 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/GroupCuratedSchemasScreenJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/GroupCuratedSchemasScreenJvmTest.kt @@ -22,6 +22,7 @@ import to.curare.compose.ui.composable.navigation.routes.BroadcastGroupSignedEve import to.curare.compose.ui.composable.navigation.routes.CuratedSuggestionListRoute import to.curare.compose.ui.composable.navigation.routes.EditGroupCuratedSchemaRoute import to.curare.compose.ui.composable.navigation.routes.Route +import to.curare.compose.ui.composable.navigation.routes.SelectCuratedSchemaTemplateRoute import to.curare.compose.ui.composable.widgets.ProvideSnackbarHost import to.curare.compose.ui.theme.CurareTheme import to.curare.compose.ui.view.state.GroupCuratedSchemasUIState @@ -33,8 +34,8 @@ import kotlin.test.assertTrue /** * The schemas of the lists the group curates on their own screen: one card per * list, most recently signed first, each with its queue and its broadcast, and - * two ways into the editor -- the card for a revision, the button for a new - * list -- for a member holding a share. + * two ways on -- the card into the editor for a revision, the button into the + * template picker for a new list -- for a member holding a share. */ @OptIn(ExperimentalTestApi::class) class GroupCuratedSchemasScreenJvmTest { @@ -129,26 +130,29 @@ class GroupCuratedSchemasScreenJvmTest { activeUserPublicKey = activeUserPublicKey, chatRoomId = chatRoomId, relayHint = null, - identifier = "books" + identifier = "books", + // A revision starts from the schema itself, not from a shape. + template = null ), opened ) } @Test - fun `the button opens the editor on no schema at all`() { + fun `the button opens the template picker, not the editor`() { var opened: Route? = null render(state(schemas = listOf(films)), onNavigateToRoute = { opened = it }) { onNodeWithText("Add schema").performClick() } + // A new list has a shape to choose before it has a form to fill, and + // the picker is what opens the editor, on the shape chosen there. assertEquals( - EditGroupCuratedSchemaRoute( + SelectCuratedSchemaTemplateRoute( activeUserPublicKey = activeUserPublicKey, chatRoomId = chatRoomId, - relayHint = null, - identifier = null + relayHint = null ), opened ) @@ -189,7 +193,7 @@ class GroupCuratedSchemasScreenJvmTest { } assertEquals( - EditGroupCuratedSchemaRoute(activeUserPublicKey, chatRoomId, relayHint = null, identifier = null), + SelectCuratedSchemaTemplateRoute(activeUserPublicKey, chatRoomId, relayHint = null), opened ) } diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/SelectCuratedSchemaTemplateScreenJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/SelectCuratedSchemaTemplateScreenJvmTest.kt new file mode 100644 index 00000000..a41a5908 --- /dev/null +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/composable/SelectCuratedSchemaTemplateScreenJvmTest.kt @@ -0,0 +1,115 @@ +package to.curare.compose.ui.composable + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.ui.Modifier +import androidx.compose.ui.test.ComposeUiTest +import androidx.compose.ui.test.ExperimentalTestApi +import androidx.compose.ui.test.assertIsDisplayed +import androidx.compose.ui.test.getBoundsInRoot +import androidx.compose.ui.test.onNodeWithContentDescription +import androidx.compose.ui.test.onNodeWithText +import androidx.compose.ui.test.performClick +import androidx.compose.ui.test.runDesktopComposeUiTest +import to.curare.compose.nostr.curated.CuratedSchemaTemplate +import to.curare.compose.ui.composable.GroupSignedWorkFixtures.activeUserPublicKey +import to.curare.compose.ui.composable.GroupSignedWorkFixtures.chatRoomId +import to.curare.compose.ui.composable.navigation.routes.EditGroupCuratedSchemaRoute +import to.curare.compose.ui.composable.navigation.routes.Route +import to.curare.compose.ui.composable.widgets.ProvideSnackbarHost +import to.curare.compose.ui.theme.CurareTheme +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** + * The first step of adding a schema: the shapes a list can start from, one + * card each, and the editor opened on the one tapped. + * + * **Every shape is offered, in the site's order, saying what it asks for.** A + * member choosing a shape reads the fields to choose, so the card lists them, + * and the list is the template's own -- a card that summarised a template in + * its own words could drift from what the editor then opens on. + * + * **A card opens the editor on its template and nothing else.** The route + * carries the key and no identifier, which is the whole of what the picker + * decides; the editor seeds from the key, so a wrong one here is a form that + * opens on the wrong fields with nothing saying so. + */ +@OptIn(ExperimentalTestApi::class) +class SelectCuratedSchemaTemplateScreenJvmTest { + + @Test + fun `every shape is offered, in the site's order, with what an entry in it asks for`() = render { + onNodeWithText("Start a list").assertIsDisplayed() + onNodeWithText("A list is a schema", substring = true).assertIsDisplayed() + + val links = onNodeWithText("Links & text").assertIsDisplayed().getBoundsInRoot().top + val discussion = onNodeWithText("Discussion").assertIsDisplayed().getBoundsInRoot().top + val catalogue = onNodeWithText("Catalogue").assertIsDisplayed().getBoundsInRoot().top + val custom = onNodeWithText("Custom").assertIsDisplayed().getBoundsInRoot().top + assertTrue( + links < discussion && discussion < catalogue && catalogue < custom, + "the shapes were drawn out of order" + ) + + // What each asks for, by label, the identifier included: the editor's + // row for it is there too and says it is derived. + onNodeWithText("Fields: Identifier, Title, Link, Text, Picture, Flair").assertIsDisplayed() + onNodeWithText("Fields: Identifier, Title, Text, Flair").assertIsDisplayed() + onNodeWithText( + "Fields: Identifier, Title, Year, Type, External id, Where to see it, Reference, " + + "Picture, Language, Description" + ).assertIsDisplayed() + onNodeWithText("Fields: Identifier, Title").assertIsDisplayed() + + // And what each is for, in the site's words. + onNodeWithText("Reddit’s default", substring = true).assertIsDisplayed() + onNodeWithText("bitcoin.mov’s shape", substring = true).assertIsDisplayed() + + onNodeWithContentDescription("Back").assertIsDisplayed() + } + + @Test + fun `a card opens the editor on its template, and on no schema`() { + var opened: Route? = null + + render(onNavigateToRoute = { opened = it }) { + onNodeWithText("Catalogue").performClick() + } + + assertEquals( + EditGroupCuratedSchemaRoute( + activeUserPublicKey = activeUserPublicKey, + chatRoomId = chatRoomId, + relayHint = null, + identifier = null, + template = CuratedSchemaTemplate.Catalogue.key + ), + opened + ) + } + + private fun render( + onNavigateToRoute: (Route) -> Unit = {}, + assertions: ComposeUiTest.() -> Unit + ) = runDesktopComposeUiTest(width = 400, height = 1600) { + setContent { + CurareTheme { + ProvideSnackbarHost { + Box(modifier = Modifier.fillMaxSize()) { + SelectCuratedSchemaTemplateScreen( + activeUserPublicKey = activeUserPublicKey, + chatRoomId = chatRoomId, + relayHint = null, + onNavigateToRoute = onNavigateToRoute, + onNavigateBack = {}, + ) + } + } + } + } + + assertions() + } +} diff --git a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/view/model/EditGroupCuratedSchemaViewModelJvmTest.kt b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/view/model/EditGroupCuratedSchemaViewModelJvmTest.kt index 09ca3683..8f536e85 100644 --- a/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/view/model/EditGroupCuratedSchemaViewModelJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/to/curare/compose/ui/view/model/EditGroupCuratedSchemaViewModelJvmTest.kt @@ -11,6 +11,7 @@ import to.curare.compose.nostr.curated.CuratedFieldType import to.curare.compose.nostr.curated.CuratedSchema import to.curare.compose.nostr.curated.CuratedSchemaEvent import to.curare.compose.nostr.curated.CuratedSchemaProblem +import to.curare.compose.nostr.curated.CuratedSchemaTemplate import to.curare.compose.nostr.curated.CuratedVisibility import to.curare.compose.repository.ChatRepository import to.curare.compose.repository.FrostSigningRepository @@ -25,9 +26,10 @@ import kotlin.time.Instant /** * The decisions the schema editor makes that nothing else can check. * - * **What a new list starts as.** The two fields every list has and the group's - * own relays, so that what the member sees is what the group will sign rather - * than what `CuratedSchema.normalized` will quietly add. + * **What a new list starts as.** Its template's fields and rules -- the two + * fields every list has, when no shape was chosen -- and the group's own relays, + * so that what the member sees is what the group will sign rather than what + * `CuratedSchema.normalized` will quietly add. * * **What cannot be taken away.** A field writing to `d` or `title` cannot be * removed, because the event would only grow it back -- so the editor has to @@ -58,11 +60,12 @@ class EditGroupCuratedSchemaViewModelJvmTest { relays = listOf("wss://relay.one.example"), ) - private fun viewModel(identifier: String? = null) = EditGroupCuratedSchemaViewModel( + private fun viewModel(identifier: String? = null, template: String? = null) = EditGroupCuratedSchemaViewModel( chatRoomId = chatRoomId, activeUserPublicKey = "a".repeat(64), relayHint = null, identifier = identifier, + template = template, initialEditGroupCuratedSchemaUIState = EditGroupCuratedSchemaUIState.Loading, chatRepository = ChatRepository.NO_OP_CHAT_REPOSITORY, frostSigningRepository = FrostSigningRepository.NO_OP_FROST_SIGNING_REPOSITORY, @@ -122,9 +125,43 @@ class EditGroupCuratedSchemaViewModelJvmTest { assertEquals(emptyList(), viewModel.suggesters) } + @Test + fun `a new schema from a template starts at the template's fields and rules`() { + val viewModel = viewModel(template = CuratedSchemaTemplate.Links.key) + viewModel.seedWorkingCopy(newSchema()) + + assertEquals(CuratedSchemaTemplate.Links.fields, viewModel.fields.toList()) + assertEquals(CuratedSchemaTemplate.Links.requireAny, viewModel.requireAny.toList()) + // The identifier is the template's: derived, with its rule in the hint. + assertTrue(viewModel.isMandatory(viewModel.fields[0])) + assertTrue(viewModel.fields[0].config.hint.orEmpty().endsWith("[rule:link-or-title]")) + // What the template does not decide is as any new list's. + assertEquals(CuratedVisibility.Public, viewModel.visibility) + assertEquals(listOf("wss://relay.one.example"), viewModel.relays) + assertEquals(emptyList(), viewModel.suggesters) + + // And the draft it makes is a usable schema as it stands, so a member + // who fills in the identity and presses the button proposes the shape. + val draft = viewModel.draft("links", "Link suggestion", "Links", "Things worth a look.", "", "") + assertTrue(draft.isUsable()) + assertEquals(listOf(listOf("link", "body")), draft.requireAny) + } + + @Test + fun `a template key this build does not know starts at the two fields every list has`() { + val viewModel = viewModel(template = "gallery") + viewModel.seedWorkingCopy(newSchema()) + + assertEquals(CuratedSchemaTemplate.Custom.fields, viewModel.fields.toList()) + assertEquals(listOf("d", "title"), viewModel.fields.map { it.tag() }) + assertEquals(emptyList(), viewModel.requireAny) + } + @Test fun `an existing schema starts as itself, and its relays are its own`() { - val viewModel = viewModel("films") + // A template beside an identifier is ignored: an edit starts from the + // schema the group signed, whatever shape the route also names. + val viewModel = viewModel("films", template = CuratedSchemaTemplate.Catalogue.key) viewModel.seedWorkingCopy(editing()) assertEquals(films.fields, viewModel.fields.toList())