Skip to content

Commit

Permalink
feat: Add avatar to MessageSender
Browse files Browse the repository at this point in the history
  • Loading branch information
nesium committed Aug 20, 2024
1 parent 1cedb0e commit 011f5d3
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 7 deletions.
11 changes: 10 additions & 1 deletion bindings/prose-sdk-js/src/types/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use prose_core_client::dtos;
use prose_core_client::dtos::{MessageServerId, ScalarRangeExt};

use crate::types::{
Attachment, AttachmentsArray, BareJid, IntoJSArray, Mention, MentionsArray, MessageSendersArray,
Attachment, AttachmentsArray, Avatar, BareJid, IntoJSArray, Mention, MentionsArray,
MessageSendersArray,
};

use super::ReactionsArray;
Expand Down Expand Up @@ -218,13 +219,15 @@ impl Reaction {
pub struct MessageSender {
id: dtos::ParticipantId,
name: String,
avatar: Option<Avatar>,
}

impl From<dtos::MessageSender> for MessageSender {
fn from(value: dtos::MessageSender) -> Self {
Self {
id: value.id,
name: value.name,
avatar: value.avatar.map(Into::into),
}
}
}
Expand Down Expand Up @@ -252,6 +255,12 @@ impl MessageSender {
pub fn name(&self) -> String {
self.name.clone()
}

/// The avatar of the message sender.
#[wasm_bindgen(getter)]
pub fn avatar(&self) -> Option<Avatar> {
self.avatar.clone()
}
}

impl From<dtos::Reaction> for Reaction {
Expand Down
3 changes: 2 additions & 1 deletion crates/prose-core-client/src/app/dtos/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use chrono::{DateTime, Utc};

use crate::domain::shared::models::ParticipantId;
use crate::dtos::{Attachment, Body, Emoji, Mention, MessageRemoteId, MessageServerId};
use crate::dtos::{Attachment, Avatar, Body, Emoji, Mention, MessageRemoteId, MessageServerId};

#[derive(Debug, Clone, PartialEq)]
pub struct Message {
Expand All @@ -31,6 +31,7 @@ pub struct Message {
pub struct MessageSender {
pub id: ParticipantId,
pub name: String,
pub avatar: Option<Avatar>,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down
8 changes: 6 additions & 2 deletions crates/prose-core-client/src/app/services/room.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,31 +739,34 @@ impl<Kind> Room<Kind> {
}

async fn resolve_message_sender(&self, id: &ParticipantId) -> MessageSender {
let (name, mut real_id) = self
let (name, avatar, mut real_id) = self
.data
.with_participants(|p| {
p.get(id).map(|p| {
(
Some(p.name().unwrap_or_participant_id(id)),
p.avatar.clone(),
p.real_id.clone(),
)
})
})
.unwrap_or_else(|| (None, None));
.unwrap_or_else(|| (None, None, None));

real_id = real_id.or_else(|| id.to_user_id());

if let Some(name) = name {
return MessageSender {
id: id.clone(),
name,
avatar,
};
}

let Some(real_id) = real_id else {
return MessageSender {
id: id.clone(),
name: ContactNameBuilder::new().unwrap_or_participant_id(id),
avatar,
};
};

Expand All @@ -778,6 +781,7 @@ impl<Kind> Room<Kind> {
MessageSender {
id: id.clone(),
name,
avatar,
}
}

Expand Down
2 changes: 2 additions & 0 deletions crates/prose-core-client/src/test/message_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ impl MessageBuilder {
name: self
.from_name
.expect("You must set a name when building a MessageDTO"),
avatar: None,
},
body: Body {
raw: body.raw,
Expand All @@ -232,6 +233,7 @@ impl MessageBuilder {
.to_user_id()
.map(|user_id| user_id.formatted_username())
.unwrap_or(sender.to_opaque_identifier()),
avatar: None,
})
.collect(),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,19 +103,22 @@ async fn test_reactions() -> Result<()> {
from: vec![
MessageSender {
id: occupant_id!("[email protected]/drs").into(),
name: "Drs".to_string()
name: "Drs".to_string(),
avatar: None,
},
MessageSender {
id: occupant_id!("[email protected]/huxx").into(),
name: "Huxx".to_string()
name: "Huxx".to_string(),
avatar: None,
}
]
},
Reaction {
emoji: "👍🏽".into(),
from: vec![MessageSender {
id: occupant_id!("[email protected]/flux").into(),
name: "Flux".to_string()
name: "Flux".to_string(),
avatar: None,
}]
}
],
Expand Down

0 comments on commit 011f5d3

Please sign in to comment.