Keep raw field order and show tags as a group
Two review fixes for the content view: - Fields now follow the order the client sent them, not alphabetical order. serde_json's default map is a BTreeMap, so map.iter() — which both the inline rows and the preview's formatted page use — walked the keys sorted. Enabling preserve_order makes it an IndexMap, so a kind-0 profile keeps "about" after the name keys, exactly as in the raw JSON. It is a global flag; the crate's other serde_json uses are order-insensitive (typed structs for apps.json and keys.json; tests compare parsed values) and the full suite still passes. - Tags get a group of their own instead of one flattened, truncated summary string. Request::Sign carries them structurally as Vec<TagView> (bounded), and the dialog renders a "Tags" card: a count and, when the list is longer than the inline cap, a "View all tags" button opening the full list in a dialog like the content preview. Each tag is one row, its name beside its full value(s), WordChar-wrapped rather than cut off.
This commit is contained in:
6 files changed
+191
-61
No files matched your search
Generated
+8
@@ -579,6 +579,12 @@ dependencies = [
|
||||
"zlib-rs",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "foldhash"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.2.2"
|
||||
@@ -1831,6 +1837,8 @@ version = "1.0.154"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6"
|
||||
dependencies = [
|
||||
"foldhash",
|
||||
"indexmap",
|
||||
"itoa",
|
||||
"memchr",
|
||||
"serde",
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@ nostr = { version = "0.45", features = ["nip04", "nip44", "nip49", "os-rng"] }
|
||||
nostr-sdk = { version = "0.45", default-features = false, features = ["rustls-tls-webpki-roots"] }
|
||||
tokio = { version = "1", features = ["rt-multi-thread", "macros", "process", "time", "signal"] }
|
||||
async-channel = "2"
|
||||
serde_json = "1"
|
||||
serde_json = { version = "1", features = ["preserve_order"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
zeroize = "1"
|
||||
qrcode = { version = "0.14", features = ["image"] }
|
||||
|
||||
+13
-4
@@ -39,6 +39,16 @@ pub struct SigningKey {
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
/// One event tag, in the shape the dialog shows it.
|
||||
///
|
||||
/// `name` is the tag's first element (`p`, `e`, `t`, …); `values` the rest. All
|
||||
/// of it is client-supplied text and must only ever be rendered as plain text.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct TagView {
|
||||
pub name: String,
|
||||
pub values: Vec<String>,
|
||||
}
|
||||
|
||||
/// What is being asked for.
|
||||
///
|
||||
/// Everything the dialog shows beyond the client identity is already reduced to
|
||||
@@ -60,10 +70,9 @@ pub enum Request {
|
||||
/// show it in full; it is **not** reduced to a short preview, because
|
||||
/// the original bytes are what gets signed.
|
||||
content: String,
|
||||
/// A short, plain summary of the event's tags, or `None` when there are
|
||||
/// none. Shown below the content, because for some kinds (a follow list,
|
||||
/// a relay list) the tags *are* the payload.
|
||||
tags: Option<String>,
|
||||
/// The event's tags, in order, for the Tags group below the content.
|
||||
/// Empty when the event has none.
|
||||
tags: Vec<TagView>,
|
||||
},
|
||||
/// A message would be encrypted to someone.
|
||||
Encrypt {
|
||||
|
||||
+24
-41
@@ -16,7 +16,9 @@ use nostr::nips::{nip04, nip44};
|
||||
use nostr::prelude::*;
|
||||
use nostr_sdk::prelude::*;
|
||||
|
||||
use self::approval::{Approver, ClientInfo, Decision, Notice, Question, Request, SigningKey};
|
||||
use self::approval::{
|
||||
Approver, ClientInfo, Decision, Notice, Question, Request, SigningKey, TagView,
|
||||
};
|
||||
use self::policy::RateLimiter;
|
||||
use crate::store::apps::{AppRegistry, Permission};
|
||||
use crate::store::audit::{self, ActivityLog, Entry};
|
||||
@@ -769,7 +771,7 @@ async fn handle_request(event: &Event, config: &SignerConfig) -> Result<Option<E
|
||||
let request = Request::Sign {
|
||||
kind,
|
||||
content: content(&incoming.content),
|
||||
tags: tag_summary(&incoming.tags),
|
||||
tags: tags(&incoming.tags),
|
||||
};
|
||||
let keys = authorize(
|
||||
config,
|
||||
@@ -1215,46 +1217,24 @@ fn content(text: &str) -> String {
|
||||
shown
|
||||
}
|
||||
|
||||
/// A short, plain summary of an event's tags: `name=value, name=value, …`.
|
||||
/// How many tags travel to the dialog, and how many values of each.
|
||||
///
|
||||
/// `None` when there are no tags, so the dialog leaves the row out rather than
|
||||
/// showing "No tags".
|
||||
fn tag_summary(tags: &[Tag]) -> Option<String> {
|
||||
if tags.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let mut parts = Vec::new();
|
||||
for tag in tags.iter().take(6) {
|
||||
let fields = tag.as_slice();
|
||||
let name = fields.first().map(String::as_str).unwrap_or_default();
|
||||
let values = fields
|
||||
.iter()
|
||||
.skip(1)
|
||||
.take(2)
|
||||
.map(|value| shorten(value))
|
||||
.collect::<Vec<_>>()
|
||||
.join(" ");
|
||||
if values.is_empty() {
|
||||
parts.push(name.to_string());
|
||||
} else {
|
||||
parts.push(format!("{name}={values}"));
|
||||
}
|
||||
}
|
||||
let mut summary = parts.join(", ");
|
||||
if tags.len() > 6 {
|
||||
summary.push_str(", …");
|
||||
}
|
||||
Some(summary)
|
||||
}
|
||||
/// The dialog shows the values in full, but a hostile event must not put an
|
||||
/// unbounded list on the channel.
|
||||
const TAG_LIMIT: usize = 500;
|
||||
const TAG_FIELDS: usize = 8;
|
||||
|
||||
/// Trim a single tag value so a wall of text cannot flood the dialog.
|
||||
fn shorten(value: &str) -> String {
|
||||
const LIMIT: usize = 16;
|
||||
let mut shown: String = value.chars().take(LIMIT).collect();
|
||||
if value.chars().count() > LIMIT {
|
||||
shown.push('…');
|
||||
}
|
||||
shown
|
||||
/// The event's tags, in order, for the dialog's Tags group.
|
||||
fn tags(tags: &[Tag]) -> Vec<TagView> {
|
||||
tags.iter()
|
||||
.take(TAG_LIMIT)
|
||||
.filter_map(|tag| {
|
||||
let mut fields = tag.as_slice().iter();
|
||||
let name = fields.next()?.clone();
|
||||
let values = fields.take(TAG_FIELDS).cloned().collect();
|
||||
Some(TagView { name, values })
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// The peer pubkey that the first parameter of the nip04/nip44 calls refers to.
|
||||
@@ -1903,7 +1883,10 @@ mod tests {
|
||||
assert_eq!(shown.0, 1);
|
||||
assert!(shown.1.contains("hello world"), "content was {:?}", shown.1);
|
||||
assert!(
|
||||
shown.2.as_deref().unwrap_or_default().contains("t=nostr"),
|
||||
shown
|
||||
.2
|
||||
.iter()
|
||||
.any(|tag| tag.name == "t" && tag.values.iter().any(|value| value == "nostr")),
|
||||
"tags were {:?}",
|
||||
shown.2
|
||||
);
|
||||
|
||||
+2
-2
@@ -467,8 +467,8 @@ fn details(
|
||||
}
|
||||
// Tags stay in view rather than collapsed under Details, because for
|
||||
// some kinds they are the payload itself.
|
||||
if let Some(tags) = tags {
|
||||
content.append(&plain::text_field("Tags", tags));
|
||||
if !tags.is_empty() {
|
||||
content.append(&content::tags_card(tags, widget, deadline));
|
||||
}
|
||||
}
|
||||
Request::Encrypt { preview, .. } => content.append(&block("Message", preview)),
|
||||
|
||||
+143
-13
@@ -18,12 +18,16 @@ use gtk::prelude::*;
|
||||
use libadwaita as adw;
|
||||
use serde_json::Value;
|
||||
|
||||
use crate::signer::approval::TagView;
|
||||
use crate::ui::approval::remaining;
|
||||
use crate::ui::{kind, plain};
|
||||
|
||||
/// How many top-level fields the inline card shows before the "+N more" row.
|
||||
const INLINE_FIELDS: usize = 8;
|
||||
|
||||
/// How many tags the inline Tags card shows before the "+N more tags" row.
|
||||
const INLINE_TAGS: usize = 8;
|
||||
|
||||
/// How deep the renderer walks before it stops and says so.
|
||||
const MAX_DEPTH: usize = 8;
|
||||
|
||||
@@ -174,20 +178,8 @@ pub fn card(
|
||||
.css_classes(["dim-label"])
|
||||
.build(),
|
||||
);
|
||||
// A `GtkButton` shows either a label or an icon: setting both replaces one
|
||||
// with the other, so the label and the go-next arrow are an explicit child.
|
||||
let view_button = gtk::Button::builder()
|
||||
.css_classes(["flat"])
|
||||
.valign(gtk::Align::Center)
|
||||
.build();
|
||||
let view_button = link_button("View full content");
|
||||
view_button.set_tooltip_text(Some("Show the full content and its original text"));
|
||||
let view_content = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(6)
|
||||
.build();
|
||||
view_content.append(>k::Image::from_icon_name("go-next-symbolic"));
|
||||
view_content.append(>k::Label::new(Some("View full content")));
|
||||
view_button.set_child(Some(&view_content));
|
||||
header.append(&view_button);
|
||||
container.append(&header);
|
||||
|
||||
@@ -220,6 +212,135 @@ pub fn card(
|
||||
container
|
||||
}
|
||||
|
||||
/// A flat button with a go-next arrow and a label.
|
||||
///
|
||||
/// A `GtkButton` shows either a label or an icon: setting both replaces one with
|
||||
/// the other, so the label and the arrow are an explicit child.
|
||||
fn link_button(label: &str) -> gtk::Button {
|
||||
let button = gtk::Button::builder()
|
||||
.css_classes(["flat"])
|
||||
.valign(gtk::Align::Center)
|
||||
.build();
|
||||
let content = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(6)
|
||||
.build();
|
||||
content.append(>k::Image::from_icon_name("go-next-symbolic"));
|
||||
content.append(>k::Label::new(Some(label)));
|
||||
button.set_child(Some(&content));
|
||||
button
|
||||
}
|
||||
|
||||
/// The Tags card: one row per tag, capped, with a "View all tags" button when
|
||||
/// the list is longer.
|
||||
///
|
||||
/// Tags can be the payload itself (a follow list, a relay list), so they get a
|
||||
/// group of their own below the content rather than a collapsed row.
|
||||
pub fn tags_card(
|
||||
tags: &[TagView],
|
||||
parent: &impl IsA<gtk::Widget>,
|
||||
deadline: Option<Instant>,
|
||||
) -> gtk::Box {
|
||||
let container = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(4)
|
||||
.build();
|
||||
|
||||
let header = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Horizontal)
|
||||
.spacing(12)
|
||||
.build();
|
||||
header.append(
|
||||
>k::Label::builder()
|
||||
.label("Tags")
|
||||
.xalign(0.0)
|
||||
.hexpand(true)
|
||||
.css_classes(["dim-label"])
|
||||
.build(),
|
||||
);
|
||||
header.append(
|
||||
>k::Label::builder()
|
||||
.label(tags.len().to_string())
|
||||
.css_classes(["dim-label"])
|
||||
.build(),
|
||||
);
|
||||
if tags.len() > INLINE_TAGS {
|
||||
let view_button = link_button("View all tags");
|
||||
view_button.set_tooltip_text(Some("Show every tag"));
|
||||
header.append(&view_button);
|
||||
let window = parent.root().and_downcast::<gtk::Window>();
|
||||
let all = tags.to_vec();
|
||||
view_button.connect_clicked(move |_| {
|
||||
present_tags_preview(window.as_ref(), &all, deadline);
|
||||
});
|
||||
}
|
||||
container.append(&header);
|
||||
|
||||
let body = tags_body(tags, INLINE_TAGS);
|
||||
body.set_margin_top(8);
|
||||
body.set_margin_bottom(8);
|
||||
body.set_margin_start(8);
|
||||
body.set_margin_end(8);
|
||||
body.set_hexpand(true);
|
||||
|
||||
let scrolled = gtk::ScrolledWindow::builder()
|
||||
.child(&body)
|
||||
.propagate_natural_height(true)
|
||||
.max_content_height(INLINE_HEIGHT)
|
||||
.hscrollbar_policy(gtk::PolicyType::Never)
|
||||
.build();
|
||||
|
||||
let frame = gtk::Box::builder().css_classes(["card"]).build();
|
||||
frame.append(&scrolled);
|
||||
container.append(&frame);
|
||||
|
||||
container
|
||||
}
|
||||
|
||||
/// The tag rows, capped, with a "+N more tags" row when there are more.
|
||||
fn tags_body(tags: &[TagView], limit: usize) -> gtk::Widget {
|
||||
let boxed = gtk::Box::builder()
|
||||
.orientation(gtk::Orientation::Vertical)
|
||||
.spacing(6)
|
||||
.build();
|
||||
for tag in tags.iter().take(limit) {
|
||||
boxed.append(&tag_row(tag));
|
||||
}
|
||||
if tags.len() > limit {
|
||||
boxed.append(&dim(&format!("+{} more tags", tags.len() - limit)));
|
||||
}
|
||||
boxed.upcast()
|
||||
}
|
||||
|
||||
/// One tag: its name beside its full value(s).
|
||||
fn tag_row(tag: &TagView) -> gtk::Box {
|
||||
plain::field(&tag.name, &plain::label(&tag.values.join(" ")))
|
||||
}
|
||||
|
||||
/// The full tag list, in a dialog like the content preview.
|
||||
fn present_tags_preview(parent: Option<>k::Window>, tags: &[TagView], deadline: Option<Instant>) {
|
||||
let page = padded_scroller(&tags_body(tags, tags.len()));
|
||||
let header = adw::HeaderBar::new();
|
||||
let toolbar = adw::ToolbarView::new();
|
||||
toolbar.add_top_bar(&header);
|
||||
toolbar.set_content(Some(&page));
|
||||
toolbar.set_size_request(plain::DIALOG_WIDTH, -1);
|
||||
|
||||
let dialog = adw::Dialog::builder()
|
||||
.title("Event tags")
|
||||
.follows_content_size(true)
|
||||
.child(&toolbar)
|
||||
.build();
|
||||
dialog.present(parent);
|
||||
|
||||
if let Some(left) = remaining(deadline) {
|
||||
let dialog = dialog.clone();
|
||||
glib::timeout_add_local_once(left.max(Duration::from_millis(1)), move || {
|
||||
dialog.force_close();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// The inline body: labelled rows for a JSON object, otherwise pretty or plain
|
||||
/// text.
|
||||
fn inline(content: &str, kind_number: u16) -> gtk::Widget {
|
||||
@@ -500,6 +621,15 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
/// Fields follow the order the client sent, not alphabetical order: with the
|
||||
/// default `serde_json` map, `about` would sort first.
|
||||
#[test]
|
||||
fn fields_keep_the_raw_json_order() {
|
||||
let parsed = fields(r#"{"name":"n","display_name":"d","about":"a"}"#);
|
||||
let labels: Vec<&str> = parsed.iter().map(|field| field.label.as_str()).collect();
|
||||
assert_eq!(labels, ["Name", "Display name", "About"]);
|
||||
}
|
||||
|
||||
/// Only a bounded number of fields is shown inline, and the rest are counted
|
||||
/// rather than silently dropped.
|
||||
#[test]
|
||||
|
||||
Reference in new issue
Block a user