-
-
Notifications
You must be signed in to change notification settings - Fork 81
fix(pair): WA Web compliant pairing QR + companion_platform_{id,display} #593
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
bd80606
7a7a84b
1279782
506a0f9
28485d2
6410355
bf5e34a
22f88eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| //! Companion registration client type carried by the pairing QR string. | ||
| //! Mirrors `WAWebCompanionRegClientUtils.DEVICE_PLATFORM` | ||
| //! (`docs/captured-js/WAWeb/Link/DeviceQrcode.react.js`, `Companion/RegClientUtils.js`). | ||
|
|
||
| use std::fmt; | ||
|
|
||
| use waproto::whatsapp as wa; | ||
|
|
||
| /// Web-client type for the pairing QR. Discriminants are the wire integers, | ||
| /// so [`Self::code`] is a zero-cost cast. | ||
| #[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)] | ||
| #[repr(i32)] | ||
| pub enum CompanionWebClientType { | ||
|
Comment on lines
+10
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| #[default] | ||
| Unknown = 0, | ||
| Chrome = 1, | ||
| Edge = 2, | ||
| Firefox = 3, | ||
| Ie = 4, | ||
| Opera = 5, | ||
| Safari = 6, | ||
| Electron = 7, | ||
| Uwp = 8, | ||
| OtherWebClient = 9, | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
|
|
||
| impl CompanionWebClientType { | ||
| pub const fn code(self) -> i32 { | ||
| self as i32 | ||
| } | ||
| } | ||
|
|
||
| impl fmt::Display for CompanionWebClientType { | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| self.code().fmt(f) | ||
| } | ||
| } | ||
|
|
||
| /// Maps `DeviceProps.PlatformType` to the QR pairing enum. Non-web platforms | ||
| /// fall through to `OtherWebClient`, matching WA Web's fall-through for | ||
| /// unrecognised `WAWebMiscBrowserUtils.info().name`. | ||
| pub const fn companion_web_client_type_for_platform( | ||
| pt: wa::device_props::PlatformType, | ||
| ) -> CompanionWebClientType { | ||
| use CompanionWebClientType as C; | ||
| use wa::device_props::PlatformType as P; | ||
| match pt { | ||
| P::Unknown => C::Unknown, | ||
| P::Chrome => C::Chrome, | ||
| P::Firefox => C::Firefox, | ||
| P::Ie => C::Ie, | ||
| P::Opera => C::Opera, | ||
| P::Safari => C::Safari, | ||
| P::Edge => C::Edge, | ||
| P::Desktop => C::Electron, | ||
| P::Uwp => C::Uwp, | ||
| P::Ipad | ||
| | P::AndroidTablet | ||
| | P::Ohana | ||
| | P::Aloha | ||
| | P::Catalina | ||
| | P::TclTv | ||
| | P::IosPhone | ||
| | P::IosCatalyst | ||
| | P::AndroidPhone | ||
| | P::AndroidAmbiguous | ||
| | P::WearOs | ||
| | P::ArWrist | ||
| | P::ArDevice | ||
| | P::Vr | ||
| | P::CloudApi | ||
| | P::Smartglasses => C::OtherWebClient, | ||
| } | ||
| } | ||
|
|
||
| /// Missing or out-of-range `platform_type` decays to `Unknown` so the QR flow | ||
| /// never fails on DeviceProps shape. | ||
| pub fn companion_web_client_type_for_props(props: &wa::DeviceProps) -> CompanionWebClientType { | ||
| props | ||
| .platform_type | ||
| .and_then(|v| wa::device_props::PlatformType::try_from(v).ok()) | ||
| .map(companion_web_client_type_for_platform) | ||
| .unwrap_or_default() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn wire_codes_match_wa_web() { | ||
| assert_eq!(CompanionWebClientType::Unknown.code(), 0); | ||
| assert_eq!(CompanionWebClientType::Chrome.code(), 1); | ||
| assert_eq!(CompanionWebClientType::Edge.code(), 2); | ||
| assert_eq!(CompanionWebClientType::Firefox.code(), 3); | ||
| assert_eq!(CompanionWebClientType::Ie.code(), 4); | ||
| assert_eq!(CompanionWebClientType::Opera.code(), 5); | ||
| assert_eq!(CompanionWebClientType::Safari.code(), 6); | ||
| assert_eq!(CompanionWebClientType::Electron.code(), 7); | ||
| assert_eq!(CompanionWebClientType::Uwp.code(), 8); | ||
| assert_eq!(CompanionWebClientType::OtherWebClient.code(), 9); | ||
| } | ||
|
|
||
| #[test] | ||
| fn browser_platform_types_round_trip() { | ||
| use CompanionWebClientType as C; | ||
| use wa::device_props::PlatformType as P; | ||
| assert_eq!(companion_web_client_type_for_platform(P::Chrome), C::Chrome); | ||
| assert_eq!( | ||
| companion_web_client_type_for_platform(P::Firefox), | ||
| C::Firefox | ||
| ); | ||
| assert_eq!(companion_web_client_type_for_platform(P::Edge), C::Edge); | ||
| assert_eq!(companion_web_client_type_for_platform(P::Safari), C::Safari); | ||
| assert_eq!(companion_web_client_type_for_platform(P::Opera), C::Opera); | ||
| assert_eq!(companion_web_client_type_for_platform(P::Ie), C::Ie); | ||
| } | ||
|
|
||
| #[test] | ||
| fn desktop_maps_to_electron_and_uwp_preserved() { | ||
| use CompanionWebClientType as C; | ||
| use wa::device_props::PlatformType as P; | ||
| assert_eq!( | ||
| companion_web_client_type_for_platform(P::Desktop), | ||
| C::Electron | ||
| ); | ||
| assert_eq!(companion_web_client_type_for_platform(P::Uwp), C::Uwp); | ||
| } | ||
|
|
||
| #[test] | ||
| fn mobile_and_xr_collapse_to_other() { | ||
| use CompanionWebClientType as C; | ||
| use wa::device_props::PlatformType as P; | ||
| for pt in [ | ||
| P::Ipad, | ||
| P::AndroidPhone, | ||
| P::AndroidTablet, | ||
| P::IosPhone, | ||
| P::IosCatalyst, | ||
| P::AndroidAmbiguous, | ||
| P::WearOs, | ||
| P::ArWrist, | ||
| P::ArDevice, | ||
| P::Vr, | ||
| P::Ohana, | ||
| P::Aloha, | ||
| P::Catalina, | ||
| P::TclTv, | ||
| P::CloudApi, | ||
| P::Smartglasses, | ||
| ] { | ||
| assert_eq!( | ||
| companion_web_client_type_for_platform(pt), | ||
| C::OtherWebClient, | ||
| "{pt:?} must fall back to OtherWebClient", | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn for_props_reads_platform_type() { | ||
| let props = wa::DeviceProps { | ||
| platform_type: Some(wa::device_props::PlatformType::Chrome as i32), | ||
| ..Default::default() | ||
| }; | ||
| assert_eq!( | ||
| companion_web_client_type_for_props(&props), | ||
| CompanionWebClientType::Chrome, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn for_props_missing_platform_type_is_unknown() { | ||
| let props = wa::DeviceProps::default(); | ||
| assert_eq!( | ||
| companion_web_client_type_for_props(&props), | ||
| CompanionWebClientType::Unknown, | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn for_props_invalid_platform_type_is_unknown() { | ||
| let props = wa::DeviceProps { | ||
| platform_type: Some(9999), | ||
| ..Default::default() | ||
| }; | ||
| assert_eq!( | ||
| companion_web_client_type_for_props(&props), | ||
| CompanionWebClientType::Unknown, | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
make_qr_data_with_client_typeis public but its parameter type (CompanionWebClientType) is not exposed bywhatsapp-rust, so downstream crates that depend only onwhatsapp-rustcannot call this new override path without adding a directwacoredependency. That makes the advertised override seam effectively unusable for current consumers of the high-level crate.Useful? React with 👍 / 👎.