Skip to content

Commit

Permalink
feat: Add name to various objects return from Room
Browse files Browse the repository at this point in the history
Members, Occupants and Messages
  • Loading branch information
nesium committed Nov 2, 2023
1 parent 64f3c2d commit 993986e
Show file tree
Hide file tree
Showing 34 changed files with 887 additions and 241 deletions.
2 changes: 1 addition & 1 deletion bindings/prose-sdk-ffi/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl From<ProseMessage> for Message {
Message {
id: value.id,
stanza_id: value.stanza_id,
from: value.from.into(),
from: value.from.jid.to_bare().into(),
body: value.body,
timestamp: value.timestamp,
is_read: value.is_read,
Expand Down
1 change: 1 addition & 0 deletions bindings/prose-sdk-js/src/types/contact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ impl From<CoreContact> for Contact {
}

#[wasm_bindgen]
#[derive(Clone)]
pub enum Availability {
Available = 0,
Unavailable = 1,
Expand Down
29 changes: 26 additions & 3 deletions bindings/prose-sdk-js/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
// Copyright: 2023, Marc Bauer <mb@nesium.com>
// License: Mozilla Public License v2.0 (MPL v2.0)

use crate::types::IntoJSArray;
use prose_core_client::dtos;
use wasm_bindgen::prelude::*;

use prose_core_client::dtos;

use crate::types::IntoJSArray;

use super::{BareJid, BareJidArray, ReactionsArray};

#[wasm_bindgen]
Expand All @@ -20,6 +22,9 @@ pub struct Reaction {
pub from: Vec<BareJid>,
}

#[wasm_bindgen]
pub struct MessageSender(dtos::MessageSender);

#[wasm_bindgen]
impl Message {
#[wasm_bindgen(getter)]
Expand All @@ -39,7 +44,12 @@ impl Message {

#[wasm_bindgen(getter, js_name = "from")]
pub fn from_(&self) -> String {
self.0.from.to_string()
self.0.from.jid.to_string()
}

#[wasm_bindgen(getter, js_name = "user")]
pub fn user(&self) -> MessageSender {
MessageSender(self.0.from.clone())
}

#[wasm_bindgen(getter, js_name = "content")]
Expand Down Expand Up @@ -100,6 +110,19 @@ impl Reaction {
}
}

#[wasm_bindgen]
impl MessageSender {
#[wasm_bindgen(getter, js_name = "jid")]
pub fn jid(&self) -> String {
self.0.jid.to_string()
}

#[wasm_bindgen(getter, js_name = "name")]
pub fn name(&self) -> String {
self.0.name.clone()
}
}

impl From<dtos::Message> for Message {
fn from(value: dtos::Message) -> Self {
Message(value)
Expand Down
2 changes: 2 additions & 0 deletions bindings/prose-sdk-js/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub use jid::BareJid;
pub use js_array::*;
pub use message::Message;
pub use room::{RoomEnvelopeExt, RoomsArray};
pub use user_info::{UserBasicInfo, UserBasicInfoArray, UserPresenceInfo, UserPresenceInfoArray};
pub use user_metadata::UserMetadata;
pub use user_profile::UserProfile;

Expand All @@ -18,5 +19,6 @@ mod jid;
mod js_array;
mod message;
mod room;
mod user_info;
mod user_metadata;
mod user_profile;
58 changes: 17 additions & 41 deletions bindings/prose-sdk-js/src/types/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ use tracing::info;
use wasm_bindgen::prelude::wasm_bindgen;
use wasm_bindgen::{JsError, JsValue};

use prose_core_client::dtos::{ComposingUser as CoreComposingUser, MessageId};
use prose_core_client::dtos::MessageId;
use prose_core_client::services::{
DirectMessage, Generic, Group, PrivateChannel, PublicChannel, Room as SdkRoom, RoomEnvelope,
};

use crate::client::WasmError;
use crate::types::{
try_jid_vec_from_string_array, BareJid, BareJidArray, MessagesArray, StringArray,
try_jid_vec_from_string_array, MessagesArray, StringArray, UserBasicInfo, UserBasicInfoArray,
UserPresenceInfo, UserPresenceInfoArray,
};

use super::IntoJSArray;
Expand All @@ -31,9 +32,9 @@ export interface RoomBase {
readonly id: RoomID;
readonly name: string;
/// The members of a room. Only available for DirectMessage and Group (member-only rooms)
readonly members: JID[];
readonly members: UserPresenceInfo[];
/// The occupants of a room.
readonly occupants: JID[];
readonly occupants: UserBasicInfo[];
sendMessage(body: string): Promise<void>;
updateMessage(messageID: string, body: string): Promise<void>;
Expand All @@ -44,7 +45,7 @@ export interface RoomBase {
loadMessagesWithIDs(messageIDs: string[]): Promise<Message[]>;
setUserIsComposing(isComposing: boolean): Promise<void>;
loadComposingUsers(): Promise<ComposingUser[]>;
loadComposingUsers(): Promise<UserBasicInfo[]>;
saveDraft(message?: string): Promise<void>;
loadDraft(): Promise<string>;
Expand Down Expand Up @@ -152,21 +153,21 @@ macro_rules! base_room_impl {
}

#[wasm_bindgen(getter)]
pub fn members(&self) -> BareJidArray {
pub fn members(&self) -> UserPresenceInfoArray {
self.room
.members()
.iter()
.map(BareJid::from)
.collect_into_js_array::<BareJidArray>()
.into_iter()
.map(UserPresenceInfo::from)
.collect_into_js_array::<UserPresenceInfoArray>()
}

#[wasm_bindgen(getter)]
pub fn occupants(&self) -> BareJidArray {
pub fn occupants(&self) -> UserBasicInfoArray {
self.room
.occupants()
.iter()
.map(BareJid::from)
.collect_into_js_array::<BareJidArray>()
.into_iter()
.map(UserBasicInfo::from)
.collect_into_js_array::<UserBasicInfoArray>()
}

#[wasm_bindgen(js_name = "sendMessage")]
Expand Down Expand Up @@ -247,15 +248,15 @@ macro_rules! base_room_impl {
}

#[wasm_bindgen(js_name = "loadComposingUsers")]
pub async fn load_composing_users(&self) -> Result<ComposingUsersArray> {
pub async fn load_composing_users(&self) -> Result<UserBasicInfoArray> {
Ok(self
.room
.load_composing_users()
.await
.map_err(WasmError::from)?
.into_iter()
.map(ComposingUser::from)
.collect_into_js_array::<ComposingUsersArray>())
.map(UserBasicInfo::from)
.collect_into_js_array::<UserBasicInfoArray>())
}

#[wasm_bindgen(js_name = "saveDraft")]
Expand Down Expand Up @@ -343,9 +344,6 @@ channel_room_impl!(RoomPublicChannel);
extern "C" {
#[wasm_bindgen(typescript_type = "Room[]")]
pub type RoomsArray;

#[wasm_bindgen(typescript_type = "ComposingUser[]")]
pub type ComposingUsersArray;
}

pub trait RoomEnvelopeExt {
Expand Down Expand Up @@ -387,25 +385,3 @@ impl From<Vec<RoomEnvelope>> for RoomsArray {
.collect_into_js_array::<RoomsArray>()
}
}

#[wasm_bindgen]
pub struct ComposingUser(CoreComposingUser);

#[wasm_bindgen]
impl ComposingUser {
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.0.name.clone()
}

#[wasm_bindgen(getter)]
pub fn jid(&self) -> BareJid {
self.0.jid.clone().into()
}
}

impl From<CoreComposingUser> for ComposingUser {
fn from(value: CoreComposingUser) -> Self {
Self(value)
}
}
84 changes: 84 additions & 0 deletions bindings/prose-sdk-js/src/types/user_info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// 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::{
UserBasicInfo as SdkUserBasicInfo, UserPresenceInfo as SdkUserPresenceInfo,
};

use crate::types::{Availability, BareJid};

#[wasm_bindgen]
pub struct UserBasicInfo {
jid: BareJid,
name: String,
}

#[wasm_bindgen]
impl UserBasicInfo {
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}

#[wasm_bindgen(getter)]
pub fn jid(&self) -> BareJid {
self.jid.clone().into()
}
}

impl From<SdkUserBasicInfo> for UserBasicInfo {
fn from(value: SdkUserBasicInfo) -> Self {
Self {
jid: value.jid.into(),
name: value.name,
}
}
}

#[wasm_bindgen]
pub struct UserPresenceInfo {
jid: BareJid,
name: String,
availability: Availability,
}

#[wasm_bindgen]
impl UserPresenceInfo {
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}

#[wasm_bindgen(getter)]
pub fn jid(&self) -> BareJid {
self.jid.clone().into()
}

#[wasm_bindgen(getter)]
pub fn availability(&self) -> Availability {
self.availability.clone()
}
}

impl From<SdkUserPresenceInfo> for UserPresenceInfo {
fn from(value: SdkUserPresenceInfo) -> Self {
Self {
jid: value.jid.into(),
name: value.name,
availability: value.availability.into(),
}
}
}

#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "UserBasicInfo[]")]
pub type UserBasicInfoArray;

#[wasm_bindgen(typescript_type = "UserPresenceInfo[]")]
pub type UserPresenceInfoArray;
}
28 changes: 28 additions & 0 deletions crates/prose-core-client/src/app/dtos/message.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// prose-core-client/prose-core-client
//
// Copyright: 2023, Marc Bauer <mb@nesium.com>
// License: Mozilla Public License v2.0 (MPL v2.0)

use chrono::{DateTime, Utc};
use jid::Jid;

use crate::dtos::{MessageId, Reaction, StanzaId};

#[derive(Debug, Clone, PartialEq)]
pub struct Message {
pub id: Option<MessageId>,
pub stanza_id: Option<StanzaId>,
pub from: MessageSender,
pub body: String,
pub timestamp: DateTime<Utc>,
pub is_read: bool,
pub is_edited: bool,
pub is_delivered: bool,
pub reactions: Vec<Reaction>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MessageSender {
pub jid: Jid,
pub name: String,
}
8 changes: 5 additions & 3 deletions crates/prose-core-client/src/app/dtos/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
pub use url::Url;

pub use contact::Contact;
pub use message::{Message, MessageSender};

pub use crate::domain::{
contacts::models::Group,
general::models::SoftwareVersion,
messaging::models::{Emoji, Message, MessageId, Reaction, StanzaId},
rooms::models::{ComposingUser, Occupant},
shared::models::Availability,
messaging::models::{Emoji, MessageId, Reaction, StanzaId},
rooms::models::{Member, Occupant},
shared::models::{Availability, UserBasicInfo, UserPresenceInfo},
user_info::models::{LastActivity, UserActivity, UserInfo, UserMetadata},
user_profiles::models::{Address, UserProfile},
};

mod contact;
mod message;
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use prose_xmpp::{ns, Event};

use crate::app::deps::{
DynAppContext, DynClientEventDispatcher, DynConnectedRoomsRepository, DynRoomFactory,
DynRoomsDomainService, DynTimeProvider,
DynRoomsDomainService, DynTimeProvider, DynUserProfileRepository,
};
use crate::app::event_handlers::{XMPPEvent, XMPPEventHandler};
use crate::client_event::RoomEventType;
Expand All @@ -40,6 +40,8 @@ pub struct RoomsEventHandler {
room_factory: DynRoomFactory,
#[inject]
time_provider: DynTimeProvider,
#[inject]
user_profile_repo: DynUserProfileRepository,
}

#[cfg_attr(target_arch = "wasm32", async_trait(? Send))]
Expand Down Expand Up @@ -161,9 +163,13 @@ impl RoomsEventHandler {
};

info!("Received real jid for {}: {}", from, jid);

let bare_jid = jid.into_bare();
let name = self.user_profile_repo.get_display_name(&bare_jid).await?;

room.state
.write()
.insert_occupant(&from, Some(&jid.into_bare()), &affiliation);
.insert_occupant(&from, Some(&bare_jid), name.as_deref(), &affiliation);

Ok(())
}
Expand Down
Loading

0 comments on commit 993986e

Please sign in to comment.