Skip to content
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

Add consent & user preference streaming to WASM bindings #1647

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions bindings_wasm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@
- `yarn`: Installs all dependencies (required before building)
- `yarn build`: Build a release version of the WASM bindings for the current platform
- `yarn lint`: Run cargo clippy and fmt checks
- `yarn check:macos`: Run cargo check for macOS (requires Homebrew and `llvm` to be installed)

# Publishing

To release a new version of the bindings, update the version in `package.json` with the appropriate semver value and add an entry to the CHANGELOG.md file. Once merged, manually trigger the `Release WASM Bindings` workflow to build and publish the bindings.
1 change: 1 addition & 0 deletions bindings_wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"build": "yarn clean && yarn build:web && yarn build:copy && yarn clean:release",
"build:web": "cargo xtask build BindingsWasm --out-dir ./dist --plain -- --release",
"build:copy": "node scripts/copyFiles.js",
"check:macos": "CC_wasm32_unknown_unknown=/opt/homebrew/opt/llvm/bin/clang cargo check --target wasm32-unknown-unknown",
"clean:release": "rm -f ./dist/package.json",
"clean": "rm -rf ./dist",
"lint": "yarn lint:clippy && yarn lint:fmt",
Expand Down
14 changes: 14 additions & 0 deletions bindings_wasm/src/consent_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ impl From<Consent> for StoredConsentRecord {
}
}

impl From<StoredConsentRecord> for Consent {
fn from(value: StoredConsentRecord) -> Self {
Self {
entity: value.entity,
entity_type: match value.entity_type {
XmtpConsentType::Address => ConsentEntityType::Address,
XmtpConsentType::ConversationId => ConsentEntityType::GroupId,
XmtpConsentType::InboxId => ConsentEntityType::InboxId,
},
state: value.state.into(),
}
}
}

#[wasm_bindgen]
impl Client {
#[wasm_bindgen(js_name = setConsentStates)]
Expand Down
12 changes: 12 additions & 0 deletions bindings_wasm/src/conversations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -641,4 +641,16 @@ impl Conversations {
);
Ok(StreamCloser::new(stream_closer))
}

#[wasm_bindgen(js_name = "streamConsent")]
pub fn stream_consent(&self, callback: StreamCallback) -> Result<StreamCloser, JsError> {
let stream_closer =
RustXmtpClient::stream_consent_with_callback(self.inner_client.clone(), move |message| {
match message {
Ok(m) => callback.on_consent_update(m.into_iter().map(Into::into).collect()),
Err(e) => callback.on_error(JsError::from(e)),
}
});
Ok(StreamCloser::new(stream_closer))
}
}
4 changes: 4 additions & 0 deletions bindings_wasm/src/streams.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::consent_state::Consent;
use crate::conversation::Conversation;
use crate::messages::Message;
use std::{cell::RefCell, rc::Rc};
Expand All @@ -18,6 +19,9 @@ extern "C" {
#[wasm_bindgen(structural, method)]
pub fn on_message(this: &StreamCallback, item: Message);

#[wasm_bindgen(structural, method)]
pub fn on_consent_update(this: &StreamCallback, item: Consent);

#[wasm_bindgen(structural, method)]
pub fn on_conversation(this: &StreamCallback, item: Conversation);

Expand Down
Loading