Skip to content

Commit e67303f

Browse files
wesbillmanbaxennpub1cl47vfhsqpqy9pwndphpm36vcp7vvz5h2js4qpqm5yewzj7nutkq7xyw8c
authored
Gate default relay auto-connect behind release flag (#2589)
Signed-off-by: npub1cl47vfhsqpqy9pwndphpm36vcp7vvz5h2js4qpqm5yewzj7nutkq7xyw8c <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz> Co-authored-by: Bradley Axen <[email protected]> Co-authored-by: npub1cl47vfhsqpqy9pwndphpm36vcp7vvz5h2js4qpqm5yewzj7nutkq7xyw8c <c7ebe626f000404285d3686e1dc74cc07cc60a9754a150041ba132e14bd3e2ec@buzz.block.builderlab.xyz>
1 parent f3f7688 commit e67303f

11 files changed

Lines changed: 263 additions & 28 deletions

File tree

‎Justfile‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,19 @@ desktop-tauri-test-compiled-flags: _ensure-sidecar-stubs
211211
cd desktop/src-tauri
212212
echo "=== Clean build (no flag) → expect false ==="
213213
env -u BUZZ_BUILD_OBSERVER_ARCHIVE_DEFAULT \
214+
-u BUZZ_BUILD_AUTO_CONNECT_DEFAULT_RELAY \
214215
BUZZ_TEST_EXPECTED_OBSERVER_ARCHIVE_DEFAULT=false \
215216
cargo test observer_archive_default_enabled_matches_expected -- --ignored --nocapture
216-
echo "=== Internal build (flag set) → expect true ==="
217+
env -u BUZZ_BUILD_AUTO_CONNECT_DEFAULT_RELAY \
218+
BUZZ_TEST_EXPECTED_AUTO_CONNECT_DEFAULT_RELAY=false \
219+
cargo test compiled_flag_matches_expected -- --ignored --nocapture
220+
echo "=== Internal build (flags set) → expect true ==="
217221
BUZZ_BUILD_OBSERVER_ARCHIVE_DEFAULT=1 \
218222
BUZZ_TEST_EXPECTED_OBSERVER_ARCHIVE_DEFAULT=true \
219223
cargo test observer_archive_default_enabled_matches_expected -- --ignored --nocapture
224+
BUZZ_BUILD_AUTO_CONNECT_DEFAULT_RELAY=1 \
225+
BUZZ_TEST_EXPECTED_AUTO_CONNECT_DEFAULT_RELAY=true \
226+
cargo test compiled_flag_matches_expected -- --ignored --nocapture
220227
echo "Both compiled states verified."
221228
222229
# Build the full desktop Tauri app locally (unsigned, for testing)

‎desktop/src-tauri/build.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn main() {
1515
println!("cargo:rerun-if-env-changed=BUZZ_BUILD_RELAY_RECONNECT_CMD");
1616
println!("cargo:rerun-if-env-changed=BUZZ_BUILD_OBSERVER_ARCHIVE_DEFAULT");
1717
println!("cargo:rerun-if-env-changed=BUZZ_BUILD_AGENT_METRIC_ARCHIVE_DEFAULT");
18+
println!("cargo:rerun-if-env-changed=BUZZ_BUILD_AUTO_CONNECT_DEFAULT_RELAY");
1819
println!("cargo:rustc-check-cfg=cfg(buzz_updater_enabled)");
1920

2021
if let Ok(relay_url) = std::env::var("BUZZ_RELAY_URL") {
@@ -89,6 +90,13 @@ fn main() {
8990
println!("cargo:rustc-env=BUZZ_DESKTOP_BUILD_AGENT_METRIC_ARCHIVE_DEFAULT=1");
9091
}
9192

93+
// Presence-only release capability: internal desktop builds opt into
94+
// auto-connecting their configured default relay on first run. OSS builds
95+
// leave this unset and retain explicit community selection.
96+
if std::env::var("BUZZ_BUILD_AUTO_CONNECT_DEFAULT_RELAY").is_ok() {
97+
println!("cargo:rustc-env=BUZZ_DESKTOP_BUILD_AUTO_CONNECT_DEFAULT_RELAY=1");
98+
}
99+
92100
let updater_public_key = std::env::var("BUZZ_UPDATER_PUBLIC_KEY")
93101
.ok()
94102
.map(|value| value.trim().to_string())

‎desktop/src-tauri/src/commands/identity.rs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ pub fn get_default_relay_url() -> String {
5454
relay::relay_ws_url()
5555
}
5656

57+
#[tauri::command]
58+
pub fn auto_connect_default_relay_enabled() -> bool {
59+
option_env!("BUZZ_DESKTOP_BUILD_AUTO_CONNECT_DEFAULT_RELAY").is_some()
60+
}
61+
62+
#[cfg(test)]
63+
mod auto_connect_default_relay_tests {
64+
use super::auto_connect_default_relay_enabled;
65+
66+
#[test]
67+
#[ignore]
68+
fn compiled_flag_matches_expected() {
69+
let expected = std::env::var("BUZZ_TEST_EXPECTED_AUTO_CONNECT_DEFAULT_RELAY")
70+
.expect("compiled-flag test requires an expected value");
71+
assert_eq!(
72+
auto_connect_default_relay_enabled(),
73+
expected == "true" || expected == "1"
74+
);
75+
}
76+
}
77+
5778
#[tauri::command]
5879
pub fn is_shared_identity() -> bool {
5980
std::env::var("BUZZ_SHARE_IDENTITY")

‎desktop/src-tauri/src/lib.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ pub fn run() {
692692
get_presence,
693693
get_os_idle_seconds,
694694
get_default_relay_url,
695+
auto_connect_default_relay_enabled,
695696
get_legacy_workspace_storage,
696697
is_shared_identity,
697698
get_relay_ws_url,

‎desktop/src/features/communities/communityStorage.test.mjs‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ import test from "node:test";
33

44
import {
55
clearCommunityStorage,
6+
initFirstCommunity,
67
migrateLegacyCommunityStorage,
8+
shouldAutoConnectDefaultRelay,
79
} from "./communityStorage.ts";
810

911
function createMemoryStorage(initial = {}) {
1012
const values = new Map(Object.entries(initial));
1113
return {
14+
values,
1215
getItem: (key) => values.get(key) ?? null,
1316
setItem: (key, value) => values.set(key, String(value)),
1417
removeItem: (key) => values.delete(key),
@@ -46,6 +49,46 @@ test("migrateLegacyCommunityStorage does not overwrite new community state", ()
4649
assert.equal(storage.getItem("buzz-active-community-id"), "new");
4750
});
4851

52+
test("signed-build relay defaults auto-connect during first-run onboarding", () => {
53+
assert.equal(
54+
shouldAutoConnectDefaultRelay("wss://buzz.block.builderlab.xyz"),
55+
true,
56+
);
57+
assert.equal(shouldAutoConnectDefaultRelay("ws://localhost:3000"), false);
58+
assert.equal(shouldAutoConnectDefaultRelay("ws://127.0.0.1:3000"), false);
59+
assert.equal(shouldAutoConnectDefaultRelay("ws://[::1]:3000"), false);
60+
assert.equal(shouldAutoConnectDefaultRelay("ws://0.0.0.0:3000"), false);
61+
assert.equal(shouldAutoConnectDefaultRelay("http://localhost:3000"), false);
62+
assert.equal(
63+
shouldAutoConnectDefaultRelay("https://relay.example.com"),
64+
false,
65+
);
66+
assert.equal(shouldAutoConnectDefaultRelay("relay.example.com"), false);
67+
assert.equal(shouldAutoConnectDefaultRelay("not a valid relay"), false);
68+
});
69+
70+
test("failed first-community write preserves existing community data", () => {
71+
const storage = createMemoryStorage({
72+
"buzz-communities": '[{"id":"existing"}]',
73+
"buzz-workspaces": '[{"id":"legacy"}]',
74+
"buzz-active-workspace-id": "legacy",
75+
});
76+
storage.setItem = (key, value) => {
77+
if (key === "buzz-communities") {
78+
throw new Error("QuotaExceededError");
79+
}
80+
storage.values.set(key, String(value));
81+
};
82+
globalThis.localStorage = storage;
83+
globalThis.window = { localStorage: storage };
84+
85+
assert.equal(initFirstCommunity("wss://relay.example.com", "pubkey"), null);
86+
assert.equal(storage.getItem("buzz-communities"), '[{"id":"existing"}]');
87+
assert.equal(storage.getItem("buzz-active-community-id"), null);
88+
assert.equal(storage.getItem("buzz-workspaces"), '[{"id":"legacy"}]');
89+
assert.equal(storage.getItem("buzz-active-workspace-id"), "legacy");
90+
});
91+
4992
test("clearCommunityStorage removes new and legacy state", () => {
5093
const storage = createMemoryStorage({
5194
"buzz-communities": "new",

‎desktop/src/features/communities/communityStorage.ts‎

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,11 @@ export function loadCommunities(): Community[] {
8181
}
8282
}
8383

84-
export function saveCommunities(communities: Community[]): void {
85-
setLocalStorageItemWithRecovery(COMMUNITIES_KEY, JSON.stringify(communities));
84+
export function saveCommunities(communities: Community[]): boolean {
85+
return setLocalStorageItemWithRecovery(
86+
COMMUNITIES_KEY,
87+
JSON.stringify(communities),
88+
);
8689
}
8790

8891
export function clearCommunityStorage(storage: Storage = localStorage): void {
@@ -97,8 +100,8 @@ export function loadActiveCommunityId(): string | null {
97100
return localStorage.getItem(ACTIVE_COMMUNITY_KEY);
98101
}
99102

100-
export function saveActiveCommunityId(id: string): void {
101-
setLocalStorageItemWithRecovery(ACTIVE_COMMUNITY_KEY, id);
103+
export function saveActiveCommunityId(id: string): boolean {
104+
return setLocalStorageItemWithRecovery(ACTIVE_COMMUNITY_KEY, id);
102105
}
103106

104107
export function normalizeRelayUrl(url: string): string {
@@ -108,13 +111,29 @@ export function normalizeRelayUrl(url: string): string {
108111
return url;
109112
}
110113

114+
function isLocalRelayHost(hostname: string): boolean {
115+
return ["localhost", "127.0.0.1", "[::1]", "0.0.0.0"].includes(hostname);
116+
}
117+
118+
export function shouldAutoConnectDefaultRelay(relayUrl: string): boolean {
119+
try {
120+
const parsed = new URL(relayUrl);
121+
return (
122+
(parsed.protocol === "ws:" || parsed.protocol === "wss:") &&
123+
!isLocalRelayHost(parsed.hostname)
124+
);
125+
} catch {
126+
return false;
127+
}
128+
}
129+
111130
export function deriveCommunityName(relayUrl: string): string {
112131
try {
113132
const url = new URL(
114133
relayUrl.replace("ws://", "http://").replace("wss://", "https://"),
115134
);
116135
const host = url.hostname;
117-
if (host === "localhost" || host === "127.0.0.1") {
136+
if (isLocalRelayHost(host)) {
118137
return "Local Dev";
119138
}
120139
const parts = host.split(".");
@@ -136,17 +155,40 @@ export function initFirstCommunity(
136155
relayUrl: string,
137156
pubkey: string,
138157
name?: string,
139-
): Community {
158+
): Community | null {
140159
const normalizedUrl = normalizeRelayUrl(relayUrl);
141160
const trimmedName = name?.trim();
142161
const community: Community = {
143162
id: crypto.randomUUID(),
144163
name: trimmedName || deriveCommunityName(normalizedUrl),
145164
relayUrl: normalizedUrl,
165+
// Compiled default relays must admit the first token-less connection; there
166+
// is no invite-token prompt on this auto-connect path.
146167
pubkey,
147168
addedAt: new Date().toISOString(),
148169
};
149-
saveCommunities([community]);
150-
saveActiveCommunityId(community.id);
170+
const previousActiveCommunityId = localStorage.getItem(ACTIVE_COMMUNITY_KEY);
171+
const didSaveActiveCommunity = saveActiveCommunityId(community.id);
172+
if (!didSaveActiveCommunity) {
173+
return null;
174+
}
175+
176+
if (!saveCommunities([community])) {
177+
// A failed setItem leaves the existing communities value untouched. Roll
178+
// back only the active-ID write so inconsistent pre-existing data is never
179+
// destroyed while recovering from a quota failure.
180+
try {
181+
if (previousActiveCommunityId === null) {
182+
localStorage.removeItem(ACTIVE_COMMUNITY_KEY);
183+
} else {
184+
localStorage.setItem(ACTIVE_COMMUNITY_KEY, previousActiveCommunityId);
185+
}
186+
} catch {
187+
// Best effort: persistence is already unavailable, and callers will stay
188+
// on setup instead of reloading.
189+
}
190+
return null;
191+
}
192+
151193
return community;
152194
}

‎desktop/src/features/communities/useCommunityInit.ts‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ import { useEffect, useRef, useState } from "react";
22

33
import { relayClient } from "@/shared/api/relayClient";
44
import { resetRateLimitGate } from "@/shared/api/relayRateLimitGate";
5-
import { applyCommunity, getDefaultRelayUrl } from "@/shared/api/tauri";
5+
import {
6+
applyCommunity,
7+
autoConnectDefaultRelayEnabled,
8+
getDefaultRelayUrl,
9+
} from "@/shared/api/tauri";
610
import { getIdentity } from "@/shared/api/tauriIdentity";
711
import { getOverrides } from "@/shared/features";
812
import { resetMediaCaches } from "@/shared/lib/mediaUrl";
@@ -25,7 +29,10 @@ import { resetSidebarRelayConnectionCardState } from "@/features/sidebar/ui/useS
2529
import { clearMarkdownNodeCache } from "@/shared/ui/markdown/nodeCache";
2630
import { resetVideoPlayerState } from "@/shared/ui/videoPlayerState";
2731

28-
import { initFirstCommunity } from "./communityStorage";
32+
import {
33+
initFirstCommunity,
34+
shouldAutoConnectDefaultRelay,
35+
} from "./communityStorage";
2936
import type { Community } from "./types";
3037

3138
/**
@@ -107,13 +114,33 @@ export function useCommunityInit(
107114
if (!activeCommunity) {
108115
try {
109116
const defaultRelayUrl = await getDefaultRelayUrl();
117+
const autoConnectDefaultRelay =
118+
await autoConnectDefaultRelayEnabled();
110119

111-
if (isSharedIdentity) {
120+
// Internal builds explicitly opt into treating their reviewed default
121+
// relay as the first community. Public builds retain community
122+
// selection even when BUZZ_RELAY_URL is overridden at runtime.
123+
if (
124+
isSharedIdentity ||
125+
(autoConnectDefaultRelay &&
126+
shouldAutoConnectDefaultRelay(defaultRelayUrl))
127+
) {
112128
const identity = await getIdentity();
113129
if (cancelled) return;
114-
initFirstCommunity(defaultRelayUrl, identity.pubkey);
115-
if (!cancelled) {
130+
const community = initFirstCommunity(
131+
defaultRelayUrl,
132+
identity.pubkey,
133+
);
134+
if (community && !cancelled) {
116135
window.location.reload();
136+
return;
137+
}
138+
if (!cancelled) {
139+
setResult({
140+
isReady: false,
141+
needsSetup: true,
142+
defaultRelayUrl,
143+
});
117144
}
118145
return;
119146
}

‎desktop/src/shared/api/tauri.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,10 @@ export function getDefaultRelayUrl(): Promise<string> {
359359
return invokeTauri<string>("get_default_relay_url");
360360
}
361361

362+
export function autoConnectDefaultRelayEnabled(): Promise<boolean> {
363+
return invokeTauri<boolean>("auto_connect_default_relay_enabled");
364+
}
365+
362366
export function isSharedIdentity(): Promise<boolean> {
363367
return invokeTauri<boolean>("is_shared_identity");
364368
}

‎desktop/src/testing/e2eBridge.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ type E2eConfig = {
417417
};
418418
relayHttpUrl?: string;
419419
relayWsUrl?: string;
420+
autoConnectDefaultRelay?: boolean;
420421
identity?: TestIdentity;
421422
};
422423

@@ -9816,6 +9817,8 @@ export function maybeInstallE2eTauriMocks() {
98169817
return getRelayWsUrl(activeConfig);
98179818
case "get_default_relay_url":
98189819
return getRelayWsUrl(activeConfig);
9820+
case "auto_connect_default_relay_enabled":
9821+
return activeConfig?.autoConnectDefaultRelay ?? false;
98199822
case "get_legacy_workspace_storage":
98209823
return {
98219824
workspaces: null,

0 commit comments

Comments
 (0)