Skip to content

Commit cd0e740

Browse files
baxennpub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79
andauthored
Add owned community onboarding (#2123)
Signed-off-by: npub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79 <1a7496365ae2df8adb1b932a1a6f68b9cb3950f3d179c8a451c32d194e8f7dc1@sprout-oss.stage.blox.sqprod.co> Co-authored-by: npub1rf6fvdj6ut0c4kcmjv4p5mmgh89nj58n69uu3fz3cvk3jn500hqs7emz79 <1a7496365ae2df8adb1b932a1a6f68b9cb3950f3d179c8a451c32d194e8f7dc1@sprout-oss.stage.blox.sqprod.co>
1 parent bf5acab commit cd0e740

9 files changed

Lines changed: 1194 additions & 125 deletions

File tree

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

Lines changed: 59 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@ const BUILDERLAB_ORIGIN: &str = "https://app.builderlab.xyz";
2424
#[derive(Default)]
2525
pub(crate) struct BuilderlabSession(Mutex<Option<StoredSession>>);
2626

27+
#[derive(Default)]
28+
pub(crate) struct BuilderlabLogin(Mutex<Option<PendingLogin>>);
29+
30+
struct PendingLogin {
31+
id: uuid::Uuid,
32+
cancel: oneshot::Sender<()>,
33+
}
34+
2735
struct StoredSession {
2836
credential: String,
2937
}
@@ -130,6 +138,7 @@ pub(crate) async fn start_builderlab_login(
130138
app: tauri::AppHandle,
131139
app_state: tauri::State<'_, crate::app_state::AppState>,
132140
session: tauri::State<'_, BuilderlabSession>,
141+
login: tauri::State<'_, BuilderlabLogin>,
133142
) -> Result<BuilderlabAuthInfo, String> {
134143
let listener = TcpListener::bind("127.0.0.1:0")
135144
.await
@@ -158,19 +167,38 @@ pub(crate) async fn start_builderlab_login(
158167
return Err(format!("could not open Builderlab authentication: {error}"));
159168
}
160169

161-
let exchange_code = match tokio::time::timeout(LOGIN_TIMEOUT, receiver).await {
162-
Ok(Ok(Ok(code))) => code,
163-
Ok(Ok(Err(error))) => {
164-
server.abort();
165-
return Err(error);
166-
}
167-
Ok(Err(_)) => {
168-
server.abort();
169-
return Err("local authentication callback stopped unexpectedly".to_owned());
170+
let login_id = uuid::Uuid::new_v4();
171+
let (cancel_sender, mut cancel_receiver) = oneshot::channel();
172+
{
173+
let mut pending = login.0.lock().map_err(|error| error.to_string())?;
174+
if let Some(previous) = pending.take() {
175+
let _ = previous.cancel.send(());
170176
}
171-
Err(_) => {
177+
*pending = Some(PendingLogin {
178+
id: login_id,
179+
cancel: cancel_sender,
180+
});
181+
}
182+
183+
let exchange_code = tokio::select! {
184+
result = tokio::time::timeout(LOGIN_TIMEOUT, receiver) => match result {
185+
Ok(Ok(Ok(code))) => code,
186+
Ok(Ok(Err(error))) => {
187+
server.abort();
188+
return Err(error);
189+
}
190+
Ok(Err(_)) => {
191+
server.abort();
192+
return Err("local authentication callback stopped unexpectedly".to_owned());
193+
}
194+
Err(_) => {
195+
server.abort();
196+
return Err("Builderlab authentication timed out".to_owned());
197+
}
198+
},
199+
_ = &mut cancel_receiver => {
172200
server.abort();
173-
return Err("Builderlab authentication timed out".to_owned());
201+
return Err("Builderlab authentication canceled".to_owned());
174202
}
175203
};
176204
server.abort();
@@ -206,6 +234,16 @@ pub(crate) async fn start_builderlab_login(
206234
email: me.email,
207235
name: me.name,
208236
};
237+
{
238+
let mut pending = login.0.lock().map_err(|error| error.to_string())?;
239+
if pending
240+
.as_ref()
241+
.is_none_or(|pending| pending.id != login_id)
242+
{
243+
return Err("Builderlab authentication canceled".to_owned());
244+
}
245+
*pending = None;
246+
}
209247
*session.0.lock().map_err(|error| error.to_string())? = Some(StoredSession {
210248
credential: exchanged.session_credential,
211249
});
@@ -242,6 +280,16 @@ pub(crate) async fn get_builderlab_auth(
242280
}
243281
}
244282

283+
#[tauri::command]
284+
pub(crate) fn cancel_builderlab_login(
285+
login: tauri::State<'_, BuilderlabLogin>,
286+
) -> Result<(), String> {
287+
if let Some(pending) = login.0.lock().map_err(|error| error.to_string())?.take() {
288+
let _ = pending.cancel.send(());
289+
}
290+
Ok(())
291+
}
292+
245293
#[tauri::command]
246294
pub(crate) fn clear_builderlab_auth(
247295
session: tauri::State<'_, BuilderlabSession>,

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,7 @@ pub fn run() {
426426
.build()
427427
});
428428

429-
// Only register the updater in release builds that were compiled with a
430-
// real updater configuration. Local unsigned builds omit that config and
431-
// should still launch for debugging.
429+
// Register the updater only in configured release builds; omit it locally.
432430
#[cfg(buzz_updater_enabled)]
433431
let builder = if cfg!(debug_assertions) {
434432
builder
@@ -451,6 +449,7 @@ pub fn run() {
451449
.manage(ClipboardState::new())
452450
.manage(PendingCommunityDeepLinks::default())
453451
.manage(BuilderlabSession::default())
452+
.manage(BuilderlabLogin::default())
454453
.manage(commands::pairing::PairingHandle::new())
455454
.setup(move |app| {
456455
let app_handle = app.handle().clone();
@@ -744,6 +743,7 @@ pub fn run() {
744743
take_pending_community_deep_link,
745744
acknowledge_pending_community_deep_link,
746745
start_builderlab_login,
746+
cancel_builderlab_login,
747747
get_builderlab_auth,
748748
clear_builderlab_auth,
749749
get_builderlab_nostr_identity,
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import { invoke } from "@tauri-apps/api/core";
2+
3+
export const HOSTED_COMMUNITY_SUFFIX = "communities.buzz.xyz";
4+
export const HOSTED_COMMUNITY_LIMIT = 3;
5+
export const VALID_HOSTED_COMMUNITY_NAME = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
6+
7+
export type BuilderlabAuth = {
8+
email?: string;
9+
name?: string;
10+
expiresAt: string;
11+
};
12+
13+
export type HostedCommunityApiError = {
14+
code?: string;
15+
message?: string;
16+
setup_needed?: boolean;
17+
};
18+
19+
export type HostedNostrIdentity = {
20+
npub?: string;
21+
pubkey_hex?: string;
22+
};
23+
24+
export type HostedIdentityResponse = {
25+
identity?: HostedNostrIdentity;
26+
error?: HostedCommunityApiError;
27+
correlation_id?: string;
28+
};
29+
30+
export type HostedCommunity = {
31+
id?: string;
32+
name?: string;
33+
slug?: string;
34+
normalized_host?: string;
35+
owner_pubkey?: string;
36+
archived_at?: string | null;
37+
};
38+
39+
export type HostedCommunitiesResponse = {
40+
communities?: HostedCommunity[];
41+
error?: HostedCommunityApiError;
42+
correlation_id?: string;
43+
};
44+
45+
export type HostedCommunityAvailabilityResponse = {
46+
available?: boolean;
47+
normalized_host?: string;
48+
error?: HostedCommunityApiError;
49+
correlation_id?: string;
50+
};
51+
52+
export type HostedCommunityMutationResponse = {
53+
community?: HostedCommunity;
54+
error?: HostedCommunityApiError;
55+
correlation_id?: string;
56+
};
57+
58+
export type HostedCommunityAccount = {
59+
communities: HostedCommunity[];
60+
identity: HostedNostrIdentity | null;
61+
};
62+
63+
export function hostedCommunityErrorMessage(
64+
error: HostedCommunityApiError | undefined,
65+
correlationId: string | undefined,
66+
fallback: string,
67+
) {
68+
const messages: Record<string, string> = {
69+
missing_mapping: "Connect your Buzz identity before creating a community.",
70+
invalid_name: "Use lowercase letters, numbers, and hyphens.",
71+
taken: "That Buzz address is already taken.",
72+
limit_reached: `You've reached the limit of ${HOSTED_COMMUNITY_LIMIT} hosted communities.`,
73+
relay_unavailable: "Community provisioning is temporarily unavailable.",
74+
identity_already_bound:
75+
"This Builderlab account is connected to another Buzz identity.",
76+
pubkey_already_bound:
77+
"This Buzz identity is connected to another Builderlab account.",
78+
not_owner: "Only the community owner can do that.",
79+
transferee_not_registered:
80+
"That person needs a connected Buzz identity before you can transfer ownership to them.",
81+
};
82+
const message = messages[error?.code ?? ""] ?? error?.message ?? fallback;
83+
return correlationId
84+
? `${message} Correlation ID: ${correlationId}`
85+
: message;
86+
}
87+
88+
export function hostedCommunityRelayUrl(community: HostedCommunity) {
89+
const host = community.normalized_host?.trim();
90+
return host ? `wss://${host.replace(/^wss?:\/\//, "")}` : null;
91+
}
92+
93+
export function getBuilderlabAuth() {
94+
return invoke<BuilderlabAuth | null>("get_builderlab_auth");
95+
}
96+
97+
export function cancelBuilderlabLogin() {
98+
return invoke<void>("cancel_builderlab_login");
99+
}
100+
101+
export function clearBuilderlabAuth() {
102+
return invoke<void>("clear_builderlab_auth");
103+
}
104+
105+
export function startBuilderlabLogin() {
106+
return invoke<BuilderlabAuth>("start_builderlab_login");
107+
}
108+
109+
export async function loadHostedCommunityAccount(): Promise<HostedCommunityAccount> {
110+
const [identityResponse, communitiesResponse] = await Promise.all([
111+
invoke<HostedIdentityResponse>("get_builderlab_nostr_identity"),
112+
invoke<HostedCommunitiesResponse>("list_builderlab_communities"),
113+
]);
114+
if (
115+
identityResponse.error &&
116+
identityResponse.error.code !== "unauthorized" &&
117+
!identityResponse.error.setup_needed
118+
) {
119+
throw new Error(
120+
hostedCommunityErrorMessage(
121+
identityResponse.error,
122+
identityResponse.correlation_id,
123+
"Could not load the connected Buzz identity.",
124+
),
125+
);
126+
}
127+
if (communitiesResponse.error && !communitiesResponse.error.setup_needed) {
128+
throw new Error(
129+
hostedCommunityErrorMessage(
130+
communitiesResponse.error,
131+
communitiesResponse.correlation_id,
132+
"Could not load communities.",
133+
),
134+
);
135+
}
136+
return {
137+
identity: identityResponse.identity ?? null,
138+
communities: communitiesResponse.communities ?? [],
139+
};
140+
}
141+
142+
export function bindBuilderlabIdentity() {
143+
return invoke<HostedIdentityResponse>("bind_builderlab_nostr_identity");
144+
}
145+
146+
export function deleteBuilderlabIdentity() {
147+
return invoke<HostedIdentityResponse>("delete_builderlab_nostr_identity");
148+
}
149+
150+
export function checkHostedCommunityName(name: string) {
151+
return invoke<HostedCommunityAvailabilityResponse>(
152+
"check_builderlab_community_name",
153+
{ name },
154+
);
155+
}
156+
157+
export function createHostedCommunity(name: string) {
158+
return invoke<HostedCommunityMutationResponse>(
159+
"create_builderlab_community",
160+
{
161+
name,
162+
},
163+
);
164+
}

0 commit comments

Comments
 (0)