-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Support sending/receiving messages with attachments (refs #24)
- Loading branch information
Showing
33 changed files
with
710 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
bindings/prose-sdk-js/src/types/send_message_request.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// prose-core-client/prose-sdk-js | ||
// | ||
// Copyright: 2023, Marc Bauer <mb@nesium.com> | ||
// License: Mozilla Public License v2.0 (MPL v2.0) | ||
|
||
use anyhow::anyhow; | ||
use js_sys::{Object, Reflect}; | ||
use tracing::error; | ||
use wasm_bindgen::prelude::wasm_bindgen; | ||
use wasm_bindgen::{JsCast, JsValue}; | ||
|
||
use prose_core_client::dtos; | ||
|
||
use crate::types::{Attachment, AttachmentsArray, IntoJSArray}; | ||
|
||
#[wasm_bindgen] | ||
pub struct SendMessageRequest { | ||
body: Option<String>, | ||
attachments: Vec<Attachment>, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl SendMessageRequest { | ||
#[wasm_bindgen(constructor)] | ||
pub fn new() -> Self { | ||
Self { | ||
body: None, | ||
attachments: vec![], | ||
} | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn body(&self) -> Option<String> { | ||
self.body.clone() | ||
} | ||
|
||
#[wasm_bindgen(setter)] | ||
pub fn set_body(&mut self, body: Option<String>) { | ||
self.body = body; | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn attachments(&self) -> AttachmentsArray { | ||
self.attachments.iter().cloned().collect_into_js_array() | ||
} | ||
|
||
#[wasm_bindgen(setter)] | ||
pub fn set_attachments(&mut self, attachments: AttachmentsArray) { | ||
let js_val: &JsValue = attachments.as_ref(); | ||
let array: Option<&js_sys::Array> = js_val.dyn_ref(); | ||
|
||
let Some(array) = array else { | ||
error!("Tried to assign a non-Array to 'attachments' of 'SendMessageRequest'."); | ||
return; | ||
}; | ||
|
||
let Ok(length) = usize::try_from(array.length()) else { | ||
error!("Could not determine the length of the attachments array."); | ||
return; | ||
}; | ||
|
||
let mut typed_array = Vec::<Attachment>::with_capacity(length); | ||
for js in array.iter() { | ||
let obj = match js.dyn_into::<js_sys::Object>() { | ||
Ok(obj) => obj, | ||
Err(err) => { | ||
error!("Failed to parse attachment. {:?}", err); | ||
return; | ||
} | ||
}; | ||
|
||
let attachment = match Attachment::try_from(obj) { | ||
Ok(attachment) => attachment, | ||
Err(err) => { | ||
error!("Failed to parse attachment. {}", err.to_string()); | ||
return; | ||
} | ||
}; | ||
typed_array.push(attachment); | ||
} | ||
|
||
self.attachments = typed_array; | ||
} | ||
} | ||
|
||
impl TryFrom<SendMessageRequest> for dtos::SendMessageRequest { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: SendMessageRequest) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
body: value.body, | ||
attachments: value | ||
.attachments | ||
.into_iter() | ||
.map(TryFrom::try_from) | ||
.collect::<Result<Vec<_>, _>>()?, | ||
}) | ||
} | ||
} | ||
|
||
impl TryFrom<Attachment> for dtos::Attachment { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: Attachment) -> Result<Self, Self::Error> { | ||
Ok(Self { | ||
url: value.url.parse()?, | ||
description: value.description, | ||
}) | ||
} | ||
} | ||
|
||
impl TryFrom<Object> for Attachment { | ||
type Error = anyhow::Error; | ||
|
||
fn try_from(value: Object) -> Result<Self, Self::Error> { | ||
let url = Reflect::get(&value, &JsValue::from_str("url")) | ||
.ok() | ||
.and_then(|value| value.as_string()) | ||
.ok_or_else(|| anyhow!("url is not a String"))?; | ||
|
||
let description = Reflect::get(&value, &JsValue::from_str("description")) | ||
.ok() | ||
.and_then(|value| value.as_string()); | ||
|
||
Ok(Attachment { url, description }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// prose-core-client/prose-sdk-js | ||
// | ||
// Copyright: 2023, Marc Bauer <mb@nesium.com> | ||
// License: Mozilla Public License v2.0 (MPL v2.0) | ||
|
||
use wasm_bindgen::prelude::wasm_bindgen; | ||
|
||
use prose_core_client::dtos; | ||
|
||
use crate::types::{IntoJSArray, UploadHeadersArray}; | ||
|
||
#[wasm_bindgen] | ||
pub struct UploadSlot { | ||
upload_url: String, | ||
upload_headers: Vec<UploadHeader>, | ||
download_url: String, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl UploadSlot { | ||
#[wasm_bindgen(getter, js_name = "uploadURL")] | ||
pub fn upload_url(&self) -> String { | ||
self.upload_url.clone() | ||
} | ||
|
||
#[wasm_bindgen(getter, js_name = "uploadHeaders")] | ||
pub fn upload_headers(&self) -> UploadHeadersArray { | ||
self.upload_headers.iter().cloned().collect_into_js_array() | ||
} | ||
|
||
#[wasm_bindgen(getter, js_name = "downloadURL")] | ||
pub fn download_url(&self) -> String { | ||
self.download_url.clone() | ||
} | ||
} | ||
|
||
#[wasm_bindgen] | ||
#[derive(Clone)] | ||
pub struct UploadHeader { | ||
name: String, | ||
value: String, | ||
} | ||
|
||
#[wasm_bindgen] | ||
impl UploadHeader { | ||
#[wasm_bindgen(getter)] | ||
pub fn name(&self) -> String { | ||
self.name.clone() | ||
} | ||
|
||
#[wasm_bindgen(getter)] | ||
pub fn value(&self) -> String { | ||
self.value.clone() | ||
} | ||
} | ||
|
||
impl From<dtos::UploadHeader> for UploadHeader { | ||
fn from(value: dtos::UploadHeader) -> Self { | ||
Self { | ||
name: value.name, | ||
value: value.value, | ||
} | ||
} | ||
} | ||
|
||
impl From<dtos::UploadSlot> for UploadSlot { | ||
fn from(value: dtos::UploadSlot) -> Self { | ||
Self { | ||
upload_url: value.upload_url.to_string(), | ||
upload_headers: value.upload_headers.into_iter().map(Into::into).collect(), | ||
download_url: value.download_url.to_string(), | ||
} | ||
} | ||
} |
Oops, something went wrong.