Skip to content

Commit

Permalink
Add GreetingAttemptJoined event
Browse files Browse the repository at this point in the history
  • Loading branch information
vxgmichel committed Feb 6, 2025
1 parent e2648a7 commit 1a12e5d
Show file tree
Hide file tree
Showing 16 changed files with 307 additions and 6 deletions.
6 changes: 6 additions & 0 deletions bindings/electron/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,11 @@ export interface ClientEventGreetingAttemptCancelled {
token: string
greeting_attempt: string
}
export interface ClientEventGreetingAttemptJoined {
tag: "GreetingAttemptJoined"
token: string
greeting_attempt: string
}
export interface ClientEventGreetingAttemptReady {
tag: "GreetingAttemptReady"
token: string
Expand Down Expand Up @@ -828,6 +833,7 @@ export interface ClientEventWorkspacesSelfListChanged {
export type ClientEvent =
| ClientEventExpiredOrganization
| ClientEventGreetingAttemptCancelled
| ClientEventGreetingAttemptJoined
| ClientEventGreetingAttemptReady
| ClientEventIncompatibleServer
| ClientEventInvitationChanged
Expand Down
64 changes: 64 additions & 0 deletions bindings/electron/src/meths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4527,6 +4527,40 @@ fn variant_client_event_js_to_rs<'a>(
greeting_attempt,
})
}
"ClientEventGreetingAttemptJoined" => {
let token = {
let js_val: Handle<JsString> = obj.get(cx, "token")?;
{
let custom_from_rs_string =
|s: String| -> Result<libparsec::InvitationToken, _> {
libparsec::InvitationToken::from_hex(s.as_str())
.map_err(|e| e.to_string())
};
match custom_from_rs_string(js_val.value(cx)) {
Ok(val) => val,
Err(err) => return cx.throw_type_error(err),
}
}
};
let greeting_attempt = {
let js_val: Handle<JsString> = obj.get(cx, "greetingAttempt")?;
{
let custom_from_rs_string =
|s: String| -> Result<libparsec::GreetingAttemptID, _> {
libparsec::GreetingAttemptID::from_hex(s.as_str())
.map_err(|e| e.to_string())
};
match custom_from_rs_string(js_val.value(cx)) {
Ok(val) => val,
Err(err) => return cx.throw_type_error(err),
}
}
};
Ok(libparsec::ClientEvent::GreetingAttemptJoined {
token,
greeting_attempt,
})
}
"ClientEventGreetingAttemptReady" => {
let token = {
let js_val: Handle<JsString> = obj.get(cx, "token")?;
Expand Down Expand Up @@ -4901,6 +4935,36 @@ fn variant_client_event_rs_to_js<'a>(
.or_throw(cx)?;
js_obj.set(cx, "greetingAttempt", js_greeting_attempt)?;
}
libparsec::ClientEvent::GreetingAttemptJoined {
token,
greeting_attempt,
..
} => {
let js_tag = JsString::try_new(cx, "ClientEventGreetingAttemptJoined").or_throw(cx)?;
js_obj.set(cx, "tag", js_tag)?;
let js_token = JsString::try_new(cx, {
let custom_to_rs_string =
|x: libparsec::InvitationToken| -> Result<String, &'static str> { Ok(x.hex()) };
match custom_to_rs_string(token) {
Ok(ok) => ok,
Err(err) => return cx.throw_type_error(err),
}
})
.or_throw(cx)?;
js_obj.set(cx, "token", js_token)?;
let js_greeting_attempt = JsString::try_new(cx, {
let custom_to_rs_string =
|x: libparsec::GreetingAttemptID| -> Result<String, &'static str> {
Ok(x.hex())
};
match custom_to_rs_string(greeting_attempt) {
Ok(ok) => ok,
Err(err) => return cx.throw_type_error(err),
}
})
.or_throw(cx)?;
js_obj.set(cx, "greetingAttempt", js_greeting_attempt)?;
}
libparsec::ClientEvent::GreetingAttemptReady {
token,
greeting_attempt,
Expand Down
4 changes: 4 additions & 0 deletions bindings/generator/api/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class GreetingAttemptCancelled:
token: InvitationToken
greeting_attempt: GreetingAttemptID

class GreetingAttemptJoined:
token: InvitationToken
greeting_attempt: GreetingAttemptID

class TooMuchDriftWithServerClock:
server_timestamp: DateTime
client_timestamp: DateTime
Expand Down
73 changes: 73 additions & 0 deletions bindings/web/src/meths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4924,6 +4924,46 @@ fn variant_client_event_js_to_rs(obj: JsValue) -> Result<libparsec::ClientEvent,
greeting_attempt,
})
}
"ClientEventGreetingAttemptJoined" => {
let token = {
let js_val = Reflect::get(&obj, &"token".into())?;
js_val
.dyn_into::<JsString>()
.ok()
.and_then(|s| s.as_string())
.ok_or_else(|| TypeError::new("Not a string"))
.and_then(|x| {
let custom_from_rs_string =
|s: String| -> Result<libparsec::InvitationToken, _> {
libparsec::InvitationToken::from_hex(s.as_str())
.map_err(|e| e.to_string())
};
custom_from_rs_string(x).map_err(|e| TypeError::new(e.as_ref()))
})
.map_err(|_| TypeError::new("Not a valid InvitationToken"))?
};
let greeting_attempt = {
let js_val = Reflect::get(&obj, &"greetingAttempt".into())?;
js_val
.dyn_into::<JsString>()
.ok()
.and_then(|s| s.as_string())
.ok_or_else(|| TypeError::new("Not a string"))
.and_then(|x| {
let custom_from_rs_string =
|s: String| -> Result<libparsec::GreetingAttemptID, _> {
libparsec::GreetingAttemptID::from_hex(s.as_str())
.map_err(|e| e.to_string())
};
custom_from_rs_string(x).map_err(|e| TypeError::new(e.as_ref()))
})
.map_err(|_| TypeError::new("Not a valid GreetingAttemptID"))?
};
Ok(libparsec::ClientEvent::GreetingAttemptJoined {
token,
greeting_attempt,
})
}
"ClientEventGreetingAttemptReady" => {
let token = {
let js_val = Reflect::get(&obj, &"token".into())?;
Expand Down Expand Up @@ -5362,6 +5402,39 @@ fn variant_client_event_rs_to_js(rs_obj: libparsec::ClientEvent) -> Result<JsVal
});
Reflect::set(&js_obj, &"greetingAttempt".into(), &js_greeting_attempt)?;
}
libparsec::ClientEvent::GreetingAttemptJoined {
token,
greeting_attempt,
..
} => {
Reflect::set(
&js_obj,
&"tag".into(),
&"ClientEventGreetingAttemptJoined".into(),
)?;
let js_token = JsValue::from_str({
let custom_to_rs_string =
|x: libparsec::InvitationToken| -> Result<String, &'static str> { Ok(x.hex()) };
match custom_to_rs_string(token) {
Ok(ok) => ok,
Err(err) => return Err(JsValue::from(TypeError::new(err.as_ref()))),
}
.as_ref()
});
Reflect::set(&js_obj, &"token".into(), &js_token)?;
let js_greeting_attempt = JsValue::from_str({
let custom_to_rs_string =
|x: libparsec::GreetingAttemptID| -> Result<String, &'static str> {
Ok(x.hex())
};
match custom_to_rs_string(greeting_attempt) {
Ok(ok) => ok,
Err(err) => return Err(JsValue::from(TypeError::new(err.as_ref()))),
}
.as_ref()
});
Reflect::set(&js_obj, &"greetingAttempt".into(), &js_greeting_attempt)?;
}
libparsec::ClientEvent::GreetingAttemptReady {
token,
greeting_attempt,
Expand Down
7 changes: 7 additions & 0 deletions client/src/parsec/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
ClientChangeAuthenticationErrorTag,
ClientEvent,
ClientEventGreetingAttemptCancelled,
ClientEventGreetingAttemptJoined,
ClientEventGreetingAttemptReady,
ClientEventInvitationChanged,
ClientEventTag,
Expand Down Expand Up @@ -212,6 +213,12 @@ export async function login(
greetingAttempt: (event as ClientEventGreetingAttemptCancelled).greetingAttempt,
});
break;
case ClientEventTag.GreetingAttemptJoined:
distributor.dispatchEvent(Events.GreetingAttemptJoined, {
token: (event as ClientEventGreetingAttemptJoined).token,
greetingAttempt: (event as ClientEventGreetingAttemptJoined).greetingAttempt,
});
break;
case ClientEventTag.IncompatibleServer:
window.electronAPI.log('warn', `IncompatibleServerEvent: ${JSON.stringify(event)}`);
distributor.dispatchEvent(Events.IncompatibleServer, { reason: event.detail }, { delay: 5000 });
Expand Down
1 change: 1 addition & 0 deletions client/src/parsec/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export type {
ClientCreateWorkspaceError,
ClientEvent,
ClientEventGreetingAttemptCancelled,
ClientEventGreetingAttemptJoined,
ClientEventGreetingAttemptReady,
ClientEventInvitationChanged,
ClientEventPing,
Expand Down
7 changes: 7 additions & 0 deletions client/src/plugins/libparsec/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ export type ClientDeleteShamirRecoveryError =
export enum ClientEventTag {
ExpiredOrganization = 'ClientEventExpiredOrganization',
GreetingAttemptCancelled = 'ClientEventGreetingAttemptCancelled',
GreetingAttemptJoined = 'ClientEventGreetingAttemptJoined',
GreetingAttemptReady = 'ClientEventGreetingAttemptReady',
IncompatibleServer = 'ClientEventIncompatibleServer',
InvitationChanged = 'ClientEventInvitationChanged',
Expand Down Expand Up @@ -833,6 +834,11 @@ export interface ClientEventGreetingAttemptCancelled {
token: InvitationToken
greetingAttempt: GreetingAttemptID
}
export interface ClientEventGreetingAttemptJoined {
tag: ClientEventTag.GreetingAttemptJoined
token: InvitationToken
greetingAttempt: GreetingAttemptID
}
export interface ClientEventGreetingAttemptReady {
tag: ClientEventTag.GreetingAttemptReady
token: InvitationToken
Expand Down Expand Up @@ -915,6 +921,7 @@ export interface ClientEventWorkspacesSelfListChanged {
export type ClientEvent =
| ClientEventExpiredOrganization
| ClientEventGreetingAttemptCancelled
| ClientEventGreetingAttemptJoined
| ClientEventGreetingAttemptReady
| ClientEventIncompatibleServer
| ClientEventInvitationChanged
Expand Down
11 changes: 9 additions & 2 deletions client/src/services/eventDistributor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ enum Events {
LogoutRequested = 1 << 13,
EntrySyncStarted = 1 << 14,
GreetingAttemptReady = 1 << 15,
GreetingAttemptCancelled = 1 << 15,
GreetingAttemptCancelled = 1 << 16,
GreetingAttemptJoined = 1 << 17,
}

interface WorkspaceCreatedData {
Expand All @@ -45,6 +46,11 @@ interface GreetingAttemptCancelledData {
greetingAttempt: GreetingAttemptID;
}

interface GreetingAttemptJoinedData {
token: InvitationToken;
greetingAttempt: GreetingAttemptID;
}

interface UpdateAvailabilityData {
updateAvailable: boolean;
version?: string;
Expand All @@ -67,7 +73,8 @@ type EventData =
| EntrySyncData
| IncompatibleServerData
| GreetingAttemptReadyData
| GreetingAttemptCancelledData;
| GreetingAttemptCancelledData
| GreetingAttemptJoinedData;

interface Callback {
id: string;
Expand Down
16 changes: 16 additions & 0 deletions libparsec/crates/client/src/event_bus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,13 +376,29 @@ impl_events!(
/// This event is fired by the connection monitor.
///
/// It is used to inform the user that a greeting attempt has been cancelled.
/// It can be used to invalidate a previous `GreetingAttemptReady` event,
/// since this greeting attempt can no longer be joined.
///
/// Note this event will be fired (i.e. the server pushes it to us) even if
/// we are at the origin of the change (e.g. we cancelled the greeting attempt).
GreetingAttemptCancelled {
token: InvitationToken,
greeting_attempt: GreetingAttemptID,
},

/// This event is fired by the connection monitor.
///
/// It is used to inform the user that a greeting attempt has been joined.
/// It can be used to invalidate a previous `GreetingAttemptReady` event,
/// since this greeting attempt can no longer be joined.
///
/// Note this event will be fired (i.e. the server pushes it to us) even if
/// we are at the origin of the change (e.g. we cancelled the greeting attempt).
GreetingAttemptJoined {
token: InvitationToken,
greeting_attempt: GreetingAttemptID,
},

/// This event is fired by the connection monitor.
///
/// The PKI enrollments have changed on server side (e.g. a new enrollment
Expand Down
10 changes: 10 additions & 0 deletions libparsec/crates/client/src/monitors/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@ fn dispatch_api_event(event: APIEvent, event_bus: &EventBus) {
};
event_bus.send(&event);
}
APIEvent::GreetingAttemptJoined {
token,
greeting_attempt,
} => {
let event = EventGreetingAttemptJoined {
token,
greeting_attempt,
};
event_bus.send(&event);
}
APIEvent::PkiEnrollment {} => {
let event = EventPkiEnrollmentUpdated {};
event_bus.send(&event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,20 @@
}
]
},
{
"name": "GreetingAttemptJoined",
"discriminant_value": "GREETING_ATTEMPT_JOINED",
"fields": [
{
"name": "token",
"type": "InvitationToken"
},
{
"name": "greeting_attempt",
"type": "GreetingAttemptID"
}
]
},
{
"name": "PkiEnrollment",
"discriminant_value": "PKI_ENROLLMENT"
Expand Down
Loading

0 comments on commit 1a12e5d

Please sign in to comment.