Skip to content

Commit e2fae50

Browse files
kalvinnchaupeon
andauthored
fix(sidebar): save channel moves on desktop and move channels by drag (#474)
Signed-off-by: peon <9ac6794b000690b7e814eb1805ad32405d0bec7d52838de3a86cf967565dacc0@buzz.block.builderlab.xyz> Co-authored-by: peon <9ac6794b000690b7e814eb1805ad32405d0bec7d52838de3a86cf967565dacc0@buzz.block.builderlab.xyz>
1 parent 73064e3 commit e2fae50

13 files changed

Lines changed: 659 additions & 136 deletions

File tree

‎docs/channels.md‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,19 @@ preferences relocated it during the transaction. Read actions require
125125
`frontier-sync`; hosts lacking mute writes keep read-only preference projection.
126126
The packaged host decodes verified self-encrypted sidebar records and signs
127127
only validated sidebar coordinates; it uses its signed HTTP writer and confirms
128-
via readback.
128+
via readback. Sidebar records and channel recipes share kind 30078, so that
129+
writer selects the admission contract by the event's `d` coordinate: the four
130+
sidebar coordinates must match what the sidebar signer produces, and every
131+
other coordinate must satisfy the recipe contract.
129132

130133
Move channel, Create new, exclusive Starred placement and startup presentation also
131-
belong to this persistent sidebar. The session serializes placement, sort and mute
134+
belong to this persistent sidebar. Rows in saved groups and **Channels** can also be
135+
dragged with a pointer onto another saved group or **Channels**; the drop is the same
136+
Move intent, with the same gate, optimistic placement, rollback notice and Retry.
137+
Only a channel's select surface starts a drag, the surface its row menu wraps; its
138+
sessions and session draft do not.
139+
Starred, Forums and Direct messages take no part in dragging, and the row menu
140+
remains the keyboard path. The session serializes placement, sort and mute
132141
writes through one queue, retaining one confirmed preferences snapshot beneath
133142
pending Move and Sort projections. Each confirmation updates only its owned fields
134143
before reapplying pending intent; failure cannot roll back unrelated confirmed

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"dependencies": {
4040
"@base-ui/react": "1.8.0",
4141
"@deepseek-ai/cordis": "4.0.2",
42+
"@dnd-kit/core": "6.3.1",
4243
"@emoji-mart/data": "1.2.1",
4344
"@fontsource-variable/inter": "^5.2.8",
4445
"@fontsource/jetbrains-mono": "^5.3.0",

‎pnpm-lock.yaml‎

Lines changed: 37 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎scripts/design-system/check-icons.mjs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const nonIconDependencies = new Set([
1313
"@biomejs/biome",
1414
"@buzz/author",
1515
"@deepseek-ai/cordis",
16+
"@dnd-kit/core", // Pointer drag and drop for sidebar channel moves.
1617
"@emoji-mart/data",
1718
"@fontsource-variable/inter",
1819
"@fontsource/jetbrains-mono",

‎src-tauri/src/identity.rs‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ fn validate_sidebar_payload(coordinate: &str, value: &serde_json::Value) -> Resu
380380
Ok(())
381381
}
382382

383-
fn sidebar_coordinate(value: &str) -> bool {
383+
pub(crate) fn sidebar_coordinate(value: &str) -> bool {
384384
matches!(
385385
value,
386386
"channel-sections" | "channel-stars" | "channel-mutes" | "channel-sort"
@@ -496,6 +496,20 @@ impl IdentityHost {
496496
.await
497497
}
498498

499+
/// Admit for publication only a record shaped exactly like `sign_sidebar` output.
500+
pub(crate) async fn admit_sidebar(&self, event: serde_json::Value) -> Result<()> {
501+
let tags = event["tags"].clone();
502+
let decoded = self.decode_sidebar(vec![event]).await?;
503+
let (coordinate, payload) = decoded
504+
.as_object()
505+
.and_then(|records| records.iter().next())
506+
.ok_or("Invalid sidebar record")?;
507+
if tags != serde_json::json!([["d", coordinate], ["t", coordinate]]) {
508+
return Err("Invalid sidebar record".into());
509+
}
510+
validate_sidebar_payload(coordinate, payload)
511+
}
512+
499513
/// Sign only self-encrypted kind-30078 sidebar data; never expose a general NIP-44 primitive.
500514
pub(crate) async fn sign_sidebar(
501515
&self,

‎src-tauri/src/relay.rs‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,28 @@ async fn verify_owned_event(host: &IdentityHost, event: &serde_json::Value) -> R
408408
verify_signature(event)
409409
}
410410

411+
async fn admit_app_data(
412+
host: &IdentityHost,
413+
event: &serde_json::Value,
414+
community: &str,
415+
) -> Result<()> {
416+
verify_owned_event(host, event).await?;
417+
// Sidebar preferences and channel recipes share kind 30078; the `d` coordinate selects the contract.
418+
let sidebar = event["tags"].as_array().is_some_and(|tags| {
419+
tags.iter().any(|tag| {
420+
tag[0] == "d"
421+
&& tag[1]
422+
.as_str()
423+
.is_some_and(crate::identity::sidebar_coordinate)
424+
})
425+
});
426+
if sidebar {
427+
return host.admit_sidebar(event.clone()).await;
428+
}
429+
kit::validate_ciphertext(host, event, community).await?;
430+
Ok(())
431+
}
432+
411433
fn verify_signature(event: &serde_json::Value) -> Result<()> {
412434
let parsed: nostr::event::Event =
413435
serde_json::from_value(event.clone()).map_err(|_| "Invalid outgoing signature")?;
@@ -484,8 +506,7 @@ pub(crate) async fn relay_http(
484506
return Err("Choose between one and eight other people.".into());
485507
}
486508
if kind == Some(30078) {
487-
verify_owned_event(host.inner(), &event).await?;
488-
kit::validate_ciphertext(host.inner(), &event, &community).await?;
509+
admit_app_data(host.inner(), &event, &community).await?;
489510
}
490511
if kind == Some(9007) {
491512
verify_owned_event(host.inner(), &event).await?;

‎src-tauri/src/relay/tests.rs‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,83 @@ async fn sidebar_ipc_only_decodes_verified_self_coordinates_and_signs_valid_payl
853853
);
854854
}
855855

856+
#[tokio::test]
857+
async fn event_writer_admits_sidebar_records_and_recipes_by_their_own_coordinate() {
858+
let host = IdentityHost::fixture();
859+
let community = "https://relay.test";
860+
for (coordinate, payload) in [
861+
(
862+
"channel-sections",
863+
serde_json::json!({"version":1,"sections":[{"id":"group","name":"Group","order":0}],"assignments":{"channel":"group"}}),
864+
),
865+
(
866+
"channel-stars",
867+
serde_json::json!({"version":1,"channels":{"channel":{"starred":true,"updatedAt":1}}}),
868+
),
869+
(
870+
"channel-mutes",
871+
serde_json::json!({"version":1,"channels":{"channel":{"muted":true,"updatedAt":1}}}),
872+
),
873+
(
874+
"channel-sort",
875+
serde_json::json!({"version":1,"groups":{"channels":"recent"}}),
876+
),
877+
] {
878+
let event = host
879+
.sign_sidebar(coordinate.into(), payload, 1)
880+
.await
881+
.unwrap();
882+
admit_app_data(&host, &event, community).await.unwrap();
883+
}
884+
let sidebar = host
885+
.sign_sidebar(
886+
"channel-sort".into(),
887+
serde_json::json!({"version":1,"groups":{}}),
888+
1,
889+
)
890+
.await
891+
.unwrap();
892+
let mut tampered = sidebar.clone();
893+
tampered["content"] = serde_json::json!("changed");
894+
assert!(admit_app_data(&host, &tampered, community).await.is_err());
895+
let mut foreign = sidebar.clone();
896+
foreign["pubkey"] = serde_json::json!("02".repeat(32));
897+
assert!(admit_app_data(&host, &foreign, community).await.is_err());
898+
899+
// Anything outside the four sidebar coordinates is still held to the recipe contract.
900+
let sign = |tags: Vec<Vec<String>>, content: String| {
901+
host.sign(crate::identity::EventTemplate {
902+
kind: 30078,
903+
created_at: 1,
904+
content,
905+
tags,
906+
})
907+
};
908+
let recipe = serde_json::json!({"version":1,"community":community,"deleted":false,
909+
"value":{"type":"team","id":"team-one","name":"Team","agents":[]}});
910+
let ciphertext = host.kit_cipher(recipe.to_string(), true).await.unwrap();
911+
let coordinate = "buzz-channel-kit-v1:https%3A%2F%2Frelay.test:team:team-one";
912+
let kit_tags = |d: &str| {
913+
vec![
914+
vec!["d".to_owned(), d.to_owned()],
915+
vec!["t".to_owned(), "buzz-channel-kit-v1".to_owned()],
916+
]
917+
};
918+
let valid = sign(kit_tags(coordinate), ciphertext.clone())
919+
.await
920+
.unwrap();
921+
admit_app_data(&host, &valid, community).await.unwrap();
922+
// A recipe cannot borrow a sidebar coordinate, and a sidebar record cannot borrow another.
923+
for d in ["channel-sections", "read-state:other", "unknown"] {
924+
let event = sign(kit_tags(d), ciphertext.clone()).await.unwrap();
925+
assert!(admit_app_data(&host, &event, community).await.is_err());
926+
}
927+
let mut duplicate = kit_tags(coordinate);
928+
duplicate.push(vec!["d".to_owned(), "channel-stars".to_owned()]);
929+
let event = sign(duplicate, ciphertext).await.unwrap();
930+
assert!(admit_app_data(&host, &event, community).await.is_err());
931+
}
932+
856933
#[tokio::test]
857934
async fn sidebar_decoder_interoperates_with_nostr_tools_nip44_v2() {
858935
// Produced with nostr-tools 2.25.2, private key [1; 32].

‎src/bundled/channels/ChannelSidebarItem.tsx‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
useLayoutEffect,
55
useRef,
66
useSyncExternalStore,
7+
type ComponentType,
78
type ReactNode,
89
} from "react";
910
import { Avatar } from "../../shared/design-system/ui/Avatar";
@@ -53,6 +54,14 @@ type ItemProps = {
5354
onHideDm?: (id: string) => void;
5455
menuEnabled?: boolean;
5556
sectionKey?: string | undefined;
57+
/** Frames the select surface alone, like the context menu; never the child sessions. */
58+
selectFrame?:
59+
| ComponentType<{
60+
channelId: string;
61+
sectionKey: string;
62+
children: ReactNode;
63+
}>
64+
| undefined;
5665
onOpenMenu?: (
5766
channel: ChannelSummary,
5867
sectionKey: string,
@@ -111,6 +120,7 @@ function ChannelSidebarItemCore({
111120
onHideDm,
112121
menuEnabled,
113122
sectionKey,
123+
selectFrame: SelectFrame,
114124
onOpenMenu,
115125
menuOpen = false,
116126
menuAnchor,
@@ -230,7 +240,7 @@ function ChannelSidebarItemCore({
230240
);
231241
// Keep popup semantics on separate DOM nodes: activity owns the button,
232242
// the context menu wraps only its select surface, not the child sessions.
233-
return menuEnabled ? (
243+
const select = menuEnabled ? (
234244
<ContextMenuTrigger
235245
render={<div />}
236246
onKeyDown={(event) => {
@@ -249,6 +259,13 @@ function ChannelSidebarItemCore({
249259
) : (
250260
activity
251261
);
262+
return SelectFrame && sectionKey ? (
263+
<SelectFrame channelId={channel.id} sectionKey={sectionKey}>
264+
{select}
265+
</SelectFrame>
266+
) : (
267+
select
268+
);
252269
}}
253270
selected={selected}
254271
presenceDescription={

‎src/bundled/channels/Channels.module.css‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,34 @@
267267
.channelSection + .channelSection {
268268
margin-top: var(--space-4);
269269
}
270+
.channelSection[data-drop-target] {
271+
border-radius: var(--radius-row);
272+
background: var(--affordance-panel-hover);
273+
}
274+
[data-channel-dragging] {
275+
opacity: 0.3;
276+
}
277+
.channelDragOverlay {
278+
display: flex;
279+
align-items: center;
280+
gap: var(--space-2);
281+
width: max-content;
282+
max-width: 100%;
283+
padding: var(--space-1) var(--space-2);
284+
border: 1px solid var(--border-standard);
285+
border-radius: var(--radius-control);
286+
background: var(--surface-popover);
287+
box-shadow: var(--shadow-sm);
288+
color: var(--text-standard);
289+
font-size: var(--text-body-sm);
290+
line-height: var(--text-body-sm--line-height);
291+
cursor: grabbing;
292+
}
293+
.channelDragOverlay > span {
294+
overflow: hidden;
295+
text-overflow: ellipsis;
296+
white-space: nowrap;
297+
}
270298
.sectionHeader {
271299
position: relative;
272300
}

‎src/bundled/channels/SidebarSection.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { FadingLabel } from "./FadingLabel";
2-
import { useId, useState, type ReactNode } from "react";
2+
import { useId, useState, type ReactNode, type Ref } from "react";
33
import {
44
MenuIcon,
55
MenuItem,
@@ -34,6 +34,8 @@ export function SidebarSection({
3434
createChannel,
3535
newMessage,
3636
sort,
37+
dropRef,
38+
dropTarget = false,
3739
children,
3840
}: {
3941
sectionKey: string;
@@ -53,6 +55,8 @@ export function SidebarSection({
5355
change: (mode: "alpha" | "recent") => void;
5456
}
5557
| undefined;
58+
dropRef?: Ref<HTMLDivElement> | undefined;
59+
dropTarget?: boolean;
5660
children: ReactNode;
5761
}) {
5862
const id = useId();
@@ -80,7 +84,12 @@ export function SidebarSection({
8084
</div>
8185
);
8286
return (
83-
<div className={styles.channelSection} data-sidebar-section={sectionKey}>
87+
<div
88+
ref={dropRef}
89+
className={styles.channelSection}
90+
data-sidebar-section={sectionKey}
91+
data-drop-target={dropTarget || undefined}
92+
>
8493
<div className={styles.sectionHeader}>
8594
<details open={open}>
8695
{/* biome-ignore lint/a11y/noStaticElementInteractions: summary has native keyboard activation. */}

0 commit comments

Comments
 (0)